Singleton Design pattern in JAVA


Below class depicts the Singleton design pattern.

SingletonDesignPattern.java

package in.javatutorials;

/**
* @author JavaTutorials
* @version 1.0
*
*/
public final class SingletonDesignPattern {

/**
* make the class object as private as it is
* not used directly out side the class
*/
private static SingletonDesignPattern singletonObj = null;

/**
* make the constructor as private.
* Object cannot be created using the constructor outside of this class.
*/
private SingletonDesignPattern(){

}

/**
* Create the class object if it is null and
*
* @return singletonObj SingletonDesignPattern
*/
public static SingletonDesignPattern getInstance(){
if(singletonObj == null){
singletonObj = new SingletonDesignPattern();
}
return singletonObj;
}

/**
* Take the user name as parameter and return the welcome message
*
* @param userName String
* @return message String
*/
public String getWelcomeInfo(String userName){
String message = null;
//make method access to synchronized to make thread safe
synchronized (SingletonDesignPattern.class) {
System.out.println(“User Name is : ” + userName);
message = “Hello ” + userName + “!!!”;
}
return message;
}
}

 

TestSingleton.java

The main() method of below class will get the single ton object and access the other methods using the same.

package in.javatutorials;

public class TestSingleton {

public static void main(String[] args) {
//Get the singleton object
SingletonDesignPattern singletonObj = SingletonDesignPattern.getInstance();

System.out.println(singletonObj.getWelcomeInfo(“Mallik”));
}

}

JBoss Areo Gear – Build Cross-Platform Mobile Apps with Ease


When building mobile web apps, it is important to have lightweight, performant code that gives a great experience to your users no matter what device they might be using. That is where AeroGear.js comes in. This library provides everything from a simple persistence layer to a security API and everything in between.

 

To know more : http://aerogear.org/

JBoss Errai – makes rich client easy.


Errai’s mission is to make building rich client applications not just easier, but more scalable and manageable. App grow over time, features get added, and code bases balloon into an albatross for developers. By applying good practices, and leveraging frameworks which promote modularity and decoupling, many of these pain points can be avoided. Errai is such a framework.

Errai provides a comprehensive framework and tools for building rich web applications, leveraging the GWT compiler. With standard server-side APIs, such as CDI, in the browser, managing large web applications was never so easy.

http://www.jboss.org/errai?sc_cid=701600000006b8nAAA

Convert Array to Vector in Java


package com.test;

import java.util.Arrays;
import java.util.Vector;

public class ArrayTest {

public static void main(String[] args) {
ArrayTest arrayTest = new ArrayTest();
arrayTest.arrayToVector();
}
public void arrayToVector() {

String[] arrayToConvert = { “jhony”, “peter”, “don” };

Vector<String> convertedVector = new Vector<String> Arrays.asList(arrayToConvert));

System.out.println(“11111 ” + convertedVector.get(0));

System.out.println(“3” + convertedVector.size());
}
}

How to convert an array to comma separated string in java


package com.test;

public class ArrayTest {

public static void main(String[] args) {
ArrayTest arrayTest = new ArrayTest();
arrayTest.arrayJoin();
}

public void arrayJoin() {

String[] arrayToJoin = { “John”, “Peter”, “Tom”, “Scott” };

StringBuilder joinedString = new StringBuilder();
String seperator = “,”;
//System.out.println(“Converting to String “+arrayToJoin.toString());;
for (String value : arrayToJoin) {
joinedString.append(value).append(seperator);
}

System.out.println(“Final Result …….. ” + joinedString);
}
}

What design patterns are used in Struts 1.X?


Struts is based on model 2 MVC (Model-View-Controller) architecture. Struts controller uses the command design pattern and the action classes use the adapter design pattern. The process() method of the RequestProcessor uses the template method design pattern. Struts also implement the following J2EE design patterns.

  • Service to Worker
  • Dispatcher View
  • Composite View (Struts Tiles)
  • Front Controller
  • View Helper
  • Synchronizer Token

What are the benefits of XML?


There are many benefits of using XML on the Web:

  • Simplicity– Information coded in XML is easy to read and understand, plus it can be processed easily by computers.
  • Openness– XML is a W3C standard, endorsed by software industry market leaders.
  • Extensibility – There is no fixed set of tags. New tags can be created as they are needed.
  • Self-description– In traditional databases, data records require schemas set up by the database administrator. XML documents can be stored without such definitions, because they contain meta data in the form of tags and attributes.
  • Contains machine-readable context information- Tags, attributes and element structure provide context information that can be used to interpret the meaning of content, opening up new possibilities for highly efficient search engines, intelligent data mining, agents, etc.
  • Separates content from presentation– XML tags describe meaning not presentation. The motto of HTML is: “I know how it looks”, whereas the motto of XML is: “I know what it means, and you tell me how it should look.” The look and feel of an XML document can be controlled by XSL style sheets, allowing the look of a document to be changed without touching the content of the document. Multiple views or presentations of the same content are easily rendered.
  • Supports multilingual documents and Unicode-This is important for the internationalization of applications.
  • Facilitates the comparison and aggregation of data – The tree structure of XML documents allows documents to be compared and aggregated efficiently element by element.
  • Can embed multiple data types – XML documents can contain any possible data type – from multimedia data (image, sound and video) to active components (Java applets, ActiveX).
  • Can embed existing data – Mapping existing data structures like file systems or relational databases to XML is simple. XML supports multiple data formats and can cover all existing data structures.
  • Provides a ‘one-server view’ for distributed data – XML documents can consist of nested elements that are distributed over multiple remote servers. XML is currently the most sophisticated format for distributed data – the World Wide Web can be seen as one huge XML database.

Java EE 5 vs Java EE 6


