com.web.services.ExecutorServiceThread.java Source code

Java tutorial

Introduction

Here is the source code for com.web.services.ExecutorServiceThread.java

Source

package com.web.services;

/*Copyright 2013 - 2015, Arun_Soundararajan (arun_srajan_2007@yahoo.com).and/or its affiliates.
    
All files in this repository or distribution are licensed under the
Apache License, Version 2.0 (the "License");
you may not use any files in this repository or distribution except
in compliance with the License.
    
You may obtain a copy of the License at
    
http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

import org.apache.commons.vfs2.impl.VFSClassLoader;
import org.apache.commons.digester3.Digester;
import org.apache.commons.digester3.binder.DigesterLoader;
import org.apache.commons.io.input.ClassLoaderObjectInputStream;
import org.xml.sax.InputSource;

import com.web.server.WebClassLoader;
import com.web.server.WebServer;
import com.web.server.node.CustomServerSocket;
import com.web.server.node.NodeInfo;
import com.web.server.node.NodeInfoMethodParam;
import com.web.server.node.SerializableSocket;

/**
 * This class is the implementation of the Executor service Thread
 * 
 * @author arun
 * 
 */
public class ExecutorServiceThread extends Thread implements Runnable, ExecutorServiceThreadMBean {
    ServerSocketChannel serverSocketChannel;
    Hashtable executorServiceMap;
    int shutDownPort;
    Hashtable ataMap;
    Hashtable urlClassLoaderMap;
    String deployDirectory;
    String jarservicesDirectory;
    String earServicesDirectory;
    public int numberOfServicesRequests = 0;
    String[] nodesport;

    public ExecutorServiceThread() {

    }

    public ExecutorServiceThread(ServerSocketChannel serverSocketChannel, Hashtable executorServiceMap,
            int shutDownPort, Hashtable ataMap, Hashtable urlClassLoaderMap, String deployDirectory,
            String jarservicesDirectory, String earservicesDirectory, String nodesport) {
        this.serverSocketChannel = serverSocketChannel;
        this.executorServiceMap = executorServiceMap;
        this.shutDownPort = shutDownPort;
        this.ataMap = ataMap;
        this.urlClassLoaderMap = urlClassLoaderMap;
        this.deployDirectory = deployDirectory;
        this.jarservicesDirectory = jarservicesDirectory;
        this.earServicesDirectory = earservicesDirectory;
        System.out.println("Nodesport=" + nodesport);
        this.nodesport = nodesport.split(",");
    }

