com.web.services.ExecutorServicesConstruct.java Source code

Java tutorial

Introduction

Here is the source code for com.web.services.ExecutorServicesConstruct.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.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.concurrent.CopyOnWriteArrayList;

import org.apache.commons.digester3.Digester;
import org.apache.commons.digester3.binder.*;
import org.apache.commons.digester3.xmlrules.FromXmlRulesModule;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.web.server.WebClassLoader;
import com.web.server.ServerConfig;

/**
 * This class is the implementation of the executor services consturction using the digester parser
 * @author arun
 *
 */
public class ExecutorServicesConstruct {
    /**
     * This method configures the executor services during deployment of the war file
     * @param serverdigester
     * @param servicesMap
     * @param exectorServicesXml
     * @param customClassLoader
     * @throws Exception
     */
    public void getExecutorServices(Digester serverdigester, Hashtable servicesMap, File exectorServicesXml,
            WebClassLoader customClassLoader) throws Exception {
        ExecutorServices executorServices = (ExecutorServices) serverdigester
                .parse(new InputSource(new FileInputStream(exectorServicesXml)));
        CopyOnWriteArrayList<ExecutorService> executorServicesList = executorServices.getExecutorServices();
        ExecutorServiceAnnot executorServiceAnnot;
        for (ExecutorService executorService : executorServicesList) {
            Class executorServiceClass = customClassLoader
                    .loadClass(executorService.getExecutorserviceclass().toString());
            //System.out.println("executor class in ExecutorServicesConstruct"+executorServiceClass);
            //System.out.println();
            Method[] methods = executorServiceClass.getDeclaredMethods();
            for (Method method : methods) {
                Annotation[] annotations = method.getDeclaredAnnotations();
                for (Annotation annotation : annotations) {
                    if (annotation instanceof ExecutorServiceAnnot) {
                        executorServiceAnnot = (ExecutorServiceAnnot) annotation;
                        ExecutorServiceInfo executorServiceInfo = new ExecutorServiceInfo();
                        executorServiceInfo.setExecutorServicesClass(executorServiceClass);
                        executorServiceInfo.setMethod(method);
                        System.out.println(executorServiceAnnot.servicename());
                        System.out.println("method.getName()=" + method.getName());
                        System.out.println("method.getParameterTypes()=" + method.getParameterTypes());
                        executorServiceInfo.setMethodParams(method.getParameterTypes());
                        //System.out.println("method="+executorServiceAnnot.servicename());
                        //System.out.println("method info="+executorServiceInfo);
                        //if(servicesMap.get(executorServiceAnnot.servicename())==null)throw new Exception();
                        servicesMap.put(executorServiceAnnot.servicename(), executorServiceInfo);
                    }
                }
            }
        }
    }

    /**
     * This method removes the configuration of the War file
     * @param servicesMap
     * @param exectorServicesXml
     * @param customClassLoader
     * @throws Exception
     */
    public void removeExecutorServices(Hashtable servicesMap, File exectorServicesXml,
            WebClassLoader customClassLoader) throws Exception {
        DigesterLoader serverdigesterLoader = DigesterLoader.newLoader(new FromXmlRulesModule() {

            protected void loadRules() {
                // TODO Auto-generated method stub
                try {
                    loadXMLRules(new InputSource(new FileInputStream("./config/executorservices-config.xml")));
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        });
        Digester serverdigester = serverdigesterLoader.newDigester();
        ExecutorServices executorServices = (ExecutorServices) serverdigester
                .parse(new InputSource(new FileInputStream(exectorServicesXml)));
        CopyOnWriteArrayList<ExecutorService> executorServicesList = executorServices.getExecutorServices();
        ExecutorServiceAnnot executorServiceAnnot;
        for (ExecutorService executorService : executorServicesList) {
            Class executorServiceClass = customClassLoader
                    .loadClass(executorService.getExecutorserviceclass().toString());
            //System.out.println("executor class in ExecutorServicesConstruct"+executorServiceClass);
            //System.out.println();
            Method[] methods = executorServiceClass.getDeclaredMethods();
            for (Method method : methods) {
                Annotation[] annotations = method.getDeclaredAnnotations();
                for (Annotation annotation : annotations) {
                    if (annotation instanceof ExecutorServiceAnnot) {
                        executorServiceAnnot = (ExecutorServiceAnnot) annotation;
                        //if(servicesMap.get(executorServiceAnnot.servicename())==null)throw new Exception();
                        servicesMap.remove(executorServiceAnnot.servicename());
                    }
                }
            }
        }
    }

    /**
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {
        //new ExecutorServicesConstruct().getExecutorServices(new HashMap(),new File("d:/executorservices.xml"),null);

    }

}