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

« back to all changes in this revision

Viewing changes to src/org/jboss/remoting/transport/servlet/web/ServerInvokerServlet.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

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.transport.servlet.web;
24
 
 
25
 
import org.jboss.logging.Logger;
26
 
import org.jboss.remoting.InvokerLocator;
27
 
import org.jboss.remoting.InvokerRegistry;
28
 
import org.jboss.remoting.ServerInvoker;
29
 
import org.jboss.remoting.transport.servlet.ServletServerInvokerMBean;
30
 
import org.jboss.remoting.util.SecurityUtility;
31
 
import javax.management.MBeanServer;
32
 
import javax.management.MBeanServerFactory;
33
 
import javax.management.MBeanServerInvocationHandler;
34
 
import javax.management.MalformedObjectNameException;
35
 
import javax.management.ObjectName;
36
 
import javax.servlet.ServletConfig;
37
 
import javax.servlet.ServletException;
38
 
import javax.servlet.ServletInputStream;
39
 
import javax.servlet.ServletOutputStream;
40
 
import javax.servlet.http.HttpServlet;
41
 
import javax.servlet.http.HttpServletRequest;
42
 
import javax.servlet.http.HttpServletResponse;
43
 
import java.io.ByteArrayOutputStream;
44
 
import java.io.IOException;
45
 
import java.lang.reflect.InvocationTargetException;
46
 
import java.lang.reflect.Method;
47
 
import java.net.MalformedURLException;
48
 
import java.security.AccessController;
49
 
import java.security.PrivilegedAction;
50
 
import java.security.PrivilegedActionException;
51
 
import java.security.PrivilegedExceptionAction;
52
 
import java.util.ArrayList;
53
 
import java.util.Iterator;
54
 
 
55
 
/**
56
 
 * The servlet that receives the inital http request for the ServletServerInvoker.
57
 
 *
58
 
 * @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
59
 
 */
60
 
public class ServerInvokerServlet extends HttpServlet
61
 
