~ubuntu-branches/ubuntu/raring/libjboss-remoting-java/raring

« back to all changes in this revision

Viewing changes to src/main/org/jboss/remoting/marshal/MarshallerLoaderHandler.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: package-import@ubuntu.com-20110909140103-o8ucrolqt5g25k57
Tags: upstream-2.5.3.SP1
ImportĀ upstreamĀ versionĀ 2.5.3.SP1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
* JBoss, Home of Professional Open Source
 
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
 
4
* by the @authors tag. See the copyright.txt in the distribution for a
 
5
* full listing of individual contributors.
 
6
*
 
7
* This is free software; you can redistribute it and/or modify it
 
8
* under the terms of the GNU Lesser General Public License as
 
9
* published by the Free Software Foundation; either version 2.1 of
 
10
* the License, or (at your option) any later version.
 
11
*
 
12
* This software is distributed in the hope that it will be useful,
 
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
15
* Lesser General Public License for more details.
 
16
*
 
17
* You should have received a copy of the GNU Lesser General Public
 
18
* License along with this software; if not, write to the Free
 
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 
21
*/
 
22
 
 
23
package org.jboss.remoting.marshal;
 
24
 
 
25
import java.io.ByteArrayOutputStream;
 
26
import java.io.IOException;
 
27
import java.io.InputStream;
 
28
import java.net.URL;
 
29
import java.util.Iterator;
 
30
import java.util.List;
 
31
import java.util.Map;
 
32
import javax.management.MBeanServer;
 
33
import org.jboss.logging.Logger;
 
34
import org.jboss.remoting.InvocationRequest;
 
35
import org.jboss.remoting.InvokerLocator;
 
36
import org.jboss.remoting.ServerInvocationHandler;
 
37
import org.jboss.remoting.ServerInvoker;
 
38
import org.jboss.remoting.callback.InvokerCallbackHandler;
 
39
import org.jboss.remoting.loading.ClassBytes;
 
40
import org.jboss.remoting.loading.ClassUtil;
 
41
 
 
42
/**
 
43
 * The invocation handler that receives requests for getting marshallers/unmarshallers and
 
44
 * loading of these and any related classes to remoting client.
 
45
 *
 
46
 * @author <a href="mailto:tom@jboss.org">Tom Elrod</a>
 
47
 */
 
48
public class MarshallerLoaderHandler implements ServerInvocationHandler, MarshallerLoaderConstants
 
