Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * IronJacamar, a Java EE Connector Architecture implementation
   * Copyright 2012, Red Hat Inc, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file 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.jca.core.workmanager.transport.remote.socket;
 
 
 import java.util.Set;
 
 
The socket transport

Author(s):
Jesper Pedersen
 
 public class SocketTransport extends AbstractRemoteTransport<Stringimplements Runnable
 {
   
The logger
 
    private static CoreLogger log = Logger.getMessageLogger(CoreLogger.classSocketTransport.class.getName());

   
Whether trace is enabled
 
    private static boolean trace = .isTraceEnabled();

   
The bundle
 
    private static CoreBundle bundle = Messages.getBundle(CoreBundle.class);

   
The bind address
 
    private String host;

   
The bind port
 
    private int port;

   
The peers
 
    private Set<Stringpeers;

   
Is the server running ?
 
    private AtomicBoolean running;

   
The server socket
 
    private ServerSocket ss;

   
Is the transport initialized
 
    private boolean initialized;

   
Constructor
 
    public SocketTransport()
    {
       super();
       this. = null;
       this. = 0;
       this. = null;
       this. = new AtomicBoolean(false);
       this. = null;
       this. = false;
    }

   
 
    public void startup() throws Throwable
    {
      if (!.get())
      {
         InetSocketAddress address = new InetSocketAddress();
          = new ServerSocket();
         .bind(address);
         .set(true);
         getExecutorService().submit(this);
      }
   }

   
   public boolean isInitialized()
   {
      return ;
   }

   
   public void initialize() throws Throwable
   {
      if ( != null && !)
      {
         for (String addr : )
         {
            if ()
               .tracef("Peer: %s"addr);
            try
            {
               // Let other node know of us
               sendMessage(addr.getOwnAddress());
               // Update the local information
               Set<AddressworkManagers = (Set<Address>)sendMessage(addr.);
               if ()
                  .tracef("Peer WorkManagers: %s"workManagers);
               if (workManagers != null)
               {
                  for (Address a : workManagers)
                  {
                     join(aaddr);
                     long shortRunningFree =
                        (long)sendMessage(addr.a);
                     long longRunningFree =
                        (long)sendMessage(addr.a);
                     localUpdateShortRunningFree(ashortRunningFree);
                     localUpdateLongRunningFree(alongRunningFree);
                  }
               }
            }
            catch (Throwable t)
            {
               .error(t.getMessage(), t);
            }
         }
      }
       = true;
   }

   
   public void shutdown() throws Throwable
   {
      .set(false);
      if ( != null)
         .close();
   }
   protected Serializable sendMessage(String addressRequest requestSerializable... parameters)
      throws WorkException
   {
      String[] addressPart = address.split(":");
      Socket socket = null;
      ObjectOutputStream oos = null;
      if ()
         .tracef("%s:%d: sending message=%s to %s:%s".getInetAddress().getHostName(),
                    .getLocalPort(), requestaddressPart[0], addressPart[1]);
      try
      {
         socket = new Socket(addressPart[0], Integer.valueOf(addressPart[1]));
         oos = new ObjectOutputStream(socket.getOutputStream());
         oos.writeInt(request.ordinal());
         oos.writeInt(request.getNumberOfParameter());
         if (parameters != null)
         {
            for (Serializable o : parameters)
            {
               oos.writeObject(o);
            }
         }
         oos.flush();
         return parseResponse(socket);
      }
      catch (Throwable t)
      {
         if (.isDebugEnabled())
         {
            .debug("Error sending command: " + t.getMessage(), t);
         }
         if (t instanceof WorkException)
         {
            throw (WorkExceptiont;
         }
         else
         {
            WorkException we = new WorkException(t.getMessage());
            we.initCause(t);
            throw we;
         }
      }
      finally
      {
         if (oos != null)
         {
            try
            {
               oos.close();
            }
            catch (IOException e)
            {
               //ignore it
            }
         }
         if (socket != null)
         {
            try
            {
               socket.close();
            }
            catch (IOException e)
            {
               //ignore it
            }
         }
      }
   }
   private Serializable parseResponse(Socket socketthrows Throwable
   {
      ObjectInputStream ois = null;
      try
      {
         ois = new ObjectInputStream(socket.getInputStream());
         int commandOrdinalPosition = ois.readInt();
         int numberOfParameters = ois.readInt();
         Serializable[] parameters = new Serializable[numberOfParameters];
         for (int i = 0; i < numberOfParametersi++)
         {
            Serializable parameter = (Serializable)ois.readObject();
            parameters[i] = parameter;
         }
         Response response = Response.values()[commandOrdinalPosition];
         switch (response)
         {
            case  : {
               return null;
            }
            case  : {
               return parameters[0];
            }
            case  : {
               WorkException we = (WorkException)parameters[0];
               throw we;
            }
            case  : {
               Throwable t = (Throwable)parameters[0];
               throw t;
            }
            default :
               if (.isDebugEnabled())
               {
                  .debug("Unknown response received on socket Transport");
               }
               throw new WorkException("Unknown response received on socket Transport");
         }
      }
      finally
      {
         if (ois != null)
         {
            try
            {
               ois.close();
            }
            catch (IOException e)
            {
               //ignore it
            }
         }
      }
   }

   
Set the host.

Parameters:
host The host to set.
   public void setHost(String host)
   {
      this. = host;
   }

   
Set the port.

Parameters:
port The port to set.
   public void setPort(int port)
   {
      this. = port;
   }

   
Set the peers

Parameters:
peers The peers
   public void setPeers(Set<Stringpeers)
   {
      this. = peers;
   }

   
Get the physical address

Returns:
The value
   public String getOwnAddress()
   {
      return  + ":" + ;
   }
   public void run()
   {
      while (.get())
      {
         try
         {
            java.net.Socket socket = .accept();
            Runnable r = new Communication(thissocket);
            this.getExecutorService().submit(r);
         }
         catch (Exception e)
         {
            if (.isTraceEnabled())
               .trace(e.getMessage());
         }
      }
   }
   public String toString()
   {
      return "SocketTransport [host=" +  + ", port=" +  + ", running=" +  + ", ss=" +  + "]";
   }
New to GrepCode? Check out our FAQ X