KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > marshall > dynamic > remote > http > MarshallerLoadingServer


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.remoting.marshall.dynamic.remote.http;
8
9 import org.apache.log4j.Level;
10 import org.jboss.remoting.AbstractInvokerTest;
11 import org.jboss.remoting.InvocationRequest;
12 import org.jboss.remoting.InvokerCallbackHandler;
13 import org.jboss.remoting.InvokerLocator;
14 import org.jboss.remoting.ServerInvocationHandler;
15 import org.jboss.remoting.ServerInvoker;
16 import org.jboss.remoting.marshall.dynamic.remote.socket.MarshallerLoadingConstants;
17 import org.jboss.remoting.marshall.dynamic.remote.socket.TestMarshaller;
18 import org.jboss.remoting.marshall.dynamic.remote.socket.TestUnMarshaller;
19 import org.jboss.remoting.transport.Connector;
20
21 import javax.management.MBeanServer JavaDoc;
22
23 /**
24  * Simple remoting server. Uses inner class SampleInvocationHandler
25  * as the invocation target handler class.
26  *
27  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
28  */

29 public class MarshallerLoadingServer extends AbstractInvokerTest implements org.jboss.remoting.marshall.dynamic.remote.socket.MarshallerLoadingConstants
30 {
31
32    // String to be returned from invocation handler upon client invocation calls.
33
private static final String JavaDoc RESPONSE_VALUE = "This is the return to SampleInvocationHandler invocation";
34
35    public MarshallerLoadingServer(String JavaDoc name)
36    {
37       super(name);
38    }
39
40    public MarshallerLoadingServer(String JavaDoc name, int numberOfInstances)
41    {
42       super(name, numberOfInstances);
43    }
44
45    public MarshallerLoadingServer(String JavaDoc name, String JavaDoc transport, int port)
46    {
47       super(name, transport, port);
48    }
49
50    public MarshallerLoadingServer(String JavaDoc name, String JavaDoc transport, int port, int numberOfInstances)
51    {
52       super(name, transport, port, numberOfInstances);
53    }
54
55
56    public void setupServer() throws Exception JavaDoc
57    {
58       String JavaDoc mylocatorURI = MarshallerLoadingConstants.transport + "://" + host + ":" + org.jboss.remoting.marshall.dynamic.remote.socket.MarshallerLoadingConstants.port + "/?" +
59                               InvokerLocator.DATATYPE + "=" + dataType + "&" +
60                               InvokerLocator.LOADER_PORT + "=" + 5401 + "&" +
61                               InvokerLocator.MARSHALLER + "=" + TestMarshaller.class.getName() + "&" +
62                               InvokerLocator.UNMARSHALLER + "=" + TestUnMarshaller.class.getName();
63
64       System.out.println("mylocatorURI: " + mylocatorURI);
65       System.out.println("locatorURI: " + locatorURI);
66       //InvokerLocator locator = new InvokerLocator(locatorURI);
67
InvokerLocator locator = new InvokerLocator(mylocatorURI);
68       System.out.println("Starting remoting server with locator uri of: " + locatorURI);
69       Connector connector = new Connector();
70       connector.setInvokerLocator(locator.getLocatorURI());
71       connector.start();
72
73       SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
74       // first parameter is sub-system name. can be any String value.
75
connector.addInvocationHandler("sample", invocationHandler);
76    }
77
78    public void serverTest() throws Exception JavaDoc
79    {
80          setupServer();
81          startup(getNumberOfInstances());
82          shutdown();
83    }
84
85    /**
86     * Can pass transport and port to be used as parameters.
87     * Valid transports are 'rmi' and 'socket'.
88     *
89     * @param args
90     */

91    public static void main(String JavaDoc[] args)
92    {
93       org.apache.log4j.BasicConfigurator.configure();
94       org.apache.log4j.Category.getRoot().setLevel(Level.INFO);
95       org.apache.log4j.Category.getInstance("org.jboss.remoting").setLevel(Level.DEBUG);
96       //org.apache.log4j.Category.getInstance(DistributedTestCase.class).setLevel(Level.DEBUG);
97

98       /*
99       if(args != null && args.length == 2)
100       {
101          transport = args[0];
102          port = Integer.parseInt(args[1]);
103       }
104       */

105       MarshallerLoadingServer server = new MarshallerLoadingServer(MarshallerLoadingServer.class.getName());
106       try
107       {
108          server.serverTest();
109
110          // sleep the thread for 10 seconds while waiting for client to call
111
Thread.sleep(10000);
112       }
113       catch(Exception JavaDoc e)
114       {
115          e.printStackTrace();
116          System.exit(1);
117       }
118       System.exit(0);
119    }
120
121    /**
122     * Simple invocation handler implementation.
123     */

124    public static class SampleInvocationHandler implements ServerInvocationHandler
125    {
126       /**
127        * called to handle a specific invocation
128        *
129        * @param invocation
130        * @return
131        * @throws Throwable
132        */

133       public Object JavaDoc invoke(InvocationRequest invocation) throws Throwable JavaDoc
134       {
135          // Print out the invocation request
136
System.out.println("Invocation request is: " + invocation.getParameter());
137
138          // Just going to return static string as this is just simple example code.
139
return RESPONSE_VALUE;
140       }
141
142       /**
143        * Adds a callback handler that will listen for callbacks from
144        * the server invoker handler.
145        *
146        * @param callbackHandler
147        */

148       public void addListener(InvokerCallbackHandler callbackHandler)
149       {
150          // NO OP as do not handling callback listeners in this example
151
}
152
153       /**
154        * Removes the callback handler that was listening for callbacks
155        * from the server invoker handler.
156        *
157        * @param callbackHandler
158        */

159       public void removeListener(InvokerCallbackHandler callbackHandler)
160       {
161          // NO OP as do not handling callback listeners in this example
162
}
163
164       /**
165        * set the mbean server that the handler can reference
166        *
167        * @param server
168        */

169       public void setMBeanServer(MBeanServer JavaDoc server)
170       {
171          // NO OP as do not need reference to MBeanServer for this handler
172
}
173
174       /**
175        * set the invoker that owns this handler
176        *
177        * @param invoker
178        */

179       public void setInvoker(ServerInvoker invoker)
180       {
181          // NO OP as do not need reference back to the server invoker
182
}
183
184    }
185 }
Popular Tags