KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > scenario > util > isac > engine > sessionobject > SessionObjectManager


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.engine.sessionobject;
24
25 import java.io.InputStream JavaDoc;
26 import java.lang.reflect.Constructor JavaDoc;
27 import java.lang.reflect.InvocationTargetException JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import java.util.Properties JavaDoc;
30
31 import javax.xml.parsers.SAXParser JavaDoc;
32 import javax.xml.parsers.SAXParserFactory JavaDoc;
33
34 import org.objectweb.clif.scenario.util.isac.FileName;
35 import org.objectweb.clif.scenario.util.isac.exception.IsacRuntimeException;
36 import org.objectweb.clif.scenario.util.isac.plugin.PluginDescriptionFileHandler;
37 import org.objectweb.clif.scenario.util.isac.util.SessionObjectHashtable;
38 import org.objectweb.clif.scenario.util.isac.util.xml.IsacEntityResolver;
39 import org.objectweb.clif.util.ClifClassLoader;
40 import org.w3c.dom.Element JavaDoc;
41 import org.w3c.dom.NamedNodeMap JavaDoc;
42 import org.w3c.dom.Node JavaDoc;
43 import org.w3c.dom.NodeList JavaDoc;
44 import org.xml.sax.InputSource JavaDoc;
45 import org.xml.sax.XMLReader JavaDoc;
46
47 /**
48  * This class is the implementation of the session object manager This class
49  * will manage all the sessions objects used in the scenario.
50  *
51  * @author JC Meillaud
52  * @author A Peyrard
53  */

54 public class SessionObjectManager {
55     // attributes
56
private Hashtable JavaDoc sessionObjectTable;
57
58     private Hashtable JavaDoc sessionObjectForABehavior ;
59
60     private Hashtable JavaDoc methodNameConversionTable;
61
62     private Hashtable JavaDoc pluginSessionObjectClassTable;
63
64     private Hashtable JavaDoc sessionObjectPluginNameTable;
65
66     // temporary attribute which will be used in the session object analysed
67
private Hashtable JavaDoc analysedParams;
68
69     /**
70      * Constructor, build a new session object manager
71      */

72     public SessionObjectManager(Hashtable JavaDoc mnct, Hashtable JavaDoc sopnt) {
73         // copy a reference of the method name conversion table
74
this.methodNameConversionTable = mnct;
75         // copy a reference of the plugin name of the sessions objects table
76
this.sessionObjectPluginNameTable = sopnt;
77         // build the others tables
78
this.sessionObjectTable = new Hashtable JavaDoc();
79         this.pluginSessionObjectClassTable = new Hashtable JavaDoc();
80         // this table will be init in this manager, but will be fill up by the behavior manager
81
this.sessionObjectForABehavior = new Hashtable JavaDoc() ;
82         // init the two temporary attributes to a empty value
83
this.analysedParams = new Hashtable JavaDoc();
84     }
85
86     /**
87      * This method adds a new session object to the manager This method will
88      * analyse the node tree storing datas of the session object.
89      *
90      * @param node
91      * The root node of the tree
92      */

93     public void addSessionObject(Node JavaDoc node) {
94         // get the attributes of the root node,
95
// id and name...
96
NamedNodeMap JavaDoc attributes = node.getAttributes();
97         String JavaDoc id = attributes.getNamedItem("id").getNodeValue();
98         String JavaDoc pluginName = attributes.getNamedItem("name").getNodeValue();
99         // check if this plugin has been previously analyzed
100
if (!this.pluginSessionObjectClassTable.contains(pluginName)) {
101             // first use of this plugin, so analyze it description file
102
this.analyzeNewPlugin(pluginName);
103         }
104         // now visit the description tree node
105
this.visitSessionObjectNode(node);
106         // get the class of the plugin
107
String JavaDoc clazzName = (String JavaDoc) this.pluginSessionObjectClassTable
108                 .get(pluginName);
109         // create a new null instance of the session object that will be
110
// initialized after
111
Object JavaDoc instance = null;
112         try {
113             Class JavaDoc clazz = ClifClassLoader.getClassLoader().loadClass(clazzName);
114             // build the clazz parameters of the constructor
115
// get the constructor of the session object
116
Constructor JavaDoc constructor = clazz.getConstructor(new Class JavaDoc[]{Hashtable JavaDoc.class});
117             // get an instance of the session object
118
instance = constructor.newInstance(new Object JavaDoc[]{this.analysedParams});
119         } catch (ClassNotFoundException JavaDoc e) {
120             throw new IsacRuntimeException(
121                     "Unable to find a plugin session object class : "
122                             + clazzName, e);
123         } catch (NoSuchMethodException JavaDoc e) {
124             throw new IsacRuntimeException(
125                     "Unable to find the constructor of the session object",e);
126         } catch (InstantiationException JavaDoc e) {
127             throw new IsacRuntimeException(
128                     "Unable to initialize the instance of the session object",e);
129         } catch (IllegalAccessException JavaDoc e) {
130             throw new IsacRuntimeException(
131                     "Unable to initialize the instance of the session object",e);
132         } catch (InvocationTargetException JavaDoc e) {
133             throw new IsacRuntimeException(
134                     "Unable to initialize the instance of the session object",e);
135         }
136         // now put the entries in the tables
137
this.sessionObjectTable.put(id, instance);
138         this.sessionObjectPluginNameTable.put(id, pluginName);
139     }
140
141     /**
142      * This method analyze a new Plugin
143      *
144      * @param pluginName
145      * The name of the plugin to analyze
146      */

147     private void analyzeNewPlugin(String JavaDoc pluginName)
148     {
149         // define the directory of the plugin
150
String JavaDoc dirName = pluginName + "/";
151         // load the plugin properties
152
Properties JavaDoc properties = new Properties JavaDoc();
153         try {
154             InputStream JavaDoc is = ClifClassLoader.getClassLoader().getResourceAsStream(dirName
155                     + FileName.PLUGIN_PROPERTIES_FILE);
156             properties.load(is);
157             is.close();
158         } catch (Exception JavaDoc e) {
159             throw new IsacRuntimeException("Unable to analyse property file : "
160                     + dirName + FileName.PLUGIN_PROPERTIES_FILE,e);
161         }
162         // load the properties defined in the file
163
String JavaDoc pluginXMLFile = properties.getProperty("plugin.xmlFile");
164         // open the xml definiton file
165
PluginDescriptionFileHandler handler = new PluginDescriptionFileHandler(
166                 pluginName, this.pluginSessionObjectClassTable,
167                 this.methodNameConversionTable);
168         try {
169             InputSource JavaDoc is = new InputSource JavaDoc(ClifClassLoader.getClassLoader().getResourceAsStream(dirName
170                     + pluginXMLFile));
171             // parse the xml file
172
SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
173             factory.setValidating(true);
174             SAXParser JavaDoc saxParser = factory.newSAXParser();
175             XMLReader JavaDoc reader = saxParser.getXMLReader();
176             reader.setContentHandler(handler);
177             reader.setErrorHandler(handler);
178             reader.setEntityResolver(new IsacEntityResolver(ClifClassLoader.getClassLoader()));
179             reader.parse(is);
180         } catch (Exception JavaDoc e) {
181             throw new IsacRuntimeException(" ---> Parser error : " + dirName
182                     + pluginXMLFile + " -> " + e);
183         }
184     }
185
186     /**
187      * This method visit recursively
188      *
189      * @param node
190      * The root node of the tree to be analyzed
191      */

192     private void visitSessionObjectNode(Node JavaDoc node) {
193         // switch between node type
194
String JavaDoc tagName = null;
195         switch (node.getNodeType()) {
196         case Node.ELEMENT_NODE:
197             tagName = ((Element JavaDoc) node).getTagName();
198             // switch between tag names
199
if (tagName
200                     .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.USE)) {
201                 // do nothing
202
}
203             if (tagName
204                     .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.PARAMS)) {
205                 // set the temporary params table to an empty value
206
this.analysedParams.clear();
207             }
208             if (tagName
209                     .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.PARAM)) {
210                 // get the value attribute of the node
211
NamedNodeMap JavaDoc attributes = node.getAttributes();
212                 String JavaDoc name = attributes.getNamedItem("name").getNodeValue();
213                 String JavaDoc value = attributes.getNamedItem("value").getNodeValue();
214                 // add the value to the params table
215
this.analysedParams.put(name,value);
216             }
217         }
218         // analyse the child
219
if (node.hasChildNodes()) {
220             NodeList JavaDoc children = node.getChildNodes();
221             for (int i = 0; i < children.getLength(); i++) {
222                 Node JavaDoc tempNode = children.item(i);
223                 if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
224                     visitSessionObjectNode(tempNode);
225                 }
226             }
227         }
228     }
229
230     /**
231      * This method permit to get a specified session object with it id.
232      *
233      * @param id
234      * The id of the session object
235      * @return The session object searched
236      */

