KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.HashMap JavaDoc;
7 /**
8  * A <code>Extension</code> is a kind of listener descriptor that will be
9  * installed on a concret <code>ExtensionPoint</code> that act as kind of
10  * Publisher.
11  *
12  * @author joa23
13  */

14 public class Extension {
15   private PluginDescriptor fDescriptor;
16   private String JavaDoc fId;
17   private String JavaDoc fTargetPoint;
18   private String JavaDoc fClazz;
19   private HashMap JavaDoc fAttributes;
20   /**
21    * @param pDescriptor
22    * a plugin descriptor
23    * @param pExtensionPoint
24    * an extension porin
25    * @param pId
26    * an unique id of the plugin
27    */

28   public Extension(PluginDescriptor pDescriptor, String JavaDoc pExtensionPoint,
29                    String JavaDoc pId, String JavaDoc pExtensionClass) {
30     fAttributes = new HashMap JavaDoc();
31     setDiscriptor(pDescriptor);
32     setExtensionPoint(pExtensionPoint);
33     setId(pId);
34     setClazz(pExtensionClass);
35   }
36   /**
37    * @param point
38    */

39   private void setExtensionPoint(String JavaDoc point) {
40     fTargetPoint = point;
41   }
42   /**
43    * Returns a attribute value, that is setuped in the manifest file and is
44    * definied by the extension point xml schema.
45    *
46    * @param pKey
47    * a key
48    * @return String a value
49    */

50   public String JavaDoc getAttribute(String JavaDoc pKey) {
51     return (String JavaDoc) fAttributes.get(pKey);
52   }
53   /**
54    * Returns the full class name of the extension point implementation
55    *
56    * @return String
57    */

58   public String JavaDoc getClazz() {
59     return fClazz;
60   }
61   /**
62    * Return the unique id of the extension.
63    *
64    * @return String
65    */

66   public String JavaDoc getId() {
67     return fId;
68   }
69   /**
70    * Adds a attribute and is only used until model creation at plugin system
71    * start up.
72    *
73    * @param pKey
74    * a key
75    * @param pValue
76    * a value
77    */

78   public void addAttribute(String JavaDoc pKey, String JavaDoc pValue) {
79     fAttributes.put(pKey, pValue);
80   }
81   /**
82    * Sets the Class that implement the concret extension and is only used
83    * until model creation at system start up.
84    *
85    * @param extensionClazz
86    * The extensionClazz to set
87    */

88   public void setClazz(String JavaDoc extensionClazz) {
89     fClazz = extensionClazz;
90   }
91   /**
92    * Sets the unique extension Id and is only used until model creation at
93    * system start up.
94    *
95    * @param extensionID
96    * The extensionID to set
97    */

98   public void setId(String JavaDoc extensionID) {
99     fId = extensionID;
100   }
101   /**
102    * Returns the Id of the extension point, that is implemented by this
103    * extension.
104    */

105   public String JavaDoc getTargetPoint() {
106     return fTargetPoint;
107   }
108   /**
109    * Return an instance of the extension implementatio. Before we create a
110    * extension instance we startup the plugin if it is not already done. The
111    * plugin instance and the extension instance use the same
112    * <code>PluginClassLoader</code>. Each Plugin use its own classloader.
113    * The PluginClassLoader knows only own <i>Plugin runtime libraries </i>
114    * setuped in the plugin manifest file and exported libraries of the
115    * depenedend plugins.
116    *
117    * @return Object An instance of the extension implementation
118    */

119   public Object JavaDoc getExtensionInstance() throws PluginRuntimeException {
120     // Must synchronize here to make sure creation and initialization
121
// of a plugin instance and it extension instance are done by
122
// one and only one thread.
123
// The same is in PluginRepository.getPluginInstance().
124
// Suggested by Stefan Groschupf <sg@media-style.com>
125
synchronized (getId()) {
126       try {
127         PluginClassLoader loader = fDescriptor.getClassLoader();
128         Class JavaDoc extensionClazz = loader.loadClass(getClazz());
129         // lazy loading of Plugin in case there is no instance of the plugin
130
// already.
131
PluginRepository.getInstance().getPluginInstance(getDiscriptor());
132         Object JavaDoc object = extensionClazz.newInstance();
133         return object;
134       } catch (ClassNotFoundException JavaDoc e) {
135         throw new PluginRuntimeException(e);
136       } catch (InstantiationException JavaDoc e) {
137         throw new PluginRuntimeException(e);
138       } catch (IllegalAccessException JavaDoc e) {
139         throw new PluginRuntimeException(e);
140       }
141     }
142   }
143   /**
144    * return the plugin descriptor.
145    *
146    * @return PluginDescriptor
147    */

148   public PluginDescriptor getDiscriptor() {
149     return fDescriptor;
150   }
151   /**
152    * Sets the plugin descriptor and is only used until model creation at system
153    * start up.
154    *
155    * @return PluginDescriptor
156    */

157   public void setDiscriptor(PluginDescriptor pDescriptor) {
158     fDescriptor = pDescriptor;
159   }
160 }
161
Popular Tags