at.spardat.xma.plugins.PluginManager Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* s IT Solutions AT Spardat GmbH - initial API and implementation
*******************************************************************************/
// @(#) $Id: PluginManager.java 2246 2008-01-03 09:22:50Z s2877 $
package at.spardat.xma.plugins;
import java.util.HashMap;
import at.spardat.enterprise.exc.SysException;
import at.spardat.xma.exception.Codes;
/**
* Base class for client and server side plugin managers.
* @author s2877
*/
public abstract class PluginManager {
/**
* Key is the name of an interface, value is the implementation class
*/
private HashMap plugIns_ = new HashMap();
/**
* Finds the configured Plugin implementing the given interface.
*
* @param pluginInterface the interface of the plugin to find.
* @return the found Plugin implementation
* @throws RuntimeException if no implementation is configured for the interface.
*/
public Object getPlugin (Class pluginInterface) {
return getPlugin (pluginInterface.getName());
}
/**
* Returns a plugin implementation for a specified interface name.
*
* @param interfaceName the fully qualified name of a java interface, specifying
* the interface the implementation has to implement. Both, the
* interface and the implementation must be loadable by the classloader
* that loaded this class.
* @return an object implementing the provided interface.
* @throws RuntimeException if no implementation is configured for the interface.
*/
public Object getPlugin (String interfaceName) {
return getPlugin (interfaceName, this.getClass().getClassLoader());
}
/**
* Returns a plugin implementation for a specified interface name. Once an
* implementation for a particular interface is found, it is cached inside
* the PluginManager and future calls always return the same instance.
*
* @param interfaceName the fully qualified name of a java interface, specifying
* the interface the implementation has to implement. Both, the
* interface and the implementation must be loadable by the classloader
* that loaded this class.
* @param cl the ClassLoader where the implementation class can be loaded from.
* @return an object implementing the provided interface.
* @throws RuntimeException if no implementation is configured for the interface.
*/
public Object getPlugin (String interfaceName, ClassLoader cl) {
Object implementation = null;
/**
* look up the HashMap and optionally resolve plugin implementation
*/
synchronized (plugIns_) {
implementation = plugIns_.get(interfaceName);
if (implementation == null) {
implementation = resolvePlugin (interfaceName, cl);
plugIns_.put(interfaceName, implementation);
}
}
return implementation;
}
/**
* Resolves a plugin implementation for a specified interface name. This method
* is called just once per interface name. It is required that this operation
* should terminate as fast as possible, optimally just constructing the
* implementation object.
*
* @param interfaceName the fully qualified name of a java interface, specifying
* the interface the implementation has to implement. Both, the
* interface and the implementation must be loadable by the classloader
* that loaded this class.
* @param cl the ClassLoader where the implementation class can be loaded from.
* @return an object implementing the provided interface.
*/
protected abstract Object resolvePlugin (String interfaceName, ClassLoader cl);
/**
* Uses the default constructor to construct an instance of a given class's
* name in a provided ClassLoader and returns it.
*
* @exception SysException with code Codes.PLUGIN_CANNOT_CONSTRUCT_OBJECT on faults.
*/
protected Object newInstanceOf (String className, ClassLoader loader) {
try {
Class clazz = loader.loadClass(className);
return clazz.newInstance();
} catch (Exception ex) {
throw new SysException (ex, "cannot create instance of " + className)
.setCode (Codes.PLUGIN_CANNOT_CONSTRUCT_OBJECT);
}
}
/**
* Checks if a plugin is configured for the given interface. If called on client side, it checks for
* the definition of a client side implementaition. If called on server side, it checks for
* the definition of a server side implementation.
* @param pluginInterface the interface of the plugin to find.
* @return true if a plugin is declared for the given interface in xma-app.xml, false otherwise.
* @author s2877
*/
public boolean isPluginDeclared(Class pluginInterface) {
return isPluginDeclared(pluginInterface.getName());
}
/**
* Checks if a plugin is configured for the given interface. If called on client side, it checks for
* the definition of a client side implementaition. If called on server side, it checks for
* the definition of a server side implementation.
* @param interfaceName the fully qualified name of a java interface, specifying
* the interface the plugin has to implement.
* @return true if a plugin is declared for the given interface in xma-app.xml, false otherwise.
* @author s2877
*/
public abstract boolean isPluginDeclared(String interfaceName);
}