{
62
 
   private static Logger log = Logger.getLogger(ServerInvokerServlet.class);
63
 
   private ServletServerInvokerMBean servletInvoker;
64
 
   private static final long serialVersionUID = 8796224225710165263L;
65
 
 
66
 
   /**
67
 
    * Initializes the servlet.
68
 
    */
69
 
   public void init(ServletConfig config) throws ServletException
70
 
   {
71
 
      super.init(config);
72
 
 
73
 
      // first see if the invoker is specified by its URL; if not, then see if the invoker was specified by name
74
 
      servletInvoker = getInvokerFromInvokerUrl(config);
75
 
 
76
 
      if (servletInvoker == null)
77
 
      {
78
 
         servletInvoker = getInvokerFromInvokerName(config);
79
 
 
80
 
         if (servletInvoker == null)
81
 
         {
82
 
            throw new ServletException("Could not find init parameter for 'locatorUrl' or 'locatorName' - one of which must be supplied for ServerInvokerServlet to function.");
83
 
         }
84
 
         else
85
 
         {
86
 
            log.debug("Got ServletServerInvoker from InvokerName: " + config.getInitParameter("invokerName"));
87
 
         }
88
 
      }
89
 
      else
90
 
      {
91
 
         log.debug("Got ServletServerInvoker from InvokerLocator: " + config.getInitParameter("locatorUrl"));
92
 
      }
93
 
   }
94
 
 
95
 
   /**
96
 
    * Destroys the servlet.
97
 
    */
98
 
   public void destroy()
99
 
   {
100
 
 
101
 
   }
102
 
 
103
 
   /**
104
 
    * Read a MarshalledInvocation and dispatch it to the target JMX object
105
 
    * invoke(Invocation) object.
106
 
    *
107
 
    * @param request  servlet request
108
 
    * @param response servlet response
109
 
    */
110
 
   protected void processRequest(HttpServletRequest request, HttpServletResponse response)
111
 
         throws ServletException, IOException
112
 
   {
113
 
      boolean trace = log.isTraceEnabled();
114
 
      if (trace)
115
 
      {
116
 
         log.trace("processRequest, ContentLength: " + request.getContentLength());
117
 
         log.trace("processRequest, ContentType: " + request.getContentType());
118
 
      }
119
 
 
120
 
      int bufferSize = 1024;
121
 
      byte[] byteBuffer = new byte[bufferSize];
122
 
      ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
123
 
 
124
 
      int pointer = 0;
125
 
      int contentLength = request.getContentLength();
126
 
      ServletInputStream inputStream = request.getInputStream();
127
 
      int amtRead = inputStream.read(byteBuffer);
128
 
 
129
 
      while (amtRead > 0)
130
 
      {
131
 
         byteOutputStream.write(byteBuffer, pointer, amtRead);
132
 
         //pointer+=amtRead;
133
 
         if (amtRead < bufferSize && byteOutputStream.size() >= contentLength)
134
 
         {
135
 
            //done reading, so process
136
 
            break;
137
 
         }
138
 
         amtRead = inputStream.read(byteBuffer);
139
 
      }
140
 
      byteOutputStream.flush();
141
 
      byte[] totalByteArray = byteOutputStream.toByteArray();
142
 
      byte[] out = processRequest(servletInvoker, request, totalByteArray, response);
143
 
      ServletOutputStream outStream = response.getOutputStream();
144
 
      outStream.write(out);
145
 
      outStream.flush();
146
 
      outStream.close();
147
 
      //response.setContentLength(out.length);
148
 
   }
149
 
 
150
 
   /**
151
 
    * Handles the HTTP <code>GET</code> method.
152
 
    *
153
 
    * @param request  servlet request
154
 
    * @param response servlet response
155
 
    */
156
 
   protected void doGet(HttpServletRequest request, HttpServletResponse response)
157
 
         throws ServletException, IOException
158
 
   {
159
 
      processRequest(request, response);
160
 
   }
161
 
 
162
 
   /**
163
 
    * Handles the HTTP <code>POST</code> method.
164
 
    *
165
 
    * @param request  servlet request
166
 
    * @param response servlet response
167
 
    */
168
 
   protected void doPost(HttpServletRequest request, HttpServletResponse response)
169
 
         throws ServletException, IOException
170
 
   {
171
 
      processRequest(request, response);
172
 
   }
173
 
 
174
 
   /**
175
 
    * Returns a short description of the servlet.
176
 
    */
177
 
   public String getServletInfo()
178
 
   {
179
 
      return "Servlet front to JBossRemoting servlet server invoker.";
180
 
   }
181
 
 
182
 
   /**
183
 
    * Returns the servlet server invoker but only if it was specified via the
184
 
    * "invokerUrl" init parameter.
185
 
    *
186
 
    * @param config the servlet configuration
187
 
    * @return the servlet server invoker as specified by the "invokerUrl", or
188
 
    *         <code>null</code> if "invokerUrl" init parameter was not specified
189
 
    * @throws ServletException
190
 
    */
191
 
   protected ServletServerInvokerMBean getInvokerFromInvokerUrl(ServletConfig config)
192
 
         throws ServletException
193
 
   {
194
 
      String locatorUrl = config.getInitParameter("locatorUrl");
195
 
      if (locatorUrl == null)
196
 
      {
197
 
         return null;
198
 
      }
199
 
      try
200
 
      {
201
 
         InvokerLocator validatedLocator = new InvokerLocator(locatorUrl);
202
 
         locatorUrl = InvokerLocator.validateLocator(validatedLocator).getLocatorURI();
203
 
      }
204
 
      catch (MalformedURLException e)
205
 
      {
206
 
         log.warn("malformed URL: " + locatorUrl);
207
 
         return null;
208
 
      }
209
 
 
210
 
      ServerInvoker[] serverInvokers = InvokerRegistry.getServerInvokers();
211
 
      if (serverInvokers != null && serverInvokers.length > 0)
212
 
      {
213
 
         for (int x = 0; x < serverInvokers.length; x++)
214
 
         {
215
 
            ServerInvoker svrInvoker = serverInvokers[x];
216
 
            InvokerLocator locator = svrInvoker.getLocator();
217
 
            if (locatorUrl.equalsIgnoreCase(locator.getLocatorURI()))
218
 
            {
219
 
               return (ServletServerInvokerMBean) svrInvoker;
220
 
            }
221
 
         }
222
 
 
223
 
         throw new ServletException("Can not find servlet server invoker with same locator as specified (" + locatorUrl + ")");
224
 
      }
225
 
 
226
 
      throw new ServletException("Can not find any server invokers registered.  " +
227
 
                                 "Could be that servlet server invoker not registered or " +
228
 
                                 "has been created using different classloader.");
229
 
   }
230
 
 
231
 
   /**
232
 
    * Returns the servlet server invoker but only if it was specified via the
233
 
    * "invokerName" init parameter.
234
 
    *
235
 
    * @param config the servlet configuration
236
 
    * @return the servlet server invoker as specified by the "invokerName", or
237
 
    *         <code>null</code> if "invokerName" init parameter was not specified
238
 
    * @throws ServletException
239
 
    */
240
 
   protected ServletServerInvokerMBean getInvokerFromInvokerName(ServletConfig config)
241
 
         throws ServletException
242
 
   {
243
 
      ObjectName localInvokerName = null;
244
 
 
245
 
      String name = config.getInitParameter("invokerName");
246
 
      if (name == null)
247
 
      {
248
 
         return null;
249
 
      }
250
 
 
251
 
      try
252
 
      {
253
 
         localInvokerName = new ObjectName(name);
254
 
         log.debug("localInvokerName=" + localInvokerName);
255
 
      }
256
 
      catch (MalformedObjectNameException e)
257
 
      {
258
 
         throw new ServletException("Failed to build invokerName", e);
259
 
      }
260
 
 
261
 
      // Lookup the MBeanServer
262
 
      String mbeanServerId = config.getInitParameter("mbeanServer");
263
 
      MBeanServer mbeanServer = getMBeanServer(mbeanServerId);
264
 
      if (mbeanServer == null)
265
 
      {
266
 
         throw new ServletException("Failed to locate the MBeanServer");
267
 
      }
268
 
 
269
 
      return (ServletServerInvokerMBean)
270
 
            MBeanServerInvocationHandler.newProxyInstance(mbeanServer,
271
 
                                                          localInvokerName,
272
 
                                                          ServletServerInvokerMBean.class,
273
 
                                                          false);
274
 
   }
275
 
 
276
 
   /**
277
 
    * Returns the MBeanServer where the server invoker should be found.  This should only be
278
 
    * used if the "invokerName" init parameter is specified (where that invoker name is the
279
 
    * object name registered in the returned MBeanServer).
280
 
    *
281
 
    * @param mbeanServerId indicates which MBeanServer to use
282
 
    *
283
 
    * @return MBeanServer where the invoker is supposed to be registered
284
 
    */
285
 
   protected MBeanServer getMBeanServer(String mbeanServerId)
286
 
   {
287
 
      if (mbeanServerId == null)
288
 
      {
289
 
         mbeanServerId = "jboss";
290
 
      }
291
 
 
292
 
      if (mbeanServerId.equals("*platform*"))
293
 
      {
294
 
         try
295
 
         {
296
 
            MBeanServer s = getPlatformMBeanServer();
297
 
            log.debug("Using platform MBeanServer");
298
 
            return s;
299
 
         }
300
 
         catch (Exception e)
301
 
         {
302
 
            mbeanServerId = "jboss";
303
 
         }
304
 
      }
305
 
      
306
 
      Iterator i = findMBeanServer(null).iterator();
307
 
      while(i.hasNext())
308
 
      {
309
 
         MBeanServer server = (MBeanServer) i.next();
310
 
         
311
 
         if (server.getDefaultDomain() == null)
312
 
         {
313
 
            continue;
314
 
         }
315
 
         if (server.getDefaultDomain().equals(mbeanServerId))
316
 
         {
317
 
            log.debug("Using MBeanServer with defaultDomain: " + mbeanServerId);
318
 
            return server;
319
 
         }
320
 
      }
321
 
      
322
 
      return null;
323
 
   }
324
 
   
325
 
   static private ArrayList findMBeanServer(final String agentId)
326
 
   {
327
 
      if (SecurityUtility.skipAccessControl())
328
 
      {
329
 
         return MBeanServerFactory.findMBeanServer(agentId);
330
 
      }
331
 
      
332
 
      return (ArrayList)AccessController.doPrivileged( new PrivilegedAction()
333
 
      {
334
 
         public Object run()
335
 
         {
336
 
            return MBeanServerFactory.findMBeanServer(agentId);
337
 
         }
338
 
      });
339
 
   }
340
 
   
341
 
   static private MBeanServer getPlatformMBeanServer()
342
 
   throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
343
 
   {
344
 
      if (SecurityUtility.skipAccessControl())
345
 
      {
346
 
         Class c = null;
347
 
         try
348
 
         {
349
 
            c = Class.forName("java.lang.management.ManagementFactory");
350
 
         }
351
 
         catch (Exception e)
352
 
         {
353
 
            System.out.println("Unable to access java.lang.management.ManagementFactory: must be using jdk 1.4");
354
 
            return null;
355
 
         }
356
 
         Method m = c.getMethod("getPlatformMBeanServer", new Class[] {});
357
 
         MBeanServer s = (MBeanServer) m.invoke(null, new Object[] {});
358
 
         return s;
359
 
      }
360
 
      
361
 
      try
362
 
      {
363
 
         return (MBeanServer) AccessController.doPrivileged( new PrivilegedExceptionAction()
364
 
         {
365
 
            public Object run()
366
 
            throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
367
 
            {
368
 
               Class c = null;
369
 
               try
370
 
               {
371
 
                  c = Class.forName("java.lang.management.ManagementFactory");
372
 
               }
373
 
               catch (Exception e)
374
 
               {
375
 
                  System.out.println("Unable to access java.lang.management.ManagementFactory: must be using jdk 1.4");
376
 
                  return null;
377
 
               }
378
 
               Method m = c.getMethod("getPlatformMBeanServer", new Class[] {});
379
 
               MBeanServer s = (MBeanServer) m.invoke(null, new Object[] {});
380
 
               return s;
381
 
            }
382
 
         });
383
 
      }
384
 
      catch (PrivilegedActionException e)
385
 
      {
386
 
        Throwable cause = e.getCause();
387
 
        if (cause instanceof NoSuchMethodException)
388
 
           throw (NoSuchMethodException) cause;
389
 
        else if (cause instanceof IllegalAccessException)
390
 
           throw (IllegalAccessException) cause;
391
 
        else
392
 
           throw (InvocationTargetException) cause;
393
 
      }  
394
 
   }
395
 
   
396
 
   static private byte[] processRequest(final ServletServerInvokerMBean invoker,
397
 
         final HttpServletRequest request,
398
 
         final byte[] byteArray,
399
 
         final HttpServletResponse response)
400
 
   throws ServletException, IOException
401
 
   {
402
 
      if (SecurityUtility.skipAccessControl())
403
 
      {
404
 
         return invoker.processRequest(request, byteArray, response);
405
 
      }
406
 
 
407
 
      try
408
 
      {
409
 
         return (byte[]) AccessController.doPrivileged( new PrivilegedExceptionAction()
410
 
         {
411
 
            public Object run() throws ServletException, IOException
412
 
            {
413
 
               return invoker.processRequest(request, byteArray, response);
414
 
            }
415
 
         });
416
 
      }
417
 
      catch (PrivilegedActionException e)
418
 
      {
419
 
         Throwable cause = e.getCause();
420
 
         if (cause instanceof ServletException)
421
 
            throw (ServletException) cause;
422
 
         else
423
 
            throw (IOException) e.getCause();
424
 
      }  
425
 
   }
426
 
}
 
 
b'\\ No newline at end of file'