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.transport.rmi;
 
 import  org.jboss.logging.Logger;
 
 import java.util.Map;

RMIClientInvoker

Author(s):
Jeff Haynie
Tom Elrod
Version:
$Revision: 2390 $
 
 public class RMIClientInvoker extends RemoteClientInvoker
 {
    private static final Logger log = Logger.getLogger(RMIClientInvoker.class);
    private static final boolean isTraceEnabled = .isTraceEnabled();
    
    private RMIServerInvokerInf server;

   
Need flag to indicate if have been able to lookup registry and set stub. Can't do this in the constructor, as need to throw CannotConnectException so for clustering capability.

Parameters:
locator
 
    private boolean connected = false;
 
    public RMIClientInvoker(InvokerLocator locator)
    {
       super(locator);
    }
 
    public RMIClientInvoker(InvokerLocator locatorMap configuration)
    {
       super(locatorconfiguration);
    }
 
 
    private int getRegistryPort(InvokerLocator locator)
    {
       int port = .;
 
       // See if locator contains a specific registry port
       Map params = locator.getParameters();
       if(params != null)
       {
          String value = (Stringparams.get(.);
          if(value != null)
          {
             try
             {
                port = Integer.parseInt(value);
                .debug("Using port " + port + " for rmi registry.");
             }
            catch(NumberFormatException e)
            {
               .error("Can not set the RMIServerInvoker RMI registry to port " + value + ".  This is not a valid port number.");
            }
         }
      }
      return port;
   }

   
get the server stub

Parameters:
server
   public void setServerStub(RMIServerInvokerInf server)
   {
      this. = server;
      .trace(this.);
   }

   
return the RMI server stub

Returns:
   {
      return this.;
   }

   
subclasses must implement this method to provide a hook to connect to the remote server, if this applies to the specific transport. However, in some transport implementations, this may not make must difference since the connection is not persistent among invocations, such as SOAP. In these cases, the method should silently return without any processing.

Throws:
ConnectionFailedException
   protected void handleConnect()
         throws ConnectionFailedException
   {
      //TODO: -TME Need to figure this out a little better as am now dealing with
      // with 2 ports, the rmi server and the registry.
      try
      {
         String host = .getHost();
         int port = getRegistryPort();
         Registry regsitry = LocateRegistry.getRegistry(hostport);
         Remote remoteObj = regsitry.lookup("remoting/RMIServerInvoker/" + .getPort());
         .debug("Remote RMI Stub: " + remoteObj);
         setServerStub((RMIServerInvokerInfremoteObj);
          = true;
      }
      catch(Exception e)
      {
          = false;
         .debug("Error connecting RMI invoker client."e);
         throw new CannotConnectException("Error connecting RMI invoker client"e);
      }
   }

   
subclasses must implement this method to provide a hook to disconnect from the remote server, if this applies to the specific transport. However, in some transport implementations, this may not make must difference since the connection is not persistent among invocations, such as SOAP. In these cases, the method should silently return without any processing.
   protected void handleDisconnect()
   {
   }
   protected String getDefaultDataType()
   {
      return .;
   }
   protected void storeLocalConfig(Map config)
   {
      HashMap localConfig = new HashMap(config);
      // If a specific SocketFactory was passed in, use it.  If a SocketFactory was
      // generated from SSL parameters, discard it.  It will be recreated later by
      // SerializableSSLClientSocketFactory with any additional parameters sent
      // from server.
      if ( != null &&
            ! &&
            AbstractInvoker.isCompleteSocketFactory())
         localConfig.put(.);
      // Save configuration for SerializableSSLClientSocketFactory.
      RemotingRMIClientSocketFactory.addLocalConfiguration(localConfig);
   }
   
   protected Object transport(String sessionIdObject invocationMap metadataMarshaller marshallerUnMarshaller unmarshaller)
         throws IOExceptionConnectionFailedException
   {
      if(this. == null)
      {
         .error("Server stub has not been set in RMI invoker client.  See previous errors for details.");
         //throw new IOException("Server stub hasn't been set!");
         throw new CannotConnectException("Server stub has not been set.");
      }
      try
      {
         Object payload = invocation;
         if(marshaller != null && !(marshaller instanceof RMIMarshaller))
         {
            if(marshaller instanceof MarshallerDecorator)
            {
               payload = ((MarshallerDecoratormarshaller).addDecoration(payload);
            }
            else
            {
               ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
               if (marshaller instanceof VersionedMarshaller)
                  ((VersionedMarshallermarshaller).write(payloadbyteOut, Version.getDefaultVersion());
               else
                  marshaller.write(payloadbyteOut);
               ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
               ObjectInputStream ois = new ObjectInputStream(byteIn);
               try
               {
                  byteOut.close();
                  payload = ois.readObject();
                  ois.close();
               }
               catch(ClassNotFoundException e)
               {
                  .error("Could not marshall invocation payload object " + payloade);
                  throw new IOException(e.getMessage());
               }
            }
         }
         Object response = .transport(payload);
         /* Jira Issue: JBREM-167
         if(unmarshaller != null && !(unmarshaller instanceof RMIUnMarshaller))
         {
            PipedOutputStream pos = new PipedOutputStream();
            PipedInputStream pis = new PipedInputStream();
            pos.connect(pis);
            ObjectOutputStream oos = new ObjectOutputStream(pos);
            oos.writeObject(response);
            oos.flush();
            oos.reset();
            oos.writeObject(Boolean.TRUE);
            oos.flush();
            oos.reset();
            try
            {
               oos.close();
               response = unmarshaller.read(pis, metadata);
               pis.close();
            }
            catch(ClassNotFoundException e)
            {
               log.error("Could not unmarshall invocation response" + response, e);
               throw new IOException(e.getMessage());
            }
         }
         */
         return response;
      }
      catch(RemoteException e)
      {
         .error(e);
         throw new CannotConnectException("Error making invocation in RMI client invoker."e);
      }
   }
New to GrepCode? Check out our FAQ X