Friday 27 September 2013

Generating a Web Service Java Client from a given WSDL using Eclipse

Objective

To develop a Java web service client from a given WSDL using Eclipse IDE.

Environment

Java 7
Eclipse IDE (Juno)
A WS wsdl xml file

Development

  • Open your Eclipse IDE and Workspace.
  • Create a new Java Project (File -> New -> Java Project).
  • Give Project name as "TestWSClient" and click on Finish
  • Copy the given WSDL file under Project "TestWSClient" root folder.
  • Right click on Project and select New -> Other -> Web Service Client  and then Next.
  • A new window will come up, give Service Definition value by clicking on Browse -> Browser and then select the WSDL file we copied earlier into Project root folder. Click OK -> Finish.

  • Ensue that the actual WS for which the WSDL is present exists and running; otherwise we will get exception in above step. At successful creation we will see many java classes get created under project src folder with the name ending with Stub, Proxy, Location, etc.

Testing

  • Now the Java APIs are generated for the WS client, let us write a simple Java class to test how can we call operations exposed by WS.
  • Create a Java class with name "Client" (File -> New -> Class) and write code similar to what shown below in your main method:
AlarmingServiceLocator asLocator = new AlarmingServiceLocator();
asLocator.setEndpointAddress("AlarmPort",
                    "http://localhost:9001/alarmingService");
Alarm service = asLocator.getAlarmPort();
service.sendAlarm("test mahesh alarm");

  • What matters here is the right parameters on call to setEndpointAddress() method when called from Locator class. And then how you get the reference to your Service from Locator which will then be used to call the WS operations (here we called sendAlarm() operation).
  • In case you face some compilation or runtime exceptions just browse through the generated Location, Stub and Proxy classes to get the idea of how things work.
Thanks!

A HelloWorld OSGI Module using Ecipse Equinox OSGI Framework

Objective

To create a HelloWorld OSGI Module using Equinox OSGI Framework.

Environment

  • Eclipse IDE (Juno)
  • OSGI Equinox Framework

Development

  • Open you Eclipse IDE and Workspace.
  • Create a new Plug-in Project (File -> New -> Other -> Plug-in Project).
  • Give Project name as "HelloWorldModule". Select target as "an OSGI Framework: standard.
  • Select Next -> Next and choose template as "Hello OSGI Bundle"; then Next -> Finish.
  • You will see a Java class Activator (implementing BundleActivator) gets created with start() and stop() methods. Both the methods have BundleContext parameter passed, the BundleContext is a way through which we can interact with OSGI framework.
  • The Activator class start() and stop() method provide a way to define what should be done when the bundle is started and stopped while running under an OSGI framework.
  • You will also see few System out messages under start() and stop() method which will help us test when these methods are called.

Testing

  • Right click on project and select Run As -> Run Configurations..
  • Create a new OSGI Configuration by right clicking on OSGI Framework. Give name as HelloWorldModule and select following Bundles
  • Cock on Apply and Run
  • In Eclipse Console view the following message and prompt will appear
Hello World!!
osgi> 
  • Type ss on osgi prompt, this will list the current bundles installed and their status
osgi> ss
"Framework is launched."
id    State       Bundle
0    ACTIVE      org.eclipse.osgi_3.8.2.v20130124-134944
                Fragments=2
1    ACTIVE      org.apache.felix.gogo.command_0.8.0.v201108120515
2    RESOLVED    org.eclipse.equinox.weaving.hook_1.0.200.I20130319-1000
                Master=0
3    ACTIVE      org.apache.felix.gogo.runtime_0.8.0.v201108120515
4    ACTIVE      org.eclipse.equinox.console_1.0.0.v20120522-1841
5    ACTIVE      HelloWorldModule_1.0.0.qualifier
6    ACTIVE      org.apache.felix.gogo.shell_0.8.0.v201110170705

  • Now to see what happens when we stop a bundle; give command osgi> stop <bundl-id>. In our case it would be stop 5 since 5 is the bundle if of HelloWorldModule.
  • We will see following output on console
osgi> stop 5
Goodbye World!!
  • Now to exit from osgi prompt type exit and then y
  • So you noticed the message "Hello World!!" and "Goodby World!!" when the HelloWorldModule bundle started and stopped. In real world application this is where we would write the actual code which we want to execute during start and stop of a module.


A HelloWorld Servlet in JBoss EAP 6 using Eclipse

Objective

To show a simple JEE6 HelloWorld Servlet development

Environment

  • Eclipse Juno
  • JBoss EAP 6.0 Application Server
  • Java 7

Development

1.       Start Eclipse.
2.       Configuring Server
a.       (Go to Menu Window -> Preferences)
b.      Select button Add, this will open a new window
c.       Select Server “JBoss Enterprise Application Platform 6.0 Runtime”. 
In case the server is not listed please download the one via selecting “Download additional server adapters” link shown on top right corner.
d.      Specify correct path for JBoss EAP 6.0 and right JRE
e.      Click on button “Finish” and then “OK” on previous window.
f.        Open Servers view (Window -> Show View -> Servers)
g.       On Servers view click on “new server wizard...” link
h.      A “New Server” window will open
Ensure the “JBoss EAP 6.0 Runtime” Server runtime environment is selected and click on Finish.
i.         Specify correct path for JBoss EAP 6.0 and right JRE

3.       Create a Dynamic Web Project (File -> New -> Dynamic Web Project)
4.       Give project name as “HelloWorldServlet”
Click on Finish.
5.       You should see the project “HelloWorldServlet” created. Now right click on project and click New -> Servlet.
6.       Specify Servlet name as “HelloWorldServlet” and package as “com.test”
Click on Finish.
7.       Make following changes on doGet() and doPost() method as:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   PrintWriter out = response.getWriter();
   out.print("<html><body><b>Respons from HelloWorld Servlet</b></body></html>");
   out.close();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
              doGet(request, response);
}
8.       Right click on project and select “Run As” -> “Run on Server”
Click on Finish.
9.       You should see “Console” view showing server startup logs. Once server is started successfully a browser window will open with URL http://localhost:8080/HelloWorldServlet

Testing

1.       You must hit the URL http://localhost:8080/HelloWorldServlet/HelloWorldServlet either in Eclipse Web Browser window or any other Web Browser you have.
2.       Once hit the browser must show message “Respons from HelloWorld Servlet
Congratulations this completes your HelloWorldServlet which is a Servlet 3.0 Spec compliant running in JBoss EAP 6.0.