Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
 
 package org.jboss.remoting.stream;
 
 import  org.jboss.logging.Logger;
 
This is a helper class that runs internal to remoting on the client side. It contains a reference to a local input stream and creates a remoting server to receive calls from a target remoting server (via calls from a StreamHandler initiated by a server invoker handler).

NOTE: That once this class receives the close() method called from the server, it will also stop and destroy the internal remoting server, since is assumed there will be no more callbacks (since the stream itself is closed).

Author(s):
Tom Elrod
 
 public class StreamServer
 {
    private InputStream streamSource = null;
 
    private String transport = "socket";
    private String host = "localhost";
    private int port = 5405;
 
    private Connector connector = null;
 
    private boolean internalConnector = true;
 
    private static final Logger log = Logger.getLogger(StreamServer.class);
 
    public static final String STREAM_TRANSPORT_KEY = "remoting.stream.transport";
    public static final String STREAM_HOST_KEY = "remoting.stream.host";
    public static final String STREAM_PORT_KEY = "remoting.stream.port";
 
    private static final InetAddress LOCAL_HOST;
 
    static
    {
       try
       {
           = (InetAddress) AccessController.doPrivilegednew PrivilegedExceptionAction()
          {
             public Object run() throws UnknownHostException
             {
                try
                {
                   return InetAddress.getLocalHost();
                }
                catch (UnknownHostException e)
                {
                   return InetAddress.getByName("127.0.0.1");
                }
             }
          });
       }
       catch (PrivilegedActionException e)
       {
          .debug(StreamServer.class.getName() + " unable to get local host address"e.getCause());
         throw new ExceptionInInitializerError(e.getCause());
      }
      catch (SecurityException e)
      {
         .debug(StreamServer.class.getName() + " unable to get local host address"e);
         throw e;
      }
   }
   
   
Creates the server wrapped around the specified input stream. This will create the remoting server as well.

