KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > scenario > util > isac > plugin > PluginDescription


1 /*
2  * CLIF is a Load Injection Framework
3  * Copyright (C) 2004 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * CLIF
20  *
21  * Contact: clif@objectweb.org
22  */

23 package org.objectweb.clif.scenario.util.isac.plugin;
24
25 import java.io.FileInputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.Hashtable JavaDoc;
30 import java.util.Properties JavaDoc;
31 import java.util.Vector JavaDoc;
32
33 import javax.xml.parsers.SAXParser JavaDoc;
34 import javax.xml.parsers.SAXParserFactory JavaDoc;
35
36 import org.apache.log4j.Category;
37 import org.objectweb.clif.scenario.util.isac.FileName;
38 import org.objectweb.clif.scenario.util.isac.plugin.gui.PluginGUIManager;
39 import org.objectweb.clif.scenario.util.isac.plugin.parser.AnalyseSaxPlugin;
40 import org.objectweb.clif.scenario.util.isac.util.tree.Node;
41 import org.objectweb.clif.scenario.util.isac.util.tree.NodeDescription;
42 import org.objectweb.clif.scenario.util.isac.util.xml.IsacEntityResolver;
43 import org.xml.sax.InputSource JavaDoc;
44 import org.xml.sax.XMLReader JavaDoc;
45 /**
46  * Implementation of an object which stre the plugin description
47  *
48  * @author JC Meillaud
49  * @author A Peyrard
50  */

