KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > marshal > MarshallerLoaderHandler


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.marshal;
8
9 import java.util.Map JavaDoc;
10 import javax.management.MBeanServer JavaDoc;
11 import org.jboss.logging.Logger;
12 import org.jboss.remoting.InvocationRequest;
13 import org.jboss.remoting.InvokerLocator;
14 import org.jboss.remoting.ServerInvocationHandler;
15 import org.jboss.remoting.ServerInvoker;
16 import org.jboss.remoting.callback.InvokerCallbackHandler;
17 import org.jboss.remoting.loading.ClassBytes;
18 import org.jboss.remoting.loading.ClassUtil;
19
20 /**
21  * The invocation handler that receives requests for getting marshallers/unmarshallers and
22  * loading of these and any related classes to remoting client.
23  *
24  * @author <a HREF="mailto:tom@jboss.org">Tom Elrod</a>
25  */

26 public class MarshallerLoaderHandler implements ServerInvocationHandler, MarshallerLoaderConstants
27 {
28    private ServerInvoker invoker = null;
29    private MBeanServer JavaDoc server = null;
30
31    protected final static Logger log = Logger.getLogger(MarshallerLoaderHandler.class);
32
33
34    /**
35     * set the mbean server that the handler can reference
36     *
37     * @param server
38     */

39    public void setMBeanServer(MBeanServer JavaDoc server)
40    {
41       this.server = server;
42    }
43
44    /**
45     * set the invoker that owns this handler
46     *
47     * @param invoker
48     */

49    public void setInvoker(ServerInvoker invoker)
50    {
51       this.invoker = invoker;
52    }
53
54    /**
55     * called to handle a specific invocation. Please take care to make sure implementations are thread safe and can,
56     * and often will, receive concurrent calls on this method.
57     *
58     * @param invocation
59     * @return
60     * @throws Throwable
61     */

62    public Object JavaDoc invoke(InvocationRequest invocation)
63          throws Throwable JavaDoc
64    {
65       Object JavaDoc ret = null;
66
67       Object JavaDoc param = invocation.getParameter();
68       Map JavaDoc metadMap = invocation.getRequestPayload();
69       String JavaDoc dataType = (String JavaDoc) metadMap.get(InvokerLocator.DATATYPE);
70
71       log.debug("MarshallerLoaderHandler received invocation with param of " + param + " and data type of " + dataType);
72
73       if(GET_MARSHALLER_METHOD.equals(param))
74       {
75          ret = MarshalFactory.getMarshaller(dataType);
76       }
77       else if(GET_UNMARSHALLER_METHOD.equals(param))
78       {
79          ret = MarshalFactory.getUnMarshaller(dataType);
80       }
81       else if(LOAD_CLASS_METHOD.equals(param))
82       {
83          String JavaDoc className = (String JavaDoc) metadMap.get(CLASSNAME);
84          if(className != null)
85          {
86             ret = loadClassBytes(className, invoker.getClassLoader());
87          }
88          else
89          {
90             log.error("Received invocation " + param + " to load class, but metadata map key " + CLASSNAME +
91                       " contains a null value for the class name to load.");
92          }
93       }
94       else if(LOAD_MARSHALLER_METHOD.equals(param))
95       {
96          // load based on data type
97
Marshaller marshaller = MarshalFactory.getMarshaller(dataType);
98          if(marshaller != null)
99          {
100             String JavaDoc className = marshaller.getClass().getName();
101             ret = loadClassBytes(className, invoker.getClassLoader());
102          }
103          else
104          {
105             log.warn("Could not find registered marshaller for data type: " + dataType);
106          }
107       }
108       else if(LOAD_UNMARSHALLER_METHOD.equals(param))
109       {
110          UnMarshaller unmarshaller = MarshalFactory.getUnMarshaller(dataType);
111          if(unmarshaller != null)
112          {
113             String JavaDoc className = unmarshaller.getClass().getName();
114             ret = loadClassBytes(className, invoker.getClassLoader());
115          }
116          else
117          {
118             log.warn("Could not find registered unmarshaller for data type: " + dataType);
119          }
120       }
121       else
122       {
123          log.warn("Received invocation with unknown parameter request: " + param);
124       }
125
126
127       return ret;
128    }
129
130    private Object JavaDoc loadClassBytes(String JavaDoc className, ClassLoader JavaDoc classLoader)
131    {
132       ClassBytes classBytes = null;
133
134       if(className != null)
135       {
136          byte[] classDefinition = ClassUtil.getClassBytes(className, classLoader);
137          classBytes = new ClassBytes(className, classDefinition);
138       }
139       return classBytes;
140    }
141
142    /**
143     * Adds a callback handler that will listen for callbacks from the server invoker handler.
144     *
145     * @param callbackHandler
146     */

147    public void addListener(InvokerCallbackHandler callbackHandler)
148    {
149       //NO OP as don't won't allow listeners
150
}
151
152    /**
153     * Removes the callback handler that was listening for callbacks from the server invoker handler.
154     *
155     * @param callbackHandler
156     */

157    public void removeListener(InvokerCallbackHandler callbackHandler)
158    {
159       //NO OP as don't won't allow listeners
160
}
161 }
162
Popular Tags