    public void run() {

        // create a selector that will by used for multiplexing. The selector
        // registers the socketserverchannel as
        // well as all socketchannels that are created
        String CLIENTCHANNELNAME = "clientChannel";
        String SERVERCHANNELNAME = "serverChannel";
        String channelType = "channelType";

        ConcurrentHashMap<SelectionKey, Object> resultMap = new ConcurrentHashMap<SelectionKey, Object>();
        ClassLoader classLoader = null;
        ByteArrayOutputStream bstr;
        ByteBuffer buffer = null;
        ByteBuffer lengthBuffer = ByteBuffer.allocate(4);
        int bytesRead;
        InputStream bais;
        ObjectInputStream ois;
        Object object;
        ExecutorServiceInfo executorServiceInfo = null;
        Random random = new Random(System.currentTimeMillis());
        // register the serversocketchannel with the selector. The OP_ACCEPT
        // option marks
        // a selection key as ready when the channel accepts a new connection.
        // When the
        // socket server accepts a connection this key is added to the list of
        // selected keys of the selector.
        // when asked for the selected keys, this key is returned and hence we
        // know that a new connection has been accepted.
        try {
            Selector selector = Selector.open();
            SelectionKey socketServerSelectionKey = serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

            // SelectionKey socketServerSelectionKey1 =
            // channel.register(selector1,
            // SelectionKey.OP_ACCEPT);

            // set property in the key that identifies the channel
            Map<String, String> properties = new ConcurrentHashMap<String, String>();
            properties.put(channelType, SERVERCHANNELNAME);
            socketServerSelectionKey.attach(properties);
            // wait for the selected keys
            SelectionKey key = null;
            Set<SelectionKey> selectedKeys;
            // logger.info("Instance Number"+instanceNumber);
            Iterator<SelectionKey> iterator = null;
            SocketChannel clientChannel = null;
            while (true) {
                try {
                    // the select method is a blocking method which returns when
                    // atleast
                    // one of the registered
                    // channel is selected. In this example, when the socket
                    // accepts
                    // a
                    // new connection, this method
                    // will return. Once a socketclient is added to the list of
                    // registered channels, then this method
                    // would also return when one of the clients has data to be
                    // read
                    // or
                    // written. It is also possible to perform a nonblocking
                    // select
                    // using the selectNow() function.
                    // We can also specify the maximum time for which a select
                    // function
                    // can be blocked using the select(long timeout) function.

                    if (selector.select() >= 0) {
                        selectedKeys = selector.selectedKeys();
                        iterator = selectedKeys.iterator();
                    }
                    while (iterator.hasNext()) {
                        try {
                            key = iterator.next();
                            // the selection key could either by the
                            // socketserver
                            // informing
                            // that a new connection has been made, or
                            // a socket client that is ready for read/write
                            // we use the properties object attached to the
                            // channel
                            // to
                            // find
                            // out the type of channel.
                            if (((Map) key.attachment()).get(channelType).equals(SERVERCHANNELNAME)) {
                                // a new connection has been obtained. This
                                // channel
                                // is
                                // therefore a socket server.
                                ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel();
                                // accept the new connection on the server
                                // socket.
                                // Since
                                // the
                                // server socket channel is marked as non
                                // blocking
                                // this channel will return null if no client is
                                // connected.
                                SocketChannel clientSocketChannel = serverSocketChannel.accept();

                                if (clientSocketChannel != null) {
                                    // set the client connection to be non
                                    // blocking
                                    clientSocketChannel.configureBlocking(false);
                                    SelectionKey clientKey = clientSocketChannel.register(selector,
                                            SelectionKey.OP_READ, SelectionKey.OP_WRITE);
                                    Map<String, String> clientproperties = new ConcurrentHashMap<String, String>();
                                    clientproperties.put(channelType, CLIENTCHANNELNAME);
                                    clientKey.attach(clientproperties);
                                    clientKey.interestOps(SelectionKey.OP_READ);
                                    // clientSocketChannel.close();
                                    // write something to the new created client
                                    /*
                                     * CharBuffer buffer =
                                     * CharBuffer.wrap("Hello client"); while
                                     * (buffer.hasRemaining()) {
                                     * clientSocketChannel.write
                                     * (Charset.defaultCharset()
                                     * .encode(buffer)); }
                                     * clientSocketChannel.close();
                                     * buffer.clear();
                                     */
                                }

                            } else {
                                // data is available for read
                                // buffer for reading
                                clientChannel = (SocketChannel) key.channel();
                                if (key.isReadable()) {
                                    // the channel is non blocking so keep it
                                    // open
                                    // till
                                    // the
                                    // count is >=0
                                    clientChannel = (SocketChannel) key.channel();
                                    if (resultMap.get(key) == null) {
                                        //System.out.println(key);
                                        bstr = new ByteArrayOutputStream();
                                        object = null;
                                        clientChannel.read(lengthBuffer);
                                        int length = lengthBuffer.getInt(0);
                                        lengthBuffer.clear();
                                        //System.out.println(length);
                                        buffer = ByteBuffer.allocate(length);
                                        if ((bytesRead = clientChannel.read(buffer)) > 0) {
                                            // buffer.flip();
                                            // System.out
                                            // .println(bytesRead);
                                            bstr.write(buffer.array(), 0, bytesRead);
                                            buffer.clear();
                                        }
                                        buffer.clear();
                                        //System.out.println("Message1"+new String(bstr
                                        //      .toByteArray()));
                                        bais = new ByteArrayInputStream(bstr.toByteArray());
                                        ois = new ObjectInputStream(bais); // Offending
                                        // line.
                                        // Produces
                                        // the
                                        // StreamCorruptedException.
                                        //System.out.println("In read obect");
                                        object = ois.readObject();
                                        //System.out.println("Class Cast");
                                        //System.out.println("Class Cast1");
                                        ois.close();
                                        byte[] params = bstr.toByteArray();
                                        bstr.close();
                                        //System.out.println("readObject");
                                        //System.out.println("After readObject");
                                        if (object instanceof CloseSocket) {
                                            resultMap.remove(key);
                                            clientChannel.close();
                                            key.cancel();
                                        }
                                        // clientChannel.close();
                                        String serviceurl = (String) object;
                                        String[] serviceRegistry = serviceurl.split("/");
                                        //System.out.println("classLoaderMap"
                                        //      + urlClassLoaderMap);
                                        //System.out.println(deployDirectory
                                        //      + "/" + serviceRegistry[0]);

                                        int servicenameIndex;
                                        //System.out.println(earServicesDirectory
                                        //      + "/" + serviceRegistry[0]
                                        //      + "/" + serviceRegistry[1]);
                                        if (serviceRegistry[0].endsWith(".ear")) {
                                            classLoader = (VFSClassLoader) urlClassLoaderMap
                                                    .get(earServicesDirectory + "/" + serviceRegistry[0] + "/"
                                                            + serviceRegistry[1]);
                                            servicenameIndex = 2;
                                        } else if (serviceRegistry[0].endsWith(".jar")) {
                                            classLoader = (WebClassLoader) urlClassLoaderMap
                                                    .get(jarservicesDirectory + "/" + serviceRegistry[0]);
                                            servicenameIndex = 1;
                                        } else {
                                            classLoader = (WebClassLoader) urlClassLoaderMap
                                                    .get(deployDirectory + "/" + serviceRegistry[0]);
                                            servicenameIndex = 1;
                                        }
                                        String serviceName = serviceRegistry[servicenameIndex];
                                        // System.out.println("servicename:"+serviceName);;
                                        synchronized (executorServiceMap) {
                                            executorServiceInfo = (ExecutorServiceInfo) executorServiceMap
                                                    .get(serviceName.trim());
                                        }
                                        ExecutorServiceInfoClassLoader classLoaderExecutorServiceInfo = new ExecutorServiceInfoClassLoader();
                                        classLoaderExecutorServiceInfo.setClassLoader(classLoader);
                                        classLoaderExecutorServiceInfo.setExecutorServiceInfo(executorServiceInfo);
                                        resultMap.put(key, classLoaderExecutorServiceInfo);
                                        // key.interestOps(SelectionKey.OP_READ);
                                        // System.out.println("Key interested Ops");
                                        // continue;
                                    }
                                    //Thread.sleep(100);
                                    /*
                                     * if (classLoader == null) throw new
                                     * Exception(
                                     * "Could able to obtain deployed class loader"
                                     * );
                                     */
                                    /*
                                     * System.out.println(
                                     * "current context classloader" +
                                     * classLoader);
                                     */
                                    //System.out.println("In rad object");
                                    bstr = new ByteArrayOutputStream();
                                    lengthBuffer.clear();
                                    int numberofDataRead = clientChannel.read(lengthBuffer);
                                    //System.out.println("numberofDataRead"
                                    //      + numberofDataRead);
                                    int length = lengthBuffer.getInt(0);
                                    if (length <= 0) {
                                        iterator.remove();
                                        continue;
                                    }
                                    lengthBuffer.clear();
                                    //System.out.println(length);
                                    buffer = ByteBuffer.allocate(length);
                                    buffer.clear();
                                    if ((bytesRead = clientChannel.read(buffer)) > 0) {
                                        // buffer.flip();
                                        // System.out
                                        // .println(bytesRead);
                                        bstr.write(buffer.array(), 0, bytesRead);
                                        buffer.clear();
                                    }
                                    if (bytesRead <= 0 || bytesRead < length) {
                                        //System.out.println("bytesRead<length");
                                        iterator.remove();
                                        continue;
                                    }
                                    //System.out.println(new String(bstr
                                    //   .toByteArray()));
                                    bais = new ByteArrayInputStream(bstr.toByteArray());

                                    ExecutorServiceInfoClassLoader classLoaderExecutorServiceInfo = (ExecutorServiceInfoClassLoader) resultMap
                                            .get(key);
                                    ois = new ClassLoaderObjectInputStream(
                                            (ClassLoader) classLoaderExecutorServiceInfo.getClassLoader(), bais); // Offending
                                    // line.
                                    // Produces
                                    // the
                                    // StreamCorruptedException.
                                    object = ois.readObject();
                                    ois.close();
                                    bstr.close();
                                    executorServiceInfo = classLoaderExecutorServiceInfo.getExecutorServiceInfo();
                                    //System.out
                                    //      .println("inputStream Read Object");
                                    //System.out.println("Object="
                                    //      + object.getClass());
                                    // Thread.currentThread().setContextClassLoader(currentContextLoader);
                                    if (object instanceof ExecutorParams) {
                                        ExecutorParams exeParams = (ExecutorParams) object;
                                        Object returnValue = null;

                                        //System.out.println("test socket1");
                                        String ataKey;
                                        ATAConfig ataConfig;
                                        ConcurrentHashMap ataServicesMap;

                                        ATAExecutorServiceInfo servicesAvailable;
                                        Socket sock1 = new Socket("0.0.0.0",
                                                Integer.parseInt(nodesport[random.nextInt(nodesport.length)]));
                                        OutputStream outputStr = sock1.getOutputStream();
                                        ObjectOutputStream objOutputStream = new ObjectOutputStream(outputStr);
                                        NodeInfo nodeInfo = new NodeInfo();
                                        nodeInfo.setClassNameWithPackage(
                                                executorServiceInfo.getExecutorServicesClass().getName());
                                        nodeInfo.setMethodName(executorServiceInfo.getMethod().getName());

                                        nodeInfo.setWebclassLoaderURLS(((WebClassLoader) classLoader).geturlS());
                                        NodeInfoMethodParam nodeInfoMethodParam = new NodeInfoMethodParam();
                                        nodeInfoMethodParam.setMethodParams(exeParams.getParams());
                                        nodeInfoMethodParam
                                                .setMethodParamTypes(executorServiceInfo.getMethodParams());
                                        //System.out.println("Serializable socket="+sock);
                                        //nodeInfo.setSock(sock);
                                        //nodeInfo.setOstream(sock.getOutputStream());
                                        objOutputStream.writeObject(nodeInfo);
                                        objOutputStream = new ObjectOutputStream(outputStr);
                                        objOutputStream.writeObject(nodeInfoMethodParam);
                                        ObjectInputStream objInputStream1 = new ObjectInputStream(
                                                sock1.getInputStream());
                                        returnValue = objInputStream1.readObject();
                                        objOutputStream.close();
                                        objInputStream1.close();
                                        sock1.close();
                                        /*returnValue = executorServiceInfo
                                              .getMethod()
                                              .invoke(executorServiceInfo
                                                    .getExecutorServicesClass()
                                                    .newInstance(),
                                                    exeParams.getParams());*/
                                        // Thread.currentThread().setContextClassLoader(oldCL);

                                        //   System.out.println("Written Value="
                                        //         + returnValue.toString());
                                        resultMap.put(key, returnValue);
                                    }
                                    key.interestOps(SelectionKey.OP_WRITE);
                                    //System.out.println("Key interested Ops1");
                                } else if (key.isWritable()) {
                                    // the channel is non blocking so keep it
                                    // open
                                    // till the
                                    // count is >=0
                                    //System.out.println("In write");
                                    ByteArrayOutputStream baos = new ByteArrayOutputStream(); // make
                                    // a
                                    // BAOS
                                    // stream
                                    ObjectOutputStream oos = new ObjectOutputStream(baos); // wrap and OOS around the
                                                                                           // stream
                                    Object result = resultMap.get(key);
                                    oos.writeObject(result); // write an object
                                    // to
                                    // the stream
                                    oos.flush();
                                    oos.close();
                                    byte[] objData = baos.toByteArray(); // get
                                    // the
                                    // byte
                                    // array
                                    baos.close();
                                    buffer = ByteBuffer.wrap(objData); // wrap
                                    // around
                                    // the
                                    // data
                                    buffer.rewind();
                                    // buffer.flip(); //prep for writing
                                    //System.out.println(new String(objData));
                                    //while (buffer.hasRemaining())
                                    clientChannel.write(buffer); // write
                                    resultMap.remove(key);
                                    buffer.clear();
                                    key.cancel();
                                    clientChannel.close();
                                    //System.out.println("In write1");
                                    numberOfServicesRequests++;
                                    //System.out.println("Key interested Ops2");
                                }

                            }

                            iterator.remove();
                        } catch (Exception ex) {
                            ex.printStackTrace();
                            key.cancel();
                            clientChannel.close();
                            resultMap.remove(key);
                            //ex.printStackTrace();
                        }

                    }

                } catch (Exception ex) {

                    //ex.printStackTrace();
                }
            }
        } catch (Exception ex) {
            //ex.printStackTrace();
        }
    }

