WARDeployment.java :  » J2EE » ow2-easybeans » org » ow2 » easybeans » deployment » Java Open Source

Java Open Source » J2EE » ow2 easybeans 
ow2 easybeans » org » ow2 » easybeans » deployment » WARDeployment.java
/**
 * EasyBeans
 * Copyright (C) 2007 Bull S.A.S.
 * Contact: easybeans@ow2.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * --------------------------------------------------------------------------
 * $Id:WARDeployment.java 2059 2007-11-22 17:22:33Z benoitf $
 * --------------------------------------------------------------------------
 */

package org.ow2.easybeans.deployment;

import java.util.LinkedList;
import java.util.List;

import org.ow2.easybeans.deployment.annotations.analyzer.WarAnnotationDeploymentAnalyzer;
import org.ow2.easybeans.deployment.annotations.exceptions.AnalyzerException;
import org.ow2.easybeans.deployment.annotations.exceptions.ResolverException;
import org.ow2.easybeans.deployment.xml.WARDeploymentDesc;
import org.ow2.easybeans.deployment.xml.WARDeploymentDescException;
import org.ow2.easybeans.deployment.xml.struct.Listener;
import org.ow2.easybeans.deployment.xml.struct.Servlet;
import org.ow2.easybeans.deployment.xml.struct.Tag;
import org.ow2.easybeans.deployment.xml.struct.WAR;
import org.ow2.util.ee.deploy.api.archive.IArchive;
import org.ow2.util.log.Log;
import org.ow2.util.log.LogFactory;

/**
 * This class will parse given war file and extract meta-data.
 * @author Florent Benoit
 */
public class WARDeployment {

    /**
     * Logger.
     */
    private static Log logger = LogFactory.getLog(WARDeployment.class);

    /**
     * Web archive that will be analyzed.
     */
    private IArchive archive = null;

    /**
     * War Deployment analyzer.
     */
    private WarAnnotationDeploymentAnalyzer warAnnotationDeploymentAnalyzer = null;

    /**
     * Constructor.<br> Archive which will be used when analyzing.
     * @param archive the archive to analyze.
     * @param classLoader the given classloader used to read the classes.
     */
    public WARDeployment(final IArchive archive, final ClassLoader classLoader) {
        this.archive = archive;
        this.warAnnotationDeploymentAnalyzer = new WarAnnotationDeploymentAnalyzer(archive, classLoader);
    }


    /**
     * Analyzes the web application.
     * @throws AnalyzerException if analyze of war file fails.
     * @throws ResolverException if resolver fails.
     * @throws WARDeploymentDescException if parsing fails.
     */
    @SuppressWarnings("boxing")
    public void analyze() throws AnalyzerException, WARDeploymentDescException, ResolverException {

        // Get WAR DD.
        WAR war = WARDeploymentDesc.getWAR(archive);

        // for time debugging
        long tAnalyzeStart = System.currentTimeMillis();

        // Build list of class to analyze
        List<String> classNames = new LinkedList<String>();

        // Add the servlets
        for (Servlet servlet : war.getServlets()) {
            addClassIfNotAlreadyPresent(servlet.getClassName(), classNames);
        }

        // Add the listeners
        for (Listener listener : war.getListeners()) {
            addClassIfNotAlreadyPresent(listener.getListenerClassName(), classNames);
        }

        // Add the tags
        for (Tag tag : war.getTags()) {
            addClassIfNotAlreadyPresent(tag.getTagClassName(), classNames);
        }

        // Analyze these servlet classes.
        warAnnotationDeploymentAnalyzer.analyze(classNames);

        // time if debugging
        if (logger.isDebugEnabled()) {
            long tAnalyzeStartEnd = System.currentTimeMillis();
            if (logger.isDebugEnabled()) {
                logger.debug("Analyze of file {0} took {1} ms.", archive.getName(), (tAnalyzeStartEnd - tAnalyzeStart));
            }
        }

    }

    /**
     * Add a given class in the list only if it is not yet present in the list.
     * @param className the given name of the class
     * @param classNames the lsit of existing classes.
     */
    protected void addClassIfNotAlreadyPresent(final String className, final List<String> classNames) {
        // Sometimes, servlet classname is null as it is referenced as a jsp-file
        if (className == null) {
            return;
        }
        if (!classNames.contains(className)) {
            classNames.add(className);
        }
    }

    /**
     * @return annotation deployment analyzer
     */
    public WarAnnotationDeploymentAnalyzer getAnnotationDeploymentAnalyzer() {
        return warAnnotationDeploymentAnalyzer;
    }

    /**
     * @return the archive of this deployment.
     */
    public IArchive getArchive() {
        return archive;
    }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.