KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > marshall > dynamic > remote > socket > 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.socket;
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.transport.Connector;
17
18 import javax.management.MBeanServer JavaDoc;
19
20 /**
21  * Simple remoting server. Uses inner class SampleInvocationHandler
22  * as the invocation target handler class.
23  *
24  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
25  */

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

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

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

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

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

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

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

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

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

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