com.web.server.EJBDeployer.java Source code

Java tutorial

Introduction

Here is the source code for com.web.server.EJBDeployer.java

Source

package com.web.server;

/*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.lang.annotation.Annotation;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.rmi.Remote;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.Topic;

import org.apache.commons.vfs2.FileChangeEvent;
import org.apache.commons.vfs2.FileListener;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemOptions;
import org.apache.commons.vfs2.impl.DefaultFileMonitor;
import org.apache.commons.vfs2.impl.StandardFileSystemManager;
import org.apache.commons.vfs2.provider.ftp.FtpFileSystemConfigBuilder;
import org.hornetq.jms.server.embedded.EmbeddedJMS;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;

import ejb.annotations.Stateless;
import ejb.container.BeanPool;
import ejb.container.EJBContainer;
import ejb.container.ProxyFactory;
import ejb.exceptions.ManyInterfaceImplementationException;
import ejb.logger.Log;

public class EJBDeployer implements Runnable, EJBDeployerMBean {

    private String scanDirectory;
    private Registry registry;
    private int servicesRegistryPort;
    private HashMap<String, EJBContext> jarEJBMap = new HashMap<String, EJBContext>();
    private ConcurrentHashMap<String, ConcurrentHashMap<String, MDBContext>> jarMDBMap = new ConcurrentHashMap<String, ConcurrentHashMap<String, MDBContext>>();
    EmbeddedJMS jms;
    ConnectionFactory connectionFactory;

    public EJBDeployer(String scanDirectory, Registry registry, int servicesRegistryPort, EmbeddedJMS jms) {
        this.scanDirectory = scanDirectory;
        this.registry = registry;
        this.servicesRegistryPort = servicesRegistryPort;
        this.jms = jms;
        connectionFactory = (ConnectionFactory) jms.lookup("java:/ConnectionFactory");
    }

    @Override
    public void run() {
        EJBJarFileListener jarFileListener = new EJBJarFileListener(registry, this.servicesRegistryPort, jarEJBMap,
                jarMDBMap, jms, connectionFactory);
        DefaultFileMonitor fm = new DefaultFileMonitor(jarFileListener);
        FileObject listendir = null;
        StandardFileSystemManager fsManager = new StandardFileSystemManager();
        String[] dirsToScan = scanDirectory.split(";");
        EJBContext ejbContext;
        try {
            File scanDirFile = new File(dirsToScan[0]);
            File[] scanJarFiles = scanDirFile.listFiles();
            System.out.println("SCANDIRECTORY=" + scanDirectory);
            if (scanJarFiles != null) {
                for (File scanJarFile : scanJarFiles) {
                    if (scanJarFile.isFile() && scanJarFile.getAbsolutePath().endsWith(".jar")) {
                        URLClassLoader classLoader = new URLClassLoader(
                                new URL[] { new URL("file:///" + scanJarFile.getAbsolutePath()) },
                                Thread.currentThread().getContextClassLoader());
                        ConfigurationBuilder config = new ConfigurationBuilder();
                        config.addUrls(ClasspathHelper.forClassLoader(classLoader));
                        config.addClassLoader(classLoader);
                        org.reflections.Reflections reflections = new org.reflections.Reflections(config);
                        EJBContainer container = EJBContainer
                                .getInstance("file:///" + scanJarFile.getAbsolutePath(), config);
                        container.inject();
                        Set<Class<?>> cls = reflections.getTypesAnnotatedWith(Stateless.class);
                        Set<Class<?>> clsMessageDriven = reflections.getTypesAnnotatedWith(MessageDriven.class);
                        Object obj;
                        System.gc();
                        if (cls.size() > 0) {
                            ejbContext = new EJBContext();
                            ejbContext.setJarPath(scanJarFile.getAbsolutePath());
                            ejbContext.setJarDeployed(scanJarFile.getName());
                            for (Class<?> ejbInterface : cls) {
                                //BeanPool.getInstance().create(ejbInterface);
                                obj = BeanPool.getInstance().get(ejbInterface);
                                System.out.println(obj);
                                ProxyFactory factory = new ProxyFactory();
                                obj = UnicastRemoteObject.exportObject((Remote) factory.createWithBean(obj),
                                        servicesRegistryPort);
                                String remoteBinding = container.getRemoteBinding(ejbInterface);
                                System.out.println(remoteBinding + " for EJB" + obj);
                                if (remoteBinding != null) {
                                    //registry.unbind(remoteBinding);
                                    registry.rebind(remoteBinding, (Remote) obj);
                                    ejbContext.put(remoteBinding, obj.getClass());
                                }
                                //registry.rebind("name", (Remote) obj);
                            }
                            jarEJBMap.put("file:///" + scanJarFile.getAbsolutePath().replace("\\", "/"),
                                    ejbContext);
                        }
                        System.out.println("Class Message Driven" + clsMessageDriven);
                        if (clsMessageDriven.size() > 0) {
                            System.out.println("Class Message Driven");
                            MDBContext mdbContext;
                            ConcurrentHashMap<String, MDBContext> mdbContexts;
                            if (jarMDBMap.get(scanJarFile.getAbsolutePath()) != null) {
                                mdbContexts = jarMDBMap.get(scanJarFile.getAbsolutePath());
                            } else {
                                mdbContexts = new ConcurrentHashMap<String, MDBContext>();
                            }
                            jarMDBMap.put("file:///" + scanJarFile.getAbsolutePath().replace("\\", "/"),
                                    mdbContexts);
                            MDBContext mdbContextOld;
                            for (Class<?> mdbBean : clsMessageDriven) {
                                String classwithpackage = mdbBean.getName();
                                System.out.println("class package" + classwithpackage);
                                classwithpackage = classwithpackage.replace("/", ".");
                                System.out.println("classList:" + classwithpackage.replace("/", "."));
                                try {
                                    if (!classwithpackage.contains("$")) {
                                        //System.out.println("executor class in ExecutorServicesConstruct"+executorServiceClass);
                                        //System.out.println();
                                        if (!mdbBean.isInterface()) {
                                            Annotation[] classServicesAnnot = mdbBean.getDeclaredAnnotations();
                                            if (classServicesAnnot != null) {
                                                for (int annotcount = 0; annotcount < classServicesAnnot.length; annotcount++) {
                                                    if (classServicesAnnot[annotcount] instanceof MessageDriven) {
                                                        MessageDriven messageDrivenAnnot = (MessageDriven) classServicesAnnot[annotcount];
                                                        ActivationConfigProperty[] activationConfigProperties = messageDrivenAnnot
                                                                .activationConfig();
                                                        mdbContext = new MDBContext();
                                                        mdbContext.setMdbName(messageDrivenAnnot.name());
                                                        for (ActivationConfigProperty activationConfigProperty : activationConfigProperties) {
                                                            if (activationConfigProperty.propertyName()
                                                                    .equals(MDBContext.DESTINATIONTYPE)) {
                                                                mdbContext.setDestinationType(
                                                                        activationConfigProperty.propertyValue());
                                                            } else if (activationConfigProperty.propertyName()
                                                                    .equals(MDBContext.DESTINATION)) {
                                                                mdbContext.setDestination(
                                                                        activationConfigProperty.propertyValue());
                                                            } else if (activationConfigProperty.propertyName()
                                                                    .equals(MDBContext.ACKNOWLEDGEMODE)) {
                                                                mdbContext.setAcknowledgeMode(
                                                                        activationConfigProperty.propertyValue());
                                                            }
                                                        }
                                                        if (mdbContext.getDestinationType()
                                                                .equals(Queue.class.getName())) {
                                                            mdbContextOld = null;
                                                            if (mdbContexts.get(mdbContext.getMdbName()) != null) {
                                                                mdbContextOld = mdbContexts
                                                                        .get(mdbContext.getMdbName());
                                                                if (mdbContextOld != null
                                                                        && mdbContext.getDestination().equals(
                                                                                mdbContextOld.getDestination())) {
                                                                    throw new Exception(
                                                                            "Only one MDB can listen to destination:"
                                                                                    + mdbContextOld
                                                                                            .getDestination());
                                                                }
                                                            }
                                                            mdbContexts.put(mdbContext.getMdbName(), mdbContext);
                                                            Queue queue = (Queue) jms
                                                                    .lookup(mdbContext.getDestination());
                                                            Connection connection = connectionFactory
                                                                    .createConnection("guest", "guest");
                                                            connection.start();
                                                            Session session;
                                                            if (mdbContext.getAcknowledgeMode() != null
                                                                    && mdbContext.getAcknowledgeMode()
                                                                            .equals("Auto-Acknowledge")) {
                                                                session = connection.createSession(false,
                                                                        Session.AUTO_ACKNOWLEDGE);
                                                            } else {
                                                                session = connection.createSession(false,
                                                                        Session.AUTO_ACKNOWLEDGE);
                                                            }
                                                            MessageConsumer consumer = session
                                                                    .createConsumer(queue);
                                                            consumer.setMessageListener(
                                                                    (MessageListener) mdbBean.newInstance());
                                                            mdbContext.setConnection(connection);
                                                            mdbContext.setSession(session);
                                                            mdbContext.setConsumer(consumer);
                                                            System.out.println("Queue=" + queue);
                                                        } else if (mdbContext.getDestinationType()
                                                                .equals(Topic.class.getName())) {
                                                            if (mdbContexts.get(mdbContext.getMdbName()) != null) {
                                                                mdbContextOld = mdbContexts
                                                                        .get(mdbContext.getMdbName());
                                                                if (mdbContextOld.getConsumer() != null)
                                                                    mdbContextOld.getConsumer()
                                                                            .setMessageListener(null);
                                                                if (mdbContextOld.getSession() != null)
                                                                    mdbContextOld.getSession().close();
                                                                if (mdbContextOld.getConnection() != null)
                                                                    mdbContextOld.getConnection().close();
                                                            }
                                                            mdbContexts.put(mdbContext.getMdbName(), mdbContext);
                                                            Topic topic = (Topic) jms
                                                                    .lookup(mdbContext.getDestination());
                                                            Connection connection = connectionFactory
                                                                    .createConnection("guest", "guest");
                                                            connection.start();
                                                            Session session;
                                                            if (mdbContext.getAcknowledgeMode() != null
                                                                    && mdbContext.getAcknowledgeMode()
                                                                            .equals("Auto-Acknowledge")) {
                                                                session = connection.createSession(false,
                                                                        Session.AUTO_ACKNOWLEDGE);
                                                            } else {
                                                                session = connection.createSession(false,
                                                                        Session.AUTO_ACKNOWLEDGE);
                                                            }
                                                            MessageConsumer consumer = session
                                                                    .createConsumer(topic);
                                                            consumer.setMessageListener(
                                                                    (MessageListener) mdbBean.newInstance());
                                                            mdbContext.setConnection(connection);
                                                            mdbContext.setSession(session);
                                                            mdbContext.setConsumer(consumer);
                                                            System.out.println("Topic=" + topic);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                } catch (Exception e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }
                        }
                        classLoader.close();
                        System.out.println(scanJarFile.getAbsolutePath() + " Deployed");
                    }
                }
            }
            FileSystemOptions opts = new FileSystemOptions();
            FtpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
            fsManager.init();
            for (String dir : dirsToScan) {
                if (dir.startsWith("ftp://")) {
                    listendir = fsManager.resolveFile(dir, opts);
                } else {
                    listendir = fsManager.resolveFile(dir);
                }
                fm.addFile(listendir);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        fm.setRecursive(true);
        fm.setDelay(1000);
        fm.start();
    }
}

class EJBJarFileListener implements FileListener {

    Registry registry;
    int servicesRegistryPort;
    HashMap<String, EJBContext> jarEJBMap;
    EmbeddedJMS jms;
    Connection connection;
    Session session;
    ConnectionFactory connectionFactory;
    private ConcurrentHashMap<String, ConcurrentHashMap<String, MDBContext>> jarMDBMap;

    public EJBJarFileListener(Registry registry, int servicesRegistryPort, HashMap<String, EJBContext> jarEJBMap,
            ConcurrentHashMap<String, ConcurrentHashMap<String, MDBContext>> jarMDBMap, EmbeddedJMS jms,
            ConnectionFactory connectionFactory) {
        this.registry = registry;
        this.servicesRegistryPort = servicesRegistryPort;
        this.jarEJBMap = jarEJBMap;
        this.jms = jms;
        this.connectionFactory = connectionFactory;
        this.jarMDBMap = jarMDBMap;
    }

    @Override
    public void fileChanged(FileChangeEvent arg0) throws Exception {
        try {
            FileObject baseFile = arg0.getFile();
            EJBContext ejbContext;
            if (baseFile.getName().getURI().endsWith(".jar")) {
                fileDeleted(arg0);
                URLClassLoader classLoader = new URLClassLoader(new URL[] { new URL(baseFile.getName().getURI()) },
                        Thread.currentThread().getContextClassLoader());
                ConfigurationBuilder config = new ConfigurationBuilder();
                config.addUrls(ClasspathHelper.forClassLoader(classLoader));
                config.addClassLoader(classLoader);
                org.reflections.Reflections reflections = new org.reflections.Reflections(config);
                EJBContainer container = EJBContainer.getInstance(baseFile.getName().getURI(), config);
                container.inject();
                Set<Class<?>> cls = reflections.getTypesAnnotatedWith(Stateless.class);
                Set<Class<?>> clsMessageDriven = reflections.getTypesAnnotatedWith(MessageDriven.class);
                Object obj;
                System.gc();
                if (cls.size() > 0) {
                    ejbContext = new EJBContext();
                    ejbContext.setJarPath(baseFile.getName().getURI());
                    ejbContext.setJarDeployed(baseFile.getName().getBaseName());
                    for (Class<?> ejbInterface : cls) {
                        //BeanPool.getInstance().create(ejbInterface);
                        obj = BeanPool.getInstance().get(ejbInterface);
                        System.out.println(obj);
                        ProxyFactory factory = new ProxyFactory();
                        obj = UnicastRemoteObject.exportObject((Remote) factory.createWithBean(obj),
                                servicesRegistryPort);
                        String remoteBinding = container.getRemoteBinding(ejbInterface);
                        System.out.println(remoteBinding + " for EJB" + obj);
                        if (remoteBinding != null) {
                            //registry.unbind(remoteBinding);
                            registry.rebind(remoteBinding, (Remote) obj);
                            ejbContext.put(remoteBinding, obj.getClass());
                        }
                        //registry.rebind("name", (Remote) obj);
                    }
                    jarEJBMap.put(baseFile.getName().getURI(), ejbContext);
                }
                System.out.println("Class Message Driven" + clsMessageDriven);
                System.out.println("Class Message Driven" + clsMessageDriven);
                if (clsMessageDriven.size() > 0) {
                    System.out.println("Class Message Driven");
                    MDBContext mdbContext;
                    ConcurrentHashMap<String, MDBContext> mdbContexts;
                    if (jarMDBMap.get(baseFile.getName().getURI()) != null) {
                        mdbContexts = jarMDBMap.get(baseFile.getName().getURI());
                    } else {
                        mdbContexts = new ConcurrentHashMap<String, MDBContext>();
                    }
                    jarMDBMap.put(baseFile.getName().getURI(), mdbContexts);
                    MDBContext mdbContextOld;
                    for (Class<?> mdbBean : clsMessageDriven) {
                        String classwithpackage = mdbBean.getName();
                        System.out.println("class package" + classwithpackage);
                        classwithpackage = classwithpackage.replace("/", ".");
                        System.out.println("classList:" + classwithpackage.replace("/", "."));
                        try {
                            if (!classwithpackage.contains("$")) {
                                //System.out.println("executor class in ExecutorServicesConstruct"+executorServiceClass);
                                //System.out.println();
                                if (!mdbBean.isInterface()) {
                                    Annotation[] classServicesAnnot = mdbBean.getDeclaredAnnotations();
                                    if (classServicesAnnot != null) {
                                        for (int annotcount = 0; annotcount < classServicesAnnot.length; annotcount++) {
                                            if (classServicesAnnot[annotcount] instanceof MessageDriven) {
                                                MessageDriven messageDrivenAnnot = (MessageDriven) classServicesAnnot[annotcount];
                                                ActivationConfigProperty[] activationConfigProperties = messageDrivenAnnot
                                                        .activationConfig();
                                                mdbContext = new MDBContext();
                                                mdbContext.setMdbName(messageDrivenAnnot.name());
                                                for (ActivationConfigProperty activationConfigProperty : activationConfigProperties) {
                                                    if (activationConfigProperty.propertyName()
                                                            .equals(MDBContext.DESTINATIONTYPE)) {
                                                        mdbContext.setDestinationType(
                                                                activationConfigProperty.propertyValue());
                                                    } else if (activationConfigProperty.propertyName()
                                                            .equals(MDBContext.DESTINATION)) {
                                                        mdbContext.setDestination(
                                                                activationConfigProperty.propertyValue());
                                                    } else if (activationConfigProperty.propertyName()
                                                            .equals(MDBContext.ACKNOWLEDGEMODE)) {
                                                        mdbContext.setAcknowledgeMode(
                                                                activationConfigProperty.propertyValue());
                                                    }
                                                }
                                                if (mdbContext.getDestinationType().equals(Queue.class.getName())) {
                                                    mdbContextOld = null;
                                                    if (mdbContexts.get(mdbContext.getMdbName()) != null) {
                                                        mdbContextOld = mdbContexts.get(mdbContext.getMdbName());
                                                        if (mdbContextOld != null && mdbContext.getDestination()
                                                                .equals(mdbContextOld.getDestination())) {
                                                            throw new Exception(
                                                                    "Only one MDB can listen to destination:"
                                                                            + mdbContextOld.getDestination());
                                                        }
                                                    }
                                                    mdbContexts.put(mdbContext.getMdbName(), mdbContext);
                                                    Queue queue = (Queue) jms.lookup(mdbContext.getDestination());
                                                    Connection connection = connectionFactory
                                                            .createConnection("guest", "guest");
                                                    connection.start();
                                                    Session session;
                                                    if (mdbContext.getAcknowledgeMode() != null && mdbContext
                                                            .getAcknowledgeMode().equals("Auto-Acknowledge")) {
                                                        session = connection.createSession(false,
                                                                Session.AUTO_ACKNOWLEDGE);
                                                    } else {
                                                        session = connection.createSession(false,
                                                                Session.AUTO_ACKNOWLEDGE);
                                                    }
                                                    MessageConsumer consumer = session.createConsumer(queue);
                                                    consumer.setMessageListener(
                                                            (MessageListener) mdbBean.newInstance());
                                                    mdbContext.setConnection(connection);
                                                    mdbContext.setSession(session);
                                                    mdbContext.setConsumer(consumer);
                                                    System.out.println("Queue=" + queue);
                                                } else if (mdbContext.getDestinationType()
                                                        .equals(Topic.class.getName())) {
                                                    if (mdbContexts.get(mdbContext.getMdbName()) != null) {
                                                        mdbContextOld = mdbContexts.get(mdbContext.getMdbName());
                                                        if (mdbContextOld.getConsumer() != null)
                                                            mdbContextOld.getConsumer().setMessageListener(null);
                                                        if (mdbContextOld.getSession() != null)
                                                            mdbContextOld.getSession().close();
                                                        if (mdbContextOld.getConnection() != null)
                                                            mdbContextOld.getConnection().close();
                                                    }
                                                    mdbContexts.put(mdbContext.getMdbName(), mdbContext);
                                                    Topic topic = (Topic) jms.lookup(mdbContext.getDestination());
                                                    Connection connection = connectionFactory
                                                            .createConnection("guest", "guest");
                                                    connection.start();
                                                    Session session;
                                                    if (mdbContext.getAcknowledgeMode() != null && mdbContext
                                                            .getAcknowledgeMode().equals("Auto-Acknowledge")) {
                                                        session = connection.createSession(false,
                                                                Session.AUTO_ACKNOWLEDGE);
                                                    } else {
                                                        session = connection.createSession(false,
                                                                Session.AUTO_ACKNOWLEDGE);
                                                    }
                                                    MessageConsumer consumer = session.createConsumer(topic);
                                                    consumer.setMessageListener(
                                                            (MessageListener) mdbBean.newInstance());
                                                    mdbContext.setConnection(connection);
                                                    mdbContext.setSession(session);
                                                    mdbContext.setConsumer(consumer);
                                                    System.out.println("Topic=" + topic);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
                classLoader.close();
                System.out.println(baseFile.getName().getURI() + " Deployed");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    @Override
    public void fileCreated(FileChangeEvent arg0) throws Exception {
        try {
            FileObject baseFile = arg0.getFile();
            EJBContext ejbContext;
            if (baseFile.getName().getURI().endsWith(".jar")) {
                System.out.println(baseFile.getName().getURI());
                URLClassLoader classLoader = new URLClassLoader(new URL[] { new URL(baseFile.getName().getURI()) },
                        Thread.currentThread().getContextClassLoader());
                ConfigurationBuilder config = new ConfigurationBuilder();
                config.addUrls(ClasspathHelper.forClassLoader(classLoader));
                config.addClassLoader(classLoader);
                org.reflections.Reflections reflections = new org.reflections.Reflections(config);
                EJBContainer container = EJBContainer.getInstance(baseFile.getName().getURI(), config);
                container.inject();
                Set<Class<?>> clsStateless = reflections.getTypesAnnotatedWith(Stateless.class);
                Set<Class<?>> clsMessageDriven = reflections.getTypesAnnotatedWith(MessageDriven.class);
                Object obj;
                System.gc();
                if (clsStateless.size() > 0) {
                    ejbContext = new EJBContext();
                    ejbContext.setJarPath(baseFile.getName().getURI());
                    ejbContext.setJarDeployed(baseFile.getName().getBaseName());
                    for (Class<?> ejbInterface : clsStateless) {
                        //BeanPool.getInstance().create(ejbInterface);
                        obj = BeanPool.getInstance().get(ejbInterface);
                        System.out.println(obj);
                        ProxyFactory factory = new ProxyFactory();
                        obj = UnicastRemoteObject.exportObject((Remote) factory.createWithBean(obj),
                                servicesRegistryPort);
                        String remoteBinding = container.getRemoteBinding(ejbInterface);
                        System.out.println(remoteBinding + " for EJB" + obj);
                        if (remoteBinding != null) {
                            //registry.unbind(remoteBinding);
                            registry.rebind(remoteBinding, (Remote) obj);
                            ejbContext.put(remoteBinding, obj.getClass());
                        }
                        //registry.rebind("name", (Remote) obj);
                    }
                    jarEJBMap.put(baseFile.getName().getURI(), ejbContext);
                }
                System.out.println("Class Message Driven" + clsMessageDriven);
                System.out.println("Class Message Driven" + clsMessageDriven);
                if (clsMessageDriven.size() > 0) {
                    System.out.println("Class Message Driven");
                    MDBContext mdbContext;
                    ConcurrentHashMap<String, MDBContext> mdbContexts;
                    if (jarMDBMap.get(baseFile.getName().getURI()) != null) {
                        mdbContexts = jarMDBMap.get(baseFile.getName().getURI());
                    } else {
                        mdbContexts = new ConcurrentHashMap<String, MDBContext>();
                    }
                    jarMDBMap.put(baseFile.getName().getURI(), mdbContexts);
                    MDBContext mdbContextOld;
                    for (Class<?> mdbBean : clsMessageDriven) {
                        String classwithpackage = mdbBean.getName();
                        System.out.println("class package" + classwithpackage);
                        classwithpackage = classwithpackage.replace("/", ".");
                        System.out.println("classList:" + classwithpackage.replace("/", "."));
                        try {
                            if (!classwithpackage.contains("$")) {
                                //System.out.println("executor class in ExecutorServicesConstruct"+executorServiceClass);
                                //System.out.println();
                                if (!mdbBean.isInterface()) {
                                    Annotation[] classServicesAnnot = mdbBean.getDeclaredAnnotations();
                                    if (classServicesAnnot != null) {
                                        for (int annotcount = 0; annotcount < classServicesAnnot.length; annotcount++) {
                                            if (classServicesAnnot[annotcount] instanceof MessageDriven) {
                                                MessageDriven messageDrivenAnnot = (MessageDriven) classServicesAnnot[annotcount];
                                                ActivationConfigProperty[] activationConfigProperties = messageDrivenAnnot
                                                        .activationConfig();
                                                mdbContext = new MDBContext();
                                                mdbContext.setMdbName(messageDrivenAnnot.name());
                                                for (ActivationConfigProperty activationConfigProperty : activationConfigProperties) {
                                                    if (activationConfigProperty.propertyName()
                                                            .equals(MDBContext.DESTINATIONTYPE)) {
                                                        mdbContext.setDestinationType(
                                                                activationConfigProperty.propertyValue());
                                                    } else if (activationConfigProperty.propertyName()
                                                            .equals(MDBContext.DESTINATION)) {
                                                        mdbContext.setDestination(
                                                                activationConfigProperty.propertyValue());
                                                    } else if (activationConfigProperty.propertyName()
                                                            .equals(MDBContext.ACKNOWLEDGEMODE)) {
                                                        mdbContext.setAcknowledgeMode(
                                                                activationConfigProperty.propertyValue());
                                                    }
                                                }
                                                if (mdbContext.getDestinationType().equals(Queue.class.getName())) {
                                                    mdbContextOld = null;
                                                    if (mdbContexts.get(mdbContext.getMdbName()) != null) {
                                                        mdbContextOld = mdbContexts.get(mdbContext.getMdbName());
                                                        if (mdbContextOld != null && mdbContext.getDestination()
                                                                .equals(mdbContextOld.getDestination())) {
                                                            throw new Exception(
                                                                    "Only one MDB can listen to destination:"
                                                                            + mdbContextOld.getDestination());
                                                        }
                                                    }
                                                    mdbContexts.put(mdbContext.getMdbName(), mdbContext);
                                                    Queue queue = (Queue) jms.lookup(mdbContext.getDestination());
                                                    Connection connection = connectionFactory
                                                            .createConnection("guest", "guest");
                                                    connection.start();
                                                    Session session;
                                                    if (mdbContext.getAcknowledgeMode() != null && mdbContext
                                                            .getAcknowledgeMode().equals("Auto-Acknowledge")) {
                                                        session = connection.createSession(false,
                                                                Session.AUTO_ACKNOWLEDGE);
                                                    } else {
                                                        session = connection.createSession(false,
                                                                Session.AUTO_ACKNOWLEDGE);
                                                    }
                                                    MessageConsumer consumer = session.createConsumer(queue);
                                                    consumer.setMessageListener(
                                                            (MessageListener) mdbBean.newInstance());
                                                    mdbContext.setConnection(connection);
                                                    mdbContext.setSession(session);
                                                    mdbContext.setConsumer(consumer);
                                                    System.out.println("Queue=" + queue);
                                                } else if (mdbContext.getDestinationType()
                                                        .equals(Topic.class.getName())) {
                                                    if (mdbContexts.get(mdbContext.getMdbName()) != null) {
                                                        mdbContextOld = mdbContexts.get(mdbContext.getMdbName());
                                                        if (mdbContextOld.getConsumer() != null)
                                                            mdbContextOld.getConsumer().setMessageListener(null);
                                                        if (mdbContextOld.getSession() != null)
                                                            mdbContextOld.getSession().close();
                                                        if (mdbContextOld.getConnection() != null)
                                                            mdbContextOld.getConnection().close();
                                                    }
                                                    mdbContexts.put(mdbContext.getMdbName(), mdbContext);
                                                    Topic topic = (Topic) jms.lookup(mdbContext.getDestination());
                                                    Connection connection = connectionFactory
                                                            .createConnection("guest", "guest");
                                                    connection.start();
                                                    Session session;
                                                    if (mdbContext.getAcknowledgeMode() != null && mdbContext
                                                            .getAcknowledgeMode().equals("Auto-Acknowledge")) {
                                                        session = connection.createSession(false,
                                                                Session.AUTO_ACKNOWLEDGE);
                                                    } else {
                                                        session = connection.createSession(false,
                                                                Session.AUTO_ACKNOWLEDGE);
                                                    }
                                                    MessageConsumer consumer = session.createConsumer(topic);
                                                    consumer.setMessageListener(
                                                            (MessageListener) mdbBean.newInstance());
                                                    mdbContext.setConnection(connection);
                                                    mdbContext.setSession(session);
                                                    mdbContext.setConsumer(consumer);
                                                    System.out.println("Topic=" + topic);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
                classLoader.close();
            }
            System.out.println(baseFile.getName().getURI() + " Deployed");
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    @Override
    public void fileDeleted(FileChangeEvent arg0) throws Exception {
        FileObject baseFile = arg0.getFile();
        if (baseFile.getName().getURI().endsWith(".jar")) {
            System.out.println(jarEJBMap.get(baseFile.getName().getURI()));
            EJBContext ejbContext = jarEJBMap.get(baseFile.getName().getURI());
            if (ejbContext != null) {
                HashMap<String, Class> bindings = ejbContext.getRemoteBindings();
                Set<String> remoteBindings = bindings.keySet();
                Iterator<String> remoteBinding = remoteBindings.iterator();
                while (remoteBinding.hasNext()) {
                    String binding = (String) remoteBinding.next();
                    registry.unbind(binding);
                    System.out.println("unregistering the class" + bindings.get(binding));
                }
                jarEJBMap.remove(baseFile.getName().getURI());
            }
            ConcurrentHashMap<String, MDBContext> mdbContexts = jarMDBMap.get(baseFile.getName().getURI());
            MDBContext mdbContext;
            if (mdbContexts != null) {
                Iterator<String> mdbnames = mdbContexts.keySet().iterator();
                while (mdbnames.hasNext()) {
                    String mdbname = mdbnames.next();
                    mdbContext = mdbContexts.get(mdbname);
                    if (mdbContext.getConsumer() != null) {
                        mdbContext.getConsumer().setMessageListener(null);
                        mdbContext.getConsumer().close();
                    }
                    if (mdbContext.getSession() != null)
                        mdbContext.getSession().close();
                    if (mdbContext.getConnection() != null)
                        mdbContext.getConnection().close();
                    mdbContexts.remove(mdbname);
                }
                jarMDBMap.remove(baseFile.getName().getURI());
            }
        }
        System.out.println(baseFile.getName().getURI() + " UnDeployed");
    }

}