This post visualizes changes between Java EE Standards 5 and 6. The comparison of standards is listed in four sections Web-Services, Web-Container, Enterprise Application technologies and Maintenance. Hope this helps someone.

Web Service related changes

JAVA EE 5 (JSR-244) JAVA EE 6 (JSR-316)
JAX-RPC 1.1 JSR 101 JAX-RPC 1.1
Enterprise Web Services 1.2 JSR 109 Enterprise Web Services 1.3 (new version)
Web Service Metadata 1.0 JSR 181 Web Service Metadata 1.0
Streaming API for XML 1.0 JSR 173 Streaming API for XML 1.0
JAX-WS 2.0  JSR 224 JAX-WS 2.2 (new version)
JAXB 2.0 JSR 222 JAXB 2.2 (new version)
SOAP with Attachments API for Java (SAAJ)JSR 67 Java APIs for XML Messaging 1.3 (new version)spec
new! JAX-RS 1.1 JSR 311
new! Java API for XML Registries (JAXR 1.0)JSR 93

The new redesigned Java API for XML Web Services (JAX-WS) is the base or a middle part of a newly Java EE 6 Web service stack.  The new stack  includes JAX-WS 2.0, JAXB 2.0, and SAAJ 1.3. and is also called “integrated stack”.  JAX-WS was designed to take place of JAX-RPC. Due this also JSR-109 was updated because it describes run time architecture of JEE Web Services Stack. JAXB which provides an easy way to bind an XML schema to java and vice verse, was updated to.

The SOAP with Attachments API for Java (SAAJ) (also known as Java APIs for XML Messaging (JAXM)) provides a standard way to send XML documents over the Internet from the Java platform and was updated slightly containing now other consolidated standard.

New are JAX-RS, which provides support for RESTful Web services and JAXR which enables pull-parsing API for reading and writing XML documents. Also available in Java SE.

Web Applications related changes

JAVA EE 5 JAVA EE 6
JSTL JSR 52 JSTL
JavaServer Faces 1.2 JSR 252 JavaServer Faces 2.0 (new version)
JavaServer Pages 2.1 JSR 245 JavaServer Pages 2.2 /EL 2.2 (new version)
Java Servlet 2.5 JSR 154 Java Servlet 3.0 JSR 315 (new version)
new! Debugging Support for Other Languages 1.0 JSR 45

In Java EE 6 we have updates of all technologies of the Web Container except JSTL. So e.g. Servlet 3.0 improves Servlet concept in pluggability and some ease of development. It’s also introduces Async Servlet, and long waited File Uploading!. Also now configuration can be done by annotations.

New a specification of Debugging Support for Other Languages 1.0
This describes standardized tools for correlating Java virtual machine byte code to source code of languages other than the Java programming language, so it would guarantee debugging possibility of everything what runs is JSR-45 certified container.

Enterprise Technologies changes

JAVA EE 5 JAVA EE 6
Common Annotations JSR 250 Common Annotations
JCA 1.5 JSR 112 JCA 1.6 JSR 322 (new version)
JavaMail 1.4 JavaMail 1.4
JMS 1.1 JSR 914 JMS 1.1
JTA 1.1 JSR 907 JTA 1.1
Enterprise JavaBeans 3.0 JSR 220 Enterprise JavaBeans 3.1 JSR 318
(new version)
JPA 1.0 JSR 220 (together with EJB 3.0) JPA 2.0 JSR 317 (new version)
new! Contexts and Dependency Injection for Java (Web Beans 1.0) JSR 299
new! Dependency Injection for Java 1.0 JSR 330
new! Bean Validation 1.0 JSR 303
new! Managed Beans 1.0 JSR-316

In Enterprise Application section we see some important changes and new specifications. Most famous and important is  JSR-299 Context and Dependency Injection (CDI) which is there to unify the JavaServer Faces-managed bean component model with the Enterprise JavaBeans component model to simplify the programming model and architecture of web-based applications. Look an Weld Framework as reference implementation to this.

The similar sounding Standard Dependency Injection for Java JSR-330 just define a standard and common known DI like in spring and other frameworks. Look at popular Guice DI-Framework from Google which implements JSR-330.

Bean Validation  introduces a very cool annotation based and architecture layer independent Java Bean validation.

There are also some interesting improvements in EJBs. Singleton is a new type and can be only one per container, it is also possible to use @Local Beans (Same VM) without interface. Also JPA 2.0 has advanced query possibilities and validation.

Management Technologies

JAVA EE 5 JAVA EE 6
J2EE Application Deployment 1.2 JSR 88 J2EE Application Deployment 1.2
JavaBeans Activation Framework (JAF) 1.1 JSR 925 JavaBeans Activation Framework (JAF) 1.1
J2EE Management 1.0  JSR 77 J2EE Management 1.1 (new version)
Java Authorization Contract for Containers 1.1 JSR 115 Java Authorization Contract for Containers 1.3(new version)
new! Java Authentication Service Provider Interface for Containers JSR 196
new! [JavaSE] JAXP 1.3 JSR 206
new! [JavaSE] JDBC 4.0 JSR 221
new! [JavaSE] JMX 2.0 JSR 255

Nothing special to mention here.

How to Copy an Array into Another Array?


Contents of one array can be copied into another array by using the arraycopy() method of the System class in Java.

  • arraycopy() method accepts source array, source length, destination array and destination length.
  • Following program shows the use of arraycopy() method to copy the contents of one array to another.
package com.Test;

public class ArrayCopyTest{
 public static void main(String[] args){
    int[] src = new int[] {1, 2, 3, 4, 5};
    int[] dest = new int[src.length];
    System.arraycopy(src, 0, dest, 0, src.length);
    for (int i = 0; i < dest.length; i++){
       System.out.println(dest[i]);
    }
  }
}