KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > nutch > plugin > PluginDescriptor


1 /*
2  * Copyright (c) 2003 The Nutch Organization. All rights reserved. Use subject
3  * to the conditions in http://www.nutch.org/LICENSE.txt.
4  */

5 package net.nutch.plugin;
6 import java.io.File JavaDoc;
7 import java.io.IOException JavaDoc;
8 import java.net.MalformedURLException JavaDoc;
9 import java.net.URL JavaDoc;
10 import java.util.ArrayList JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.Locale JavaDoc;
13 import java.util.MissingResourceException JavaDoc;
14 import java.util.ResourceBundle JavaDoc;
15 import java.util.logging.Logger JavaDoc;
16 import net.nutch.util.LogFormatter;
17 /**
18  * The <code>PluginDescriptor</code> provide access to all meta information of
19  * a nutch-plugin, as well to the internationalizable resources and the plugin
20  * own classloader. There are meta information about <code>Plugin</code>,
21  * <code>ExtensionPoint</code> and <code>Extension</code>. To provide
22  * access to the meta data of a plugin via a descriptor allow a lazy loading
23  * mechanism.
24  *
25  * @author joa23
26  */

27 public class PluginDescriptor {
28   private String JavaDoc fPluginPath;
29   private String JavaDoc fPluginClass = Plugin.class.getName();
30   private String JavaDoc fPluginId;
31   private String JavaDoc fVersion;
32   private String JavaDoc fName;
33   private String JavaDoc fProviderName;
34   private HashMap JavaDoc fMessages = new HashMap JavaDoc();
35   private ArrayList JavaDoc fExtensionPoints = new ArrayList JavaDoc();
36   private ArrayList JavaDoc fDependencies = new ArrayList JavaDoc();
37   private ArrayList JavaDoc fExportedLibs = new ArrayList JavaDoc();
38   private ArrayList JavaDoc fNotExportedLibs = new ArrayList JavaDoc();
39   private ArrayList JavaDoc fExtensions = new ArrayList JavaDoc();
40   private PluginClassLoader fClassLoader;
41   public static final Logger JavaDoc LOG = LogFormatter
42     .getLogger(PluginDescriptor.class.getName());
43   /**
44    * Constructor
45    *
46    * @param pId
47    * @param pVersion
48    * @param pName
49    * @param pProviderName
50    * @param pPluginclazz
51    * @param pPath
52    */

53   public PluginDescriptor(String JavaDoc pId, String JavaDoc pVersion, String JavaDoc pName,
54                           String JavaDoc pProviderName, String JavaDoc pPluginclazz, String JavaDoc pPath) {
55     setPath(pPath);
56     setPluginId(pId);
57     setVersion(pVersion);
58     setName(pName);
59     setProvidername(pProviderName);
60
61     if (pPluginclazz != null)
62       setPluginClass(pPluginclazz);
63
64   }
65   /**
66    * @param pPath
67    */

68   private void setPath(String JavaDoc pPath) {
69     fPluginPath = pPath;
70   }
71   /**
72    * Returns the name of the plugin.
73    *
74    * @return String
75    */

76   public String JavaDoc getName() {
77     return fName;
78   }
79   /**
80    * @param providerName
81    */

82   private void setProvidername(String JavaDoc providerName) {
83     fProviderName = providerName;
84   }
85   /**
86    * @param name
87    */

88   private void setName(String JavaDoc name) {
89     fName = name;
90   }
91   /**
92    * @param version
93    */

94   private void setVersion(String JavaDoc version) {
95     fVersion = version;
96   }
97   /**
98    * Returns the fully qualified name of the class which implements the
99    * abstarct <code>Plugin</code> class.
100    *
101    * @return the name of this plug-in's runtime class or <code>null</code>.
102    */

103   public String JavaDoc getPluginClass() {
104     return fPluginClass;
105   }
106   /**
107    * Returns the unique identifier of the plug-in or <code>null</code>.
108    *
109    * @return String
110    */

111   public String JavaDoc getPluginId() {
112     return fPluginId;
113   }
114   /**
115    * Returns an array of extensions.
116    *
117    * @return Exception[]
118    */

119   public Extension[] getExtensions() {
120     return (Extension[]) fExtensions.toArray(new Extension[fExtensions
121                                                            .size()]);
122   }
123   /**
124    * Adds a extension.
125    *
126    * @param pExtension
127    */

128   public void addExtension(Extension pExtension) {
129     fExtensions.add(pExtension);
130   }
131   /**
132    * Sets the pluginClass.
133    *
134    * @param pluginClass
135    * The pluginClass to set
136    */

137   private void setPluginClass(String JavaDoc pluginClass) {
138     fPluginClass = pluginClass;
139   }
140   /**
141    * Sets the plugin Id.
142    *
143    * @param pluginId
144    * The pluginId to set
145    */

146   private void setPluginId(String JavaDoc pluginId) {
147     fPluginId = pluginId;
148   }
149   /**
150    * Adds a extension point.
151    *
152    * @param extensionPoint
153    */

154   public void addExtensionPoint(ExtensionPoint extensionPoint) {
155     fExtensionPoints.add(extensionPoint);
156   }
157   /**
158    * Returns a array of extension points.
159    *
160    * @return ExtensionPoint[]
161    */

162   public ExtensionPoint[] getExtenstionPoints() {
163     return (ExtensionPoint[]) fExtensionPoints
164       .toArray(new ExtensionPoint[fExtensionPoints.size()]);
165   }
166   /**
167    * Returns a array of plugin ids.
168    *
169    * @return String[]
170    */

171   public String JavaDoc[] getDependencies() {
172     return (String JavaDoc[]) fDependencies
173       .toArray(new String JavaDoc[fDependencies.size()]);
174   }
175   /**
176    * Adds a dependency
177    *
178    * @param pId
179    * id of the dependent plugin
180    */

181   public void addDependency(String JavaDoc pId) {
182     fDependencies.add(pId);
183   }
184   /**
185    * Adds a exported library with a relative path to the plugin directory.
186    *
187    * @param plibPath
188    */

189   public void addExportedLibRelative(String JavaDoc pLibPath)
190     throws MalformedURLException JavaDoc {
191     URL JavaDoc url = new File JavaDoc(getPluginPath() + File.separator + pLibPath).toURL();
192     fExportedLibs.add(url);
193   }
194   /**
195    * Returns the directory path of the plugin.
196    *
197    * @return String
198    */

199   public String JavaDoc getPluginPath() {
200     return fPluginPath;
201   }
202   /**
203    * Returns a array exported librareis as URLs
204    *
205    * @return URL[]
206    */

207   public URL JavaDoc[] getExportedLibUrls() {
208     return (URL JavaDoc[]) fExportedLibs.toArray(new URL JavaDoc[0]);
209   }
210   /**
211    * Adds a not exported library with a plugin directory relativ path.
212    *
213    * @param pLibPath
214    */

215   public void addNotExportedLibRelative(String JavaDoc pLibPath)
216     throws MalformedURLException JavaDoc {
217     URL JavaDoc url = new File JavaDoc(getPluginPath() + File.separator + pLibPath).toURL();
218     fNotExportedLibs.add(url);
219   }
220   /**
221    * Returns a array of libraries as URLs that are not exported by the plugin.
222    *
223    * @return URL[]
224    */

225   public URL JavaDoc[] getNotExportedLibUrls() {
226     return (URL JavaDoc[]) fNotExportedLibs
227       .toArray(new URL JavaDoc[fNotExportedLibs.size()]);
228   }
229   /**
230    * Returns a cached classloader for a plugin. Until classloader creation all
231    * needed libraries are collected. A classloader use as first the plugins
232    * own libraries and add then all exported libraries of dependend plugins.
233    *
234    * @return PluginClassLoader the classloader for the plugin
235    */

236   public PluginClassLoader getClassLoader() {
237     if (fClassLoader != null)
238       return fClassLoader;
239     ArrayList JavaDoc arrayList = new ArrayList JavaDoc();
240     arrayList.addAll(fExportedLibs);
241     arrayList.addAll(fNotExportedLibs);
242     arrayList.addAll(getDependencyLibs());
243     File JavaDoc file = new File JavaDoc(getPluginPath());
244     File JavaDoc[] files = file.listFiles();
245     try {
246       for (int i = 0; i < files.length; i++) {
247         File JavaDoc file2 = files[i];
248         String JavaDoc path = file2.getAbsolutePath();
249         if (file2.getAbsolutePath().endsWith("properties"))
250           arrayList.add(file2.getParentFile().toURL());
251       }
252     } catch (MalformedURLException JavaDoc e) {
253       LOG.fine(getPluginId() + " " + e.toString());
254     }
255     URL JavaDoc[] urls = (URL JavaDoc[]) arrayList.toArray(new URL JavaDoc[arrayList.size()]);
256     fClassLoader = new PluginClassLoader(urls, PluginDescriptor.class
257                                          .getClassLoader());
258     return fClassLoader;
259   }
260   /**
261    * @return Collection
262    */

263   private ArrayList JavaDoc getDependencyLibs() {
264     ArrayList JavaDoc list = new ArrayList JavaDoc();
265     collectLibs(list, this);
266     return list;
267   }
268   /**
269    * @param list
270    */

271   private void collectLibs(ArrayList JavaDoc pLibs, PluginDescriptor pDescriptor) {
272     String JavaDoc[] pPluginIds = pDescriptor.getDependencies();
273     for (int i = 0; i < pPluginIds.length; i++) {
274       String JavaDoc id = pPluginIds[i];
275       PluginDescriptor descriptor = PluginRepository.getInstance()
276         .getPluginDescriptor(id);
277       URL JavaDoc[] libs = descriptor.getExportedLibUrls();
278       for (int j = 0; j < libs.length; j++) {
279         URL JavaDoc url = libs[j];
280         pLibs.add(url);
281       }
282       collectLibs(pLibs, descriptor);
283     }
284   }
285   /**
286    * Returns a internationalizabel resource string. The resource bundles could
287    * be stored in root directory of a plugin in the well know i18n file name
288    * conventions.
289    *
290    * @param pKey
291    * @param pLocale
292    * @return String
293    * @throws IOException
294    */

295   public String JavaDoc getResourceString(String JavaDoc pKey, Locale JavaDoc pLocale)
296     throws IOException JavaDoc {
297     if (fMessages.containsKey(pLocale.toString())) {
298       ResourceBundle JavaDoc bundle = (ResourceBundle JavaDoc) fMessages.get(pLocale
299                                                              .toString());
300       try {
301         return bundle.getString(pKey);
302       } catch (MissingResourceException JavaDoc e) {
303         return '!' + pKey + '!';
304       }
305     }
306     try {
307       ResourceBundle JavaDoc res = ResourceBundle.getBundle("messages", pLocale,
308                                                     getClassLoader());
309       return res.getString(pKey);
310     } catch (MissingResourceException JavaDoc x) {
311       return '!' + pKey + '!';
312     }
313   }
314 }
315
Popular Tags