1 55 package org.lateralnz.panther.deploy; 56 57 import java.io.InputStreamReader ; 58 import java.lang.reflect.Constructor ; 59 import java.util.jar.JarEntry ; 60 import java.util.jar.JarFile ; 61 import java.util.ArrayList ; 62 import java.util.HashMap ; 63 import java.util.List ; 64 import java.util.Map ; 65 66 import org.w3c.dom.Document ; 67 import org.w3c.dom.Element ; 68 import org.w3c.dom.Node ; 69 import org.w3c.dom.NodeList ; 70 71 import org.lateralnz.common.util.Constants; 72 import org.lateralnz.common.util.IOUtils; 73 import org.lateralnz.common.util.StringUtils; 74 import org.lateralnz.common.util.XMLUtils; 75 import org.lateralnz.panther.util.EJBConstants; 76 77 82 public final class EJBJarUtils implements Constants, EJBConstants { 83 private static final String JAVA_LANG_STRING = "java.lang.String"; 84 private static final Class [] CONS_PARAMS = { String .class }; 85 86 private EJBJarUtils() { 87 } 88 89 93 public static final Document getEJBJarXML(JarFile jar) throws Exception { 94 JarEntry ejbxml = jar.getJarEntry("META-INF/ejb-jar.xml"); 95 if (ejbxml == null) { 96 return null; 97 } 98 99 InputStreamReader isr = null; 100 String s; 101 try { 102 isr = new InputStreamReader (jar.getInputStream(ejbxml)); 103 s = StringUtils.readFrom(isr); 104 } 105 finally { 106 IOUtils.close(isr); 107 } 108 109 110 Document doc = XMLUtils.parse(s); 111 return doc; 112 } 113 114 120 public static final List getSessionDescriptors(Document doc) throws Exception { 121 ArrayList al = new ArrayList (); 122 123 NodeList sessionBeans = doc.getElementsByTagName(SESSION); 124 for (int i = 0; i < sessionBeans.getLength(); i++) { 125 Node n = sessionBeans.item(i); 126 127 String name = XMLUtils.getNodeValue(XMLUtils.getNamedNode(n, EJB_NAME)); 128 String remote = XMLUtils.getNodeValue(XMLUtils.getNamedNode(n, REMOTE)); 129 String home = XMLUtils.getNodeValue(XMLUtils.getNamedNode(n, HOME)); 130 String ejb = XMLUtils.getNodeValue(XMLUtils.getNamedNode(n, EJB_CLASS)); 131 String session = XMLUtils.getNodeValue(XMLUtils.getNamedNode(n, SESSION_TYPE)); 132 String trans = XMLUtils.getNodeValue(XMLUtils.getNamedNode(n, TRANSACTION_TYPE)); 133 Map transEntries; 134 if (trans.equalsIgnoreCase(CONTAINER)) { 135 transEntries = getTransactionAttr(doc, name); 136 } 137 else { 138 transEntries = new HashMap (); 139 } 140 141 SessionDescriptor sd = new SessionDescriptor(name, remote, home, ejb, session); 142 sd.setEnvEntries(getEnvEntries(n)); 143 sd.setTransEntries(transEntries); 144 145 al.add(sd); 146 } 147 148 return al; 149 } 150 151 154 private static Map getTransactionAttr(Document doc, String ejbName) { 155 boolean debug = false; 156 157 HashMap rtn = new HashMap (); 158 NodeList transList = doc.getElementsByTagName(CONTAINER_TRANSACTION); 159 for (int i = 0; i < transList.getLength(); i++) { 160 Element e1 = (Element )transList.item(i); 161 NodeList methodList = e1.getElementsByTagName(METHOD); 162 for (int j = 0; j < methodList.getLength(); j++) { 163 Element e2 = (Element )methodList.item(j); 164 String method = e2.getElementsByTagName("method-name").item(0).getFirstChild().getNodeValue(); 165 if (!StringUtils.isEmpty(method)) { 166 NodeList nameList = e2.getElementsByTagName(EJB_NAME); 167 if (nameList.getLength() > 0) { 168 Element e3 = (Element )nameList.item(0); 169 String name = e3.getFirstChild().getNodeValue(); 170 if (name.equals(ejbName)) { 171 NodeList transAttList = e1.getElementsByTagName(TRANS_ATTRIBUTE); 172 if (transAttList.getLength() > 0) { 173 rtn.put(method, transAttList.item(0).getFirstChild().getNodeValue()); 174 } 175 176 break; 177 } 178 } 179 } 180 } 181 } 182 183 if (!rtn.containsKey("*")) { 184 rtn.put("*", "NotSupported"); 185 } 186 187 return rtn; 188 } 189 190 193 public static Map getEnvEntries(Node n) throws Exception { 194 HashMap hm = new HashMap (); 195 NodeList nl = n.getChildNodes(); 196 for (int i = 0; i < nl.getLength(); i++) { 197 Node tst = nl.item(i); 198 if (tst.getNodeName().equalsIgnoreCase(ENV_ENTRY)) { 199 NodeList envitems = tst.getChildNodes(); 200 String name = null; 201 String type = null; 202 String value = null; 203 for (int j = 0; j < envitems.getLength(); j++) { 204 Node env = envitems.item(j); 205 String nodename = env.getNodeName(); 206 if (nodename.equals(ENV_ENTRY_NAME)) { 207 name = env.getFirstChild().getNodeValue(); 208 } 209 else if (nodename.equals(ENV_ENTRY_TYPE)) { 210 type = env.getFirstChild().getNodeValue(); 211 } 212 else if (nodename.equals(ENV_ENTRY_VALUE)) { 213 value = env.getFirstChild().getNodeValue(); 214 } 215 } 216 217 if (StringUtils.isEmpty(name) || StringUtils.isEmpty(type) || StringUtils.isEmpty(value)) { 218 System.out.println("note: missing one of name/type/value in env-entry"); 219 continue; 220 } 221 else if (type.equalsIgnoreCase(JAVA_LANG_STRING)) { 222 hm.put(name, value); 223 } 224 else { 225 try { 226 Class c = Class.forName(type); 227 Constructor cons = c.getConstructor(CONS_PARAMS); 228 Object obj = cons.newInstance(new Object []{ value }); 229 hm.put(name, obj); 230 } 231 catch (Exception e) { 232 System.out.println("note (error): " + e.getMessage()); 233 } 234 } 235 } 236 } 237 return hm; 238 } 239 240 public static final void main(String [] args) { 242 try { 243 java.io.File f = new java.io.File (args[0]); 244 JarFile jar = new JarFile (f, true, JarFile.OPEN_READ); 245 Document doc = EJBJarUtils.getEJBJarXML(jar); 246 EJBJarUtils.getSessionDescriptors(doc); 247 } 248 catch (Exception e) { 249 e.printStackTrace(); 250 } 251 } 252 }
| Popular Tags
|