CPE5009 – Tutorial 1

idiotdiscΛογισμικό & κατασκευή λογ/κού

15 Αυγ 2012 (πριν από 5 χρόνια και 2 μέρες)

387 εμφανίσεις

FIT5030



Tutorial
3



Configuring the Axis2 Engine in Netbeans and Deploying a Web
Service Using Axis 2



Exercise 1
: HelloWorld Web Service Deployment Using
Axis2

Web Server


In this tutorial you will
configure the Netbeans to deploy services using Apach
e
Tomcat and the Axis2 Web Server. You will
write
a simple

web service and deploy it
using the
Axis2
Web Server.



Note: This only works for Netbeans V 6.1 or Above.


This tutorial is based on the exercise specified at:

http://www.netbeans.org/kb/docs/websvc/gs
-
axis.html

1.

You need to first download and install Apache Tomcat
Server.

-

Download the Apache Tomcat 6.0.x version from:

http
://tomcat.apache.org/download
-
60.cgi


-

Make sure you get the zip file from the Binary Distributions/Core and
NOT
the Windows Installer


-

Extract the zipped fil
e into the folder of your choice


-

There will be a RUNNING.txt file


read that!


-

Make sure that t
he JAVA_HOME
Environment V
ariable is setup correctly

to
point to where have JDK installed


-

I suggest that you do not use the default 8080 port for running the Tomcat
server as the Monash Proxy uses that port. Change the port to something else
(e.g.8090)

in the C:
\
<tomcat_home>
\
conf
\
server.xml file.


-

Also create a user
name/password
for yourself in the
C:
\
<tomcat_home>
\
conf
\
server.xml file

by adding the following
highlighted line:



<?xml version='1.0' encoding='utf
-
8'?>


<tomcat
-
users>




<role rolen
ame="manager"/>




<role rolename="admin"/>

<
user username="admin" password="admin"

roles="manager,admin"/>


</tomcat
-
users>





-

To
make sure that it all working correctly,
start a Tomcat server from
a command prompt:


Goto:
C:
\
[depending on where
you have this
-

apache
-
tomcat
-
6.0.18
]
\
bin

Type: startup.bat


Open a web browser and type:
http://localhost:8090

(if you set your Tomcat
Server port to 8090. Otherwise use whatever number port you have chosen)


If you
get the Apache Tomcat Page


then you have setup the server
correctly. If there is an error you will get “No such page etc.”.


-

To shutdown a Tomcat server from a command prompt:


Goto:
C:
\
[depending on where you have this
-

apache
-
tomcat
-
6.0.18
]
\
bin

Type
: shutdown




2.
You need to download Axis2 Web Server.


-

Download the Web Application Archive
distribution
of Axis1.4.1 from:
http://ws.apache.org/axis2/download/1_4_1/download.cgi

-

U
nzip the file and copy the a
xis2.war file to your [Apache Tomcat]/webapps
folder

-

You may need to re
-
start the Tomcat server after copying the file.


Setting Up Axis2 and NetBeans IDE

In this section you learn how to download an Axis2 WAR file, embed it in
an Apache Tomcat, and
configure NetBeans IDE to upload AAR files to the server.


Installing the Axis2 Support Plugin

This tutorial requires the Axis2 Support plugin for the NetBeans IDE. In the IDE, go to the Plugin
Manager, under the Tools menu, and check

whether Axis2 Support Plugin is installed, under the
Installed tab. If it is installed, check whether an update is available in the Updates tab. If the Axis2
support is not installed, install it from the Available Plugins tab, as shown below.


Setting Up Axis2 Options for Tomcat


Axis services run faster on Tomcat than on GlassFish v1 or v2, but the setup is slightly more
complicated. You can eas
ily deploy the same service to both Tomcat and GlassFish by first deploying
to one server, then changing the Axis2 options and finally deploying to the other server.


If you want to deploy to GlassFish instead of Tomcat, go to
Setting Up Axis2 Options for GlassFish
.


To set up Axis2 options for Tomcat:

1.

Unpack the downloaded archive file containing
axis2.war

to your Tomcat
CATALINA_BASE/w
ebapps

directory. If you do not know where your
CATALINA_BASE

is,
start the IDE and open the Services tab. Expand the Servers node. Right
-
click the Tomcat
6.0.x node and select Properties from the context menu. The Catalina Base location is
visible in the
Connection tab.

If there is no
CATALINA_BASE
, you have not run your Tomcat before. Start and stop
Tomcat and
CATALINA_BASE

will be generated.



2.

