com.app.messaging.MessagingClassConstruct.java Source code

Java tutorial

Introduction

Here is the source code for com.app.messaging.MessagingClassConstruct.java

Source

package com.app.messaging;

/*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.Iterator;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

import org.apache.commons.digester3.Digester;
import org.apache.log4j.Logger;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.app.server.WebClassLoader;
import com.app.services.ExecutorService;
import com.app.services.ExecutorServiceAnnot;
import com.app.services.ExecutorServiceInfo;
import com.app.services.ExecutorServices;

/**
 * This class is implementation of the construction of the messaging services class which is done through the War File deployed from the War deployer and parsed by the Digester class and metadata is configured
 * @author arun
 *
 */
public class MessagingClassConstruct {

    /**
     * This method parses the messagingclass.xml which is obtained after deploying the War file and metadata is constructed and kept in HashMap 
     * @param messagedigester
     * @param configFile
     * @param customClassLoader
     * @param messagingClassMap
     * @throws ClassNotFoundException
     * @throws FileNotFoundException
     * @throws IOException
     * @throws SAXException
     */
    Logger log = Logger.getLogger(MessagingClassConstruct.class);

    public void getMessagingClass(Digester messagedigester, File configFile, WebClassLoader customClassLoader,
            Hashtable messagingClassMap)
            throws ClassNotFoundException, FileNotFoundException, IOException, SAXException {
        MessagingClasses messagingClasses = (MessagingClasses) messagedigester
                .parse(new InputSource(new FileInputStream(configFile)));

        for (MessagingClass messagingclass : messagingClasses.messagingClasses) {
            Class messagingClass = customClassLoader.loadClass(messagingclass.getMessagingclass());
            messagingclass.setMessageClass(messagingClass);
            if (messagingclass.getMessagetype().equals("RandomQueue")) {
                ConcurrentHashMap randomqueuemap = (ConcurrentHashMap) messagingClassMap.get("RandomQueue");
                //System.out.println("MessageElemName="+messagingclass.getMessageelemname().trim());
                if (randomqueuemap == null) {
                    System.out.print("No RandomQueue Available");
                } else {
                    CopyOnWriteArrayList queueclassList = (CopyOnWriteArrayList) randomqueuemap
                            .get(messagingclass.getMessageelemname().trim());
                    if (queueclassList != null) {
                        queueclassList.add(messagingclass);
                    } else {
                        System.out.print("No RandomQueue Available");
                    }
                }
            } else if (messagingclass.getMessagetype().equals("RoundRobinQueue")) {
                ConcurrentHashMap roundrobinqueuemap = (ConcurrentHashMap) messagingClassMap.get("RoundRobinQueue");
                if (roundrobinqueuemap == null) {
                    System.out.print("No RoundRobinQueue Available");
                } else {
                    CopyOnWriteArrayList queueclassList = (CopyOnWriteArrayList) roundrobinqueuemap
                            .get(messagingclass.getMessageelemname().trim());
                    if (queueclassList != null) {
                        queueclassList.add(messagingclass);
                    } else {
                        System.out.print("No RoundRobinQueue Available");
                    }
                }
            } else if (messagingclass.getMessagetype().equals("Topic")) {
                ConcurrentHashMap topicmap = (ConcurrentHashMap) messagingClassMap.get("Topic");
                if (topicmap == null) {
                    System.out.print("No Topic Available");
                } else {
                    CopyOnWriteArrayList topicclassList = (CopyOnWriteArrayList) topicmap
                            .get(messagingclass.getMessageelemname().trim());
                    if (topicclassList != null) {
                        topicclassList.add(messagingclass);
                    } else {
                        System.out.print("No Topic Available");
                    }
                }
            }
        }
        //System.out.println(messagingClassMap);
    }

