1 23 24 package com.sun.enterprise.admin.server.core.mbean.config; 25 26 import java.util.logging.Logger ; 28 import com.sun.enterprise.admin.server.core.AdminService; 29 30 import com.sun.enterprise.config.serverbeans.ServerTags; 32 33 import java.io.File ; 34 import java.io.FileInputStream ; 35 import java.io.IOException ; 36 37 import java.util.ArrayList ; 38 39 import javax.xml.parsers.DocumentBuilder ; 40 import javax.xml.parsers.DocumentBuilderFactory ; 41 import javax.xml.parsers.FactoryConfigurationError ; 42 import javax.xml.parsers.ParserConfigurationException ; 43 44 import org.w3c.dom.Document ; 45 import org.w3c.dom.DOMException ; 46 import org.w3c.dom.Node ; 47 import org.w3c.dom.NamedNodeMap ; 48 49 import org.xml.sax.SAXException ; 50 import org.xml.sax.SAXParseException ; 51 import org.xml.sax.EntityResolver ; 52 import org.xml.sax.InputSource ; 53 54 import com.sun.enterprise.deployment.xml.DTDRegistry; 56 import com.sun.enterprise.admin.common.exception.ControlException; 58 import com.sun.enterprise.util.i18n.StringManager; 59 60 public class ModulesXMLHelper 61 { 62 public static final int MODULE_TYPE_EJB = 0x01; 63 public static final int MODULE_TYPE_WEB = 0x02; 64 public static final int MODULE_TYPE_JAVA = 0x04; 65 public static final int MODULE_TYPE_ALL = 0xff; 66 67 public static final int EJB_TYPE_SESSION = 0x01; 68 public static final int EJB_TYPE_ENTITY = 0x02; 69 public static final int EJB_TYPE_MSGDRIVEN = 0x04; 70 public static final int EJB_TYPE_ALL = 0xff; 71 72 static final String APPLICATION_TAG = "application"; 74 static final String MODULE_TAG = "module"; 75 static final String EJB_MODULE_TAG = "ejb"; 76 static final String WEB_MODULE_TAG = "web"; 77 static final String WEB_URI_TAG = "web-uri"; 78 static final String CONTEXT_ROOT_TAG= "context-root"; 79 static final String JAVA_MODULE_TAG = "java"; 80 81 static final String ENTERPRISE_BEANS_TAG = "enterprise-beans"; 83 static final String SESSION_TAG = "session"; 84 static final String ENTITY_TAG = "entity"; 85 static final String MESSAGE_DRIVEN_TAG = "message-driven"; 86 static final String EJB_NAME_TAG = "ejb-name"; 87 88 static final String SERVLET_TAG = "servlet"; 90 static final String SERVLET_NAME_TAG = "servlet-name"; 91 92 static final Logger sLogger = AdminService.sLogger; 93 94 Document document; 95 96 public ModulesXMLHelper(String fileName) throws Exception 97 { 98 document = createDocument(fileName); 99 } 100 101 public static String [] getModulesFromApplicationLocation(String appLocation, int moduleType) throws Exception 102 { 103 ModulesXMLHelper myObj = new ModulesXMLHelper(appLocation + "/META-INF/application.xml"); 104 return myObj.getModules(moduleType); 105 } 106 107 public static boolean isModuleExists(String appLocation, String moduleName, int moduleType) throws Exception 108 { 109 String [] strs = getModulesFromApplicationLocation(appLocation, moduleType); 110 if(strs!=null) 111 for(int i=0; i<strs.length; i++) 112 if(moduleName.equals(strs[i])) 113 return true; 114 return false; 115 } 116 117 public static String [] getEnterpriseBeansForEjbModule(String location, String ejbModuleName, int ejbTypes) throws Exception 118 { 119 if(ejbModuleName!=null) 120 { if(ejbModuleName.endsWith(".jar")) 122 ejbModuleName = ejbModuleName.substring(0, ejbModuleName.length()-4); 123 location = location + "/" + ejbModuleName + "_jar"; 124 } 125 ModulesXMLHelper myObj = new ModulesXMLHelper(location + "/META-INF/ejb-jar.xml"); 126 return myObj.getEnterpriseBeans(ejbTypes); 127 } 128 129 public static String [] getServletsForWebModule(String location, String webModuleName) throws Exception 130 { 131 if(webModuleName!=null) 132 { if(webModuleName.endsWith(".war")) 134 webModuleName = webModuleName.substring(0, webModuleName.length()-4); 135 location = location + "/" + webModuleName + "_war"; 136 } 137 ModulesXMLHelper myObj = new ModulesXMLHelper(location + "/WEB-INF/web.xml"); 138 return myObj.getServlets(); 139 } 140 141 public String [] getModules(int moduleType) throws Exception 142 { 143 if (document != null) 144 { 145 ArrayList arr = findChildNodesByName(document.getDocumentElement(), MODULE_TAG); 146 String [] strs = new String [arr.size()]; 147 int noNullCount = 0; 148 for (int i=0; i<arr.size(); i++) 149 { 150 String str = getModuleNameFromNode((Node )arr.get(i), moduleType); 151 if(str!=null) 152 strs[noNullCount++] = str; 153 } 154 String [] res = new String [noNullCount]; 155 for (int i=0; i<noNullCount; i++) 156 { 157 res[i] = strs[i]; 158 } 159 return res; 160 } 161 return new String [0]; 162 163 } 164 165 public String [] getEnterpriseBeans(int ejbType) throws Exception 166 { 167 if (document != null) 168 { 169 Node beansListNode = findChildNodeByName(document.getDocumentElement(), ENTERPRISE_BEANS_TAG); 170 if(beansListNode!=null) 171 { 172 ArrayList resList = new ArrayList (); 173 if((ejbType&EJB_TYPE_SESSION)!=0) 174 addToListEjbNames(resList, beansListNode, SESSION_TAG); 175 if((ejbType&EJB_TYPE_SESSION)!=0) 176 addToListEjbNames(resList, beansListNode, ENTITY_TAG); 177 if((ejbType&EJB_TYPE_SESSION)!=0) 178 addToListEjbNames(resList, beansListNode, MESSAGE_DRIVEN_TAG); 179 String [] res = new String [resList.size()]; 180 for (int i=0; i<res.length; i++) 181 { 182 res[i] = (String )(resList.get(i)); 183 } 184 return res; 185 } 187 } 188 return new String [0]; 189 } 190 191 public String [] getServlets() throws Exception 192 { 193 if (document != null) 194 { 195 ArrayList arr = findChildNodesByName(document.getDocumentElement(), SERVLET_TAG); 196 String [] res = new String [arr.size()]; 197 for (int i=0; i<arr.size(); i++) 198 { 199 Node nameNode = findChildNodeByName((Node )arr.get(i), SERVLET_NAME_TAG); 200 res[i] = getTextForNode(nameNode); 201 } 202 return res; 203 } 204 return new String [0]; 205 } 206 207 private void addToListEjbNames(ArrayList listToAdd, Node listNode, String tag) 208 { 209 ArrayList arr = findChildNodesByName(listNode, tag); 210 for (int i=0; i<arr.size(); i++) 211 { 212 Node nameNode = findChildNodeByName((Node )arr.get(i), EJB_NAME_TAG); 213 listToAdd.add(getTextForNode(nameNode)); 214 } 215 } 216 217 private Document createDocument(String fileName) throws Exception 218 { 219 DocumentBuilderFactory factory = 220 DocumentBuilderFactory.newInstance(); 221 Document document; 222 try 223 { 224 DocumentBuilder builder = factory.newDocumentBuilder(); 225 EntityResolver resolver = new AdminEntityResolver(); 226 builder.setEntityResolver(resolver); 227 document = builder.parse(new File (fileName)); 228 } 229 catch (SAXException sxe) 230 { 231 Exception x = sxe; 233 if (sxe.getException() != null) 234 x = sxe.getException(); 235 sLogger.throwing(getClass().getName(), "createDocument", x); 236 throw new ControlException(x.getLocalizedMessage()); 237 238 } 239 catch (ParserConfigurationException pce) 240 { 241 sLogger.throwing(getClass().getName(), "createDocument", pce); 243 throw new ControlException(pce.getLocalizedMessage()); 244 } 245 catch (IOException ioe) 246 { 247 sLogger.throwing(getClass().getName(), "createDocument", ioe); 249 throw new ControlException(ioe.getLocalizedMessage()); 250 } 251 return document; 252 } 253 254 private ArrayList findChildNodesByName(Node node, String name) 255 { 256 ArrayList resNodes = new ArrayList (); 257 for (node = node.getFirstChild(); node != null; node = node.getNextSibling()) 258 { 259 if(node.getNodeName().equalsIgnoreCase(name)) 260 resNodes.add(node); 261 } 262 return resNodes; 263 } 264 265 private Node findChildNodeByName(Node node, String name) 266 { 267 ArrayList resNodes = new ArrayList (); 268 for (node = node.getFirstChild(); node != null; node = node.getNextSibling()) 269 { 270 if(node.getNodeName().equalsIgnoreCase(name)) 271 return node; 272 } 273 return null; 274 } 275 276 private String getModuleNameFromNode(Node node, int moduleType) 277 { 278 ArrayList arr; 279 if((moduleType&MODULE_TYPE_EJB)!=0 && 280 (arr=findChildNodesByName(node, EJB_MODULE_TAG)).size()>0) 281 return getTextForNode((Node )arr.get(0)); 282 if((moduleType&MODULE_TYPE_JAVA)!=0 && 283 (arr=findChildNodesByName(node, JAVA_MODULE_TAG)).size()>0) 284 return getTextForNode((Node )arr.get(0)); 285 if((moduleType&MODULE_TYPE_WEB)!=0 && 286 (arr=findChildNodesByName(node, WEB_MODULE_TAG)).size()>0) 287 { 288 node = (Node )arr.get(0); 289 if((arr=findChildNodesByName(node, WEB_URI_TAG))!=null && 290 arr.size()>0) 291 { 292 String desc = getTextForNode((Node )arr.get(0)); 293 307 return desc; 308 } 309 } 310 return null; 311 } 312 313 private String getTextForNode(Node node) 314 { 315 for (node = node.getFirstChild(); node != null; node = node.getNextSibling()) 316 { 317 if(node.getNodeType()==Node.TEXT_NODE) 318 return node.getNodeValue(); 319 } 320 return null; 321 } 322 } 323 324 class AdminEntityResolver implements EntityResolver 325 { 326 static final Logger sLogger = AdminService.sLogger; 327 private static StringManager localStrings = StringManager.getManager 328 (com.sun.enterprise.admin.server.core.mbean.config.ModulesXMLHelper.class); 329 330 public InputSource resolveEntity(String publicId, String systemId) throws 331 SAXException , IOException 332 { 333 InputSource is = null; 334 String completeDTDPath =""; 335 if (completeDTDPath != null) 336 { 337 339 is = new InputSource 340 (ClassLoader.getSystemResourceAsStream(completeDTDPath.substring(1))); 341 sLogger.finest("publicId = " + publicId); 342 sLogger.finest("dtd path = " + completeDTDPath); 343 } 344 else 345 { 346 String msg = localStrings.getString 347 ("admin.server.core.mbean.config.invalid_public_id", publicId); 348 SAXException se = new SAXException (msg); 349 sLogger.throwing(getClass().getName(), "resolveEntity", se); 350 throw se; 351 } 352 return is; 353 } 354 } 355 | Popular Tags |