1 52 package com.go.trove.util.plugin; 53 54 import com.go.trove.util.PropertyMap; 55 56 import java.util.Iterator ; 57 import java.util.Set ; 58 59 66 public class PluginFactory { 67 68 private static final String cClassKey = "class"; 69 private static final String cInitKey = "init"; 70 private static final String cPluginsKey = "plugins"; 71 72 public static final Plugin createPlugin(String name, PluginFactoryConfig config) 73 throws PluginFactoryException 74 { 75 Plugin result; 76 String className = config.getProperties().getString(cClassKey); 77 PropertyMap props = config.getProperties().subMap(cInitKey); 78 try { 79 Class clazz = Class.forName(className); 80 result = (Plugin) clazz.newInstance(); 81 PluginConfig pluginConfig = new PluginConfigSupport 82 (props, config.getLog(), config.getPluginContext(), name); 83 result.init(pluginConfig); 84 } catch (PluginException e) { 85 throw new PluginFactoryException(e); 86 } catch (ClassNotFoundException e) { 87 throw new PluginFactoryException(e); 88 } catch (InstantiationException e) { 89 throw new PluginFactoryException(e); 90 } catch (IllegalAccessException e) { 91 throw new PluginFactoryException(e); 92 } 93 return result; 94 } 95 96 public static final Plugin[] createPlugins(PluginFactoryConfig config) 97 throws PluginFactoryException 98 { 99 Plugin[] result; 100 PropertyMap properties = config.getProperties().subMap(cPluginsKey); 101 Set keySet = properties.subMapKeySet(); 102 result = new Plugin[keySet.size()]; 103 Iterator iterator = keySet.iterator(); 104 for (int i=0; iterator.hasNext(); i++) { 105 String name = (String ) iterator.next(); 106 PropertyMap initProps = properties.subMap(name); 107 PluginFactoryConfig conf = new PluginFactoryConfigSupport 108 (initProps, config.getLog(), config.getPluginContext()); 109 result[i] = createPlugin(name, conf); 110 } 111 return result; 112 } 113 114 } 115 116 | Popular Tags |