February 28, 2010

Using JAX-WS in a Web Archive, as a Servlet, Running on JBoss 5.1

In my previous blog I showed how to get started with JAX-WS in a EJB archieve, in this blog I will show you how to do the same thing, but in a web archive instead, as a Servlet. If you think about it, the two different approaches ends with the same result – a Servlet that answer HTTP request with SOAP content.

Now lets create a simple web archieve with Maven archetype plugin.



Now lets add EJB dependency and JBoss Maven plugin that will do our deployment work.



Then we continue with our Web Service class.



Not that we don't need to define a interface. Then we write our web.xml, that points to our concrete Web Service class.



Finally we need to add the JBoss specific web deployment descriptor. Note the location of the file – src/main/webapp/META-INF/jboss-web.xml, i.e. not in the WEB-INF directory, beside the web.xml.



Now we are ready to build and deploy our archieve



Then we start our JBoss server and opens a web browser pointing to http://localhost:8080/ws-web/personServiceBean?wsdl and checks that the web service is working. Instead of building a web service client by hand, we use the soapUI tool.

 

 

Now soapUI has created us a testcase that we can double click and click the green play button on.

February 25, 2010

How To See Expiration Date and other Information of a p12 File (PKCS12).

Sooner or later your client or server certificate will expire and it will be nice to update that before it really happens, but to do that you need to know how to get that information. In this blog I will show you how to do that in a Linux environment with openssl, that is a typical scenario when the certificate is located on a remote Linux server that you access with ssh.

First thing to do is to convert the p12 file (PKCS12 format) to X509 format, to do so we use the openssl command.



When prompted enter the password. Now is the content printed out, find out which CN (Common Name) that is for the server, typically it is the dns name of the server. After located the certificate then copy everything between the below start and end tag,



Past the copied content into a file with prefix .crt. If you were on a Windows system you could now simple double click the crt-file and the information of the certification would be display, but here we will continue using openssl. To use openssl, use the below command using the file name of the file you pasted the certificate into.



Now is the expiration date printed among other information.

February 21, 2010

Configure Apache Web Server as Reverse Proxy in front of a Tomcat or J2EE Container

One production like scenario when using SSL or load balancing is to use a Apache Web Server in front of a Java Web Container or J2EE Container. There are two ways to make the Apache Web Server and the behind Java container – mod_proxy or mod_jk. In this blog I will show you how to use the mod_proxy alternative, acting as a reverse proxy. I will also use Ubuntu to show you how easy there is to install software on Ubuntu with Synaptic Package Manager.

The first thing to do is to install Apache Web Server and the Tomcat Container.



After installing the above software you can use the apache2ctl command to view the active sites configuration



or you can also list the files in /etc/apache2/sites-enabled



Before continue you can also check that everything is working by open a web browser and open http://localhost/ (Apache Web Server) and http://localhost:8080/ (Tomcat)

Now we go ahead and creating a new virtual host that is going to act as reverse proxy.





After creating the new site we need to link it from sites-enabled, either use ln -s or you can use the apache tool – a2ensite.



With the configuration in place, you need to separate the virtual host in customapp and default. For a locale environment you can separate them by using different IP – 127.0.0.1 and your computer IP.



The last thing before restarting the Apache Web Server is to install proxy and proxy_http modules you can do that with the Apache tool a2enmod



And finally you need to restart the Apache Web Server.

February 18, 2010

Getting Started with JBoss Web Serivce using JAX-WS on JBoss 5.1 using Maven, OpenEJB and eviware soapUI

The easiest way to get started with writing Web Service is using Java API for XML Web Service, JAX-WS. The downside with this is that the creation of the WSDL file is automatic, and that is not the preferred way of writing cross platform independent web service. If that is your design goal, you should handwrite the WSDL, i.e. contract first. But if you know that the only clients are going to be Java client, than automatically generating the WSDL could be fine.

A good practice when writing all kind of code, is to make the code testable and also make it testable outside the container. To achieve that I will use OpenEJB, if have never heard or used OpenEJB and you are writing EJB 3.0 applications, it is definitely worth the time to check it out.

Lets start with writing a standard Stateless EJB.





src/main/resources/META-INF/ejb-jar.xml


src/main/resources/META-INF/jboss.xml


pom.xml


Now lets test our EJB with OpenEJB. The first thing we need to do, is to add the OpenEJB dependency to our pom.xml



Then we write our JUnit test case.