49
{
 
50
   private ServerInvoker invoker = null;
 
51
   private MBeanServer server = null;
 
52
   private List classLoaders;
 
53
 
 
54
   protected final static Logger log = Logger.getLogger(MarshallerLoaderHandler.class);
 
55
 
 
56
   public MarshallerLoaderHandler(List classLoaders)
 
57
   {
 
58
      this.classLoaders = classLoaders;
 
59
   }
 
60
   
 
61
   /**
 
62
    * set the mbean server that the handler can reference
 
63
    *
 
64
    * @param server
 
65
    */
 
66
   public void setMBeanServer(MBeanServer server)
 
67
   {
 
68
      this.server = server;
 
69
   }
 
70
 
 
71
   /**
 
72
    * set the invoker that owns this handler
 
73
    *
 
74
    * @param invoker
 
75
    */
 
76
   public void setInvoker(ServerInvoker invoker)
 
77
   {
 
78
      this.invoker = invoker;
 
79
   }
 
80
 
 
81
   /**
 
82
    * called to handle a specific invocation.  Please take care to make sure implementations are thread safe and can,
 
83
    * and often will, receive concurrent calls on this method.
 
84
    *
 
85
    * @param invocation
 
86
    * @return
 
87
    * @throws Throwable
 
88
    */
 
89
   public Object invoke(InvocationRequest invocation)
 
90
         throws Throwable
 
91
   {
 
92
      Object ret = null;
 
93
 
 
94
      Object param = invocation.getParameter();
 
95
      Map metadMap = invocation.getRequestPayload();
 
96
      if(metadMap == null)
 
97
      {
 
98
         throw new RuntimeException("Can not load class as invocation request metadat map is null.");
 
99
      }
 
100
 
 
101
      String dataType = (String) metadMap.get(InvokerLocator.DATATYPE);
 
102
 
 
103
      log.debug("MarshallerLoaderHandler received invocation with param of " + param + " and data type of " + dataType);
 
104
 
 
105
      if(GET_MARSHALLER_METHOD.equals(param))
 
106
      {
 
107
         ret = MarshalFactory.getMarshaller(dataType, invoker.getSerializationType());
 
108
      }
 
109
      else if(GET_UNMARSHALLER_METHOD.equals(param))
 
110
      {
 
111
         ret = MarshalFactory.getUnMarshaller(dataType, invoker.getSerializationType());
 
112
      }
 
113
      else if(LOAD_CLASS_METHOD.equals(param))
 
114
      {
 
115
         String className = (String) metadMap.get(CLASSNAME);
 
116
         log.debug("MarshallerLoaderHandler: loading class: " + className);
 
117
         if(className != null)
 
118
         {
 
119
            ret = loadClassBytes(className, invoker.getClassLoader());
 
120
            log.debug("MarshallerLoaderHandler: returning class: " + className + ": " + ret);
 
121
         }
 
122
         else
 
123
         {
 
124
            log.error("Received invocation " + param + " to load class, but metadata map key " + CLASSNAME +
 
125
                      " contains a null value for the class name to load.");
 
126
         }
 
127
      }
 
128
      else if(LOAD_MARSHALLER_METHOD.equals(param))
 
129
      {
 
130
         // load based on data type
 
131
         Marshaller marshaller = MarshalFactory.getMarshaller(dataType, invoker.getSerializationType());
 
132
         if(marshaller != null)
 
133
         {
 
134
            String className = marshaller.getClass().getName();
 
135
            ret = loadClassBytes(className, invoker.getClassLoader());
 
136
         }
 
137
         else
 
138
         {
 
139
            log.warn("Could not find registered marshaller for data type: " + dataType);
 
140
         }
 
141
      }
 
142
      else if(LOAD_UNMARSHALLER_METHOD.equals(param))
 
143
      {
 
144
         UnMarshaller unmarshaller = MarshalFactory.getUnMarshaller(dataType, invoker.getSerializationType());
 
145
         if(unmarshaller != null)
 
146
         {
 
147
            String className = unmarshaller.getClass().getName();
 
148
            ret = loadClassBytes(className, invoker.getClassLoader());
 
149
         }
 
150
         else
 
151
         {
 
152
            log.warn("Could not find registered unmarshaller for data type: " + dataType);
 
153
         }
 
154
      }
 
155
      else
 
156
      {
 
157
         log.warn("Received invocation with unknown parameter request: " + param);
 
158
      }
 
159
 
 
160
 
 
161
      return ret;
 
162
   }
 
163
 
 
164
   private Object loadClassBytes(String className, ClassLoader classLoader)
 
165
   {
 
166
      ClassBytes classBytes = null;
 
167
 
 
168
      if(className != null)
 
169
      {
 
170
         byte[] classDefinition = ClassUtil.getClassBytes(className, classLoader);
 
171
         
 
172
         if (classDefinition == null && classLoaders != null)
 
173
         {
 
174
            Iterator it = classLoaders.iterator();
 
175
            while (it.hasNext())
 
176
            {
 
177
               ClassLoader cl = (ClassLoader) it.next();
 
178
               log.debug("trying classLoader " + cl);
 
179
               classDefinition = ClassUtil.getClassBytes(className, cl);
 
180
               if (classDefinition != null)
 
181
               {
 
182
                  break;
 
183
               }
 
184
            }
 
185
         }
 
186
         
 
187
         if (classDefinition == null)
 
188
         {
 
189
            log.debug("unable to load class " + className);
 
190
         }
 
191
         classBytes = new ClassBytes(className, classDefinition);
 
192
      }
 
193
      return classBytes;
 
194
   }
 
195
 
 
196
   /**
 
197
    * Adds a callback handler that will listen for callbacks from the server invoker handler.
 
198
    *
 
199
    * @param callbackHandler
 
200
    */
 
201
   public void addListener(InvokerCallbackHandler callbackHandler)
 
202
   {
 
203
      //NO OP as don't won't allow listeners
 
204
   }
 
205
 
 
206
   /**
 
207
    * Removes the callback handler that was listening for callbacks from the server invoker handler.
 
208
    *
 
209
    * @param callbackHandler
 
210
    */
 
211
   public void removeListener(InvokerCallbackHandler callbackHandler)
 
212
   {
 
213
      //NO OP as don't won't allow listeners
 
214
   }
 
215
   
 
216
   /**
 
217
    * Adapted from org.jboss.mx.loading.RepositoryClassLoader.
 
218
    * 
 
219
    * @param cl
 
220
    * @param classname
 
221
    * @return
 
222
    * @throws ClassNotFoundException
 
223
    * @throws IOException
 
224
    */
 
225
   protected byte[] loadByteCode(ClassLoader cl, String classname)
 
226
   throws ClassNotFoundException, IOException
 
227
   {
 
228
      byte[] bytecode = null;
 
229
      URL classURL = getClassURL(cl, classname);
 
230
 
 
231
      // Load the class bytecode
 
232
      InputStream is = null;
 
233
      try
 
234
      {
 
235
         is = classURL.openStream();
 
236
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 
237
         byte[] tmp = new byte[1024];
 
238
         int read = 0;
 
239
         while( (read = is.read(tmp)) > 0 )
 
240
         {
 
241
            baos.write(tmp, 0, read);
 
242
         }
 
243
         bytecode = baos.toByteArray();
 
244
      }
 
245
      finally
 
246
      {
 
247
         if( is != null )
 
248
            is.close();
 
249
      }
 
250
 
 
251
      return bytecode;
 
252
   }
 
253
   
 
254
   /**
 
255
    * Adapted from org.jboss.mx.loading.RepositoryClassLoader.
 
256
    * 
 
257
    * @param cl
 
258
    * @param classname
 
259
    * @return
 
260
    * @throws ClassNotFoundException
 
261
    */
 
262
   private URL getClassURL(ClassLoader cl, String classname) throws ClassNotFoundException
 
263
   {
 
264
      String classRsrcName = classname.replace('.', '/') + ".class";
 
265
      URL classURL = cl.getResource(classRsrcName);
 
266
      if( classURL == null )
 
267
      {
 
268
         String msg = "Failed to find: "+classname+" as resource: "+classRsrcName;
 
269
         throw new ClassNotFoundException(msg);
 
270
      }
 
271
      return classURL;
 
272
   }
 
273
}