    /**
     * This method is the implementation of the executor service which runs now
     * through the Node Server
     */
    /*public void run1() {
        
       ServerSocket serverSocket = null;
       Socket sock;
       try {
     serverSocket = new ServerSocket(this.requestPort, 500);
     System.out.println("serverSocket" + serverSocket);
       } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
       }
       java.util.concurrent.ExecutorService executor = new java.util.concurrent.ScheduledThreadPoolExecutor(
        500);
       while (true) {
        
     try {
        sock = (Socket) serverSocket.accept();
        System.out.println("A new Socket=" + sock);
        ExecutorServiceProcessor executorServiceProcessor = new ExecutorServiceProcessor(
              sock, executorServiceMap, urlClassLoaderMap,
              deployDirectory);
        executor.execute(executorServiceProcessor);
        // executorServiceProcessor.start();
     } catch (Exception ex) {
        try {
           Socket socket = new Socket("localhost", shutDownPort);
           OutputStream outputStream = socket.getOutputStream();
           outputStream.write("shutdown WebServer\r\n\r\n".getBytes());
           outputStream.close();
        } catch (IOException e1) {
        
           e1.printStackTrace();
        }
     }
       }
    }*/

    /**
     * This class is the implementation of the Executor service processor.
     * 
     * @author arun
     * 
     */
    class ExecutorServiceProcessor extends Thread implements Runnable {
        transient Socket sock;
        ConcurrentHashMap executorServicesMap;
        ConcurrentHashMap urlClassLoaderMap;
        String deployDirectory;