After verifying that our Stateless Session Bean is working, we now continue to deploy it to the JBoss container.



We start our JBoss container and checks that the EJB is successfully deployed.

Now lets add Web Service support to our EJB.





Now lets write our JUnit test case, to test our web service outside the JBoss container.



Before running the testcase we need to update our pom.xml with a concrete web service implementation.



After running the testcase from withing Eclipse, we can now be confident that we our is executing as expected. Now lets deploy our EJB to the JBoss container.



If you have stopped the JBoss, then restarted and verify that everything is looking healthy in the server log. Verify that the web service is working by firing up a web browser and go to http://localhost:8080/jbossws-example/HelloServiceBean?wsdl, to verify that you see the wsdl file.

Instead of writing a web service client, we are going to use a good test tool for testing web service – eviware soapUI. Start the soapUI and then select new project.

 

  

 

February 17, 2010

Viewing/Monitoring your Log4j Generated Log File with Chainsaw

Chainsaw is a open source tool for viewing and/or monitoring Log4j generating system. But the documentation on the homepage lack some explanation how to get started. To begin with maybe the simplest scenario, to view a log4j generated log file. The first thing one might misunderstand is that you need to defines a new configuration file for Chainsaw, so just pointing to your log4j.xm configuration file is not enough. Lets start with a example.



And the configuration file for Chainsaw.



Before we open Chainsaw lets write a small test program, that generates some log post, so we have something to view.



Now lets open Chainsaw. When first starting Chainsaw you are prompted to define a Receiver. Here we select our configuration file.



After selecting our Chainsaw configuration file, Chainsaw opens our log file in a new tab.



You might have already noticed the different formatting of logFormat attribute, compared with the Log4j and it is a question to me why they have done that. You can read more about the format in the LogFilePatternReceiver Javadoc.

February 14, 2010

Sending Log Errors with Log4j and SMTPAppender

Log4j comes with several Appenders and if you like to send mail notification if severe errors occur the you will be pleased to see, that Log4j comes with a SMTPAppender, but there are some pitfalls when using the SMTP appender:
  • The SMTP apppender does not out of the box support a different SMTP port.
  • Using secure connection, such as SSL or TLS.
  • The derived standardized method setThreshold is ignored, and replaced with TriggeringEventEvaluator class.
Configure SMTP appender to use a different port, than the default 25
If your mailserver is configured to answer on a different port than the default 25 for SMTP, you need to configure the underlying javax.mail.Session property "mail.smtp.port". But when looking at the API for SMTPAppender you see no set method for that. What to do? Writing you own extended log4j class? No, that is not neccessary. If you look at the source code of SMTPAppender, you see the following in the createSession():



These means that SMTPAppender is reading system properties, which means you can add the following argument when starting you application.



This is a not gracefefully way, and sometimes also not applicable, because you are not in charge of the deployment environment. And what happens when you have two different application hosted on the same server, that wants to use different ports?
There is also the oppurtunity to do it programmatically:



This has also drawbacks. For examaple if your application is a J2EE application or a web application your code is not the main start class. You can of course always write a boot strap class, but what happens when you have several bootstrap classes, then things starts to get more hard to overlook for a more junior programmer.

Another way if you have problems settings the System Property is to create a class that implements the interface org.apache.log4j.spi.TriggeringEventEvaluator and there set the system properties and then in your log4j properties or xml file override the default EvaluatorClass for SMTPAppender. This is a little hacky, but it will get the work done and it keeps your code clean from log4j configuration code that should in the first place be placed in the log4j configration file.

Also what is lacking is, if you are deploying to a container you might want to define your java.mail.Session properties in the container and then bind them to JNDI, then it should be nice if Log4j could read these property from JNDI, but that is not possible.

Using Secure Connection
The next problem comes when your mailserver uses secure connection as TLS or SSL, then you need to add the following javax.mail.Session properties:



Bellow follows the source code for the example.









February 1, 2010

Java Generics Example Strategy Pattern

With Java Generics Java has continued its path to make it's language more strong typed, with comparison to all new dynamic language where they have thrown out strong typing. But Java Generics can also be hard to understand and one of the best example to show the main benefit with it is the strategy pattern. Lets start to look of a example.


If we were using Java JDK 1.4 we have to do down-casting in the concrete strategy classes, but with Generics that is not necessary.

The Strategy Context/Client


The Strategy itself



And the extended Data classes


And the extended Strategy


And our test class.