Wednesday, November 12, 2008

Fetching Property Files Using Resource Bundle

Put the property Files in the classpath ,which contains key value pairs.
If the files are putin any other location thean classpath , give the path in tomcat startup.sh or classpath.sh next to the bootstrap jar listing.
Now access the files as accessing any other Bundle
ResourceBundle myResources = ResourceBundle.getBundle("propertfilename");
Then u can do a bundle.getValue() to the required key value pair.

XML -RPC

Apache XML-RPC is a Java implementation of XML-RPC, a popular protocol that uses XML over HTTP to implement remote procedure calls.
Steps
1.Client class
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL("http://127.0.0.1:8080/xmlrpc"));
XmlRpcClient client = new XmlRpcClient();
client.setConfig(config);
Object[] params = new Object[]{new Integer(33), new Integer(9)};
Integer result = (Integer) client.execute("Calculator.add", params);

2.Server Class
public class Calculator {
public int add(int i1, int i2) {
return i1 + i2;
}
public int subtract(int i1, int i2) {
return i1 - i2;
}
}

3. Create Propert File
Create a property file, which contains at least one property. The property name is arbitrary, and the property value is the fully qualified name of the Calculator class. For example, like that:
Calculator=org.apache.xmlrpc.demo.Calculator
The property file must be called XmlRpcServlet.properties, and it must be located in the package org.apache.xmlrpc.webserver. In other words, you would typically put it into the directory org/apache/xmlrpc/webserver and add it to your jar file.

4. Add Entry to Web.xml
<servlet>
<servlet-name> XmlRpcServlet</servlet-name>
<servlet-class>org.apache.xmlrpc.webserver.XmlRpcServlet</servlet-class>
<init-param>
<param-name>
enabledForExtensions</param-name>
<param-value>true</param-value>
<description> Sets, whether the servlet supports vendor extensions for XML-RPC. </description>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>XmlRpcServlet</servlet-name>
<url-pattern>/xmlrpc</url-pattern>
</servlet-mapping>
Reference http://ws.apache.org/xmlrpc/

XStream - Convert XMl to Java & Vice Versa

XStream is a simple library to serialize objects to XML and back again.
XStream xstream = new XStream();
Person joe = new Person("Joe", "Walnes");
String xml = xstream.toXML(joe)
The resulting XML looks like this:
<code>
<person>
<firstname>Joe</firstname>
<lastname>Walnes</lastname>
<phone>
<code>123</code>
<number>1234-456</number>
</phone>
<fax>
<code>123</code>
<number>9999-999</number>
</fax>
</person></code>
Deserializing an object back from XML
Person newJoe = (Person)xstream.fromXML(xml);
Reference :http://xstream.codehaus.org/

 
Free Domain Names @ .co.nr!