Getting SOA Started w/ Java, GlassFish, Metro, and Eclipse

by Brian R. Miedlar.   Last updated on 11/9/2007.

A step-by-step approach to getting a development environment implemented to use in a service-oriented architecure.

The components in this implementation will be as follows:

  • Java SE Development Kit
  • Eclipse 3.3 w/ WTP 2
  • GlassFish Java System Application Server
  • Java Metro

Following is a guide to setting up the web server development environment. In this case, the web server will be on the same machine as the programming tools. This allows for the programmer to have a test environment when designing his programs.

2.1 Service Oriented Architecture Overview

The architecture we are focusing on is that of a service-oriented architecture. Figure 1 represents the typical service points of communication. The applications will be talking to databases. The users (consumers) of the applications will be talking to the web servers (producers). Additionally, the applications may be talking to each other.

2.2 Web Server Architecture

Each instance of a web server will have a defined architecture to produce the request/response service to the consumers. This is the web server definition for this architecture:

2.1 Service Oriented Architecture Overview

The architecture we are focusing on is that of a service-oriented architecture. Figure 1 represents the typical service points of communication. The applications will be talking to databases. The users (consumers) of the applications will be talking to the web servers (producers). Additionally, the applications may be talking to each other.

2.2.1 A - Language Runtimes

This is the base layer of your application. This layer provides all of the fundamental libraries and utilities needed to program, compile, and execute your application.

In this architecture we are choosing Java as the main server language, so in this case we will use JRE 1.5/1.6 as the language runtime.

2.2.2 B - Business Logic Libraries

In the object oriented design practice, most of business logic should be placed in separate libraries so they can be used by separate applications. These will also require the language runtime in order to compile. The business logic libraries will be referenced by the Web Application as it presents data to the http consumer.

2.2.3 C - Application Logic

This layer has been simplified in the figure. This logic layer will contain a design pattern that works alongside the business logic libraries. This layer will contain techniques to work with events from the consumer, control and work with the data model, and produce data for the consumer.

2.2.4 D - Web Service Hooks

This is an extension to the application that will allow hooks into program routines. These hooks will be made available as additional web handlers that allow extra entry points into the program.

In this architecture we are choosing Metro as the web service provider. Metro is a new component in Java JRE 1.6, but can be included in JRE 1.5.

2.2.5 E - Web Handlers

All requests from the http consumer will be sent through web handlers. The handler’s job is to take in the http request and produce a data response to return. These are the entry points to your program. One may have html response based entry points such as a JSP handler. Additionally, entry points may exist through web service handlers. Data response is not limited to HTML, but can be in any form of data.

2.2.6 F - Http Server

This is HTTP server of the web applications. The server listens on a chosen socket for http consumer requests. These requests will be forwarded to the appropriate web application and the produced response from the application will be relayed back to the consumer.

In this architecture we are choosing GlassFish as the http server because it provides the latest web service support and security. GlassFish and Metro together are robust enough to communicate to other software programs such as .Net.

2.2.7 G - Web Application

The web application contains the application logic, referencing the business logic needed. This application is deployed to the http server in a compiled form. Many web applications can exist on one http server.

2.2.8 H - Web Server

The web server contains an http server that can listen on multiple ports for requests from a consumer.

3 Installation

Following is a guide to setting up the web server development environment. In this case, the web server will be on the same machine as the programming tools. This allows for the programmer to have a test environment when designing his programs.

Components in this environment to be installed:

  • Java SE Development Kit
  • Eclipse 3.3 w/ WTP 2
  • GlassFish Java System Application Server
  • Java Metro

3.1 Install Java SE Development Kit

The latest development kit will include the Java runtime environment (JRE) and tools necessary to build Java-based applications:

  1. Download the latest JDK at http://java.sun.com/javase/downloads/index.jsp
    • At the time of writing the latest JDK is JDK 6 Update 3

  2. Run the installer
  3. The JDK should be installed in a location such as C:\Program Files\Java\jdk1.6.0_03
  4. Add the environment variables using the correct path that you have installed
    • Add/Modify system environment variables
      • JAVA_HOME = C:\Program Files\Java\jdk1.6.0_03
      • JAVA_HOME_6 = C:\Program Files\Java\jdk1.6.0_03

3.2 Install Eclipse 3.3 w/ WTP 2

The integrated development environment will allow for rich java and web development to deploy to Glassfish:

  1. Download the complete package at http://download.eclipse.org/webtools/downloads/drops/R2.0/R-2.0.1-20070926042742/
  2. Extract to root (such as c:\)
  3. You should now have a folder called c:\eclipse