51 public class PluginDescription {
52     static Category cat = Category.getInstance(PluginDescription.class
53             .getName());
54     private String JavaDoc name;
55     private Hashtable JavaDoc samples;
56     private Hashtable JavaDoc tests;
57     private Hashtable JavaDoc timers;
58     private ObjectDescription object;
59     private Vector JavaDoc help;
60
61     /**
62      * constructor, use the 'loadPluginDescription' method to build an instance
63      * of the object which is contains in a XML file
64      *
65      * @param name
66      * The jar file name
67      * @param s
68      * @param tests
69      * @param t
70      * @param o
71      * @param h
72      */

73     public PluginDescription(String JavaDoc name, Hashtable JavaDoc s,
74             Hashtable JavaDoc tests, Hashtable JavaDoc t, ObjectDescription o, Vector JavaDoc h) {
75         cat.debug("-> constructor");
76         this.name = name;
77         this.samples = s;
78         this.tests = tests;
79         this.timers = t;
80         this.object = o;
81         this.help = h;
82     }
83
84     /**
85      * Create the nodes descriptions of each actions defined in this plugin of
86      * the given type
87      *
88      * @return The nodes descriptions
89      */

90     public Vector JavaDoc createNodesDescriptions(String JavaDoc type) {
91         cat.debug("-> createNodesDescriptions");
92         Vector JavaDoc result = new Vector JavaDoc();
93         if (type.equals(Node.SAMPLE)) {
94             Enumeration JavaDoc e = this.samples.elements();
95             while (e.hasMoreElements()) {
96                 NodeDescription temp = new NodeDescription(Node.SAMPLE);
97                 temp.setPlugin(this.name);
98                 SampleDescription tempSample = (SampleDescription) e
99                         .nextElement();
100                 tempSample.createNodeDescription(temp);
101                 result.add(temp);
102             }
103             return result;
104         }
105         if (type.equals(Node.TIMER)) {
106             Enumeration JavaDoc e = this.timers.elements();
107             while (e.hasMoreElements()) {
108                 NodeDescription temp = new NodeDescription(Node.TIMER);
109                 temp.setPlugin(this.name);
110                 ((TimerDescription) e.nextElement())
111                         .createNodeDescription(temp);
112                 result.add(temp);
113             }
114             return result;
115         }
116         if (type.equals(Node.TEST)) {
117             Enumeration JavaDoc e = this.tests.elements();
118             while (e.hasMoreElements()) {
119                 NodeDescription temp = new NodeDescription(Node.TEST);
120                 temp.setPlugin(this.name);
121                 ((TestDescription) e.nextElement()).createNodeDescription(temp);
122                 result.add(temp);
123             }
124             return result;
125         }
126         if (type.equals(Node.USE)) {
127             if (object != null) {
128                 NodeDescription temp = new NodeDescription(Node.USE);
129                 temp.setPlugin(this.name);
130                 object.createNodeDescription(temp);
131                 result.add(temp);
132             }
133             return result;
134         }
135         return null;
136     }
137
138     /**
139      * Create a node description of a selected action descripted by it type and
140      * name
141      *
142      * @param type
143      * The type of the action
144      * @param name
145      * The name of the action
146      * @return The node desciption for this action
147      */

148     public NodeDescription createNodeDescription(String JavaDoc type, String JavaDoc name) {
149         cat.debug("createNodeDescription");
150         // enumerate each type which could be
151
if (type.equals(Node.USE)) {
152             NodeDescription node = new NodeDescription(Node.USE);
153             node.setPlugin(this.name);
154             this.object.createNodeDescription(node);
155             return node;
156         }
157         if (type.equals(Node.TIMER)) {
158             NodeDescription node = new NodeDescription(Node.TIMER);
159             node.setPlugin(this.name);
160             if (this.timers.containsKey(name)) {
161                 TimerDescription timer = (TimerDescription) this.timers
162                         .get(name);
163                 timer.createNodeDescription(node);
164                 return node;
165             } else
166                 return null;
167         }
168         if (type.equals(Node.SAMPLE)) {
169             NodeDescription node = new NodeDescription(Node.SAMPLE);
170             node.setPlugin(this.name);
171             if (this.samples.containsKey(name)) {
172                 SampleDescription sample = (SampleDescription) this.samples
173                         .get(name);
174                 sample.createNodeDescription(node);
175                 return node;
176             } else
177                 return null;
178         }
179         // for controler type, we create a node description with test
180
// description but the type of the node
181
// is the type of the controler
182
if (Node.isControllerNode(type)) {
183             NodeDescription node = new NodeDescription(type);
184             node.setPlugin(this.name);
185             if (this.tests.containsKey(name)) {
186                 TestDescription test = (TestDescription) this.tests.get(name);
187                 test.createNodeDescription(node);
188                 return node;
189             } else
190                 return null;
191         }
192         return null;
193     }
194
195     /**
196      * Load a plugin description from a properties file which has it path
197      * specified in parameter
198      *
199      * @param dirName
200      * The directory name of the plugin
201      * @return A PluginDescription object
202      */

203     public static PluginDescription loadPluginDescription(String JavaDoc dirName, ClassLoader JavaDoc cl) {
204         cat.debug("-> loadPluginDescription");
205         // load the plugin properties
206
Properties JavaDoc properties = new Properties JavaDoc();
207         try {
208             InputStream JavaDoc is = new FileInputStream JavaDoc(dirName
209                     + FileName.PLUGIN_PROPERTIES_FILE);
210             properties.load(is);
211             is.close();
212         } catch (IOException JavaDoc ioe) {
213             cat.warn("Unable to analyse property file : " + dirName
214                     + "plugin.properties");
215         }
216         // load the properties defined in the file
217
String JavaDoc pluginXMLFile = properties.getProperty("plugin.xmlFile");
218         String JavaDoc guiXMLFile = properties.getProperty("plugin.guiFile");
219         String JavaDoc pluginName = properties.getProperty("plugin.name");
220
221         // open the xml definiton file
222
AnalyseSaxPlugin handler = new AnalyseSaxPlugin();
223         try {
224             InputSource JavaDoc is = new InputSource JavaDoc(new FileInputStream JavaDoc(dirName
225                     + pluginXMLFile));
226             SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
227             factory.setValidating(true);
228             SAXParser JavaDoc saxParser = factory.newSAXParser();
229             XMLReader JavaDoc reader = saxParser.getXMLReader();
230             reader.setContentHandler(handler);
231             reader.setErrorHandler(handler);
232             reader.setEntityResolver(new IsacEntityResolver(cl)) ;
233             reader.parse(is);
234         } catch (Exception JavaDoc e) {
235             cat.warn(" ---> Parser error : " + dirName + pluginXMLFile);
236             e.printStackTrace(System.out);
237             return null;
238         }
239         PluginDescription tempDesc = handler.getPluginDescription();
240         if ((guiXMLFile != null) && (!guiXMLFile.equals("null")))
241             (PluginGUIManager.getPluginGUIManager(null)).createPanels(tempDesc,
242                     dirName + guiXMLFile);
243         else
244             (PluginGUIManager.getPluginGUIManager(null)).createPanels(tempDesc,
245                     null);
246
247         return tempDesc;
248     }
249
250     /**
251      * Attribute name getter
252      *
253      * @return The name of the plugin
254      */

255     public String JavaDoc getName() {
256         cat.debug("-> getName");
257         return name;
258     }
259
260     /**
261      * This method return the action help of the action whose name is given in
262      * parameter
263      *
264      * @param type
265      * The type of the action
266      * @param action
267      * The name of the action
268      * @return The help lines of the action
269      */

270     public Vector JavaDoc getActionHelp(String JavaDoc type, String JavaDoc action) {
271         cat.debug("-> getActionHelp");
272         if (type.equals(Node.USE))
273             return this.object.getHelp();
274         if (type.equals(Node.SAMPLE)) {
275             if (this.samples.containsKey(action)) {
276                 SampleDescription temp = (SampleDescription) this.samples
277                         .get(action);
278                 return temp.getHelp();
279             } else
280                 return null;
281         }
282         if (type.equals(Node.TIMER)) {
283             if (this.timers.containsKey(action)) {
284                 TimerDescription temp = (TimerDescription) this.timers
285                         .get(action);
286                 return temp.getHelp();
287             } else
288                 return null;
289         }
290         if (type.equals(Node.TEST)) {
291             if (this.tests.containsKey(action)) {
292                 TestDescription temp = (TestDescription) this.tests.get(action);
293                 return temp.getHelp();
294             } else
295                 return null;
296         }
297         return null;
298     }
299
300     /**
301      * Set the guiKey in the right action description
302      *
303      * @param key
304      * The key to be set
305      * @param type
306      * The type of the action
307      * @param name
308      * The name of the action
309      */

310     public void setActionGUIKey(String JavaDoc key, String JavaDoc type, String JavaDoc name) {
311         cat.debug("-> setActionGUIKey");
312     }
313
314     /**
315      * Get the gui key of a specified action
316      *
317      * @param type
318      * The type of the action
319      * @param actionName
320      * The action name
321      * @return The gui key
322      */

323     public String JavaDoc getActionGUIKey(String JavaDoc type, String JavaDoc actionName) {
324         cat.debug("-> getActionGUIKey");
325         if (type.equals(Node.USE)) {
326             return this.object.getGUIKey();
327         }
328         if (type.equals(Node.SAMPLE)) {
329             if (this.samples.containsKey(actionName)) {
330                 SampleDescription sample = (SampleDescription) this.samples
331                         .get(actionName);
332                 return sample.getGUIKey();
333             }
334             return null;
335         }
336         if (type.equals(Node.TIMER)) {
337             if (this.timers.containsKey(actionName)) {
338                 TimerDescription timer = (TimerDescription) this.timers
339                         .get(actionName);
340                 return timer.getGUIKey();
341             }
342             return null;
343         }
344         if (type.equals(Node.TEST)) {
345             if (this.tests.containsKey(actionName)) {
346                 TestDescription test = (TestDescription) this.tests
347                         .get(actionName);
348                 return test.getGUIKey();
349             }
350             return null;
351         }
352         return null;
353     }
354     /**
355      * @return Returns the object.
356      */

357     public ObjectDescription getObject() {
358         cat.debug("-> getObject");
359         return object;
360     }
361     /**
362      * @return Returns the samples.
363      */

364     public Hashtable JavaDoc getSamples() {
365         cat.debug("-> getSamples");
366         return samples;
367     }
368     /**
369      * @return Returns the tests.
370      */

371     public Hashtable JavaDoc getTests() {
372         cat.debug("-> getTests");
373         return tests;
374     }
375     /**
376      * @return Returns the timers.
377      */

378     public Hashtable JavaDoc getTimers() {
379         cat.debug("-> getTimers");
380         return timers;
381     }
382 }
Popular Tags