Lets consider a method like the following –
public void purchaseOrder(String item, int quantity, String description)
When this method is serialized to SOAP packet using JAX-RPC engine, the packet looks like the below
This is called SOAP encoding. The method name is the root element under the Body tag of the soap packet. Each parameter is having type defined and the value. This type will be used by the SOAP brokers/engines to de-serialize to String or int values.
The same method needs to be written like “public void someMethod(PurchesOrder po)”
to be serialized to the similar SOAP packet using JAX-WS Document literal engine .
The same method can be left unchanged to be serialized to the similar SOAP packet using JAX-WS Document literal Wrapped (wrapped means, the parameters are wrapped inside the method name) engine .
The sample SOAP packet for JAX-WS is shown below-
In case of JAX-WS, the entire XML inside the Body is deserialized to an JavaBean following JAXB. The WSDL will contain the XSD for the PurchesOrder Bean.
Monday, December 7, 2009
Monday, August 24, 2009
Anti Patterns
Just finished reading this book on anti patterns. It is a very nice book which describes how we (the developer as well as the architects and managers) fail to deliver a software project properly even after all of our good intentions. Sometimes we follow wrong architecture, sometime the deadly sins of sloth, narrow-mindness, Apathy towards technology etc etc..
I quite like the anecdotal evidences that the authors have come up with for each of the well known anti patterns. While reading the book, sometimes I just jumped out from chair and shouted "oh god, I have experienced such problems before". I am sure, as a developer or lead or budding architect, I will be very careful now onwards to look for anti patterns while designing as well as while doing coding or reviews.
I quite like the anecdotal evidences that the authors have come up with for each of the well known anti patterns. While reading the book, sometimes I just jumped out from chair and shouted "oh god, I have experienced such problems before". I am sure, as a developer or lead or budding architect, I will be very careful now onwards to look for anti patterns while designing as well as while doing coding or reviews.
Sunday, May 31, 2009
Futuristic Build Process
Build – by definition is to compile all the source code of a project and create the deliverables. For example, build is to compile all Java code using ant or maven and then create the executable jar files or war files or ear files.
An effective build system goes long way to help the project. Nightly or at least weekly development build bits are essential to check the status of the project. Lets divide build systems in its effectiveness.
Step 0 – Build system checks the syntax errors. This is really basic and a must do for build systems. If there is a syntax error in any code , build should fails. It usually does as the compiler stops if there is a syntax error.
Step 1 – Build fails if the unit test fails on the code. This is where most of current projects are. Build fires the compilation, followed by creation of deployable jars/wars. As the last step, build fires the JUnit test cases. If the JUnit test case fails, build breaks. This makes sure the build output is sane and no unit test cases have failed on the build output.
Step 2 – Now lets consider of a build system which breaks if the code checkin has more complexity than the standard, or if the code coverage of the code checkin is less. There are several software metrics that are available now, like “Code Coverage”, “Cyclomatic complexity of code”, “CRAP Metric (Change Risk Analyzer and Predictor)”. [ there are already plugins available on the CRAP and Cyclomatic Metric, where can be found here]
If Build system is integrated with calculation of these metrics, that would guarentee a uniform code complexity and if anytime code complexity numbers come high from the code analysis tools, build should break. This is surely a cool thing to do considering the fact that now-a-days much of the code is getting auto generated. Example, you have a webservice which was using Framework version 1 {like axis1} and you were compiling WSDL file to generate the stub code. You move to Version 2 and build breaks because the auto generated code is more complex and hard to maintain.
It should be a collective decision of the project team whether to go for higher complexity of the code as a team.
This is where at Step2, I think the next generation build systems will go.
An effective build system goes long way to help the project. Nightly or at least weekly development build bits are essential to check the status of the project. Lets divide build systems in its effectiveness.
Step 0 – Build system checks the syntax errors. This is really basic and a must do for build systems. If there is a syntax error in any code , build should fails. It usually does as the compiler stops if there is a syntax error.
Step 1 – Build fails if the unit test fails on the code. This is where most of current projects are. Build fires the compilation, followed by creation of deployable jars/wars. As the last step, build fires the JUnit test cases. If the JUnit test case fails, build breaks. This makes sure the build output is sane and no unit test cases have failed on the build output.
Step 2 – Now lets consider of a build system which breaks if the code checkin has more complexity than the standard, or if the code coverage of the code checkin is less. There are several software metrics that are available now, like “Code Coverage”, “Cyclomatic complexity of code”, “CRAP Metric (Change Risk Analyzer and Predictor)”. [ there are already plugins available on the CRAP and Cyclomatic Metric, where can be found here]
If Build system is integrated with calculation of these metrics, that would guarentee a uniform code complexity and if anytime code complexity numbers come high from the code analysis tools, build should break. This is surely a cool thing to do considering the fact that now-a-days much of the code is getting auto generated. Example, you have a webservice which was using Framework version 1 {like axis1} and you were compiling WSDL file to generate the stub code. You move to Version 2 and build breaks because the auto generated code is more complex and hard to maintain.
It should be a collective decision of the project team whether to go for higher complexity of the code as a team.
This is where at Step2, I think the next generation build systems will go.
Saturday, March 21, 2009
How to connect to Tomcat to gather information from JMX MBeans?
Tomcat provides see of information through its JMX MBeans. It is also easy to connect to tomcat over JMX, just run tocat using the following options -
This will start the tomcat server on port 9999. Use these JVM options in the catalina.bat file - and keep them as JAVA_OPTS.
Once tomcat has opened this port, connect to tomcat using this sample code -
This will connect to the tomcat server running on localhost with port 9999.
To fetch the value of an attribute - use the following code -
The above code will fetch the JVM version of the tomcat JVM using the standard runtime MBean of the JVM.
- -Dcom.sun.management.jmxremote.port=9999
- -Dcom.sun.management.jmxremote.ssl=false
- -Dcom.sun.management.jmxremote.authenticate=false
This will start the tomcat server on port 9999. Use these JVM options in the catalina.bat file - and keep them as JAVA_OPTS.
Once tomcat has opened this port, connect to tomcat using this sample code -
- JMXServiceURL jurl = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:9999/jmxrmi");
- JMXConnector jmxc = JMXConnectorFactory.connect(jurl, null);
- MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
This will connect to the tomcat server running on localhost with port 9999.
To fetch the value of an attribute - use the following code -
-
mbsc.getAttribute(
new ObjectName(ManagementFactory.RUNTIME_MXBEAN_NAME, "VmVersion");
The above code will fetch the JVM version of the tomcat JVM using the standard runtime MBean of the JVM.
Sunday, January 25, 2009
Trie in Java
Trie is a good datastucture, details can be found here - http://en.wikipedia.org/wiki/Trie . I came across Trie while performing "Prefix" based searching. Have you ever Wondered what Data Structure is used behind the Mobile SMSes ? When we type SMS, it finishes the words or proposes what could be the end of the word. They are usually done by Trie or data structures like Trie.
Trie consumes less space so they are hot favourite data structures on PDAs or devices with less memory. Searching of a string in Trie takes around O(m) where m = length of the key. Compare it with Binary Tree which is O(logn) where n = number of elements in the tree. For a dictionary, this n could be real large.
Here is a simple implimentation of the Trie..
One class is called Node class which holds the Key and the value.
}
And the Main Trie class which builds the tree and finds any key...
package com.ds.trie;
Trie consumes less space so they are hot favourite data structures on PDAs or devices with less memory. Searching of a string in Trie takes around O(m) where m = length of the key. Compare it with Binary Tree which is O(logn) where n = number of elements in the tree. For a dictionary, this n could be real large.
Here is a simple implimentation of the Trie..
One class is called Node class which holds the Key and the value.
- public class Node {
- private char key;
- private String value;
- private List
children; - private boolean isTerminal;
- public boolean isTerminal() {
- return isTerminal;
- }
- public void setTerminal(boolean isTerminal) {
- this.isTerminal = isTerminal;
- }
- public Node(char key,String value)
- {
- this(key,value,new ArrayList());
- }
- public Node(char key, String value, List
children) { - super();
- this.key = key;
- this.value = value;
- this.children = children;
- }
- public char getKey() {
- return key;
- }
- public void setKey(char key) {
- this.key = key;
- }
- public String getValue() {
- return value;
- }
- public void setValue(String value) {
- this.value = value;
- }
- public List
getChildren() { - return children;
- }
- public void setChildren(List
children) { - this.children = children;
- }
- public void addChildren(Node child)
- {
- this.children.add(child);
- }
}
And the Main Trie class which builds the tree and finds any key...
package com.ds.trie;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Queue;
- public class Trie {
- private Node root;
- private File fname;
- public Trie (String filename)
- {
- this.root = new Node('r',"root");
- this.fname = new File(filename);
- }
- public void buildTrie()
- {
- try {
- if (!fname.isFile())
- throw new FileNotFoundException("");
- BufferedReader f = new BufferedReader(new FileReader(fname));
- String key;
- try {
- while ((key = f.readLine()) != null)
- {
- // making value = full name only = key
- build(key,root,key);
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- // key = shamik
- private void build(String key,Node root,String value) {
- // TODO Auto-generated method stub
- char c = key.charAt(0); // c = s
- String tail = null;
- if (key.length() > 1)
- tail = key.substring(1); // hamik
- Node resultNode = findKeyinChildren(root,c); // find 's' in the children
- if (resultNode == null)
- {
- // this is a new key
- Node newNode = new Node(c,value);
- if (tail == null)
- newNode.setTerminal(true);
- else
- newNode.setTerminal(false);
- root.addChildren(newNode);
- if (tail == null)
- {
- return;
- }
- else
- build(tail,newNode,value);
- }
- else // got the key - s
- {
- if (tail == null)
- return;
- build (tail,resultNode,value);
- }
- }
- private Node findKeyinChildren(Node root, char c) {
- // TODO Auto-generated method stub
- List
list = root.getChildren(); - if (list == null || list.size() == 0)
- return null;
- for (int i=0; i < list.size(); i++)
- {
- Node oneNode = list.get(i);
- if (oneNode.getKey() == c)
- return oneNode;
- }
- return null;
- }
- public List
findKey(String key) - {
- String value = null;
- List
values = new ArrayList (); - Node resultNode = findinTrie(key,root);
- if (resultNode == null)
- {
- value = "Not found";
- values.add(value);
- }
- else
- {
- printTree(resultNode,values);
- }
- return values;
- }
- private void printTree(Node resultNode,List
values) { - // TODO Auto-generated method stub
- List
list = resultNode.getChildren(); - if (resultNode.isTerminal())
- values.add(resultNode.getValue());
- if (list.size() == 0)
- {
- return;
- }
- else
- {
- for (int i=0; i<list.size();i++)
- {
- Node oneNode = list.get(i);
- printTree(oneNode,values);
- }
- }
- }
- private Node findinTrie(String key, Node root) {
- // TODO Auto-generated method stub
- // Implementing the Breath First Traversal
- char first = key.charAt(0);
- String tail = null;
- if (key.length() > 1)
- tail = key.substring(1);
- Node resultNode = this.findKeyinChildren(root, first);
- if (resultNode == null)
- {
- return null;
- }
- else
- {
- if (tail == null)
- return resultNode;
- else
- resultNode = findinTrie(tail,resultNode );
- }
- return resultNode;
- }
- public static void main(String[] args)
- {
- if (args.length <= 0)
- {
- System.out.println("Usage : Trie
"); - System.exit(1);
- }
- Trie trie = new Trie("C:\\test\\dictionary");
- trie.buildTrie();
- System.out.println("Going to look for " + args[0]);
- List
vals = trie.findKey(args[0]); - for (int i=0; i<vals.size(); i++)
- {
- System.out.println("possible value = " + vals.get(i));
- }
- }
- }
Thursday, January 15, 2009
Job Interview Question Part 2
For a tree, find out the largest possible value from the root to the leaf.
private int maxResult = 0;
public int findMaxRoot(Node root)
{
int result = root.getValue();
List nodes = root.getChilds();
if (nodes.size() == 0)
{
return result;
}
else
{
int maxChildTreeValue = 0;
for (int i=0; i {
//result = root.getValue();
Node oneNode = nodes.get(i);
result = findMaxRoot(oneNode);
if (result > maxChildTreeValue)
maxChildTreeValue = result;
}
result = maxChildTreeValue + root.getValue();
if (result > maxResult)
{
maxResult = result;
}
//maxResult = maxResult + root.getValue();
}
return maxResult;
}
private int maxResult = 0;
public int findMaxRoot(Node root)
{
int result = root.getValue();
List
if (nodes.size() == 0)
{
return result;
}
else
{
int maxChildTreeValue = 0;
for (int i=0; i
//result = root.getValue();
Node oneNode = nodes.get(i);
result = findMaxRoot(oneNode);
if (result > maxChildTreeValue)
maxChildTreeValue = result;
}
result = maxChildTreeValue + root.getValue();
if (result > maxResult)
{
maxResult = result;
}
//maxResult = maxResult + root.getValue();
}
return maxResult;
}
Job interview question Part 1
public static int getCountOfOnes(int n) {
/*
Please implement this method to
return the number of '1's in the binary representation of n
for any integer n, where n > 0
Example: for n=6 the binary representation is '110' and the
number of '1's in that
representation is 2
*/
int count = 0;
int a = 1;
for (int i=1 ; i < 31; i++)
{
int val = n & a ;
if (val > 0 )
count++;
a = a << 1;
}
return count;
}
This is an ideal question to find out whether the developer is aware of bitwise operators ... logic is to perform bitwise and and see the value. If the value if more than 0, then that position contains 1.
Example 6 = 110
Perfor bitwise & with 1, result is 110 & 001 = 0 so there is no 1 at this position
bitwise & with 2 (10), result is 110 & 010 = 010 so there is one 1 at the second positions etc..
/*
Please implement this method to
return the number of '1's in the binary representation of n
for any integer n, where n > 0
Example: for n=6 the binary representation is '110' and the
number of '1's in that
representation is 2
*/
int count = 0;
int a = 1;
for (int i=1 ; i < 31; i++)
{
int val = n & a ;
if (val > 0 )
count++;
a = a << 1;
}
return count;
}
This is an ideal question to find out whether the developer is aware of bitwise operators ... logic is to perform bitwise and and see the value. If the value if more than 0, then that position contains 1.
Example 6 = 110
Perfor bitwise & with 1, result is 110 & 001 = 0 so there is no 1 at this position
bitwise & with 2 (10), result is 110 & 010 = 010 so there is one 1 at the second positions etc..
Thursday, January 8, 2009
Java Vs C++ an analogy
I think any C++ developer tend to think when they move to Java - what they gained and what they lost. I was thinking about Java Vs C++ in terms of memory management and this thought came to my mind. I would like to describe that - working in C++ as if you are preparing food at home vs working in Java is as if you are having your food at a restaurant.
The difference between java and c++ comes clear if we think of having food at a restaurant vs preparing and having food at home. When we have food at restaurant we do not worry about where from the plates are coming (think of who allocates the memory) and who will wash them after having food (think of who frees the memory). So the good think of working in Java or having food at restaurants is that we are free from some house hold activities which are otherwise boring and error prone.
But the actual strength of preparing food at home is that - we can do anything with the food. It all depends upon us to determine how much salt or pepper we need to use in the food. Compared it with having food at restaurant where we have little control over the ingredients of the food (of course I am not referring to open kitchen style of restaurants).
So C++ might still be stronger than Java as there are more freedom given to the developer as what he wants to do - but the developer should be pretty good in it otherwise go out for dinner :-)
The difference between java and c++ comes clear if we think of having food at a restaurant vs preparing and having food at home. When we have food at restaurant we do not worry about where from the plates are coming (think of who allocates the memory) and who will wash them after having food (think of who frees the memory). So the good think of working in Java or having food at restaurants is that we are free from some house hold activities which are otherwise boring and error prone.
But the actual strength of preparing food at home is that - we can do anything with the food. It all depends upon us to determine how much salt or pepper we need to use in the food. Compared it with having food at restaurant where we have little control over the ingredients of the food (of course I am not referring to open kitchen style of restaurants).
So C++ might still be stronger than Java as there are more freedom given to the developer as what he wants to do - but the developer should be pretty good in it otherwise go out for dinner :-)
Subscribe to:
Posts (Atom)