KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > 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.test.remoting.marshall.dynamic.local;
8
9 import java.io.IOException JavaDoc;
10 import java.util.Enumeration JavaDoc;
11 import javax.management.MBeanServer JavaDoc;
12 import org.apache.log4j.Level;
13 import org.jboss.jrunit.extensions.ServerTestCase;
14 import org.jboss.logging.Logger;
15 import org.jboss.remoting.InvocationRequest;
16 import org.jboss.remoting.InvokerLocator;
17 import org.jboss.remoting.ServerInvocationHandler;
18 import org.jboss.remoting.ServerInvoker;
19 import org.jboss.remoting.callback.InvokerCallbackHandler;
20 import org.jboss.remoting.transport.Connector;
21
22 /**
23  * Simple remoting server. Uses inner class SampleInvocationHandler
24  * as the invocation target handler class.
25  *
26  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
27  */

28 public class MarshallerLoadingServer extends ServerTestCase implements MarshallerLoadingConstants
29 {
30    private Connector connector;
31
32    private static final Logger log = Logger.getLogger(MarshallerLoadingServer.class);
33
34    // String to be returned from invocation handler upon client invocation calls.
35
private static final String JavaDoc RESPONSE_VALUE = "This is the return to SampleInvocationHandler invocation";
36
37
38    public void setupServer() throws Exception JavaDoc
39    {
40       InvokerLocator locator = new InvokerLocator(locatorURI);
41       System.out.println("Starting remoting server with locator uri of: " + locatorURI);
42       log.info("Starting remoting server with locator uri of: " + locatorURI);
43       Connector connector = new Connector();
44       connector.setInvokerLocator(locator.getLocatorURI());
45       connector.start();
46
47       SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
48       // first parameter is sub-system name. can be any String value.
49
connector.addInvocationHandler("sample", invocationHandler);
50    }
51
52    public void setUp() throws Exception JavaDoc
53    {
54       setupServer();
55    }
56
57    public void tearDown() throws Exception JavaDoc
58    {
59       if(connector != null)
60       {
61          connector.stop();
62          connector.destroy();
63       }
64    }
65
66    public void setLogging()
67    {
68       org.apache.log4j.BasicConfigurator.configure();
69       org.apache.log4j.Category.getRoot().setLevel(Level.DEBUG);
70       org.apache.log4j.Category.getInstance("org.jboss.remoting").setLevel(Level.DEBUG);
71       org.apache.log4j.Category.getInstance("org.jboss.remoting.marshall.dynamic.local").setLevel(Level.DEBUG);
72       org.apache.log4j.Category.getInstance("org.jboss.dtf").setLevel(Level.DEBUG);
73       org.apache.log4j.Category.getInstance("org.jgroups").setLevel(Level.FATAL);
74
75       org.apache.log4j.SimpleLayout layout = new org.apache.log4j.SimpleLayout();
76       try
77       {
78          org.apache.log4j.FileAppender fileAppender = new org.apache.log4j.FileAppender(layout, getClass().getName() + "_output.log");
79          fileAppender.setThreshold(Level.DEBUG);
80          org.apache.log4j.Category.getRoot().addAppender(fileAppender);
81       }
82       catch(IOException JavaDoc e)
83       {
84          e.printStackTrace();
85       }
86
87 // org.apache.log4j.ConsoleAppender consoleAppender = new org.apache.log4j.ConsoleAppender();
88
// consoleAppender.setThreshold(Level.INFO);
89
// org.apache.log4j.Category.getRoot().addAppender(consoleAppender);
90

91       //System.out.println("Root log level = " + org.apache.log4j.Category.getRoot().getLevel());
92
Enumeration JavaDoc appenders = org.apache.log4j.Category.getRoot().getAllAppenders();
93       while(appenders.hasMoreElements())
94       {
95          org.apache.log4j.Appender appender = (org.apache.log4j.Appender) appenders.nextElement();
96          //System.out.println(appender.getName());
97
if(appender instanceof org.apache.log4j.ConsoleAppender)
98          {
99             ((org.apache.log4j.ConsoleAppender) appender).setThreshold(Level.INFO);
100          }
101       }
102    }
103
104    /**
105     * Simple invocation handler implementation.
106     */

107    public static class SampleInvocationHandler implements ServerInvocationHandler
108    {
109       /**
110        * called to handle a specific invocation
111        *
112        * @param invocation
113        * @return
114        * @throws Throwable
115        */

116       public Object JavaDoc invoke(InvocationRequest invocation) throws Throwable JavaDoc
117       {
118          // Print out the invocation request
119
System.out.println("Invocation request is: " + invocation.getParameter());
120
121          // Just going to return static string as this is just simple example code.
122
return RESPONSE_VALUE;
123       }
124
125       /**
126        * Adds a callback handler that will listen for callbacks from
127        * the server invoker handler.
128        *
129        * @param callbackHandler
130        */

131       public void addListener(InvokerCallbackHandler callbackHandler)
132       {
133          // NO OP as do not handling callback listeners in this example
134
}
135
136       /**
137        * Removes the callback handler that was listening for callbacks
138        * from the server invoker handler.
139        *
140        * @param callbackHandler
141        */

142       public void removeListener(InvokerCallbackHandler callbackHandler)
143       {
144          // NO OP as do not handling callback listeners in this example
145
}
146
147       /**
148        * set the mbean server that the handler can reference
149        *
150        * @param server
151        */

152       public void setMBeanServer(MBeanServer JavaDoc server)
153       {
154          // NO OP as do not need reference to MBeanServer for this handler
155
}
156
157       /**
158        * set the invoker that owns this handler
159        *
160        * @param invoker
161        */

162       public void setInvoker(ServerInvoker invoker)
163       {
164          // NO OP as do not need reference back to the server invoker
165
}
166
167    }
168 }
Popular Tags