Saturday, August 15, 2009

Jboss Error message while accessing the web service from client

I have recently encountered the following error. Rrror message looks something like this: "setProperty must be overridden by all subclasses of SOAPMessage". I did not understand what the Issue was. I bing it for a while, and came up with the solution. I just had to place the jboss-saaj.jar in "../jboss-home/lib/endorsed" directory. This library exists in "jboss-home/server/default/lib" directory
ERROR MESSAGE:
java.lang.UnsupportedOperationException: setProperty must be overridden by all subclasses of SOAPMessage
at javax.xml.soap.SOAPMessage.setProperty(SOAPMessage.java:441)
at org.jboss.ws.core.soap.SOAPMessageImpl.(SOAPMessageImpl.java:82)
13:02:47,366 ERROR [RequestHandlerImpl] Error processing web service request
org.jboss.ws.WSException: java.lang.UnsupportedOperationException: setProperty must be overridden by all subclasses of SOAPMessage
at org.jboss.ws.WSException.rethrow(WSException.java:68)
at org.jboss.wsf.stack.jbws.RequestHandlerImpl.handleRequest(RequestHandlerImpl.java:336)
RESOLUTION: copy "jboss-saaj.jar" from /jboss-home/server/default/lib to .../jbosshome/lib/endorsed folder

Monday, August 10, 2009

Convert Date to Calendar

Convert date to calendar:

Both Date and Calendar are availabel in java.util package. Date object represents the
specific instant of time. Calendar class is an abstract calss used for converting
between a Date object and a set of integer fields.. YEAR, MONTH,DAY, HOUR...etc.

Following example program converts a Date object into Calendar object.

This is achieved in the following steps:
1. getting the calendar instance.
Calendar cal = Calendar.getInstance();
2. getting the date object which you want to convert to Calendar object.
Assuming that we are going to convert the current date into calendar object,
Date d = new Date();
3. use the setTime(date) method available in Calendar class to convert the date to calendar object.
cal.setTime(d);

import java.util.Date;
import java.util.Calendar;

public class Date2Calendar {
public static void main(String[] rags){
Calendar cal = Calendar.getInstance();
Date date = new Date();
cal.setTime(date)
System.out.println("Current date is:"+cal);
}
}

Saturday, August 08, 2009

String versus StringBuffer



Generally, we use the String and StringBuffer classes for manipulating the character data. Both, String and StringBuffer are available in "java.lang" package.


String class is used to represent the character strings that cannot be changed. In other words, String type objects are read-only and immutable. StringBuffer objects represent the character strings that can be changed.

The other difference between the two is the performance. StringBuffer is much faster than that of String class when performing concatenations. Following lines of code is used to perform the concatenation.

String:
String string = new String("Java ");
string += "Language";

As String class is immutable, the initial string is first converted into StringBuffer and append the second string, then converts it back to String object using the toString() method.

StringBUffer: String buffer class has an "append()" method to concatenate the string data.
StringBuffer strBuf = new StringBuffer("Java ");
strBuf.append("Language");

First method - created three objects. initial String object, temporary StringBuffer and String object to hold the converted StringBuffer data.

Second method - everything is done with just one object creation, i.e. StringBuffer, and appends the data to the StringBuffer.

Overall, Using the StringBuffer there is no need of conversion from String->StringBuffer, and converting back to String, which is an expensive operation. So StringBuffer is a best option for developers to choose when they want to manipulate the character string data.

ArrayList vs Vector

Internally, Vector and ArrayList holds their contents using an array. From API point of view, vector and arraylist are very similar. Vectors are synchronized, where as ArrayList are not. Sometimes its better to use Vector, sometimes its better to user ArrayList. Your choice depends upon the needs. Following are the similarities and differences between the two.

Similarities between ArrayLists and Vectors

  • Both can grow up during run time.
  • Both implement List interface.
  • With both, it is easier to remove or add elements at the end or start, but if you try to add or remove elements somewhere at middle of collection, they suffer performance wise. (Use LinkedLists if your programme need to do that a lot, but LinkList requires more memory and computation)

Differences between ArrayLists and Vectors

  • The major difference is just that vectors are synchronized. This means that if more than one thread in your code is to use that data, you are in trouble with ArrayList as the data is asynchronous. Though there are ways by which you can make your ArrayLists synchronous, but by default they are not. The obvious downside with vectors is the additional computation to handle threads.
  • Vector contains many legacy methods that are not part of collection framework. With Java2 release, Vector reengineered to extend the class AbstractList and implements List interface, now it is fully compatible with collections framework.
  • The other difference is that with vectors, you can specify the incremental value, which is the amount with which the data structure will grow during the runtime. But with ArrayLists you have no option but to accept default that is the list will grow up 50% of original size everytime it needs additional space. It is advisable in both the cases to choose the initial size carefully.

Thursday, August 06, 2009

Converting an Array to a Set

To my knowledge we do not have any direct methods to convert from an array to a set. this To achieve this, we have to do the conversion process in two steps.
1. Converting an array to List
2. Converting the resulting List to a Set

Following program constructs an array with strings, String array is converted into a List using the asList method available in Arrays. Then the resulting list is converted into a Set.

---
package com.blog.javaexposure.util;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Array2Set {
public static void main(String rags[]){
// Creating a string array
String[] str = {"One","Two","Three","Four","
Five"};

// Convert array to List
List list = Arrays.asList(str);

System.out.println("Elements in the list are:");
//Checking the elements in the list
for(String s : list){
System.out.println(s);
}

// Convert list to set
Set set = new HashSet(list);

System.out.println("Elements in the set are:");
//Checking the elements in the set
for(String s : set){
System.out.println(s);
}
}
}
È