Package org.apache.geronimo.plugin.packaging

Source Code of org.apache.geronimo.plugin.packaging.PackageBuilderShell

/**
*
* Copyright 2005 The Apache Software Foundation
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file 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.
*/
package org.apache.geronimo.plugin.packaging;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;
import java.util.Collection;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.geronimo.system.repository.Maven1Repository;
import org.apache.geronimo.system.repository.Maven2Repository;
import org.apache.geronimo.system.configuration.RepositoryConfigurationStore;
import org.apache.maven.jelly.MavenJellyContext;
import org.apache.maven.project.Dependency;
import org.apache.maven.repository.Artifact;

/**
* JellyBean that builds a Geronimo Configuration using the local Mavem
* infrastructure.
*
* @version $Rev: 406500 $ $Date: 2006-05-15 04:13:19 +0200 (Mon, 15 May 2006) $
*/
public class PackageBuilderShell {
    private static final String PACKAGING_CLASSPATH_PROPERTY = "packaging.classpath";
    private static final String PACKAGING_CONFIG_PROPERTY = "packaging.config.order";
    private static Log log = LogFactory.getLog(PlanProcessor.class);

    private List artifacts;
    private List pluginArtifacts;
    private MavenJellyContext context;

    private static ClassLoader classLoader;

    private File repository;
    private File targetRepository;
    private Collection deploymentConfigList;
    private String deployerName;

    private File planFile;
    private File moduleFile;
    private File packageFile;
    private String mainClass;
    private String mainGBean;
    private String mainMethod;
    private String configurations;
    private String classPath;
    private String endorsedDirs;
    private String extensionDirs;
    private String explicitResolutionLocation;
    private String logLevel = "INFO";

    public File getRepository() {
        return repository;
    }

    /**
     * Set the location of the Maven repository; typically ${maven.repo.local}
     *
     * @param repository the location of the Maven repository
     */
    public void setRepository(File repository) {
        this.repository = repository;
    }

    public File getTargetRepository() {
        return targetRepository;
    }

    public void setTargetRepository(File targetRepository) {
        this.targetRepository = targetRepository;
    }

    public String getDeployerName() {
        return deployerName;
    }

    /**
     * Set the name of the GBean that is the Deployer.
     *
     * @param deployerName the name of the Deployer GBean
     */
    public void setDeployerName(String deployerName) {
        this.deployerName = deployerName;
    }

    public File getPlanFile() {
        return planFile;
    }

    /**
     * Set the File that is the deployment plan.
     *
     * @param planFile the deployment plan
     */
    public void setPlanFile(File planFile) {
        this.planFile = planFile;
    }

    public File getModuleFile() {
        return moduleFile;
    }

    /**
     * Set the File that is the module being deployed.
     *
     * @param moduleFile the module to deploy
     */
    public void setModuleFile(File moduleFile) {
        this.moduleFile = moduleFile;
    }

    public File getPackageFile() {
        return packageFile;
    }

    /**
     * Set the File where the Configuration will be stored; normally the artifact being produced.
     *
     * @param packageFile the package file to produce
     */
    public void setPackageFile(File packageFile) {
        this.packageFile = packageFile;
    }

    public String getMainClass() {
        return mainClass;
    }

    /**
     * Set the name of the class containing the main method for a executable configuration.
     *
     * @param mainClass
     */
    public void setMainClass(String mainClass) {
        this.mainClass = mainClass;
    }

    public String getMainGBean() {
        return mainGBean;
    }

    public void setMainGBean(String mainGBean) {
        this.mainGBean = mainGBean;
    }

    public String getMainMethod() {
        return mainMethod;
    }

    public void setMainMethod(String mainMethod) {
        this.mainMethod = mainMethod;
    }

    public String getConfigurations() {
        return configurations;
    }

    public void setConfigurations(String configurations) {
        this.configurations = configurations;
    }

    public String getClassPath() {
        return classPath;
    }

    public void setClassPath(String classPath) {
        this.classPath = classPath;
    }

    public String getEndorsedDirs() {
        return endorsedDirs;
    }

    public void setEndorsedDirs(String endorsedDirs) {
        this.endorsedDirs = endorsedDirs;
    }

    public String getExtensionDirs() {
        return extensionDirs;
    }

    public void setExtensionDirs(String extensionDirs) {
        this.extensionDirs = extensionDirs;
    }

    public List getArtifacts() {
        return artifacts;
    }

    public void setArtifacts(List artifacts) {
        this.artifacts = artifacts;
        TreeMap tree = new TreeMap();
        for (Iterator iterator = artifacts.iterator(); iterator.hasNext();) {
            Artifact artifact = (Artifact) iterator.next();
            Dependency dependency = artifact.getDependency();
            if (dependency.getProperty(PACKAGING_CONFIG_PROPERTY) != null) {
                String orderString = dependency.getProperty(PACKAGING_CONFIG_PROPERTY);
                try {
                    Integer order = Integer.decode(orderString);
                    String artifactString = dependency.getGroupId() + "/" + dependency.getArtifactId() + "/" + dependency.getVersion() + "/" + dependency.getType();
                    tree.put(order, artifactString);
                } catch(NumberFormatException e) {
                    System.out.println("Could not interpret order for " + dependency);
                }
            }
        }

        deploymentConfigList = tree.values();
    }