Start or restart the Tomcat server. Tomcat unpacks the
axis2.war

file into an
axis2

folder in
CATALINA_BASE/webapps
.




3.

From the top menu bar in the IDE, choose Tools > Options. The Options dialog opens.

4.

Click the Axis2 icon. The Axis2 deployment opt
ions page opens.


5.

Set the target location for Axis2 AAR files to your
CATALINA_BASE

(not
TOMCAT_HOME
)/
webapps/axis2

directory.

6.

Make sure t
he Axis2 URL field contains the correct port number for your Tomcat server. To
check the port number, start Tomcat (from the Services tab or from Tools
-
> Servers) and
see what port Coyote HTTP/1.1 uses.


7.

For convenience, select Use Tomcat Manager for Deployment. This function means that the
IDE will launch Tomcat Manager in the background and run the
reload

command on
axis2.war

every time you ma
ke a change to
axis2.war
. If you do not select Use Tomcat
Manager for Deployment, you will have to manually run Tomcat Manager or use other
Tomcat tools to reload
axis2.war

every time you make a change to it.

The default Tomcat Manager username and passwo
rd varies. You can find this information
in the
Properties page for the Tomcat server

To double
-
check that Tomcat Manager is
deployed and has the default manager rol
e username and password, open
CATALINA_BASE/conf/tomcat
-
users.xml
. The manager role should be defined and it
should have a username and password assigned to it, as in the following version of the file.
(You can copy and paste this file if necessary.)

<?xml

version='1.0' encoding='utf
-
8'?>

<tomcat
-
users>


<role rolename="manager"/>


<role rolename="admin"/>


<user username="ide" password="JTtrQw99" roles="manager,admin"/>

</tomcat
-
users>

Developing an Axis2 Web Service

In this section, you use NetBeans ID
E to create, deploy, test, and modify an Axis2 web service.

Creating an Axis2 Web Service

With NetBeans IDE, you can create an Axis2 web service from a Java class. You can only do this
from a Java application or Java library project. In this tutorial, you
create a Java library project
(because you do not need a main method), create an Axis2 web service in that project (creating the
Java class at the same time) and deploy the Axis2 web service to a server.

You can only create an Axis2 web service from a Java

or Java Library project. This is because the
axis.aar

file (the deployable archive into which web services and Axis configuration files are
packed) is neither a WAR nor an EAR and cannot be deployed normally as a web (EAR) application.

To create an Axis2

web service:

1.

Click the New Project icon or File
-
> New Project. The New Project wizard opens. From the
Java category, select a Java class library project. Click Next.

2.

Name the project
AxisHello
. Check that you are using the project folder name and
locati
on that you want. It is up to you whether to share the project. Click Finish, and the
IDE creates the project.

3.

Right
-
click the project node. The context menu opens. In the context menu, choose New
-
>
Other. The New File wizard opens. From the Web Services

category, choose Axis2 Service
from Java and click Next.


4.

The Service Type Selection page of the New File wizard is now open. You do not have
any
Java classes in the project, so select "Create an Empty Web Service." If you had already
coded a Java class, you would have selected Create a Web Service from an Existing Java
Class. If you wanted to edit the WSDL of the web service, for example to add

or change
namespaces, you would select Generate a WSDL from Java Source Code. Editing WSDL is
outside the scope of this tutorial, so leave this unselected. The wizard should look like the
following image.


5.

Click Next. The Name and Location page opens. Name the Java class HelloAxisWorld. Name
the package axishello. Leave Generate Sample Method selected. This generates a method in
the Java class
that returns "Hello, World."

6.

Click Finish. The IDE generates a
HelloAxisWorld.java

class in the axishello source
package and a HelloAxisWorld Axis2 web service that mirrors this Java class. You can see
that both the Java class and the Axis2 web service ha
ve a hello:String operation, shown in
the Navigator tab and as a node of the Axis2 web service, respectively.


Deploying and Testing an Axis2

Web Service

Once you have created an Axis2 web service, you need to deploy it to a server. Actually,
deployment to a server involves two steps:

1.

Copying the Axis2 web service to the axis2.war file used by the server. This step can be
taken with the server

offline.

2.

Redeploying the updated axis2.war file to the server.

If you are deploying to Tomcat with the Use Tomcat Manager for Deployment option enabled (see
Setting Up Axis2 Options for Tomcat
), or you are deploying to GlassFish and
axis2.war

is in the
GLASSFISH_DOMAIN/autodeploy

folder (see
Setting Up Axis2 Options for GlassFish
), the updated
axis2.war

is automatically redeployed to the server. Otherwise, you have to redeploy
axis2.war

manually, using application server tools. In this tutorial we assume you have set up the Axi
s2
options so redeployment is automatic.





To deploy an Axis2 web service to the server:

1.

Right
-
click the web service's node. The context menu opens. Select Deploy to Server. The
IDE compiles an Axis2 AAR file and copies it to the
axis2.war

file used by
the application
server.

2.

If you have enabled automatic deployment, the web service is deployed to the server. If the
server is not running, start it and the web service is automatically deployed.

3.

To test the service, expand the web service node to reveal
the operations. Right
-
click the
hello:String node and select Test Operation in Browser.


4.

Your browser opens with a test value of your variab
les. The test value is appended to the
URL.


5.

Change the variable value in the URL and press Enter. The test result changes as well.


Changing the Web Service's Operations

To change the web service operations, edit the Java file in the project. The operations in the web
service chan
ge simultaneously. Add a simple
add

method to
HelloAxisWorld.java
, as below.

public class HelloAxisWorld {



/** Sample method


*/


public String hello(String name) {


return "Hello "+name;


}


public int add(int x, int y) {



return x+y;


}


}

Save the Java file, and the operation appears as a subnode of the web service.


Redeploy the web service and
test it as described in
Deploying and Testing an Axis2 Web Service
.

Troubleshooting

Here are some difficulties you may experience and their solutions:



The ser
vice is not automatically deployed on Tomcat.

If autodeploy does not appear
to work on Tomcat, open
TOMCAT_HOME/conf/server.xml

and check that the
autoDeploy

attribute is set to
"true"

in the
Host

element:



<Host name="localhost" appBase="webapps"




u
npackWARs="true" autoDeploy="true"




xmlValidation="false" xmlNamespaceAware="false">








When you change the operations in the service and try to redeploy the service on
Tomcat, it fails with the following message in the Tomcat server output win
dow:

INFO:
validateJarFile(C:
\
...
\
apache
-
tomcat
-
6.0.16_base
\
webapps
\
axis2
\
WEB
-
INF
\
lib
\
servlet
-
api
-
2.3.jar)
-

jar not loaded. See Servlet Spec 2.3,
section 9.7.2. Offending class: javax/servlet/Servlet.class


In this case, stop the server, delete the
CATALI
NA_BASE/webapps/axis2

folder, and restart the
server. This regenerates a new axis2 folder.


To setup Axis2 to work with Glassfish instead of Tomcat


if you so desire, here are the
steps to follow:



Setting Up Axis2 Options for GlassFish

You can deploy A
xis2 web services to the GlassFish application server. You can easily deploy the
same service to both Tomcat and GlassFish by first deploying to one server, then changing the
Axis2 options and finally deploying to the other server.

If you want to deploy to

Tomcat instead of GlassFish, go to
Setting Up Axis2 Options for Tomcat
.

To set up Axis2 options for Glassfish:

1.

Unpack the downloaded archive
file containing
axis2.war

to
GLASSFISH_HOME/domains/DOMAIN_NAME/autodeploy
. To find GLASSFISH_HOME and
the name of your domain, start the IDE and open the Services tab. Expand the Servers
node. Right
-
click the GlassFish V2 or V3 node and select Properties
from the context menu.
The Domains folder location and the name of the domain are visible in the Connection tab.
For example, from the following image, you know to copy
axis2.war

to
G:
\
glassfish
-
v2ur2
\
domains
\
domain1
\
autodeploy
.


2.

Start the IDE. From the top menu bar, choose Tools
-
> Options. The Options dialog opens.

3.

Click the Axis2 icon. The Axis2 deployment options page opens.


4.

Set the target location for Axis2 AAR files to the
axis2.war

file you unpacked into the
GlassFish
autodeploy

directory.

By placing
axis2.war

into

autodeploy
, you enable GlassFish to automatically redeploy
axis2.war

every time you alter the file. On GlassFish v3 Prelude, however, you cannot
redeploy the WAR file while the server is running.

5.

Make sure the Axis2 URL field contains the correct port num
ber for your GlassFish server.
To check the port number, start GlassFish (from the Services tab or from Tools
-
> Servers)
and see what 80xx port HTTP 1.1 uses. The default port number is 8080. In the following
image, the correct port number is 8081 (becaus
e another server already uses 8080).


Note:
If you are using Windows Vista, have GlassFish installed to Program Files, and have User
Access
Control enabled, you will not be able to make changes to the
axis2.war

file from the IDE.
Either install GlassFish to a different location or disable UAC.