[jboss-cvs] JBossRemoting/src/main/org/jboss/remoting/transport/servlet/web ...

Tom Elrod tom.elrod at jboss.com
Sun Jul 16 23:36:20 EDT 2006


  User: telrod  
  Date: 06/07/16 23:36:20

  Modified:    src/main/org/jboss/remoting/transport/servlet/web 
                        ServerInvokerServlet.java
  Log:
  JBREM-542 - added back in ability to use 'invokerName' param.
  
  Revision  Changes    Path
  1.7       +126 -35   JBossRemoting/src/main/org/jboss/remoting/transport/servlet/web/ServerInvokerServlet.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: ServerInvokerServlet.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossRemoting/src/main/org/jboss/remoting/transport/servlet/web/ServerInvokerServlet.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -b -r1.6 -r1.7
  --- ServerInvokerServlet.java	15 Jul 2006 04:40:11 -0000	1.6
  +++ ServerInvokerServlet.java	17 Jul 2006 03:36:20 -0000	1.7
  @@ -28,6 +28,11 @@
   import org.jboss.remoting.ServerInvoker;
   import org.jboss.remoting.transport.servlet.ServletServerInvokerMBean;
   
  +import javax.management.MBeanServer;
  +import javax.management.MBeanServerFactory;
  +import javax.management.MBeanServerInvocationHandler;
  +import javax.management.MalformedObjectNameException;
  +import javax.management.ObjectName;
   import javax.servlet.ServletConfig;
   import javax.servlet.ServletException;
   import javax.servlet.ServletInputStream;
  @@ -37,6 +42,7 @@
   import javax.servlet.http.HttpServletResponse;
   import java.io.ByteArrayOutputStream;
   import java.io.IOException;
  +import java.util.Iterator;
   
   /**
    * The servlet that receives the inital http request for the ServletServerInvoker.
  @@ -56,43 +62,18 @@
      {
         super.init(config);
   
  -      // Get the locator url from config so can use to
  -      // find serlvet invoker to pass calls onto.
  -      String locatorUrl = config.getInitParameter("locatorUrl");
  -      if(locatorUrl != null)
  -      {
  -         ServerInvoker[] serverInvokers = InvokerRegistry.getServerInvokers();
  -         if(serverInvokers != null && serverInvokers.length > 0)
  -         {
  -            for(int x = 0; x < serverInvokers.length; x++)
  -            {
  -               ServerInvoker svrInvoker = serverInvokers[x];
  -               InvokerLocator locator = svrInvoker.getLocator();
  -               if(locatorUrl.equalsIgnoreCase(locator.getOriginalURI()))
  -               {
  -                  servletInvoker = (ServletServerInvokerMBean)svrInvoker;
  -                  break;
  -               }
  -            }
  +      // first see if the invoker is specified by its URL; if not, then see if the invoker was specified by name
  +      servletInvoker = getInvokerFromInvokerUrl(config);
   
  -            // was the servlet invoker found
  -            if(servletInvoker == null)
  +      if (servletInvoker == null)
               {
  -               throw new ServletException("Can not find servlet server invoker with same locator as specified (" + locatorUrl +")");
  -            }
  -         }
  -         else
  +         servletInvoker = getInvokerFromInvokerName(config);
  +
  +         if (servletInvoker == null)
            {
  -            throw new ServletException("Can not find any server invokers registered.  " +
  -                                       "Could be that servlet server invoker not registered or " +
  -                                       "has been created using different classloader.");
  -         }
  +            throw new ServletException("Could not find init parameter for 'locatorUrl' or 'locatorName' - one of which must be supplied for ServerInvokerServlet to function.");
         }
  -      else
  -      {
  -         throw new ServletException("Could not find init parameter for 'locatorUrl' which must be supplied for ServerInvokerServlet to function.");
         }
  -
      }
   
      /**
  @@ -114,7 +95,7 @@
            throws ServletException, IOException
      {
         boolean trace = log.isTraceEnabled();
  -      if(trace)
  +      if (trace)
         {
            log.trace("processRequest, ContentLength: " + request.getContentLength());
            log.trace("processRequest, ContentType: " + request.getContentType());
  @@ -129,11 +110,11 @@
         ServletInputStream inputStream = request.getInputStream();
         int amtRead = inputStream.read(byteBuffer);
   
  -      while(amtRead > 0)
  +      while (amtRead > 0)
         {
            byteOutputStream.write(byteBuffer, pointer, amtRead);
            //pointer+=amtRead;
  -         if(amtRead < bufferSize && byteOutputStream.size() >= contentLength)
  +         if (amtRead < bufferSize && byteOutputStream.size() >= contentLength)
            {
               //done reading, so process
               break;
  @@ -183,4 +164,114 @@
         return "Servlet front to JBossRemoting servlet server invoker.";
      }
   
  +   /**
  +    * Returns the servlet server invoker but only if it was specified via the
  +    * "invokerUrl" init parameter.
  +    *
  +    * @param config the servlet configuration
  +    * @return the servlet server invoker as specified by the "invokerUrl", or
  +    *         <code>null</code> if "invokerUrl" init parameter was not specified
  +    * @throws ServletException
  +    */
  +   protected ServletServerInvokerMBean getInvokerFromInvokerUrl(ServletConfig config)
  +         throws ServletException
  +   {
  +      String locatorUrl = config.getInitParameter("locatorUrl");
  +      if (locatorUrl == null)
  +      {
  +         return null;
  +      }
  +
  +      ServerInvoker[] serverInvokers = InvokerRegistry.getServerInvokers();
  +      if (serverInvokers != null && serverInvokers.length > 0)
  +      {
  +         for (int x = 0; x < serverInvokers.length; x++)
  +         {
  +            ServerInvoker svrInvoker = serverInvokers[x];
  +            InvokerLocator locator = svrInvoker.getLocator();
  +            if (locatorUrl.equalsIgnoreCase(locator.getOriginalURI()))
  +            {
  +               return (ServletServerInvokerMBean) svrInvoker;
  +            }
  +         }
  +
  +         throw new ServletException("Can not find servlet server invoker with same locator as specified (" + locatorUrl + ")");
  +      }
  +
  +      throw new ServletException("Can not find any server invokers registered.  " +
  +                                 "Could be that servlet server invoker not registered or " +
  +                                 "has been created using different classloader.");
  +   }
  +
  +   /**
  +    * Returns the servlet server invoker but only if it was specified via the
  +    * "invokerName" init parameter.
  +    *
  +    * @param config the servlet configuration
  +    * @return the servlet server invoker as specified by the "invokerName", or
  +    *         <code>null</code> if "invokerName" init parameter was not specified
  +    * @throws ServletException
  +    */
  +   protected ServletServerInvokerMBean getInvokerFromInvokerName(ServletConfig config)
  +         throws ServletException
  +   {
  +      ObjectName localInvokerName = null;
  +
  +      String name = config.getInitParameter("invokerName");
  +      if (name == null)
  +      {
  +         return null;
  +      }
  +
  +      try
  +      {
  +         localInvokerName = new ObjectName(name);
  +         log.debug("localInvokerName=" + localInvokerName);
  +      }
  +      catch (MalformedObjectNameException e)
  +      {
  +         throw new ServletException("Failed to build invokerName", e);
  +      }
  +
  +      // Lookup the MBeanServer
  +      MBeanServer mbeanServer = getMBeanServer();
  +      if (mbeanServer == null)
  +      {
  +         throw new ServletException("Failed to locate the MBeanServer");
  +      }
  +
  +      return (ServletServerInvokerMBean)
  +            MBeanServerInvocationHandler.newProxyInstance(mbeanServer,
  +                                                          localInvokerName,
  +                                                          ServletServerInvokerMBean.class,
  +                                                          false);
  +   }
  +
  +   /**
  +    * Returns the MBeanServer where the server invoker should be found.  This should only be
  +    * used if the "invokerName" init parameter is specified (where that invoker name is the
  +    * object name registered in the returned MBeanServer).
  +    *
  +    * @return MBeanServer where the invoker is supposed to be registered
  +    */
  +   protected MBeanServer getMBeanServer()
  +   {
  +      // the intention of having this as a separate protected method is for subclasses to override
  +      // it in case this servlet is not running in JBossAS and thus needs to find an non-JBoss
  +      // MBeanServer.  This design won't work however since when this servlet is loaded, it will
  +      // still need to load in this JBoss specific MBeanServerLocator.  But, this servlet also
  +      // requires JBoss logging too so its not like this is the only place that breaks if not running
  +      // in JBossAS.  To complete this design, we must make this parent servlet an abstract class,
  +      // which this method abstract.  Then we need to create a JBoss-specific subclass with this
  +      // method's code in its getMBeanServer().
  +      for (Iterator i = MBeanServerFactory.findMBeanServer(null).iterator(); i.hasNext();)
  +      {
  +         MBeanServer server = (MBeanServer) i.next();
  +         if (server.getDefaultDomain().equals("jboss"))
  +         {
  +            return server;
  +         }
  +      }
  +      return null;
  +   }
   }
  \ No newline at end of file
  
  
  



More information about the jboss-cvs-commits mailing list