KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > marshall > dynamic > local > 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.local;
8
9 import org.apache.log4j.Level;
10 import org.jboss.logging.Logger;
11 import org.jboss.remoting.AbstractInvokerTest;
12 import org.jboss.remoting.InvocationRequest;
13 import org.jboss.remoting.InvokerCallbackHandler;
14 import org.jboss.remoting.InvokerLocator;
15 import org.jboss.remoting.ServerInvocationHandler;
16 import org.jboss.remoting.ServerInvoker;
17 import org.jboss.remoting.transport.Connector;
18
19 import javax.management.MBeanServer JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.util.Enumeration 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 MarshallerLoadingConstants
30 {
31    private static final Logger log = Logger.getLogger(MarshallerLoadingServer.class);
32
33    // String to be returned from invocation handler upon client invocation calls.
34
private static final String JavaDoc RESPONSE_VALUE = "This is the return to SampleInvocationHandler invocation";
35
36    public MarshallerLoadingServer(String JavaDoc name)
37    {
38       super(name);
39    }
40
41    public MarshallerLoadingServer(String JavaDoc name, int numberOfInstances)
42    {
43       super(name, numberOfInstances);
44    }
45
46    public MarshallerLoadingServer(String JavaDoc name, String JavaDoc transport, int port)
47    {
48       super(name, transport, port);
49    }
50
51    public MarshallerLoadingServer(String JavaDoc name, String JavaDoc transport, int port, int numberOfInstances)
52    {
53       super(name, transport, port, numberOfInstances);
54    }
55
56
57    public void setupServer() throws Exception JavaDoc
58    {
59       try
60       {
61          log.info("2");
62          InvokerLocator locator = new InvokerLocator(locatorURI);
63          System.out.println("Starting remoting server with locator uri of: " + locatorURI);
64          log.info("Starting remoting server with locator uri of: " + locatorURI);
65          Connector connector = new Connector();
66          log.info("3");
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       catch(Exception JavaDoc e)
75       {
76          e.printStackTrace();
77          log.error("Error starting server.", e);
78          throw e;
79       }
80    }
81
82    public void serverTest() throws Exception JavaDoc
83    {
84       log.info("1");
85          setupServer();
86          startup(getNumberOfInstances());
87
88          Thread.sleep(5000);
89
90          shutdown();
91    }
92
93    public void setLogging()
94    {
95       org.apache.log4j.BasicConfigurator.configure();
96       org.apache.log4j.Category.getRoot().setLevel(Level.DEBUG);
97       org.apache.log4j.Category.getInstance("org.jboss.remoting").setLevel(Level.DEBUG);
98       org.apache.log4j.Category.getInstance("org.jboss.remoting.marshall.dynamic.local").setLevel(Level.DEBUG);
99       org.apache.log4j.Category.getInstance("org.jboss.dtf").setLevel(Level.DEBUG);
100       org.apache.log4j.Category.getInstance("org.jgroups").setLevel(Level.FATAL);
101
102       org.apache.log4j.SimpleLayout layout = new org.apache.log4j.SimpleLayout();
103       try
104       {
105          org.apache.log4j.FileAppender fileAppender = new org.apache.log4j.FileAppender(layout, getClass().getName() + "_output.log");
106          fileAppender.setThreshold(Level.DEBUG);
107          org.apache.log4j.Category.getRoot().addAppender(fileAppender);
108       }
109       catch(IOException JavaDoc e)
110       {
111          e.printStackTrace();
112       }
113
114 // org.apache.log4j.ConsoleAppender consoleAppender = new org.apache.log4j.ConsoleAppender();
115
// consoleAppender.setThreshold(Level.INFO);
116
// org.apache.log4j.Category.getRoot().addAppender(consoleAppender);
117

118       //System.out.println("Root log level = " + org.apache.log4j.Category.getRoot().getLevel());
119
Enumeration JavaDoc appenders = org.apache.log4j.Category.getRoot().getAllAppenders();
120       while(appenders.hasMoreElements())
121       {
122          org.apache.log4j.Appender appender = (org.apache.log4j.Appender) appenders.nextElement();
123          //System.out.println(appender.getName());
124
if(appender instanceof org.apache.log4j.ConsoleAppender)
125          {
126             ((org.apache.log4j.ConsoleAppender) appender).setThreshold(Level.INFO);
127          }
128       }
129    }
130
131    /**
132     * Can pass transport and port to be used as parameters.
133     * Valid transports are 'rmi' and 'socket'.
134     *
135     * @param args
136     */

137    public static void main(String JavaDoc[] args)
138    {
139       /*
140       if(args != null && args.length == 2)
141       {
142          transport = args[0];
143          port = Integer.parseInt(args[1]);
144       }
145       */

146       MarshallerLoadingServer server = new MarshallerLoadingServer(MarshallerLoadingServer.class.getName());
147
148       //DEBUG -TME
149
server.setLogging();
150
151       try
152       {
153          server.serverTest();
154       }
155       catch(Exception JavaDoc e)
156       {
157          e.printStackTrace();
158          System.exit(1);
159       }
160       System.exit(0);
161    }
162
163    /**
164     * Simple invocation handler implementation.
165     */

166    public static class SampleInvocationHandler implements ServerInvocationHandler
167    {
168       /**
169        * called to handle a specific invocation
170        *
171        * @param invocation
172        * @return
173        * @throws Throwable
174        */

175       public Object JavaDoc invoke(InvocationRequest invocation) throws Throwable JavaDoc
176       {
177          // Print out the invocation request
178
System.out.println("Invocation request is: " + invocation.getParameter());
179
180          // Just going to return static string as this is just simple example code.
181
return RESPONSE_VALUE;
182       }
183
184       /**
185        * Adds a callback handler that will listen for callbacks from
186        * the server invoker handler.
187        *
188        * @param callbackHandler
189        */

190       public void addListener(InvokerCallbackHandler callbackHandler)
191       {
192          // NO OP as do not handling callback listeners in this example
193
}
194
195       /**
196        * Removes the callback handler that was listening for callbacks
197        * from the server invoker handler.
198        *
199        * @param callbackHandler
200        */

201       public void removeListener(InvokerCallbackHandler callbackHandler)
202       {
203          // NO OP as do not handling callback listeners in this example
204
}
205
206       /**
207        * set the mbean server that the handler can reference
208        *
209        * @param server
210        */

211       public void setMBeanServer(MBeanServer JavaDoc server)
212       {
213          // NO OP as do not need reference to MBeanServer for this handler
214
}
215
216       /**
217        * set the invoker that owns this handler
218        *
219        * @param invoker
220        */

221       public void setInvoker(ServerInvoker invoker)
222       {
223          // NO OP as do not need reference back to the server invoker
224
}
225
226    }
227 }
Popular Tags