Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2011 BonitaSoft S.A. BonitaSoft, 32 rue Gustave Eiffel - 38000 Grenoble This library 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 version 2.1 of the License. This library 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 program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
 package org.bonitasoft.engine.api;
 
 import java.util.List;
 import java.util.Map;
 

Author(s):
Matthieu Chaffotte
 
 public class TCPServerAPI implements ServerAPI {
 
     private static final long serialVersionUID = 1L;
     private List<TcpDestinationdestinations = new ArrayList<TcpDestination>();
     private final Random random;
 
     public TCPServerAPI(final Map<StringStringparametersthrows ServerAPIException {
         //System.out.println(this.getClass().getSimpleName() + " - constructor...");
         final String destinationsList = parameters.get("destinations");
         final String[] splittedDestinations = destinationsList.split(",");
         for (final String destination : splittedDestinations) {
             this..add(getTcpdDestinationFromPattern(destination));
         }
         this. = new Random();
     }
 
     private TcpDestination getTcpdDestinationFromPattern(final String s) {
         final int separatorIndex = s.indexOf(":");
         final String host = s.substring(0, separatorIndex);
         final int port = Integer.valueOf(s.substring(separatorIndex + 1));
         final TcpDestination tcpDestination = new TcpDestination(hostport);
         //System.out.println(this.getClass().getSimpleName() + " - constructor, tcpDestination built: " + tcpDestination);
         return tcpDestination;
     }
 
     @Override
     public Object invokeMethod(final Map<StringSerializableoptionsfinal String apiInterfaceNamefinal String methodName,
             final List<StringclassNameParametersfinal Object[] parametersValuesthrows ServerWrappedExceptionRemoteException {
 
         //        System.out.println(this.getClass().getSimpleName() + " - invoking: with parameters: " 
         //                + ", options: " + options
         //                + ", apiInterfaceName: " + apiInterfaceName
         //                + ", methodName: " + methodName
         //                + ", classNameParameters: " + classNameParameters
         //                + ", parametersValues: " + parametersValues
         //                + "...");
         Socket remoteServerAPI = null;
         ObjectOutputStream oos = null;
         ObjectInputStream ois = null;
         try {
             final TcpDestination tcpDestination = this..get(.nextInt(this..size()));
             //System.out.println(this.getClass().getSimpleName() + " - building a clientSocket...");
             remoteServerAPI = new Socket(tcpDestination.getHost(), tcpDestination.getPort());
             //System.out.println(this.getClass().getSimpleName() + " - client socket buit: " + remoteServerAPI);
             final InputStream socketInputStream = remoteServerAPI.getInputStream();
             oos = new ObjectOutputStream(remoteServerAPI.getOutputStream());
             final MethodCall methodCall = new MethodCall(optionsapiInterfaceNamemethodNameclassNameParametersparametersValues);
             //System.out.println(this.getClass().getSimpleName() + " - invoking " + tcpDestination + " with methodCall: " + methodCall);
             oos.writeObject(methodCall);
             oos.flush();
             //System.out.println(this.getClass().getSimpleName() + " - flushed, waiting for return...");
             ois = new ObjectInputStream(socketInputStream);
             final Object callReturn = ois.readObject();
             //System.out.println(this.getClass().getSimpleName() + " - received return: " + callReturn);
             return checkInvokeMethodReturn(callReturn);
         } catch (Throwable e) {
             throw new ServerWrappedException(e);
         } finally {
 
             try {
                 if (oos != null) {
                     oos.close();
                 }
                 if (ois != null) {
                    ois.close();
                }
                if (remoteServerAPI != null) {
                    remoteServerAPI.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    private Object checkInvokeMethodReturn(final Object callReturnthrows Throwable {
        //System.out.println(this.getClass().getSimpleName() + " - checking calReturn...");
        if (callReturn != null && callReturn instanceof Throwable) {
            final Throwable throwable = (ThrowablecallReturn
            //System.out.println(this.getClass().getSimpleName() + " - callReturn was an exception, throwing it: " + throwable.getClass() + ": " + throwable.getMessage());
            throw throwable;
        }
        //System.out.println(this.getClass().getSimpleName() + " - returning calReturn as it was received...");
        return callReturn;
    }
New to GrepCode? Check out our FAQ X