    /**
     * This method parses the messagingclass.xml which is obtained after undeploying the War file and metadata which is constructed in removed from the messaingclassMap 
     * @param messagedigester
     * @param configFile
     * @param messagingClassMap
     * @throws FileNotFoundException
     * @throws IOException
     * @throws SAXException
     */
    public void removeMessagingClass(Digester messagedigester, File configFile, Hashtable messagingClassMap)
            throws FileNotFoundException, IOException, SAXException {
        try {
            MessagingClasses messagingClasses = (MessagingClasses) messagedigester
                    .parse(new InputSource(new FileInputStream(configFile)));
            for (MessagingClass messagingclass : messagingClasses.messagingClasses) {
                if (messagingclass.getMessagetype().equals("RandomQueue")) {
                    ConcurrentHashMap randomqueuemap = (ConcurrentHashMap) messagingClassMap.get("RandomQueue");
                    //System.out.println("MessageElemName="+messagingclass.getMessageelemname().trim()+"Random Queue Map"+randomqueuemap);
                    if (randomqueuemap != null) {
                        CopyOnWriteArrayList queueclassList = (CopyOnWriteArrayList) randomqueuemap
                                .get(messagingclass.getMessageelemname().trim());
                        if (queueclassList != null) {
                            for (int count = 0; count < queueclassList.size(); count++) {
                                MessagingClass messagingClassTmp = (MessagingClass) queueclassList.get(count);
                                //System.out.println(messagingClassTmp);
                                //System.out.println(messagingClassTmp.getMessageelemname()+" "+messagingclass.getMessageelemname() );
                                //System.out.println(messagingClassTmp.getMessagetype()+" "+messagingclass.getMessagetype() );
                                //System.out.println(messagingClassTmp.getMessagingclass()+" "+messagingclass.getMessagingclass() );
                                if (messagingClassTmp.getMessageelemname().trim()
                                        .equals(messagingclass.getMessageelemname().trim())
                                        && messagingClassTmp.getMessagetype().trim()
                                                .equals(messagingclass.getMessagetype().trim())
                                        && messagingClassTmp.getMessagingclass().trim()
                                                .equals(messagingclass.getMessagingclass().trim())) {
                                    queueclassList.remove(messagingClassTmp);
                                }
                                //System.out.println(count);
                                //System.out.println("MessageElemName="+messagingClassTmp);
                            }
                        }
                    }
                } else if (messagingclass.getMessagetype().equals("RoundRobinQueue")) {
                    ConcurrentHashMap roundrobinqueuemap = (ConcurrentHashMap) messagingClassMap
                            .get("RoundRobinQueue");
                    //System.out.println("RoundRobinQueueMap"+roundrobinqueuemap);
                    if (roundrobinqueuemap != null) {
                        CopyOnWriteArrayList queueclassList = (CopyOnWriteArrayList) roundrobinqueuemap
                                .get(messagingclass.getMessageelemname().trim());
                        if (queueclassList != null) {
                            for (int count = 0; count < queueclassList.size(); count++) {
                                MessagingClass messagingClassTmp = (MessagingClass) queueclassList.get(count);
                                if (messagingClassTmp.getMessageelemname()
                                        .equals(messagingclass.getMessageelemname())
                                        && messagingClassTmp.getMessagetype()
                                                .equals(messagingclass.getMessagetype())
                                        && messagingClassTmp.getMessagingclass()
                                                .equals(messagingclass.getMessagingclass())) {
                                    queueclassList.remove(messagingClassTmp);
                                }

                            }
                        }
                    }
                    //System.out.println("End RoundRobinQueueMap"+roundrobinqueuemap);
                } else if (messagingclass.getMessagetype().equals("Topic")) {
                    ConcurrentHashMap topicmap = (ConcurrentHashMap) messagingClassMap.get("Topic");
                    //System.out.println("TopicMap"+topicmap);
                    if (topicmap != null) {
                        CopyOnWriteArrayList topicclassList = (CopyOnWriteArrayList) topicmap
                                .get(messagingclass.getMessageelemname().trim());
                        if (topicclassList != null) {
                            for (int count = 0; count < topicclassList.size(); count++) {
                                MessagingClass messagingClassTmp = (MessagingClass) topicclassList.get(count);
                                if (messagingClassTmp.getMessageelemname()
                                        .equals(messagingclass.getMessageelemname())
                                        && messagingClassTmp.getMessagetype()
                                                .equals(messagingclass.getMessagetype())
                                        && messagingClassTmp.getMessagingclass()
                                                .equals(messagingclass.getMessagingclass())) {
                                    topicclassList.remove(messagingClassTmp);
                                }
                            }
                        }
                    }
                    //System.out.println("End TopicMap"+topicmap);
                }
            }
        } catch (Exception ex) {
            log.error("Error occurred in constructing messaging classes", ex);
            //ex.printStackTrace();
        }
        //System.out.println(messagingClassMap);
    }
}