001: package org.contineo.util;
002:
003: import java.io.File;
004: import java.io.FilenameFilter;
005: import java.net.MalformedURLException;
006: import java.net.URL;
007: import java.util.Arrays;
008: import java.util.Collection;
009: import java.util.Iterator;
010: import java.util.LinkedList;
011: import java.util.List;
012: import java.util.Map;
013:
014: import org.apache.commons.lang.StringUtils;
015: import org.java.plugin.JpfException;
016: import org.java.plugin.ObjectFactory;
017: import org.java.plugin.PluginManager;
018: import org.java.plugin.registry.Extension;
019: import org.java.plugin.registry.ExtensionPoint;
020: import org.java.plugin.registry.Identity;
021: import org.java.plugin.registry.PluginDescriptor;
022: import org.java.plugin.util.ExtendedProperties;
023:
024: /**
025: * Central point where plugins are loaded and handled. The class is abstract and
026: * must be personalized as needed. The used implementation can be specified with
027: * the 'contineo.app.pluginregistry' system property.
028: *
029: * @author Marco Meschieri
030: * @version $Id$
031: * @since 3.0
032: */
033: public abstract class PluginRegistry {
034:
035: // System property containing the plugin registry implementation to be used
036: public static final String CONTINEO_APP_PLUGINREGISTRY = "contineo.app.pluginregistry";
037:
038: protected PluginManager manager = null;
039:
040: private static PluginRegistry instance;
041:
042: public static PluginRegistry getInstance() {
043: if (instance == null) {
044: String pluginregistry = System
045: .getProperty(CONTINEO_APP_PLUGINREGISTRY);
046: if (StringUtils.isEmpty(pluginregistry)) {
047: pluginregistry = "org.contineo.DefaultPluginRegistry";
048: }
049: try {
050: System.out
051: .println("Instantiate concrete PluginRegistry: "
052: + pluginregistry);
053: instance = (PluginRegistry) Class.forName(
054: pluginregistry).newInstance();
055: } catch (Exception e) {
056: e.printStackTrace();
057: }
058: }
059: return instance;
060: }
061:
062: /**
063: * Initializes all found plugins
064: */
065: public void init() {
066: ExtendedProperties properties = new ExtendedProperties();
067: properties.put("org.java.plugin.PathResolver",
068: "org.java.plugin.standard.ShadingPathResolver");
069: properties
070: .put(
071: "org.java.plugin.standard.ShadingPathResolver.shadowFolder",
072: System.getProperty("contineo.app.pluginsdir")
073: + "/../.plugins");
074: properties
075: .put(
076: "org.java.plugin.standard.ShadingPathResolver.unpackMode",
077: "always");
078:
079: ObjectFactory pluginObjectFactory = ObjectFactory
080: .newInstance(properties);
081: manager = pluginObjectFactory.createManager();
082:
083: List<PluginManager.PluginLocation> pluginLocations = locatePlugins(System
084: .getProperty("contineo.app.pluginsdir"));
085:
086: if (pluginLocations.size() > 0) {
087: Map<String, Identity> plugins = null;
088: try {
089: PluginManager.PluginLocation[] pLocations = (PluginManager.PluginLocation[]) pluginLocations
090: .toArray(new PluginManager.PluginLocation[0]);
091: plugins = manager.publishPlugins(pLocations);
092: } catch (JpfException e) {
093: throw new RuntimeException("Error publishing plugins",
094: e);
095: }
096:
097: System.out.println("Succesfully registered "
098: + plugins.size() + " plugins");
099: initPlugins(plugins);
100: }
101: }
102:
103: /**
104: * Initializes found plugins
105: *
106: * @param plugins Map of found plugins
107: */
108: protected abstract void initPlugins(Map<String, Identity> plugins);
109:
110: protected List<PluginManager.PluginLocation> locatePlugins(
111: String pluginsDirectoryPath) {
112:
113: List<PluginManager.PluginLocation> pluginLocations = new LinkedList<PluginManager.PluginLocation>();
114:
115: // look for all zip files in plugin directory
116: File pluginDirectory = new File(pluginsDirectoryPath);
117:
118: System.out.println("Searching for plugins in "
119: + pluginDirectory.getAbsolutePath());
120:
121: FilenameFilter filter = new FilenameFilter() {
122: public boolean accept(File dir, String name) {
123: return (name.endsWith(".zip"));
124: }
125: };
126:
127: if (pluginDirectory.isDirectory()) {
128:
129: // find the plugins
130: List<String> pluginsList = Arrays.asList(pluginDirectory
131: .list(filter));
132:
133: if (pluginsList.size() > 0) {
134:
135: Iterator<String> i = pluginsList.iterator();
136:
137: while (i.hasNext()) {
138:
139: String pluginFilename = (String) i.next();
140:
141: File pluginZIPFile = new File(pluginDirectory
142: .getPath()
143: + "/" + pluginFilename);
144:
145: if (!pluginZIPFile.exists())
146: throw new RuntimeException("file not Found:"
147: + pluginZIPFile.getAbsolutePath());
148:
149: try {
150:
151: final URL manifestURL = new URL("jar:file:"
152: + pluginZIPFile.getAbsolutePath()
153: + "!/plugin.xml");
154:
155: final URL contextURL = pluginZIPFile.toURL();
156:
157: System.out.println("Found plugin file: "
158: + pluginZIPFile.getName());
159:
160: pluginLocations
161: .add(new PluginManager.PluginLocation() {
162: public URL getManifestLocation() {
163: return manifestURL;
164: }
165:
166: public URL getContextLocation() {
167: return contextURL;
168: }
169: });
170: } catch (MalformedURLException e) {
171: e.printStackTrace();
172: }
173: }
174: } else {
175: System.out.println("No Plugins Found");
176: }
177:
178: } else {
179: throw new RuntimeException(
180: "Unable to access Plugins directory: "
181: + pluginDirectory.getAbsolutePath());
182: }
183: return pluginLocations;
184: }
185:
186: public PluginManager getManager() {
187: return manager;
188: }
189:
190: /**
191: * Returns the extensions connected to the specified extension point
192: *
193: * @param pluginId The plugin identifier
194: * @param extensionPoint The extension point id
195: * @return List of connected extensions
196: */
197: public Collection<Extension> getExtensions(String pluginId,
198: String extensionPoint) {
199: PluginRegistry registry = PluginRegistry.getInstance();
200: PluginDescriptor descriptor = registry.getManager()
201: .getRegistry().getPluginDescriptor(pluginId);
202: ExtensionPoint dbinitExtPoint = registry.getManager()
203: .getRegistry().getExtensionPoint(descriptor.getId(),
204: extensionPoint);
205: Collection<Extension> exts = dbinitExtPoint
206: .getConnectedExtensions();
207: return exts;
208: }
209:
210: /**
211: * Retrieves the list of registered plugins
212: *
213: * @return The list of registered plugins descriptors
214: */
215: public Collection<PluginDescriptor> getPlugins() {
216: PluginRegistry registry = PluginRegistry.getInstance();
217: return registry.getManager().getRegistry()
218: .getPluginDescriptors();
219: }
220:
221: /**
222: * Retrieve the plugin descriptor
223: *
224: * @return The plugin descriptor
225: */
226: public PluginDescriptor getPlugin(String pluginId) {
227: PluginRegistry registry = PluginRegistry.getInstance();
228: return registry.getManager().getRegistry().getPluginDescriptor(
229: pluginId);
230: }
231: }
|