By Atif Khan (
January 1, 2008 at 8:22 pm)
· Filed under Java, Programming, Software, Web Service
The new and upcoming Apache CXF framework is quickly gaining steam and rightfully so. It’s very intuitive, simple to use and functional. Some of it comes from the fact the its roots are in popular XFire and Celtix frameworks. It’s still in the incubation stage at Apache but the current releases are pretty stable. The documentation is not complete right now, but it’s improving as well.
One of the cases where I found the document and example lacking is the POX (Plain Old XML) service using JAX-WS Provider model and configuring it using Spring. The examples are mainly focussed towards configuring and starting the service from Java code. So, I created this simple EchoService example that shows step by step how to do it.
First step is to create the service Java class that will echo the incoming XML message back to the requester. We will also annotate this class using the JAX-WS annotation to mark it as a web service provider and the message type to be the payload. So, the CXF implementation knows that it should only deliver the message payload to the service class. The CXF binding layer processes any binding level wrappers and headers.
In the above class we specified that we want to receive the incoming XML message as DOM. You can change it to use any concrete subclass of the Source interface (e.g. SAXSource).
Next step is to create the Spring configuration. For simplicity sake, let’s keep the name of this file to applicationContext.xml. Here is an example:
The key things to notice in the above XML are:
- Address of the service (/echo).
- Binding URI (http://www.w3.org/2004/08/wsdl/http). This is really important. This is what tells CXF that this service is going to be simple XML over HTTP.
- Declaration of service factor where we indicate that the messages are going to be of wrapped nature
All that’s remaining is to declare the Spring context listener in your web.xml for the web application that this service is going to be a part of. Here is an example of the web.xml:
As you can see that there is nothing special here at all. It’s all standard Spring configuration using the context loader listener. Also, we are declaring the CXF servlet here and mapping it to /* to make all the requests to this web application go through the CXF servlet.
Now all you have to do is to package this web application and deploy it in your favorite application server. You should be able to access the Echo Service we wrote at http://localhost:8080/AppName/echo (you will have to adjust the port and application name in the URL). Now HTTP post a XML to this URL and you should get the request echoed back to you.
Maven POM
I think it’s more useful to also specify the Maven POM that contains all the dependencies as well to build the run this example. Please keep in mind that some of the dependencies here may not be required anymore as I was playing around with a lot of CXF functionalities.
Here is a list of all the jar files that ended up being in the war file from the build using above Maven POM. Again, some of the jars here may not be needed.
Permalink
By Atif Khan (
April 16, 2007 at 6:46 pm)
· Filed under Java, Programming, Software
Sometime back I proposed a reversible enum pattern in my post. One thing missing from that implementation was the ability to successfully lookup the enum constants in cases where enum constants can have multiple values. To accomplish this, I utilized another feature introduced in Java 5, namely varargs or variable argument support. This way while building the key for the map, we utilize all the values for a constant and use the varargs in the reverse() method of the enum.
Let’s look at the interface MultiValueReversibleEnum that denotes an enum as reversible and that an enum should implement.
The things to notice in this interface are:
getAllCode() method - It returns an array of all the possible values for an enum constant
reverse( E ... code ) method - You can see the usage of varargs here. You basically give all the possible values for the enum constant you want to lookup in the right order
Next we need to look at the map
MultiValueReverseEnumMap that will store the enum constants and their value mappings.
As you can see, the getAllCodes() defined in the interface is used to build the key for the map. Also, the get( final K ... enumValues ) method that looks up the value in map uses varargs.
Now it’s time to glue everything together and see how to use the interface and map to build a multi-valued reversible enum. We will write a test class
MultiValuedEnumTest.
If you run this class, you should see following getting printed:
Reverse for [1, 11]: ONE
Reverse for [2, 22]: TWO
It’s all very simple and straightforward. If you have any suggestions or feedback to improve this, please leave me a comment.
Permalink
By Atif Khan (
April 10, 2007 at 3:05 pm)
· Filed under Java, Programming, Software, Web
I have been spending time on and off looking at various web frameworks available for a little more than a year. A lot of this has to do with keeping track of what’s going on in the web framework arena and keep in sync with the technologies. Another focus of mine is to see how the new (at least for me) frameworks affect the productivity of a developer.
I first looked at RIFE last year and I have been very impressed by the concepts.The developers of RIFE, Geert Bevin and others have been very helpful when I had some questions. I was even able to get answers on the IRC channel for RIFE. It was not until recently that I tried to write an application using RIFE that I really got a feel of it.
Here are some of the good and bad that I encountered:
Pros
- A complete framework. It includes basic web framework, ORM, Content Management, Validations, Templates etc.
- Good support from developers
- A nice quick start application to get you running quickly
- Good support for constraints for adding validations
- Continuations - ability to pause, rewind, jump through transaction steps
Cons
- Templates are really weird to work with. The syntax for the templates really did me. It is not really designer friendly like Tapestry or Wicket
- The concept of flowlink and datalink is just too verbose and not clear
- Using annotation didn’t do me any good as well for defining the flow and data links. There isn’t enough documentation for annotations
- The only way for form validations I could find was to define the a MetaData class equivalent to the domain object representing the form and define the validations in there. This just seems like way too much of work for simple validations.
- I could not figure out how to customize the error messages for form validations
In the end, I feel that it’s the lack of documentation that prevented me from really exploring it. I know Geert is a very sharp guy and if he ever gets to read my blog, I welcome his comments. I think the learning curve is too much for RIFE and lack of documentation adds to it and I am not sure if the return is proportional to the effort compared to other frameworks.
Permalink
By Atif Khan (
January 15, 2007 at 8:37 pm)
· Filed under Java, OpenLaszlo, Programming, Software
As the Web 2.0 grows in popularity, so does the urge for a lot of organizations to revamp their legacy applications and make them web based. Their are obvious benefits to this as the application becomes more accessible and requires less effort to deploy and maintain as there is single point of deployment and maintenance, the application server. What gets lost in all this excitement is the functionality that may have been available in the existing applications with rich user interfaces can not be easily replicated in the web application replicating it. By using the readily available javascript libraries and AJAX techniques some of this pain can be alleviated. It’s not necessary that web applications may always lose the usability of functionality of the original application. In fact they can actually simplify some of the transactions if the user experience is considered from beginning during the rewrite process.
I have been playing with
OpenLaszlo recently and it seems like a viable alternative for replacing the standalone applications with rich user interface. The most important feature of OpenLaszlo is the ability to write the user interface in XML based LZX language and export it to either Flash (.sfw) bytecode or DHTML. This functionality is only available with the upcoming Project Legals release of OpenLaszlo, which is already in beta.
It has a rich set of components and event model that can all be used and programmed to using the LZX language. You can also write your own components using the LZX markup. The important thing to keep in mind is that OpenLaszlo is strictly a UI technology. You still need to get your data to display from the backend whether it being database of another middleware. This is where OpenLaszo excels. At the core it can interact with a back end system using the RPC. Various RPC options include:
- Java RPC
- SOAP
- XML RPC
- XMLHttpRequest for Ajax
So, you can pretty much have you back end written in Java, PHP, Ruby or whatever you like as long as it supports either RPC, SOAP or XMLHttpRequest. If you are wondering about what do you need on your server to run OpenLaszlo, it is basically a Java Servlet based web application. So, you do need the Java servlet container on your server to run OpenLaszlo. That shouldn’t be an issue at all if you already use Java.
If you do use Java on the server side and you have been trying to decide between various web application frameworks available and there are a ton of them :), OpenLaszlo can make your life a little simpler as you don’t have to worry about the components or the component state management (JSF, Wicket, Tapestry etc). All you need is a solid framework that can preferably produce XML output and serve some data (Struts, WebWork, Spring MVC, RIFE) or any other similar framework and leave the UI aspect to OpenLaszlo. OpenLaszlo may not be suitable in all the cases, but it is worth a look if you want a web application with the look and feel and usability of a desktop application.
Permalink
By Atif Khan (
December 12, 2006 at 4:52 pm)
· Filed under Java, Programming, Software
A lot of Java applications run on Sun’s JDK due to various reasons. In fact till recently Sun had a very restrictive license policy for Java. So, effectively Sun’s JDK has been a de facto standard. That’s the reason Sun’s JDK code is supposed and expected to be of high quality and following at least the coding conventions recommended by Sun in a 1999 article that is widely used.
If you look under the hood of JDK, you will find a completely different picture. Here is an example from the Java 6 source code of StringBuilder class. (I have noticed similar stuff in 1.4 and 1.5 releases.)
How many non-conformance instances can you find? Here is what I notice:
- Indentation - not sure if it was edited in notepad (even vi does indentation) or a result of source control merge?.
- If clause doesn’t use brackets- Here is an excerpt from Sun’s Java coding conventions.
Need I say more?
- Creating “null” string instance- Why can’t it be represented by a static final constant and reused? All the string literals are stored in JVM in a String pool, even the ones defined as constants. Isn’t it more optimized if you have a direct reference to it rather than JVM doing a lookup in the pool every time you call the method?
Note: After looking at the bytecode generated by the compiler, it looks like a direct reference is pushed on the stack for the string literal. So, it effectively doesn’t matter if you use a literal or a constant. Still, if you are using a string literal over and over again, it may be a better idea to create a constant. This way if you need to change the string literal, there is only one place where you need to make change. So, you protect yourself against future changes. Thanks to Messi for pointing this out in the comments :).
- Variables can be made final-The method parameter
sb and local variables len and newcount can all be declared final for optimization. This article has good explanation for it.
Note: Again after analyzing the bytecode it seems like compiler really doesn’t care about the final declaration for the local variables. The method parameter are a different case as by declaring them final you make sure that they are not modified by mistake. Also, the local variables can be declared as final as a coding practice to mark them as immutable even though it doesn’t affect the byte code. But it makes it clear for a person looking at your code that the variable can not be modified. Thanks to Messi and Kevin Riff for pointing this out in the comments
This is just an example. There are instances like this all over the JDK code base. I want to make it clear that I am not commenting on the performance aspect of JDK, just the code conventions and formatting (for the obvious reasons that the code should be properly formatted). Now, since Sun has released the JDK source code under GPL license, do we expect to see things like this continue? Do you think people will take more responsibility and clean up the JDK code? I hope so. I also hope that Sun does not blacklist me after this post
Permalink