Syndicate This Blog
Thursday, October 12. 2006
I was thinking a long time about doing this. Writing an opinion about runtime agains checked exceptions. I decided to do it, just to form my own opinion and start doing it consistent. Please do not start the debate here, it is just my opinion. I will point you to a number of resources that can help you form you own opinion.
The reason to introduce Checked Exceptions in java is to assure that people do something with exceptions. This is made sure by the compiler. However there are a lot of side effects to this theory. Bruce Eckel has a very good entry on his blog, I quote one of his sentences that sais it all:
When someone is trying to do something and you are constantly praddin them with annoyances, they will use the quickest device available to make those annoyances go away.
One of the anti patterns is born: catch (Exception e), swallowed exceptions.
A disadvantage of Checked Exceptions is all the code you must write or rethrowing exceptions. This way you throw exceptions in layers that do not belong there or you catch exceptions and throw a new other exception. And a new anti-pattern is born. Runtime exceptions just go up untill the main thread, or you can catch them if you can can do something about it. The important part, if you do nothing you will get an exception at the top, you are not tempted to do the catch Exception part.
The blog item of Tim McCune is also a very good article about exception handling. This article states a number of anti-patterns that you will find in your code. You do not have to agree on all the items, but you can learn a lot of them. Some rules of this website that I agree upon are:
I also have some ideas of myself, I adhere to the following rules:
Of course this is not a very extensive post, but it does give an idea about how I feel about checked exceptions.
Thursday, August 31. 2006
I have created a lot of small applications that include spring and acegi. Most of the time I copy most of the resources from another project. Some time ago I learned about the maven archetypes. An easy way to create the project structure. That was the time I decided that I wanted to have my acegi springframework archetype. Last weeks I found some time to create this archetype. You can download the files here:
And the generated maven site here:
Ofcourse there is also a maven website about creating an archetype, this is not a very extensive website. In this blog item I will explain more about the archetype I created.
First thing to know, well surprise, the archetype is just a maven project. You can start you new archetype by using a maven archetype:
Have a look at the pom for the other properties; scm, distributionManagement, etc.
The project itself contains an src\main\resources\META-INF\maven\archetype.xml file that defines all the resources that need to be copied.
The structure is pretty easy, you can find all the resources under the directory:
You can use the following parameters in your file as placeholders:
http://maven.apache.org/plugins/maven-archetype-plugin/create-mojo.html. Have a look at the src\main\resources\archetype-resources\pom.xml for an example:
Hope that helps you on your way to your own archetype. If you just want to use mine:
Wednesday, August 2. 2006
After this rather long introduction, time to get the coding done.
Get your environment in place. Run the maven archetype command to create a webproject template. Alter some of the default values to make it your project and put in dependencies on the following libraries to your pom.xml file:
Execute the command to setup your eclipse environment and startup eclipse.
Time to explain a little bid what the application is going to do. Actually there is two phases in the application. First search for a known postalcode. If you have found the right one and pushed the search button, the accompanying address is obtained. Sounds easy doesn't it?
We are going to expose a service object with two methods:
It should be obvious what the implementation of this interface should do. To be able to do an easy test, I created a mock implementation for this interface. The mock contains a hashtable with some postal codes as a key and the belonging address as the value. The two methods mentioned before use this hashmap to return all postalcodes or the address for a specific postal code.
Fianlly there is a transferobject called AddressTO that contains the following 4 fields: streetname, houseNumber, city, postalCode. Download the source to have a look.
We have created the interface and an implementation. Time to do some spring configuration. We only need to create one bean, therefore the config file is very easy
After this everything is in place to configure dwr to expose the spring bean. To be able to use dwr, we first need to add the dwr servlet to the web.xml file and load the spring context:
Everything is ready to do the first test. Deploy the application to tomcat and call the following url: http://localhost:8080/ajaxdwrsample/drw
You should get a page with a link to AddressServiceFacade, click this link and have a look at the next page:
test page of dwr
On this screen you can test your dwr ajax service. Use the execute buttons and input boxes to test the services (see the red box). Great isn't it?
And now for the webpages. First add the script libraries to your war file. I added the Prototype script and the script.aculo.us. to the folder "scripts". Next to that I added to style sheets (I copied all these files from the sample by Bram Smeets). Out of the box there is no support in script.aculo.us for autocomplete via dwr. Therefore I used the excellent component from Bram to implement it. The component resides in the "autocomplete.js" file. Finally the index.jsp that implements the real automcomplete component.
The constructor for the autocomplete component has 4 parameters:
The callback function looks like this:
Doesn't look very difficult, does it? Beware of the function nameValueSelector, it receives a tag object. This can be a simple object (like in my case, a string). It can also be a javabean representation, then you need an extra method to obtain the property to show in the autocomplete box. If you return a list with AddressTO objects and want to show the postalCode of the address, this line would be : return tag.postalCode.
The autocomplete box will now look like the following image:
The id's of the span elements resemble the fields of the AddressTO javabean. With the help of the dwr utility package we can easily fill these spans with data obtain via dwr. This is done with the following to functions.
Download the sources here : ajaxdwrsample.zip
That's it, hope you like this article. Thanks again to Bram Smeets for his autocomplete component and of course the excellent dwr and script.aculo.us frameworks.
Thursday, July 13. 2006
This is not the first luntbuild article on this website. I have been working with luntbuild for a while. Still it was only recently I found out the possibility to extend luntbuild. It struc me how easy it is. This article is about the luntbuild exntesion. You can combine it with the cobertura-luntbuild article to fill in the missing link in that article.
First some luntbuild theory, how to extend the ognl possibilities? Create a bean, put it in a jar and add a properties file named luntbuild_extension.properties to this jar with the following contents:
The bean is just a plain javabean with some public methods. You can create a method that checks if the report directory is available and another method that points to the page to start if you want to have a llok at the reports. This would look like the following:
These methods can be called from luntbuild. There are two important locations where the ognl expressions are used:
Lets start with the ant configuration within Luntbuild:
Pay attention to the coberturaHtmlReportDir here you can see how to pass output from the added bean to ant. From the context you can reach the class 'com.luntsys.luntbuild.web.BuildViewer'. This class has a method getBuild, this class returns a 'com.luntsys.luntbuild.db.Build'. From there there is the method getSystem, this returns the OgnlHelper object. This object has a method getExtension that takes the name of our object CoberturaIntegration as a parameter. Now you can call the methods of your own bean which are called via reflection. Now ant knows where to copy the report files to.
The reports are copied to the right place. Now we want to create a link in the build view if the reports are available. Let us have a look at the tapestry code.
With the previous explanation you should be able to understand this code. Find the junit link and put it right in front of it. Thats it. Now you should be able to create your own screen like the following.
Finally you are done, cool stuff !
Sunday, June 25. 2006
Finally I found some time to start experimenting with a webservice. I know I am not the first to write about creating your own webservice, still I think this is worth reading. I am writing a step by step creation of a webservice via maven2, axis 1.4 and spring 2.0RC1. Ofcourse you can use older versions, but I want to be ready for the future and keep on using this sample. For now I am working with a HelloWorld sample. I will make it more complicated when needed to show interesting things.
Download the sources here
Step 1 - Configuring your maven project environment
We create a new directory to store all projects : ~/WebservicePoc
Within this directory use the archetype of maven to create a structure for a web project:
The groupid is used within the create pom. The artifactId is used as project id and to create the folder to store all files in. The archetypeArtifactId defines the type of structure to create, in our case a web project.
Now it is time to create the pom in the root project directory. Beware if you create this pom first, the archetype:create won't work. Execute the command in a temporary directory and copy the files into the sub-directory of the module we are creating.
For a complete look at the pom, have a look at the downloadable source code.
We also need to make some adjustments to the generated pom of the webservice-jaxrpc-style module. Again have a look at the sources. Time to try the first compile. Execute the following command in the root of the project:
Step 2 - Decide upon the libraries that we are going to use
For the application we are going to create we are going to use some libraries. We do not want to code ourselves to much. We are going to use the spring library and axis. Add these to the module pom.
You will notice a problem with spring and maven 2 at the moment of writing. Maven provides the solution in the error message, we need to install the maven library manually. There aresome other options, but for now this is the easiest one. First download spring version 2.0 RC1, than issue the following command:
Step 3 - Time to get the eclipse wtp to work
I want to deploy the webservice via tomcat and ofcourse I want a nice environment for debugging, deploying, and developing. I like to work with spring WebTools project, there is a pretty good integration with maven from the maven perspective. Lets use that. Fire up your eclipse (with wtp) and create a workspace. Do not create this workspace inside our projects, do it on another part of the disk. After creating this workspace, add the maven repository as a variable by using the following command:
Now let maven generate the eclipse project and classpath files, issue the following command:
You are ready to import the project into eclipse, "File > Import > Existing projects into workspace"
step 4 - Lets publish a webservice
The webservice is deployed within a web container. Both axis and the spring container run in a servlet. There is a connection between the two via the servlet endpoint which will be discussed in a while. These servlets are configured in the web.xml file. The template for the file is generated by maven, lets add the servlets, servlet mappings and a spring specific parameter.
This parameter specifies the locations where spring can find the configuration files for the container. The applicationContext.xml contains the definition of the beans in the Business service domain. There is another configuration file for spring thats needs to be present. This is the default spring config file based on the name of the servlet that configures the spring container. In our case this file does not contain any beans, but it must be available. It must be called action-servlet.xml since we have given the spring dispatcher servlet the name action. The file should reside next to the web.xml in the WEB-INF folder.
Here you can see the spring dispatcher servlet and the axis servlet. Most important to notice is the load-on-startup parameter that tells the order of loading the servlets. The axis servlet connects to the loaded spring context via the special endpoint. Therefore the spring servlet needs to be loaded first. You do not need the dispatcher servlet if you plan to expose only webservices. If you also want to include spring mvc or the like, you do need the dispatcher servlet.
Next task is to configure the axis webservice, this is done via the server-config.wsdd file.
We are creating a jaxrpc style webservice with the name JaxrpcwebserviceProvider. Axis should redirect all calls to this service to the endpoint of type ServiceProvider. This is a subclass of a special spring implementation of the servletendpoint, this class is will be discussed later on. As you can see from the wsdd file, all public methods are exposed via the webservice. As a transport object we use the MessageBean.
Now all configuration is in place, time to do some real coding. We need to implement the following classes and interfaces.
MessageService and MessageServiceImpl - Business service interface and implementation
MessageBean - Object returned via the jaxrpc webservice
RemoteMessageService - Remote interface that is implemented by the service endpoint and therefore implemented by the webservice.
ServiceProvider - the special webservice endpoint that contains access to the laoded spring context. The endpoint contains the method that is called via axis.
The most important class is the endpoint, this is the class that gets called by axis. The spring superclass takes care of obtaining the spring web context. You can use this context to find spring wired beans. In our case we use it to find the Business service object MessageService.
Time to fire up the server. You can use the run on server with a right mouse click on the project within eclipse wtp. Popup a browser and go to the following url:
That should give you an overview of all deployed services, probably only the JaxrpcwebserviceProvider.
Step 5 - Using xml spy to send a message via soap to the webservice
The webservice is deployed, you can have a look at the wsdl file, nice. Now we want to call the webservice. We can write a special java client. There is however an easier way with xmlspy. Since this blog item is all about exposing the webservice and not about creating a client, I will use xmlspy.
Fire up xmlspy and use the menu: SOAP > Create new soap request and enter the next url in the pop-up box:
After clicking oke, choose the getMessage() operating name, the following request is generated:
Then we can send the request over soap via xmlspy: SOAP > Send request to server
That concludes this article, I learned a lot while writing this item, hope you learned something while reading. The coming weeks I want to write about creating a client, doing document style webservices, using spring webservices component and security. Stay tuned.
Friday, February 3. 2006
After playing around with continuum I am back on my luntbuild feet. I really tried continuum, good community by the way, but the maven site integration is not really possible. That is at least a little bit strange. The same is valid for labelling builds. Why this introduction for a luntbuild maven story, well, with luntbuild it is possible. Maybe not perfect but possible.
Let me explain, luntbuild does have maven 2 integration, I wanted to be able to copy the results of the site target to the luntbuild artifacts directory. This is not possible out of the box. But with a small experiment I created a maven mojo that does the trick. First disadvantage, you need to provide an extra part to the pom that is only needed for copying the site. I do want to do some more research whether this was really necessary, but for now it works.
So what is in this blog item? I will give you some details on creating a plugin for maven. Then Ill present you with the configuration of luntbuild to pass parameters to maven by using environment variables. In the end just a screenshot with the artifacts of the different maven projects.
Creating the maven plugin, or mojo
There is a guide available on the maven website. Read this first, then come back for the step by step approach.
Use the appropriate archetype to create the project with maven:
Compile the project, create the eclipse project space and start up eclipse
An example for a plugin is allready available in the generated project. We will create our own by subclassing the AbstractPlugin class. I do not want to teach java specifics in this blog item. Therefore just the code:
If you have read the guide I mentioned, you now the javadoc attributes are important. These are used to set the parameter values and the name of the goal that is created.
Having done that, we can package and install the new goal and test it.
You probalby get an error message, that is because you are missing a part in the pom. You need to define the outputDirectory for the move goal.
Running the command again should result into the following:
Actually that is all there is to it. In order to shorten the amount of typing needed on the command line, you will need to add your plugin's group ID to the list of group IDs searched by default. To do this, you need to add the following to your settings.xml file:
Now you can run the following command and the result should be the same.
Integrating maven 2 with luntbuild
There a re two important screens in luntbuild with respect to maven:
Finally you have to prepare you pom(s):
If you are having problems, the sample is available at:
Beware that browsing source code from the javaforge website does not work very well, it does not refresh.
Hope this helps, please drop me a comment if it does help or if you are heving problems.
Sunday, January 29. 2006
Today I was experimenting with maven. I wanted to add code coverage. Ofcourse I start thinking about cobertura immediately. So I dived into cobertura and maven integration. It became kind of an experience. I know a lot more about maven now. I have to figure out some stuff, but for now it is good enough. This entry gives some steps to help you get up to speed with cobertura.
1. Install maven 2
quick start guide maven
2. Create an application that knows how to handle the goals package and site.
see the quick start guide
3. The fun part begins : compile and install your own cobertura plugin.
Download the sources from the codehaus subversion repository. You can use the following url to download them anonymously with a tool like tortoisesvn
Then you have the sources. Now you need to alter the pom of that project to be able to check for latest versions. See the guide on the plugins website.
Issue the following command for the cobertura-maven-plugin project you have just downloaded and altered.
Now you are ready for the next step
4. Configure the pom.xml of your project to create the cobertura report.
There are two aspects, first you need to add the plugin to the build part, then to the reports part. See the usage page of the plugin home page
5. Run the site goal and start browsing your coverage files.
This is where I am having problems, it does work but you must do some parts alone. First I will give you what works, then I will give you what I am having problems with.
The first thing that gives problems is doing the code coverage on the top project that does not have sources itself. An error due to the fact that target/classes is missing is thrown. The second problem I have is that I cannot do the two commands at once, there is a class loading issue.
I want to emphasize that this is a beginners perspective. The issues I am having might not be the complete truth. If you now more, or if I find out more, I will revise this part.
You can find the sources at my sample project at javaforge.
Friday, January 13. 2006
Yesterday I had an issue with some of the xml validations. We all know those DTD definitions in an xml document where the dtd is on the internet. We do want to use xml validation via the dtd but the dtd cannot be found if you are not on the internet. How can we resolves this?
How can you add this dtd to eclipse?
Open the Preferences window of eclipse and go to:
MyEclipse > Editors > XML > XML Catalog
Now we need to add a user catalog. Push the new button.
Browse to the dtd (you can import it into your workspace at the same time. As a key enter the part after PUBLIC. -//SPRING/DTD BEAN//EN
There you have it, code completion in your xml editor.
Tuesday, December 27. 2005
There are a lot of resources around for maven. Then why this blog item? Well actually because I want to capture what I do, if someone can use it along the way it would be nice. Therefore this blog item is here. This item describes the merging of my student sample application into a maven project.
First steps for maven:
In short : download the classes from the sun site and issue the following command
Now we have a war file that we can deploy, I did not test it yet. I do have some more issues to figure out. While creating the war file, all libraries are copied to web-inf/lib, also all dependencies. Therefore I now have spring.jar and all components jars like spring-core, spring-aop, etc as well. So double jars, to much memory, etc.
Lets move on and try to get the maven site up and running.
I must add versioning and maybe some other parts as well, but for now it is enough. In the near future I want to do more with the libraries, I do not want double spring jars etc, and I want to keep foolowing the reports. These reports are not working correct at the moment en they must be improved.
Monday, December 5. 2005
Today I upgraded the acegi library to version 1.0.0 RC1. It was a quick process, but you do need to make a few changes. The complete package structure is changed, JdbcDaoImpl class has been moved and some other changes as well. Have a look at the release notes in the downloaded acegi package. There was a nice addentum on the spring forum that you need to change the inclusion of the taglibrary as well.
<%@ taglib uri="http://acegisecurity.org/authz" prefix="authz" %>
Have fun, I will start documening the sample within a few weeks now.