Parameters:
stream
Throws:
Exception
   public StreamServer(InputStream streamthrows Exception
   {
      this. = stream;
      String locatorURI = getLocatorURI();
      setupServer(locatorURI);
   }
   public StreamServer(InputStream streamInvokerLocator locatorthrows Exception
   {
      this. = stream;
      setupServer(locator.getLocatorURI());
   }
   public StreamServer(InputStream streamConnector connectorthrows Exception
   {
      this. = stream;
      this. = connector;
      if(connector != null)
      {
         if(!connector.isStarted())
         {
            throw new IllegalStateException("Connector (" + connector + ") passed to act as stream server has not been started.");
         }
         ServerInvocationHandler invocationHandler = new Handler(connector);
         connector.addInvocationHandler("stream"invocationHandler);
          = false;
      }
      else
      {
         throw new NullPointerException("Connector passed to act as stream server can not be null.");
      }
   }
   private String getLocatorURI() throws IOException
   {
      // check for system properties for locator values
      try
      {
          = getLocalHostName();
      }
      catch(UnknownHostException e)
      {
         try
         {
            InetAddress localAddress = getLocalHost();
             = localAddress.getHostAddress();
         }
         catch(UnknownHostException e1)
         {
            .error("Stream server could not determine local host or address.");
         }
      }
      String defaultPort = "" + PortUtil.findFreePort();
      String sPort = getSystemProperty(defaultPort);
      
      try
      {
          = Integer.parseInt(sPort);
      }
      catch(NumberFormatException e)
      {
         .error("Stream server could not convert specified port " + sPort + " to a number.");
      }
      return  + "://" +  + ":" + ;
   }

   
Gets the locator to call back on this server to get the inputstream data.

Returns:
Throws:
Exception
   public String getInvokerLocator() throws Exception
   {
      String locator = null;
      if( != null)
      {
         locator = .getInvokerLocator();
      }
      return locator;
   }
   public void setupServer(String locatorURIthrows Exception
   {
      InvokerLocator locator = new InvokerLocator(locatorURI);
       = new Connector();
      .create();
      ServerInvocationHandler invocationHandler = new Handler();
      .addInvocationHandler("stream"invocationHandler);
      .start();
   }

   
Handler for accepting method calls on the input stream and perform the coresponding method call on the original input stream and returning the data.
   public class Handler implements ServerInvocationHandler
   {
      private Connector connector = null;
      public Handler(Connector connector)
      {
         this. = connector;
      }
      public Object invoke(InvocationRequest invocationthrows Throwable
      {
         Object obj = invocation.getParameter();
         // will expect the parameter to ALWAYS be of type StreamCallPaylod
         if(obj instanceof StreamCallPayload)
         {
            StreamCallPayload payload = (StreamCallPayloadobj;
            String method = payload.getMethod();
            if(..equals(method))
            {
               int i = .read();
               return new Integer(i);
            }
            else if(..equals(method))
            {
               int i = .available();
               return new Integer(i);
            }
            else if(..equals(method))
            {
               .close();
               if( != null && )
               {
                  .stop();
                  .destroy();
               }
            }
            else if(..equals(method))
            {
               .reset();
            }
            else if(..equals(method))
            {
               boolean b = .markSupported();
               return new Boolean(b);
            }
            else if(..equals(method))
            {
               Object[] param = payload.getParams();
               Integer intr = (Integerparam[0];
               int readLimit = intr.intValue();
               .mark(readLimit);
            }
            else if(..equals(method))
            {
               Object[] param = payload.getParams();
               Long lg = (Longparam[0];
               long n = lg.longValue();
               long ret = .skip(n);
               return new Long(ret);
            }
            else if(..equals(method))
            {
               Object[] param = payload.getParams();
               byte[] byteParam = (byte[]) param[0];
               int i = .read(byteParam);
               StreamCallPayload ret = new StreamCallPayload(.);
               ret.setParams(new Object[]{byteParamnew Integer(i)});
               return ret;
            }
            else
            {
               throw new Exception("Unsupported method call - " + method);
            }
         }
         else
         {
            .error("Can not process invocation request because is not of type StreamCallPayload.");
            throw new Exception("Invalid payload type.  Must be of type StreamCallPayload.");
         }
         return null;
      }

      
Adds a callback handler that will listen for callbacks from the server invoker handler.

Parameters:
callbackHandler
      public void addListener(InvokerCallbackHandler callbackHandler)
      {
         // NO OP as do not handling callback listeners in this example
      }

      
Removes the callback handler that was listening for callbacks from the server invoker handler.

Parameters:
callbackHandler
      public void removeListener(InvokerCallbackHandler callbackHandler)
      {
         // NO OP as do not handling callback listeners in this example
      }

      
set the mbean server that the handler can reference

Parameters:
server
      public void setMBeanServer(MBeanServer server)
      {
         // NO OP as do not need reference to MBeanServer for this handler
      }

      
set the invoker that owns this handler

Parameters:
invoker
      public void setInvoker(ServerInvoker invoker)
      {
         // NO OP as do not need reference back to the server invoker
      }
   }
   static private String getSystemProperty(final String namefinal String defaultValue)
   {
      if (SecurityUtility.skipAccessControl())
         return System.getProperty(namedefaultValue);
         
      String value = null;
      try
      {
         value = (String)AccessController.doPrivilegednew PrivilegedExceptionAction()
         {
            public Object run() throws Exception
            {
               return System.getProperty(namedefaultValue);
            }
         });
      }
      catch (PrivilegedActionException e)
      {
         throw (RuntimeExceptione.getCause();
      }
      
      return value;
   }
   
   static private InetAddress getLocalHost() throws UnknownHostException
   {
      if (SecurityUtility.skipAccessControl())
      {
         return doGetLocalHost();
      }
      try
      {
         return (InetAddress) AccessController.doPrivilegednew PrivilegedExceptionAction()
         {
            public Object run() throws UnknownHostException
            {
               return doGetLocalHost();
            }
         });
      }
      catch (PrivilegedActionException e)
      {
         throw (UnknownHostExceptione.getCause();
      }
   }
   
   static private InetAddress doGetLocalHost() throws UnknownHostException
   {
      if ( != null)
      {
         return ;
      }
      try
      {
         return InetAddress.getLocalHost();
      }
      catch (UnknownHostException e)
      {
         return InetAddress.getByName("127.0.0.1");
      }
   }
   
   static private String getLocalHostName() throws UnknownHostException
   {     
      if (SecurityUtility.skipAccessControl())
      {
         return doGetLocalHost().getHostName();
      }
      try
      {
         return (String) AccessController.doPrivilegednew PrivilegedExceptionAction()
         {
            public Object run() throws UnknownHostException
            {
               return doGetLocalHost().getHostName();
            }
         });
      }
      catch (PrivilegedActionException e)
      {
         throw (UnknownHostExceptione.getCause();
      }
   }
New to GrepCode? Check out our FAQ X