📄 pluginfactory.java
字号:
package org.appfuse.util.security.plugin;
/**
*
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: www.aspire-tech.com</p>
* @author chentao
* @version 1.0
*/
import java.io.File;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.appfuse.util.security.*;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import org.appfuse.util.exception.AppException;
import org.appfuse.util.exception.ExceptionConstants;
import org.appfuse.util.exception.AppSystemException;
import org.appfuse.util.exception.AppBusinessException;
import org.appfuse.util.security.PlugIn;
import org.appfuse.util.*;
public class PluginFactory {
private static final Log log = LogFactory.getLog(PluginFactory.class);
private static Element rootElement = null;
//保存interceptors实现类的名称
private static Hashtable interceptorQueue;
/**
* 载入配置文件
* @param cfg 配置文件名称
*/
public static void load(String cfg) {
try {
rootElement = new SAXBuilder().build(new File(cfg)).getRootElement();
} catch(Exception e) {
e.printStackTrace();
}
}
/**
* 获得配置文件中指定名称的Element
* @param elementName String
* @return Element
*/
public static Element getElement(String elementName) {
return rootElement.getChild(elementName);
}
/**
* 初始化interceptorQueue
*/
public static void initInterceptorQueue() {
if(interceptorQueue==null)
interceptorQueue = new Hashtable();
List intercptList = new ArrayList();
Element interceptors = getElement("plugins");
intercptList = interceptors.getChildren("plugin");
Iterator it = intercptList.iterator();
Element tmpElement = null;
while(it.hasNext()) {
tmpElement = (Element)it.next();
PluginItem plugin = new PluginItem();
//解析singleton
String singleton = tmpElement.getAttributeValue("singleton");
boolean isSingleton = false;
if(singleton != null) {
if(singleton.equalsIgnoreCase("yes")) {
isSingleton = true;
}
}
plugin.setName(tmpElement.getAttributeValue("name"));
plugin.setClassName(tmpElement.getAttributeValue("classname"));
plugin.setSpringID(tmpElement.getAttributeValue("springID"));
plugin.setSingleton(isSingleton);
interceptorQueue.put(plugin.getName(), plugin);
}
}
/**
* 获得配置文件中指定的interceptor等实现类名称
* @param InterceptorName String
* @return PluginItem
*/
public static PluginItem getPluginItem(String InterceptorName) {
PluginItem plugin = null;
if(interceptorQueue == null) {
initInterceptorQueue();
}
plugin = (PluginItem)interceptorQueue.get(InterceptorName);
return plugin;
}
public static String getClassName(String name) {
String className = "";
PluginItem plugin = getPluginItem(name);
if(plugin != null) {
className = plugin.getClassName();
}
return className;
}
public static void main(String[] args) {
PluginFactory factory = new PluginFactory();
//需要在系统加载的时候执行load()
factory.load("config/plugin.xml");
factory.initInterceptorQueue();
PluginItem plugin = factory.getPluginItem("AuthenticateInterceptor");
System.out.println("---className: " + plugin.getClassName() +
" name: " + plugin.getName() + " isSingleton: " +
plugin.isSingleton() + "---");
plugin = factory.getPluginItem("LogoutInterceptor");
System.out.println("---className: " + plugin.getClassName() +
" name: " + plugin.getName() + " isSingleton: " +
plugin.isSingleton() + "---");
plugin = factory.getPluginItem("LoginInterceptor");
System.out.println("---className: " + plugin.getClassName() +
" name: " + plugin.getName() + " isSingleton: " +
plugin.isSingleton() + "---");
}
public PluginFactory() {
}
/**
* 生成plugin对象
* @param pluginName String
* @throws AppException
* @return PlugIn
*/
public static PlugIn getPlugin(String pluginName) throws AppException {
if(interceptorQueue==null){
initInterceptorQueue();
}
PluginItem plugin = (PluginItem)interceptorQueue.get(pluginName);
int size = interceptorQueue.size();
//interceptorQueue.get
Class pluginClass = null;
try {
if(plugin == null) {
log.error("plugin object can not be null .");
return null;
}
String springID = plugin.getSpringID();
String name = plugin.getName();
if(springID != null && !"".equals(springID.trim())) {
return(PlugIn)ServiceFactory.getService(name);
}
String className = parseName(pluginName);
pluginClass = Class.forName(className);
return(PlugIn)pluginClass.newInstance();
} catch(Exception e) {
log.error("getPlugin error", e);
throw new AppSystemException("CreatePluginObject Failed", e,
ExceptionConstants.SYS_GENERAL_ERR);
}
}
/**
* Locate the implement class by service name
* @param serviceName the service name
* @return service implement class name
*/
private static String parseName(String serviceName) {
return getClassName(serviceName);
}
public static PlugIn getPluginServ(String pluginName) throws AppException {
return(PlugIn)ServiceFactory.getService(pluginName);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -