1 23 package org.objectweb.clif.scenario.util.isac.engine.sessionobject; 24 25 import java.io.InputStream ; 26 import java.lang.reflect.Constructor ; 27 import java.lang.reflect.InvocationTargetException ; 28 import java.util.Hashtable ; 29 import java.util.Properties ; 30 31 import javax.xml.parsers.SAXParser ; 32 import javax.xml.parsers.SAXParserFactory ; 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 ; 41 import org.w3c.dom.NamedNodeMap ; 42 import org.w3c.dom.Node ; 43 import org.w3c.dom.NodeList ; 44 import org.xml.sax.InputSource ; 45 import org.xml.sax.XMLReader ; 46 47 54 public class SessionObjectManager { 55 private Hashtable sessionObjectTable; 57 58 private Hashtable sessionObjectForABehavior ; 59 60 private Hashtable methodNameConversionTable; 61 62 private Hashtable pluginSessionObjectClassTable; 63 64 private Hashtable sessionObjectPluginNameTable; 65 66 private Hashtable analysedParams; 68 69 72 public SessionObjectManager(Hashtable mnct, Hashtable sopnt) { 73 this.methodNameConversionTable = mnct; 75 this.sessionObjectPluginNameTable = sopnt; 77 this.sessionObjectTable = new Hashtable (); 79 this.pluginSessionObjectClassTable = new Hashtable (); 80 this.sessionObjectForABehavior = new Hashtable () ; 82 this.analysedParams = new Hashtable (); 84 } 85 86 93 public void addSessionObject(Node node) { 94 NamedNodeMap attributes = node.getAttributes(); 97 String id = attributes.getNamedItem("id").getNodeValue(); 98 String pluginName = attributes.getNamedItem("name").getNodeValue(); 99 if (!this.pluginSessionObjectClassTable.contains(pluginName)) { 101 this.analyzeNewPlugin(pluginName); 103 } 104 this.visitSessionObjectNode(node); 106 String clazzName = (String ) this.pluginSessionObjectClassTable 108 .get(pluginName); 109 Object instance = null; 112 try { 113 Class clazz = ClifClassLoader.getClassLoader().loadClass(clazzName); 114 Constructor constructor = clazz.getConstructor(new Class []{Hashtable .class}); 117 instance = constructor.newInstance(new Object []{this.analysedParams}); 119 } catch (ClassNotFoundException e) { 120 throw new IsacRuntimeException( 121 "Unable to find a plugin session object class : " 122 + clazzName, e); 123 } catch (NoSuchMethodException e) { 124 throw new IsacRuntimeException( 125 "Unable to find the constructor of the session object",e); 126 } catch (InstantiationException e) { 127 throw new IsacRuntimeException( 128 "Unable to initialize the instance of the session object",e); 129 } catch (IllegalAccessException e) { 130 throw new IsacRuntimeException( 131 "Unable to initialize the instance of the session object",e); 132 } catch (InvocationTargetException e) { 133 throw new IsacRuntimeException( 134 "Unable to initialize the instance of the session object",e); 135 } 136 this.sessionObjectTable.put(id, instance); 138 this.sessionObjectPluginNameTable.put(id, pluginName); 139 } 140 141 147 private void analyzeNewPlugin(String pluginName) 148 { 149 String dirName = pluginName + "/"; 151 Properties properties = new Properties (); 153 try { 154 InputStream is = ClifClassLoader.getClassLoader().getResourceAsStream(dirName 155 + FileName.PLUGIN_PROPERTIES_FILE); 156 properties.load(is); 157 is.close(); 158 } catch (Exception e) { 159 throw new IsacRuntimeException("Unable to analyse property file : " 160 + dirName + FileName.PLUGIN_PROPERTIES_FILE,e); 161 } 162 String pluginXMLFile = properties.getProperty("plugin.xmlFile"); 164 PluginDescriptionFileHandler handler = new PluginDescriptionFileHandler( 166 pluginName, this.pluginSessionObjectClassTable, 167 this.methodNameConversionTable); 168 try { 169 InputSource is = new InputSource (ClifClassLoader.getClassLoader().getResourceAsStream(dirName 170 + pluginXMLFile)); 171 SAXParserFactory factory = SAXParserFactory.newInstance(); 173 factory.setValidating(true); 174 SAXParser saxParser = factory.newSAXParser(); 175 XMLReader 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 e) { 181 throw new IsacRuntimeException(" ---> Parser error : " + dirName 182 + pluginXMLFile + " -> " + e); 183 } 184 } 185 186 192 private void visitSessionObjectNode(Node node) { 193 String tagName = null; 195 switch (node.getNodeType()) { 196 case Node.ELEMENT_NODE: 197 tagName = ((Element ) node).getTagName(); 198 if (tagName 200 .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.USE)) { 201 } 203 if (tagName 204 .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.PARAMS)) { 205 this.analysedParams.clear(); 207 } 208 if (tagName 209 .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.PARAM)) { 210 NamedNodeMap attributes = node.getAttributes(); 212 String name = attributes.getNamedItem("name").getNodeValue(); 213 String value = attributes.getNamedItem("value").getNodeValue(); 214 this.analysedParams.put(name,value); 216 } 217 } 218 if (node.hasChildNodes()) { 220 NodeList children = node.getChildNodes(); 221 for (int i = 0; i < children.getLength(); i++) { 222 Node tempNode = children.item(i); 223 if (tempNode.getNodeType() == Node.ELEMENT_NODE) { 224 visitSessionObjectNode(tempNode); 225 } 226 } 227 } 228 } 229 230 237 public Object getSessionObject(String id) { 238 if (this.sessionObjectTable.containsKey(id)) 240 return this.sessionObjectTable.get(id); 241 throw new IsacRuntimeException( 243 "Unable to find the session object which has id=" + id); 244 } 245 246 251 public SessionObjectHashtable getSessionObjectsForABehavior(String id) { 252 if (this.sessionObjectForABehavior.containsKey(id)) 254 return (SessionObjectHashtable)this.sessionObjectForABehavior.get(id); 255 throw new IsacRuntimeException( 257 "Unable to find the sessions objects for the behavior which has id=" + id); 258 } 259 260 264 267 public Hashtable getSessionObjectTable() { 268 return sessionObjectTable; 269 } 270 271 274 public Hashtable getPluginSessionObjectClassTable() { 275 return pluginSessionObjectClassTable; 276 } 277 280 public Hashtable getSessionObjectForABehavior() { 281 return sessionObjectForABehavior; 282 } 283 } | Popular Tags |