    public List getPluginArtifacts() {
        return pluginArtifacts;
    }

    public void setPluginArtifacts(List pluginArtifacts) {
        this.pluginArtifacts = pluginArtifacts;
    }

    public MavenJellyContext getContext() {
        return context;
    }

    public void setContext(MavenJellyContext context) {
        this.context = context;
    }

    public String getExplicitResolutionLocation() {
        return explicitResolutionLocation;
    }

    public void setExplicitResolutionLocation(String explicitResolutionLocation) {
        this.explicitResolutionLocation = explicitResolutionLocation;
    }

    public String getLogLevel() {
        return logLevel;
    }

    public void setLogLevel(String logLevel) {
        this.logLevel = logLevel;
    }

    public void execute() throws Exception {
        try {
            Object packageBuilder = getPackageBuilder();
            set("setClassPath", classPath, String.class, packageBuilder);
            set("setDeployerName", deployerName, String.class, packageBuilder);
            set("setDeploymentConfig", deploymentConfigList, Collection.class, packageBuilder);
            set("setEndorsedDirs", endorsedDirs, String.class, packageBuilder);
            set("setExtensionDirs", extensionDirs, String.class, packageBuilder);
            set("setMainClass", mainClass, String.class, packageBuilder);
            set("setMainMethod", mainMethod, String.class, packageBuilder);
            set("setMainGBean", mainGBean, String.class, packageBuilder);
            set("setConfigurations", configurations, String.class, packageBuilder);
            set("setModuleFile", moduleFile, File.class, packageBuilder);
            set("setPackageFile", packageFile, File.class, packageBuilder);
            set("setPlanFile", planFile, File.class, packageBuilder);
            set("setRepository", repository, File.class, packageBuilder);
            set("setRepositoryClass", Maven1Repository.class.getName(), String.class, packageBuilder);
            set("setConfigurationStoreClass", MavenConfigStore.class.getName(), String.class, packageBuilder);
            set("setTargetRepository", targetRepository, File.class, packageBuilder);
            set("setTargetRepositoryClass", Maven2Repository.class.getName(), String.class, packageBuilder);
            set("setTargetConfigurationStoreClass", RepositoryConfigurationStore.class.getName(), String.class, packageBuilder);
            set("setExplicitResolutionLocation", explicitResolutionLocation, String.class, packageBuilder);
            set("setLogLevel", logLevel, String.class, packageBuilder);

            Method m = packageBuilder.getClass().getMethod("execute", new Class[]{});
            m.invoke(packageBuilder, new Object[]{});
        } catch (Exception e) {
            log.error(e.getClass().getName()+": "+e.getMessage(), e);
            throw e;
        }
    }

    private void set(String methodName, Object value, Class type, Object packageBuilder) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        Method m = packageBuilder.getClass().getMethod(methodName, new Class[]{type});
        m.invoke(packageBuilder, new Object[]{value});
    }


    private Object getPackageBuilder() throws ClassNotFoundException, IllegalAccessException, InstantiationException, MalformedURLException {
        if (classLoader == null) {
            String repo = context.getMavenRepoLocal();
            List urls = new ArrayList();
            for (Iterator iterator = pluginArtifacts.iterator(); iterator.hasNext();) {
                Artifact artifact = (Artifact) iterator.next();
                Dependency dependency = artifact.getDependency();
                if ("true".equals(dependency.getProperty(PACKAGING_CLASSPATH_PROPERTY))) {
                    String urlString = artifact.getUrlPath();
                    URL url = new File(repo + urlString).toURL();
                    urls.add(url);
                }
            }

            boolean found = false;
            for (Iterator iterator = artifacts.iterator(); iterator.hasNext();) {
                Artifact artifact = (Artifact) iterator.next();
                Dependency dependency = artifact.getDependency();
                if ("geronimo".equals(dependency.getGroupId())
                && "geronimo-packaging-plugin".equals(dependency.getArtifactId())
                && "plugin".equals(dependency.getType())) {
                    String urlString = artifact.getUrlPath();
                    URL url = new File(repo + urlString).toURL();
                    urls.add(url);
                    found = true;
                }
            }
            if (!found) {
                System.err.println("You must include the geronimo packaging plugin as a dependency in your project.xml");
                throw new RuntimeException("You must include the geronimo packaging plugin as a dependency in your project.xml");
            }
            URL[] builderClassPath = (URL[]) urls.toArray(new URL[urls.size()]);
            classLoader = new URLClassLoader(builderClassPath, ClassLoader.getSystemClassLoader());
        }
        return classLoader.loadClass(PackageBuilder.class.getName()).newInstance();
    }


}
TOP

Related Classes of org.apache.geronimo.plugin.packaging.PackageBuilderShell

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.