        public ExecutorServiceProcessor(Socket sock, ConcurrentHashMap executorServicesMap,
                ConcurrentHashMap urlClassLoaderMap, String deployDirectory) {
            this.sock = sock;
            this.executorServicesMap = executorServicesMap;
            this.urlClassLoaderMap = urlClassLoaderMap;
            this.deployDirectory = deployDirectory;
        }

        /**
         * This method is the implementation of the executor service which runs
         * now through the Node Server
         */
        public void run() {
            String value;
            char c;
            String endvalue = "\r\n\r\n";
            String urlFormEncoded;
            ObjectInputStream objInputStream = null;
            ObjectOutputStream objectOutputStream = null;
            Random random = new Random(System.currentTimeMillis());
            try {
                int ch;
                CopyOnWriteArrayList<URL> urls = new CopyOnWriteArrayList();
                URL[] url;
                ClassLoader currentContextLoader = null;
                ClassLoader oldCL = Thread.currentThread().getContextClassLoader();
                while (true) {
                    InputStream inputStream = sock.getInputStream();
                    objInputStream = new ObjectInputStream(inputStream);
                    // objInputStream=new
                    // ObjectInputStream(sock.getInputStream());
                    // System.out.println("inputStream Read Object"+objInputStream.getClass().getClassLoader());
                    Object object = (Object) objInputStream.readObject();
                    if (object instanceof CloseSocket) {
                        break;
                    }
                    String serviceurl = (String) object;
                    String[] serviceRegistry = serviceurl.split("/");
                    // System.out.println("classLoaderMap"+urlClassLoaderMap);
                    // System.out.println(deployDirectory+"/"+serviceRegistry[0]);
                    ClassLoader classLoader;
                    int servicenameIndex;
                    System.out.println(earServicesDirectory + "/" + serviceRegistry[0] + "/" + serviceRegistry[1]);
                    if (serviceRegistry[0].endsWith(".ear")) {
                        classLoader = (VFSClassLoader) urlClassLoaderMap
                                .get(earServicesDirectory + "/" + serviceRegistry[0] + "/" + serviceRegistry[1]);
                        servicenameIndex = 2;
                    } else if (serviceRegistry[0].endsWith(".jar")) {
                        classLoader = (WebClassLoader) urlClassLoaderMap
                                .get(jarservicesDirectory + "/" + serviceRegistry[0]);
                        servicenameIndex = 1;
                    } else {
                        classLoader = (WebClassLoader) urlClassLoaderMap
                                .get(deployDirectory + "/" + serviceRegistry[0]);
                        servicenameIndex = 1;
                    }
                    if (classLoader == null)
                        throw new Exception("Could able to obtain deployed class loader");
                    System.out.println("current context classloader" + classLoader);
                    objInputStream = new ClassLoaderObjectInputStream(classLoader, inputStream);
                    // objInputStream=new
                    // ObjectInputStream(sock.getInputStream());
                    System.out.println("inputStream Read Object");
                    object = (Object) objInputStream.readObject();
                    System.out.println("Object=" + object);
                    // Thread.currentThread().setContextClassLoader(currentContextLoader);
                    if (object instanceof ExecutorParams) {
                        ExecutorParams exeParams = (ExecutorParams) object;
                        String serviceName = serviceRegistry[servicenameIndex];
                        // System.out.println("servicename:"+serviceName);;
                        ExecutorServiceInfo executorServiceInfo = null;
                        synchronized (executorServicesMap) {
                            executorServiceInfo = (ExecutorServiceInfo) executorServicesMap.get(serviceName.trim());
                        }
                        System.out.println("test socket" + executorServicesMap + "= " + serviceName.trim() + " "
                                + executorServiceInfo.getMethodParams());
                        Object returnValue = null;
                        // Socket sock1=new
                        // Socket("localhost",Integer.parseInt(nodesport[random.nextInt(nodesport.length)]));
                        // OutputStream outputStr=sock1.getOutputStream();
                        // ObjectOutputStream objOutputStream=new
                        // ObjectOutputStream(outputStr);
                        // NodeInfo nodeInfo=new NodeInfo();
                        // nodeInfo.setClassNameWithPackage(executorServiceInfo.getExecutorServicesClass().getName());
                        // nodeInfo.setMethodName(executorServiceInfo.getMethod().getName());
                        //
                        // nodeInfo.setWebclassLoaderURLS(classLoader.geturlS());
                        // NodeInfoMethodParam nodeInfoMethodParam=new
                        // NodeInfoMethodParam();
                        // nodeInfoMethodParam.setMethodParams(exeParams.getParams());
                        // nodeInfoMethodParam.setMethodParamTypes(executorServiceInfo.getMethodParams());
                        // System.out.println("Serializable socket="+sock);
                        // //nodeInfo.setSock(sock);
                        // //nodeInfo.setOstream(sock.getOutputStream());
                        // objOutputStream.writeObject(nodeInfo);
                        // objOutputStream=new ObjectOutputStream(outputStr);
                        // objOutputStream.writeObject(nodeInfoMethodParam);
                        // ObjectInputStream objInputStream1=new
                        // ObjectInputStream(sock1.getInputStream());
                        // returnValue=objInputStream1.readObject();
                        // objOutputStream.close();
                        // objInputStream1.close();
                        // sock1.close();

                        System.out.println("test socket1");
                        String ataKey;
                        ATAConfig ataConfig;
                        ConcurrentHashMap ataServicesMap;

                        ATAExecutorServiceInfo servicesAvailable;
                        if (executorServiceInfo == null) {
                            Set<String> ataKeys = ataMap.keySet();
                            Iterator<String> ataKeyIterator = ataKeys.iterator();
                            // System.out.println("ataKeys"+ataKeys);
                            while (ataKeyIterator.hasNext()) {
                                ataKey = (String) ataKeyIterator.next();
                                // System.out.println("ataKey"+ataKey);
                                ataConfig = (ATAConfig) ataMap.get(ataKey);
                                // System.out.println("ataKey"+ataConfig);
                                // System.out.println("resources"+ataConfig.getResources());
                                // System.out.println("getServeraddress()"+ataConfig.getServeraddress());
                                // System.out.println("getPort()"+ataConfig.getPort());
                                ataServicesMap = ataConfig.getResources();
                                // System.out.println("services Map"+ataServicesMap);
                                synchronized (ataServicesMap) {
                                    servicesAvailable = (ATAExecutorServiceInfo) ataServicesMap
                                            .get(serviceName.trim());
                                }
                                // System.out.println("Services Available"+servicesAvailable);
                                if (servicesAvailable != null) {
                                    ExecutorClientProcess clientProcess2 = null;
                                    try {
                                        clientProcess2 = new ExecutorClientProcess("exe://"
                                                + ataConfig.getServeraddress() + ":" + ataConfig.getPort());
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                        continue;
                                    }
                                    try {
                                        returnValue = clientProcess2.executeServices(serviceName.trim(),
                                                exeParams.getParams());
                                        // System.out.println("return Value"+returnValue);
                                        clientProcess2.closeSocket();
                                        break;
                                    } catch (ClassNotFoundException | IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                }
                            }
                        } else {
                            // System.out.println(executorServiceInfo);
                            // System.out.println(executorServiceInfo.getExecutorServicesClass());
                            // System.out.println(executorServiceInfo.getMethod());
                            // Thread.currentThread().setContextClassLoader(currentContextLoader);
                            returnValue = executorServiceInfo.getMethod().invoke(
                                    executorServiceInfo.getExecutorServicesClass().newInstance(),
                                    exeParams.getParams());
                            // Thread.currentThread().setContextClassLoader(oldCL);
                        }
                        objectOutputStream = new ObjectOutputStream(sock.getOutputStream());
                        objectOutputStream.writeObject(returnValue);
                        // objectOutputStream.close();
                        // objInputStream.close();
                        System.out.println("Socket close");
                        numberOfServicesRequests++;
                    }
                }
                // sock.close();
            } catch (Exception ex) {
                ex.printStackTrace();
                System.out.print("message" + ex.getMessage());
            } finally {
                try {
                    objInputStream.close();
                    objectOutputStream.close();
                    sock.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * This method returns the number of executor service requests.
     */
    @Override
    public int getNumberOfServicesRequests() {
        // TODO Auto-generated method stub
        return numberOfServicesRequests;
    }

    class ExecutorServiceInfoClassLoader {
        ExecutorServiceInfo executorServiceInfo;
        ClassLoader classLoader;

        public ExecutorServiceInfo getExecutorServiceInfo() {
            return executorServiceInfo;
        }

        public void setExecutorServiceInfo(ExecutorServiceInfo executorServiceInfo) {
            this.executorServiceInfo = executorServiceInfo;
        }

        public ClassLoader getClassLoader() {
            return classLoader;
        }

        public void setClassLoader(ClassLoader classLoader) {
            this.classLoader = classLoader;
        }
    }
}