When you use WSO2 SOA Enablement Server in your application you need to add all necessary libraries into your classpath. Scenarios of how to embed WSO2 SOA Enablement Server and which libraries are necessary follow:
Components: Core and Built-in Serialization
Configuration: conf/clientconf.xml (default)
This configuration covers most use-cases of WSO2 SOA Enablement Server. You do not need to explicitly initialize WSO2 SOA Enablement Server because this is done automatically by runtime, which initializes with the default WSO2 SOA Enablement Server configuration file. There is a little magic with the wasp.location property, which is necessary to start WSO2 SOA Enablement Server. It is set into the runner.jar file during installation to META-INF/wasp.properties. So, if you add this JAR file into your classpath, you do not need to set this system property by hand. For more information on initializing WSO2 SOA Enablement Server, see Init and Destroy Steps.
The first example shows how to start HTTP server and publish a simple Web service:
Example 222. Starting the HTTP Server and Publishing a Web Service
// Copyright WSO2 Inc. All rights reserved. // Use is subject to license terms. package example.basics.embedding; import org.idoox.transport.TransportStartException; import org.idoox.wasp.WaspInternalException; import org.systinet.wasp.Wasp; import org.systinet.wasp.webservice.PublishException; import org.systinet.wasp.webservice.Registry; import java.net.MalformedURLException; public class HelloServer { public String hello(String string) { return string; } public static void main(String[] args) throws TransportStartException, MalformedURLException, WaspInternalException, PublishException { Wasp.startServer("http://localhost:6060"); Registry.publish( "/hello", new HelloServer()); System.out.println( "Server is started. Look at 'http://localhost:6060/hello/wsdl'."); } }
The second example shows how to embed WSDL2Java (you have to addCore-Services client into your classpath). Keep the first example running while you start this one:
Example 223. Embedding WSDL2Java
// Copyright WSO2 Inc. All rights reserved. // Use is subject to license terms. package example.basics.embedding; import org.idoox.wasp.tools.CompilerIO; import org.idoox.wasp.tools.wsdl2java.WSDL2Java; import org.idoox.wasp.tools.wsdl2java.WSDL2JavaException; import org.idoox.wasp.tools.wsdl2java.WSDL2JavaFactory; import java.io.OutputStream; import java.io.PrintWriter; import javax.wsdl.WSDLException; import javax.wsdl.factory.WSDLFactory; public class Wsdl2Java implements CompilerIO { public PrintWriter getWriter(String file) { return new PrintWriter(System.out); } public OutputStream getOutputStream(String s) { return null; } public static void main(String[] args) throws WSDL2JavaException, WSDLException { WSDL2Java wsdl2java = WSDL2JavaFactory.newWSDL2Java(); wsdl2java.setOutputBackends("java"); wsdl2java.setCompilerIO(new Wsdl2Java()); wsdl2java.setDefinition( WSDLFactory.newInstance().newWSDLReader().readWSDL(null, "http://localhost:6060/hello/wsdl")); wsdl2java.process(); } }
Components: Core + all packages from the application directory
Configuration: conf/serverconf.xml
This is useful when you want to use WSO2 SOA Enablement Server as a standalone server. The main difference from the previous scenario is that WSO2 SOA Enablement Server is initialized with serverconf.xml, which has different settings for PackageRepository - WSO2 SOA Enablement Server packages are not loaded from the classpath but from the application directory (WASP_HOME/app), each in a different classloader. Another difference is that some server transports are started when WSO2 SOA Enablement Server starts (which transports are started depends on your installation type).
Example 224 shows the initializing and destroying of a WSO2 SOA Enablement Server (wasp.location is taken from runner.jar):
Example 224. Initializing and Destroying a WSO2 SOA Enablement Server
// Copyright WSO2 Inc. All rights reserved. // Use is subject to license terms. package example.basics.embedding; import org.idoox.wasp.WaspInternalException; import org.systinet.wasp.Wasp; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class StandaloneServer { public static void main(String[] args) throws WaspInternalException, IOException { // set initial parameters Map initialParameters = new HashMap(); initialParameters.put("wasp.config.location", "conf/serverconf.xml"); initialParameters.put("idoox.debug.level", "3"); // init WASP Wasp.init(initialParameters); // wait for 'Enter' System.out.println(); System.out.print( "Look at 'http://localhost:6060' if you have installed console."); System.out.println("Press [ENTER] to shutdown WASP."); System.in.read(); // destroy WASP Wasp.destroy(); } }
If you want to embed WSO2 SOA Enablement Server in any way, look first at the relevant index Javadoc. The Javadoc says which component a class or interface belongs to. Some components have both a client part and a server part if they contains services. Add the necessary libraries to your classpath or in the application directory, depending on whether you are using the client or the server configuration file, respectively. You can use Classpath Builder to help build a classpath for your application. For more information, see the install-log.html document in your WASP_HOME directory.