Package org.jvnet.glassfish.comms.clb.core.common.chr

Source Code of org.jvnet.glassfish.comms.clb.core.common.chr.DcrPluginLoader$DcrPluginLoaderException

/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
* Copyright (c) Ericsson AB, 2004-2008. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License").  You
* may not use this file except in compliance with the License. You can obtain
* a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
* or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
* Sun designates this particular file as subject to the "Classpath" exception
* as provided by Sun in the GPL Version 2 section of the License file that
* accompanied this code.  If applicable, add the following below the License
* Header, with the fields enclosed by brackets [] replaced by your own
* identifying information: "Portions Copyrighted [year]
* [name of copyright owner]"
*
* Contributor(s):
*
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license."  If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above.  However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package org.jvnet.glassfish.comms.clb.core.common.chr;

import org.glassfish.comms.api.datacentric.DcrPlugin;

import java.io.File;
import java.io.IOException;

import java.net.URL;
import java.net.URLClassLoader;

import java.util.jar.Attributes;
import java.util.jar.JarFile;


/**
* A loader for DCR plugins. Handles loading of a DCR plugin (implementation of
* {@link DcrPlugin}) in the form of a pre-compiled java class packed in a jar-file.
*
*/
public class DcrPluginLoader {
    private File dcrFile;

    /**
     * Create a loader for the specified file.
     *
     * @param dcrFile
     * @throws DcrPluginLoaderException
     *             thrown if an unsupported file type is supplied
     */
    public DcrPluginLoader(File dcrFile) throws DcrPluginLoaderException {
        if (!isSupported(dcrFile)) {
            throw new DcrPluginLoaderException("The file was not a jar-file: " + dcrFile, null);
        }

        this.dcrFile = dcrFile;
    }

    /**
     * Check if this class supports the file type.
     * @param file the file to check
     * @return true if the file type is supported; otherwise false
     */
    public static boolean isSupported(File file) {
        String name = file.getName().toLowerCase();

        return (name.indexOf(".jar") >= 0);
    }

    /**
     * Loads and instantiates the plugin.
     *
     * @return the loaded and instantiated plugin.
     * @throws DcrPluginLoaderException
     */
    public DcrPlugin loadDcrPlugin() throws DcrPluginLoaderException {
        DcrPlugin plugin = loadDcrPlugin(dcrFile);

        //        injectAnnotations(plugin);
        return plugin;
    }

    @SuppressWarnings("unchecked")
    private DcrPlugin loadDcrPlugin(File pluginFile) throws DcrPluginLoaderException {
        Class<DcrPlugin> pluginClass;

        try {
            // The plugin file is a .jar file extract class name from
            // manifest...
            String pluginQualifiedClassName = getDcrPluginClassName(pluginFile);

            // ...and create new class loader with the plugin jar as
            // CLASSPATH...
            ClassLoader dcrPluginLoader = new URLClassLoader(new URL[] { pluginFile.toURI().toURL() }, this.getClass().getClassLoader());
            // ...and load class through new loader
            pluginClass = (Class<DcrPlugin>) dcrPluginLoader.loadClass(pluginQualifiedClassName);

            // Create a new instance
            DcrPlugin plugin = pluginClass.newInstance();

            return plugin;
        } catch (IOException e) {
            throw new DcrPluginLoaderException("Failed to load plugin: " + pluginFile.getAbsolutePath(), e);
        } catch (InstantiationException e) {
            throw new DcrPluginLoaderException("Failed to load plugin: " + pluginFile.getAbsolutePath(), e);
        } catch (IllegalAccessException e) {
            throw new DcrPluginLoaderException("Failed to load plugin: " + pluginFile.getAbsolutePath(), e);
        } catch (ClassNotFoundException e) {
            throw new DcrPluginLoaderException("Failed to load plugin: " + pluginFile.getAbsolutePath(), e);
        }
    }

    private String getDcrPluginClassName(File pluginFile) throws IOException {
        JarFile dcrJarFile = new JarFile(pluginFile);
        Attributes attributes = dcrJarFile.getManifest().getMainAttributes();

        if (attributes == null) {
            throw new RuntimeException("Manifest is missing the Plugin-Class attribute");
        }

        String pluginQualifiedClassName = attributes.getValue("Dcr-Plugin-Class");

        return pluginQualifiedClassName;
    }

    public static class DcrPluginLoaderException extends Exception {
        private static final long serialVersionUID = 1L;

        public DcrPluginLoaderException(String message, Throwable cause) {
            super(message, cause);
        }
    }
}
TOP

Related Classes of org.jvnet.glassfish.comms.clb.core.common.chr.DcrPluginLoader$DcrPluginLoaderException

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.