3.3 Install GlassFish Java System Application Server

Glassfish is the latest http server from Sun Microsystems to provide top performance and web service delivery for Java applications. This should be installed on the development clients as well so it may applications may be tested locally:

  1. Download GlassFish at https://glassfish.dev.java.net/downloads/v2-b58g.html
  2. Put the downloaded file (filename.jar )into your root server library (such as c:\server\)
  3. From command line switch to the root server library directory and type:
    java -Xmx256m -jar filename.jar
  4. You should now have a directory c:\server\glassfish
  5. Set the permissions
    • If using a Unix operating system type:
      chmod -R +x lib/ant/bin
      lib/ant/bin/ant -f setup.xml
    • In windows type:
      lib\ant\bin\ant -f setup.xml
  6. Add/Modify system environment variables
    • AS_HOME = c:\server\glassfish\
  7. Add java path to system environment variable list, if an item already exists separate it with a semi-colon
    • Path = .;c:\server\glassfish\bin; c:\server\ glassfish\lib\ant\bin\
  8. By default the server is installed as follows:
    • Runs on localhost (127.0.0.1)
    • Web sites run on port 8080
    • The administration console runs on port 4848 viewable in the web browser (http://127.0.0.1:4848)
    • The default domain is called domain1
    • Administrator has username: admin password:adminadmin
  9. If you need to start the server manually use the following command:
    asadmin start-domain domain1
  10. If you wish to run the server as a windows service:
    • At the command prompt type (Make sure to include the spaces after the equal signs):
      c:\windows\System32\sc.exe create GlassfishDomain1 binPath= "C:\server\glassfish\lib\appservService.exe \"C:\server\glassfish\bin\asadmin.bat start-domain --user admin --passwordfile C:\server\gfsecurity.txt domain1\" \"C:\server\glassfish\bin\asadmin.bat stop-domain domain1\" " start= auto DisplayName= "GlassFish Server DOMAIN1"
    • Add a file at C:\server\gfsecurity.txt
      • It should contain a line as follows (where adminadmin is your default domain1 admin password if you haven't changed it):
        AS_ADMIN_password=adminadmin
    • Start the services in the services console

3.2 Install Metro

Metro is Sun Microsystems latest addition to the Java stack to handle web service operations and security. These libraries make development simple and provide methods to communicate seamlessly with other applications such as .Net:

  1. Download metro at https://metro.dev.java.net/1.0/
  2. Put the downloaded file (metro-filename.jar )into your root server library (such as c:\server\)
  3. From command line switch to the root server library directory and type:
    java -jar metro-filename.jar
  4. After installation you should now have a directory c:\server\metro
  5. Download the java JAX-WS libraries at https://jax-ws.dev.java.net/2.1.2/
  6. Put the downloaded file (jaxws-filename.jar) to your server library root folder
  7. From command line switch to the root server library directory and type:
    java -jar jaxws-filename.jar
  8. After installation you should now have a directory c:\server\jaxws-ri
  9. Install Metro support into GlassFish:
    • From the command line type:
      Asadmin stop-domain domain1
      ant -Das.home=c:\server\glassfish\ -f c:\server\metro\wsit-on-glassfish.xml install
      Asadmin start-domain domain1
  10. Copy the JAX-WS libraries to the JDK 6 library:
    • In your JDK 6 home directory ($JDK_HOME) create a directory called “endorsed” under $JDK_HOME\jre\lib
    • Copy c:\server\jaxws-ri\lib\jaxws-api.jar to $JDK_HOME\jre\lib\endorsed
    • Copy c:\server\jaxws-ri\lib\jaxb-api.jar to $JDK_HOME\jre\lib\endorsed
  11. Add the environment variables using the correct path that you have installed:
    • Add/Modify system environment variables
      • JAXWS_HOME = c:\server\jaxws-ri\

4 Setting Up Eclipse

Following is a guide to setting up Eclipse to work with GlassFish and Metro.

Set the Java Runtime Environments:

  1. From the preferences dialog click on Installed JREs
  2. If the installed JDK 6 is not listed, add it by browse
    • You may add the JRE directory as well, but use the JDK by default
  3. Change perspective to Java EE by clicking window->open perspective->other.. java EE
  4. Click on the server tab
  5. Right click in the server window and select New->Server
  6. If GlassFish does not exist, click on “Download Additional Server Adapters”
    • Once the list is populated, select “GlassFish Java EE 5”
    • Complete Server adapter installation and restart Eclipse
    • Note: If GlassFish is not an option you may manually install the plugin at https://glassfishplugins.dev.java.net – You need to extract the contents into your Eclipse installation folder and restart Eclipse.
  7. Register the GlassFish server to be used in Eclipse:
    • Right click in the server window and select New->Server
    • Select “Server” and click “next”
    • Under “Sun Microsystems” select “GlassFish V2 Java EE 5” and click “next”
    • Select the JDK 6 (such as jdk 1.6.0_03) that you have installed
    • Select the GlassFish installation directory (such as c:\server\glassfish)
    • Click next and change any properties that you have changed. If you have not changed the defaults just click “finish”
    • In the server view tab you can stop and start your server (available in Window->Show View->Other…: Servers)
  8. Turn off automatic deployment to speed up development of code:
    • Open the “Server” view
    • Double click the GlassFish server
    • Expand the Automatic Publishing group tab
    • Select “Never Publish Automatically”
    • Close the server overview window and save
    • Start the server by right clicking on green play button in the “Server” view
    • From now on when you want to publish, click on the publish button in the “Server” view

5 Create a Test Project in Eclipse

A test project will be created to show how to setup, configure, and deploy to the GlassFish server from Eclipse.

Create a test project as follows:

  1. Open File->New->Project…
  2. Under the “Web” folder select “Dynamic Web Project”
  3. Type “WebProject1” under “Project Name”
  4. Select the GlassFish V2 Java EE 5 for the target runtime and click “Next” (leave EAR unchecked)
  5. Make sure Sun DD files is checked (this helps in deployment)
  6. The project will be created. In the server view tab you can stop and start your server
  7. Create a sample JSP file to test the installation
    • Right click on the project and select New->JSP and name it test.jsp
  8. Register the project to the server
    • Right click on the project and select Run As->Run on Server
    • Select the registered GlassFish server, and ensure the project is in the right column of configured projects
  9. In the eclipse browser, browse to http://localhost:8080/WebProject1/test.jsp
  10. As you make changes to your project they will automatically be re-deployed to the server
  11. Under default settings, you should see your deployed application in:
    • c:\server\glassfish\domains\domain1\applications\j2ee-modules
    • Different settings might cause it to publish out to a directory within your eclipse workspace folder
  12. Alternatively you can manually deploy to GlassFish by exporting to a WAR file and dropping the file into the \domains\domain1\autodeploy folder of GlassFish

6 Create a Test Web Service with Metro and Eclipse

A test web service will be created to show how to setup, configure, and deploy using Metro and the GlassFish server from Eclipse.

6.1 Configure the web.xml to handle metro web services

  1. The web.xml must be updated for the project to be able to easily create and deploy web services.
    • Replace the content of the web.xml file in the WEB-INF folder with the following:
      <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:j2ee="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> </web-app>

6.2 Create a Test Web Service

In this section, a sample web service will be created and tested using GlassFish and Metro

  1. Create a package for your class. This helps maintain structure and organization to your code.
    • Right click on the source directory (src) and select New->Package
    • Type sample.application
  2. Create a sample class to represent a test web service
    • Right click the sample.application package and select New->Class
    • Type “Test” in the Name input box
    • Click “Finish”
    • Insert a simple function to test out the web service:
                  public String showExample {
                      return "This is a test.";
                  }
                              
    • Define the class as a web service class by using the @WebService binding:
                  @WebService
                  public class Test() {
                              
    • Define the class as a web service class by using the @WebMethod binding
                  @WebMethod
                  public String showExample() {
                              
    • Add the appropriate imports as defined in section 2.2 (javax.jws)
      • If the import is not found and you are using an older Java runtime (JRE), you must add the metro web service libraries to your build path
    • In the end your class should look like the following:
                  import javax.jws.WebMethod;
                  import javax.jws.WebService;
      
                  @WebService
                  public class Test {
                      @WebMethod
                      public String showExample() {
      	                return "This is a test.";
                      }
                  }
                              
  3. Publish the web service out to GlassFish by clicking on the “Publish” icon in the “Server” view.

6.3 Verfiy and Test the new Web Service

  1. Open up the GlassFish administrative console (http://localhost:4848/) and sign in.
  2. Click on “Web Services” in the left menu and you should see your newly created service (Test).
    • Click on the Test service
    • Looking in the General tab you will see the URI is given:
      WebProject1/TestService
  3. Access the web service publicly.
    • From the web browser go to the url: http://localhost:8080/WebProject1/TestService
  4. Test the web service through the browser
    • From the web browser go to the url: http://localhost:8080/WebProject1/TestService?tester
  5. Click the “showExample” button to run the test function
  6. Notice your SOAP envelope response matches that of the test function:
                <return>This is a test.</return>
    
                            

Comments

Nickname:
Message: