You want to deploy an aspect-oriented Apache Axis web service that has been created using AspectJ.
Apache Axis is rapidly becoming one of the most popular web service implementations for Java developers. Not only is it open source but, thanks to its very active supporting developer base, it also has a high degree of success in interoperability with web services developed using other web service frameworks such as .NET.
Once you have Apache Axis running within Tomcat on your machine, you can use the following steps to compile a simple aspect-oriented web service and deploy it within Apache Axis:
Create a directory within your project area called source, and within that new directory, create a file called MyWebService.java that contains:
package com.oreilly.aspectjcookbook; public class MyWebService { public String echo(String message) { return message; } }
Create another file in the source directory called AddMessageHeaderAspect.java that contains:
package com.oreilly.aspectjcookbook; public aspect AddMessageHeaderAspect { public pointcut captureEcho(String message) : execution(public void MyWebService.echo(String)) && args(message); Object around(String message) : captureEcho(message) { return "Your original message was: " + message; } }
Create a classes directory within your project area.
Compile the two files, MyWebService.java and AddMessageHeaderAspect.java, in your source directory using the ajc command-line tool from the project root directory:
ajc -classpath %AXIS_INSTALL_DIRECTORY%/lib/axis.jar:%AXIS_ INSTALL_DIRECTORY%/lib/axis.jar -d classes/ source/*.java
Once you have successfully run the ajc command, the compiled .class files and package structure will be generated under the classes directory.
Create a file called deploy.wsdd within the classes directory that is going to be used to automatically deploy your web application to Axis. The deploy.wsdd should contain the following information:
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <service name="MyWebService" provider="java:RPC"> <parameter name="className" value="com.oreilly.aspectjcookbook.MyWebService"/> <parameter name="allowedMethods" value="*"/> </service> </deployment>
It is a good practice to create an undeploy.wsdd file in the same classes directory as the deploy.wsdd to be able to remove your web application from Axis. The undeploy.wsdd should contain the following information:
<undeployment xmlns="http://xml.apache.org/axis/wsdd/"> <service name="MyWebService"/> </undeployment>
Copy the %ASPECTJ_INSTALL_DIRECTORY%/lib/aspectjrt.jar to %AXIS_INSTALLATION_IN_TOMCAT%/WEB-INF/lib.
Copy your .class files located in the classes directory, preserving the package directory structure, to %AXIS_INSTALLATION_IN_TOMCAT%/WEB-INF/classes.
Ensuring that Tomcat is running if you are deploying to Apache Axis running within Tomcat, run the following java command from the classes directory to run the Apache Axis deployment program that registers your new web service with Axis:
java -classpath %AXIS_INSTALL_DIRECTORY%/lib/axis.jar: %AXIS_INSTALL_ DIRECTORY%/lib/jaxrpc.jar:%AXIS_INSTALL_DIRECTORY%/lib/log4j-1.2.4.jar: %AXIS_INSTALL_DIRECTORY%/lib/commons-logging.jar:%AXIS_INSTALL_DIRECTORY% /lib/commons-discovery.jar:%AXIS_INSTALL_DIRECTORY%/lib/saaj.jar org. apache.axis.client.AdminClient -h%HOST% -p%PORT% deploy.wsdd
Restart Tomcat to ensure it has refreshed all of the libraries and applications it manages, including Axis.
To test that your web service has been deployed correctly, you can go to :
/axis/services/MyWebService">http://<hostname>:<post>/axis/services/MyWebService in your browser, and you should see the output shown in Figure 3-12.
Figure 3-12. The aspect-oriented MyWebService service, initialized and ready for use inside Apache Axis
Web service clients can now use your new aspect-oriented web service. For example, the output of a simple AppleScript that uses the MyWebService is shown in Figure 3-13.
Figure 3-13. An AppleScript web service client using the echo method on the aspect-oriented MyWebService service
Compiling using the ajc command-line tool is explained in Recipe Recipe 2.2; creating and compiling an AspectJ project in Eclipse is described in Recipe 2.7; execution(Signature) pointcut is explained in Recipe 4.4; the args([TypePatterns | Identifiers]) pointcut is explained in Recipe 11.3; Programming Apache Axis by Christopher Haddad, Kevin Bedell, and Paul Brown (O'Reilly); AppleScript in a Nutshell by Bruce W. Perry (O'Reilly).