com.web.server.node.NodeServer.java Source code

Java tutorial

Introduction

Here is the source code for com.web.server.node.NodeServer.java

Source

package com.web.server.node;

/*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.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;

import org.apache.commons.io.input.ClassLoaderObjectInputStream;

import com.web.server.WebClassLoader;

/**
 * This class is the implementation of the node server which gets the executor services request 
 * executed the request and sends the response back to the main server
 * @author arun
 *
 */
public class NodeServer extends Thread implements Runnable {
    int port;

    public NodeServer(int port) {
        this.port = port;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new NodeServer(Integer.parseInt(args[0])).start();
    }

    /**
     * This method implements the node server request
     */
    public void run() {
        ServerSocket serversock = null;
        Socket sock = null;
        try {
            serversock = new ServerSocket(port);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        while (true) {
            try {
                sock = serversock.accept();
                new NodeServerRequestProcessor(sock).start();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    /**
     * This class is the implementation of the node server request processor
     * @author arun
     *
     */
    class NodeServerRequestProcessor extends Thread implements Runnable {
        Socket sock;

        public NodeServerRequestProcessor(Socket sock) {
            this.sock = sock;
        }

        public void run() {
            ObjectInputStream objinputstream = null;
            ClassLoader oldCl;
            InputStream inputStream = null;
            try {
                inputStream = sock.getInputStream();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try {
                objinputstream = new ObjectInputStream(inputStream);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            NodeInfo nodeInfo = null;
            CopyOnWriteArrayList<URL> classLoader = null;
            try {
                System.out.println("Before NodeInfo");
                nodeInfo = (NodeInfo) objinputstream.readObject();
                System.out.println("After NodeInfo");
            } catch (ClassNotFoundException | IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                System.out.println("webclassloader");
                classLoader = nodeInfo.getWebclassLoaderURLS();
                System.out.println(nodeInfo.getClassNameWithPackage());
                WebClassLoader webClassLoader = new WebClassLoader(
                        classLoader.toArray(new URL[classLoader.size()]));
                //oldCl=Thread.currentThread().getContextClassLoader();
                //Thread.currentThread().setContextClassLoader(webClassLoader);
                NodeInfoMethodParam nodeInfoMethodParam = null;
                try {
                    objinputstream = new NodeObjectInputStream(webClassLoader, inputStream);
                    nodeInfoMethodParam = (NodeInfoMethodParam) objinputstream.readObject();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                Class cls = webClassLoader.loadClass(nodeInfo.getClassNameWithPackage());
                Object obj = cls.newInstance();
                //               System.out.println(nodeInfo.getMethodParamTypes());
                //               System.out.println("InetAddress="+nodeInfo.getSock().getInputStream());;
                //               System.out.println("InetAddress1="+nodeInfo.getSock().getLocalPort());;
                //               System.out.println("InetAddress2="+nodeInfo.getSock().getPort());;
                //System.out.println("InetAddress3="+nodeInfo.getOstream());;
                //nodeInfo.getOstream().close();
                //System.out.println("InetAddress="+nodeInfo.getSock().bind(new InetSocketAddress("192.168.0.1", 0)));;
                System.out.println(nodeInfoMethodParam.getMethodParamTypes()[0]);
                Method method = cls.getMethod(nodeInfo.getMethodName(), nodeInfoMethodParam.getMethodParamTypes());
                Object returnValue = method.invoke(obj, nodeInfoMethodParam.getMethodParams());
                ObjectOutputStream objoutputstream = null;
                //Thread.currentThread().setContextClassLoader(oldCl);
                try {
                    objoutputstream = new ObjectOutputStream(sock.getOutputStream());
                    objoutputstream.writeObject(returnValue);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println(obj);
                objinputstream.close();
                objoutputstream.close();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InstantiationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                try {
                    sock.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }
}