001: /**********************************************************************
002: Copyright (c) 2006 Erik Bengtson and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015:
016: Contributors:
017: 2006 Thomas Marti - Added support for configurable plugin file names
018: ...
019: **********************************************************************/package org.jpox.plugin;
020:
021: import java.io.IOException;
022: import java.lang.reflect.InvocationTargetException;
023: import java.net.URL;
024: import java.util.HashSet;
025:
026: import org.jpox.ClassLoaderResolver;
027: import org.jpox.PersistenceConfiguration;
028: import org.jpox.exceptions.ClassNotResolvedException;
029:
030: /**
031: * Manages the registry of Extensions and Extension Points.
032: * @version $Revision: 1.35 $
033: */
034: public class PluginManager {
035: /** plugin registry **/
036: private PluginRegistry registry;
037:
038: /**
039: * Constructor
040: * @param conf the PMF Configuration
041: * @param clr the ClassLoaderResolver
042: */
043: public PluginManager(PersistenceConfiguration conf,
044: ClassLoaderResolver clr) {
045: registry = PluginRegistryFactory.newPluginRegistry(conf, clr);
046: }
047:
048: /**
049: * Accessor for the PluginRegistry class name.
050: * @return Name of the plugin registry
051: */
052: public String getRegistryClassName() {
053: return registry.getClass().getName();
054: }
055:
056: /**
057: * Acessor for the ExtensionPoint
058: * @param id the unique id of the extension point
059: * @return null if the ExtensionPoint is not registered
060: */
061: public ExtensionPoint getExtensionPoint(String id) {
062: return registry.getExtensionPoint(id);
063: }
064:
065: /**
066: * Acessor for the currently registed ExtensionPoints
067: * @return array of ExtensionPoints
068: */
069: public ExtensionPoint[] getExtensionPoints() {
070: return registry.getExtensionPoints();
071: }
072:
073: /**
074: * Register Extension Points declared in /org/jpox/plugin/plugin.xml
075: */
076: public void registerExtensionPoints() {
077: registry.registerExtensionPoints();
078: }
079:
080: /**
081: * Register ExtensionPoints and Extensions declared in plugin files
082: */
083: public void registerExtensions() {
084: registry.registerExtensions();
085: }
086:
087: /**
088: * Resolve constraints declared in bundle files.
089: * This must be invoked after registering all bundles.
090: * Should log errors if bundles are not resolvable, or raise runtime exceptions.
091: */
092: public void resolveConstraints() {
093: registry.resolveConstraints();
094: }
095:
096: /**
097: * Convenience accessor for getting the (first) ConfigurationElement for an extension (of an extension point).
098: * @param extensionPointName The extension point
099: * @param discrimAttrName Attribute on the extension to use as discriminator
100: * @param discrimAttrValue Value for discriminator attribute
101: * @return The value of the attribute
102: */
103: public ConfigurationElement getConfigurationElementForExtension(
104: String extensionPointName, String discrimAttrName,
105: String discrimAttrValue) {
106: ExtensionPoint extensionPoint = getExtensionPoint(extensionPointName);
107: if (extensionPoint != null) {
108: Extension[] ex = extensionPoint.getExtensions();
109: for (int i = 0; i < ex.length; i++) {
110: ConfigurationElement[] confElm = ex[i]
111: .getConfigurationElements();
112: for (int j = 0; j < confElm.length; j++) {
113: // Find an extension with this discriminator value
114: if (confElm[j].getAttribute(discrimAttrName)
115: .equalsIgnoreCase(discrimAttrValue)) {
116: return confElm[j];
117: }
118: }
119: }
120: }
121: return null;
122: }
123:
124: /**
125: * Convenience accessor for getting the ConfigurationElement(s) for an extension (of an extension point).
126: * @param extensionPointName The extension point
127: * @param discrimAttrName Attribute on the extension to use as discriminator
128: * @param discrimAttrValue Value for discriminator attribute
129: * @return The value of the attribute
130: */
131: public ConfigurationElement[] getConfigurationElementsForExtension(
132: String extensionPointName, String discrimAttrName,
133: String discrimAttrValue) {
134: ExtensionPoint extensionPoint = getExtensionPoint(extensionPointName);
135: HashSet elems = new HashSet();
136: if (extensionPoint != null) {
137: Extension[] ex = extensionPoint.getExtensions();
138: for (int i = 0; i < ex.length; i++) {
139: ConfigurationElement[] confElm = ex[i]
140: .getConfigurationElements();
141: for (int j = 0; j < confElm.length; j++) {
142: if (discrimAttrName == null) {
143: // No discriminator set so take all values
144: elems.add(confElm[j]);
145: } else if (confElm[j].getAttribute(discrimAttrName)
146: .equalsIgnoreCase(discrimAttrValue)) {
147: // Extension with this discriminator value
148: elems.add(confElm[j]);
149: }
150: }
151: }
152: }
153: if (elems.size() > 0) {
154: return (ConfigurationElement[]) elems
155: .toArray(new ConfigurationElement[elems.size()]);
156: }
157: return null;
158: }
159:
160: /**
161: * Convenience accessor for getting the ConfigurationElement for an extension (of an extension point).
162: * @param extensionPointName The extension point
163: * @param discrimAttrName Attribute on the extension to use as discriminator1
164: * @param discrimAttrValue Value for discriminator1 attribute
165: * @return The value of the attribute
166: */
167: public ConfigurationElement getConfigurationElementForExtension(
168: String extensionPointName, String[] discrimAttrName,
169: String[] discrimAttrValue) {
170: ExtensionPoint extensionPoint = getExtensionPoint(extensionPointName);
171: if (extensionPoint != null) {
172: Extension[] ex = extensionPoint.getExtensions();
173: for (int i = 0; i < ex.length; i++) {
174: ConfigurationElement[] confElm = ex[i]
175: .getConfigurationElements();
176: for (int j = 0; j < confElm.length; j++) {
177: // Find an extension with this discriminator value
178: boolean equals = true;
179: for (int k = 0; k < discrimAttrName.length; k++) {
180: if (!confElm[j]
181: .getAttribute(discrimAttrName[k])
182: .equalsIgnoreCase(discrimAttrValue[k])) {
183: equals = false;
184: break;
185: }
186: }
187: if (equals) {
188: return confElm[j];
189: }
190: }
191: }
192: }
193: return null;
194: }
195:
196: /**
197: * Convenience accessor for getting the value of an attribute for an extension (of an extension point).
198: * @param extensionPoint The extension point
199: * @param discrimAttrName Attribute on the extension to use as discriminator
200: * @param discrimAttrValue Value for discriminator attribute
201: * @param attributeName Name of the attribute whose value we want
202: * @return The value of the attribute
203: */
204: public String getAttributeValueForExtension(String extensionPoint,
205: String discrimAttrName, String discrimAttrValue,
206: String attributeName) {
207: ConfigurationElement elem = getConfigurationElementForExtension(
208: extensionPoint, discrimAttrName, discrimAttrValue);
209: if (elem != null) {
210: return elem.getAttribute(attributeName);
211: }
212: return null;
213: }
214:
215: /**
216: * Convenience accessor for getting the value of an attribute for an extension (of an extension point).
217: * @param extensionPoint The extension point
218: * @param discrimAttrName Attribute on the extension to use as discriminator
219: * @param discrimAttrValue Value for discriminator attribute
220: * @param attributeName Name of the attribute whose value we want
221: * @return The value(s) of the attribute
222: */
223: public String[] getAttributeValuesForExtension(
224: String extensionPoint, String discrimAttrName,
225: String discrimAttrValue, String attributeName) {
226: ConfigurationElement[] elems = getConfigurationElementsForExtension(
227: extensionPoint, discrimAttrName, discrimAttrValue);
228: if (elems != null) {
229: String[] attrValues = new String[elems.length];
230: for (int i = 0; i < elems.length; i++) {
231: attrValues[i] = elems[i].getAttribute(attributeName);
232: }
233: return attrValues;
234: }
235: return null;
236: }
237:
238: /**
239: * Convenience accessor for getting the value of an attribute for an extension (of an extension point).
240: * @param extensionPoint The extension point
241: * @param discrimAttrName Attribute on the extension to use as discriminator1
242: * @param discrimAttrValue Value for discriminator1 attribute
243: * @param attributeName Name of the attribute whose value we want
244: * @return The value of the attribute
245: */
246: public String getAttributeValueForExtension(String extensionPoint,
247: String[] discrimAttrName, String[] discrimAttrValue,
248: String attributeName) {
249: ConfigurationElement elem = getConfigurationElementForExtension(
250: extensionPoint, discrimAttrName, discrimAttrValue);
251: if (elem != null) {
252: return elem.getAttribute(attributeName);
253: }
254: return null;
255: }
256:
257: /**
258: * Convenience accessor for getting the Class of an attribute for an extension (of an extension point).
259: * @param extensionPoint The extension point
260: * @param discrimAttrName Attribute on the extension to use as discriminator
261: * @param discrimAttrValue Value for discriminator attribute
262: * @param attributeName Name of the attribute whose value we want
263: * @return The value of the attribute
264: */
265: public Object createExecutableExtension(String extensionPoint,
266: String discrimAttrName, String discrimAttrValue,
267: String attributeName, Class[] argsClass, Object[] args)
268: throws ClassNotFoundException, SecurityException,
269: NoSuchMethodException, IllegalArgumentException,
270: InstantiationException, IllegalAccessException,
271: InvocationTargetException {
272: ConfigurationElement elem = getConfigurationElementForExtension(
273: extensionPoint, discrimAttrName, discrimAttrValue);
274: if (elem != null) {
275: return registry.createExecutableExtension(elem,
276: attributeName, argsClass, args);
277: }
278: return null;
279: }
280:
281: /**
282: * Convenience accessor for getting the Class of an attribute for an extension (of an extension point).
283: * @param extensionPoint The extension point
284: * @param discrimAttrName First attribute on the extension to use as discriminator
285: * @param discrimAttrValue Value for first discriminator attribute
286: * @param attributeName Name of the attribute whose value we want
287: * @return The value of the attribute
288: */
289: public Object createExecutableExtension(String extensionPoint,
290: String[] discrimAttrName, String[] discrimAttrValue,
291: String attributeName, Class[] argsClass, Object[] args)
292: throws ClassNotFoundException, SecurityException,
293: NoSuchMethodException, IllegalArgumentException,
294: InstantiationException, IllegalAccessException,
295: InvocationTargetException {
296: ConfigurationElement elem = getConfigurationElementForExtension(
297: extensionPoint, discrimAttrName, discrimAttrValue);
298: if (elem != null) {
299: return registry.createExecutableExtension(elem,
300: attributeName, argsClass, args);
301: }
302: return null;
303: }
304:
305: /**
306: * Loads a class (do not initialize)
307: * @param pluginId the plugin id
308: * @param className the class name
309: * @return the Class
310: * @throws ClassNotResolvedException
311: */
312: public Class loadClass(String pluginId, String className)
313: throws ClassNotResolvedException {
314: try {
315: return registry.loadClass(pluginId, className);
316: } catch (ClassNotFoundException ex) {
317: throw new ClassNotResolvedException(ex.getMessage(), ex);
318: }
319: }
320:
321: /**
322: * Converts a URL that uses a user-defined protocol into a URL that uses the file protocol.
323: * @param url the url to be converted
324: * @return the converted URL
325: * @throws IOException
326: */
327: public URL resolveURLAsFileURL(URL url) throws IOException {
328: return registry.resolveURLAsFileURL(url);
329: }
330: }
|