237     public Object JavaDoc getSessionObject(String JavaDoc id) {
238         // check if the id is in the table
239
if (this.sessionObjectTable.containsKey(id))
240             return this.sessionObjectTable.get(id);
241         // the table is not containing the id, so throw an exception
242
throw new IsacRuntimeException(
243                 "Unable to find the session object which has id=" + id);
244     }
245
246     /**
247      * This method get sessions objects for a behavior
248      * @param id The id of the behavior
249      * @return A cloneable table, storing all the sessions objects
250      */

251     public SessionObjectHashtable getSessionObjectsForABehavior(String JavaDoc id) {
252         // check if the id is in the table
253
if (this.sessionObjectForABehavior.containsKey(id))
254             return (SessionObjectHashtable)this.sessionObjectForABehavior.get(id);
255         // the table is not containing the id, so throw an exception
256
throw new IsacRuntimeException(
257                 "Unable to find the sessions objects for the behavior which has id=" + id);
258     }
259
260     ///////////////////////////////////////////
261
// Attributes getters
262
///////////////////////////////////////////
263

264     /**
265      * @return Returns the sessionObjectTable.
266      */

267     public Hashtable JavaDoc getSessionObjectTable() {
268         return sessionObjectTable;
269     }
270
271     /**
272      * @return Returns the pluginSessionObjectClassTable.
273      */

274     public Hashtable JavaDoc getPluginSessionObjectClassTable() {
275         return pluginSessionObjectClassTable;
276     }
277     /**
278      * @return Returns the sessionObjectForABehavior.
279      */

280     public Hashtable JavaDoc getSessionObjectForABehavior() {
281         return sessionObjectForABehavior;
282     }
283 }
Popular Tags