1 19 package org.netbeans.modules.j2ee.jboss4.ide.ui; 20 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.FilenameFilter ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.net.ServerSocket ; 27 import java.util.Hashtable ; 28 import java.util.Iterator ; 29 import java.util.LinkedList ; 30 import java.util.List ; 31 import javax.xml.parsers.DocumentBuilderFactory ; 32 import org.netbeans.modules.j2ee.jboss4.JBDeploymentManager; 33 import org.openide.ErrorManager; 34 import org.w3c.dom.Document ; 35 import org.w3c.dom.Element ; 36 import org.w3c.dom.Node ; 37 import org.w3c.dom.NodeList ; 38 39 43 public class JBPluginUtils { 44 45 public static final String SERVER_XML = File.separator + "deploy" + File.separator + 46 "jbossweb-tomcat55.sar" + File.separator + "server.xml"; 47 48 49 55 public static Hashtable getRegisteredDomains(String serverLocation){ 56 Hashtable result = new Hashtable (); 57 59 if (isGoodJBServerLocation4x(new File (serverLocation)) || 60 isGoodJBServerLocation5x(new File (serverLocation))) 61 { 62 File file = new File (serverLocation + File.separator + "server"); 64 String [] files = file.list(new FilenameFilter (){ 65 public boolean accept(File dir, String name){ 66 if ((new File (dir.getAbsolutePath()+File.separator+name)).isDirectory()) return true; 67 return false; 68 } 69 }); 70 71 for(int i =0; i<files.length; i++){ 72 String path = file.getAbsolutePath() + File.separator + files[i]; 73 74 if (isGoodJBInstanceLocation4x(new File (path)) || 75 isGoodJBInstanceLocation5x(new File (path))) 76 { 77 result.put(files[i], path); 78 } 79 } 80 } 81 return result; 82 } 83 84 85 private static List <String > domainRequirements4x = new LinkedList <String >(); 87 static { 88 domainRequirements4x.add("conf"); domainRequirements4x.add("deploy"); domainRequirements4x.add("lib"); domainRequirements4x.add("conf/jboss-service.xml"); domainRequirements4x.add("lib/jboss-j2ee.jar"); domainRequirements4x.add("lib/jboss.jar"); domainRequirements4x.add("lib/jbosssx.jar"); domainRequirements4x.add("lib/jboss-transaction.jar"); domainRequirements4x.add("lib/jmx-adaptor-plugin.jar"); domainRequirements4x.add("lib/jnpserver.jar"); domainRequirements4x.add("lib/log4j.jar"); domainRequirements4x.add("lib/xmlentitymgr.jar"); domainRequirements4x.add("deploy/jmx-invoker-service.xml"); } 102 103 private static List <String > domainRequirements5x = new LinkedList <String >(); 104 105 static { 106 domainRequirements5x.add("conf"); domainRequirements5x.add("deploy"); domainRequirements5x.add("lib"); domainRequirements5x.add("conf/jboss-service.xml"); domainRequirements5x.add("lib/jboss-j2ee.jar"); domainRequirements5x.add("lib/jboss.jar"); domainRequirements5x.add("lib/jbosssx.jar"); domainRequirements5x.add("lib/jboss-transaction.jar"); domainRequirements5x.add("lib/jmx-adaptor-plugin.jar"); domainRequirements5x.add("lib/jnpserver.jar"); domainRequirements5x.add("lib/log4j.jar"); domainRequirements5x.add("deploy/jmx-invoker-service.xml"); } 119 120 private static boolean isGoodJBInstanceLocation(File candidate, List <String > requirements){ 121 if (null == candidate || 122 !candidate.exists() || 123 !candidate.canRead() || 124 !candidate.isDirectory() || 125 !hasRequiredChildren(candidate, requirements)) { 126 return false; 127 } 128 return true; 129 } 130 131 public static boolean isGoodJBInstanceLocation4x(File candidate){ 132 return isGoodJBInstanceLocation(candidate, domainRequirements4x); 133 } 134 135 public static boolean isGoodJBInstanceLocation5x(File candidate){ 136 return isGoodJBInstanceLocation(candidate, domainRequirements5x); 137 } 138 139 private static List <String > serverRequirements4x = new LinkedList <String >(); 141 142 static { 143 serverRequirements4x.add("bin"); serverRequirements4x.add("client"); serverRequirements4x.add("lib"); serverRequirements4x.add("server"); serverRequirements4x.add("lib/jboss-common.jar"); serverRequirements4x.add("lib/endorsed/resolver.jar"); } 150 151 private static List <String > serverRequirements5x = new LinkedList <String >(); 152 153 static { 154 serverRequirements5x.add("bin"); serverRequirements5x.add("client"); serverRequirements5x.add("lib"); serverRequirements5x.add("server"); serverRequirements5x.add("lib/jboss-common-core.jar"); serverRequirements5x.add("lib/endorsed/resolver.jar"); } 161 162 private static boolean isGoodJBServerLocation(File candidate, List <String > requirements){ 163 if (null == candidate || 164 !candidate.exists() || 165 !candidate.canRead() || 166 !candidate.isDirectory() || 167 !hasRequiredChildren(candidate, requirements)) { 168 return false; 169 } 170 return true; 171 } 172 173 public static boolean isGoodJBServerLocation4x(File candidate){ 174 return isGoodJBServerLocation(candidate, serverRequirements4x); 175 } 176 177 public static boolean isGoodJBServerLocation4x(JBDeploymentManager dm){ 178 String installDir = dm.getInstanceProperties().getProperty(JBPluginProperties.PROPERTY_ROOT_DIR); 179 return isGoodJBServerLocation4x(new File (installDir)); 180 } 181 182 public static boolean isGoodJBServerLocation5x(File candidate){ 183 return isGoodJBServerLocation(candidate, serverRequirements5x); 184 } 185 186 private static boolean hasRequiredChildren(File candidate, List <String > requiredChildren) { 187 if (null == candidate) 188 return false; 189 String [] children = candidate.list(); 190 if (null == children) 191 return false; 192 if (null == requiredChildren) 193 return true; 194 Iterator iter = requiredChildren.iterator(); 195 while (iter.hasNext()){ 196 String next = (String )iter.next(); 197 File test = new File (candidate.getPath()+File.separator+next); 198 if (!test.exists()) 199 return false; 200 } 201 return true; 202 } 203 204 206 210 public static String getDeployDir(String domainDir){ 211 String result=""; 212 result = domainDir + File.separator + "deploy"; return result; 214 } 216 217 public static String getHTTPConnectorPort(String domainDir){ 218 String defaultPort = "8080"; 219 String serverXml = domainDir + SERVER_XML; 221 File serverXmlFile = new File (serverXml); 222 if(!serverXmlFile.exists()){ 223 return defaultPort; 224 } 225 226 InputStream inputStream = null; 227 Document document = null; 228 try{ 229 inputStream = new FileInputStream (serverXmlFile); 230 document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream); 231 232 Element root = document.getDocumentElement(); 234 235 NodeList children = root.getChildNodes(); 236 for (int i = 0; i < children.getLength(); i++) { 237 Node child = children.item(i); 238 if (child.getNodeName().equals("Service")) { NodeList nl = child.getChildNodes(); 240 for (int j = 0; j < nl.getLength(); j++){ 241 Node ch = nl.item(j); 242 243 if (ch.getNodeName().equals("Connector")) { return ch.getAttributes().getNamedItem("port").getNodeValue(); 245 } 246 } 247 } 248 } 249 }catch(Exception e){ 250 e.printStackTrace(); 251 } 254 255 return defaultPort; 256 } 257 258 259 public static String getJnpPort(String domainDir){ 260 261 String serviceXml = domainDir+File.separator+"conf"+File.separator+"jboss-service.xml"; File xmlFile = new File (serviceXml); 263 if (!xmlFile.exists()) return ""; 264 265 InputStream inputStream = null; 266 Document document = null; 267 try{ 268 inputStream = new FileInputStream (xmlFile); 269 document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream); 270 271 Element root = document.getDocumentElement(); 273 274 NodeList children = root.getChildNodes(); 276 for (int i = 0; i < children.getLength(); i++) { 277 Node child = children.item(i); 278 if (child.getNodeName().equals("mbean")) { NodeList nl = child.getChildNodes(); 280 if (!child.getAttributes().getNamedItem("name").getNodeValue().equals("jboss:service=Naming")) continue; 282 for (int j = 0; j < nl.getLength(); j++){ 283 Node ch = nl.item(j); 284 285 if (ch.getNodeName().equals("attribute")) { if (!ch.getAttributes().getNamedItem("name").getNodeValue().equals("Port")) continue; 288 return ch.getFirstChild().getNodeValue(); 289 } 290 } 291 } 292 } 293 }catch(Exception e){ 294 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 295 } 296 return ""; 297 } 298 299 public static String getRMINamingServicePort(String domainDir){ 300 301 String serviceXml = domainDir+File.separator+"conf"+File.separator+"jboss-service.xml"; File xmlFile = new File (serviceXml); 303 if (!xmlFile.exists()) return ""; 304 305 InputStream inputStream = null; 306 Document document = null; 307 try{ 308 inputStream = new FileInputStream (xmlFile); 309 document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream); 310 311 Element root = document.getDocumentElement(); 313 314 NodeList children = root.getChildNodes(); 316 for (int i = 0; i < children.getLength(); i++) { 317 Node child = children.item(i); 318 if (child.getNodeName().equals("mbean")) { NodeList nl = child.getChildNodes(); 320 if (!child.getAttributes().getNamedItem("name").getNodeValue().equals("jboss:service=Naming")) continue; 322 for (int j = 0; j < nl.getLength(); j++){ 323 Node ch = nl.item(j); 324 325 if (ch.getNodeName().equals("attribute")) { if (!ch.getAttributes().getNamedItem("name").getNodeValue().equals("RmiPort")) continue; 328 return ch.getFirstChild().getNodeValue(); 329 } 330 } 331 } 332 } 333 }catch(Exception e){ 334 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 335 } 336 return ""; 337 } 338 339 public static String getRMIInvokerPort(String domainDir){ 340 341 String serviceXml = domainDir+File.separator+"conf"+File.separator+"jboss-service.xml"; File xmlFile = new File (serviceXml); 343 if (!xmlFile.exists()) return ""; 344 345 InputStream inputStream = null; 346 Document document = null; 347 try{ 348 inputStream = new FileInputStream (xmlFile); 349 document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream); 350 351 Element root = document.getDocumentElement(); 353 354 NodeList children = root.getChildNodes(); 356 for (int i = 0; i < children.getLength(); i++) { 357 Node child = children.item(i); 358 if (child.getNodeName().equals("mbean")) { NodeList nl = child.getChildNodes(); 360 if (!child.getAttributes().getNamedItem("name").getNodeValue().equals("jboss:service=invoker,type=jrmp")) continue; 362 for (int j = 0; j < nl.getLength(); j++){ 363 Node ch = nl.item(j); 364 365 if (ch.getNodeName().equals("attribute")) { if (!ch.getAttributes().getNamedItem("name").getNodeValue().equals("RMIObjectPort")) continue; 368 return ch.getFirstChild().getNodeValue(); 369 } 370 } 371 } 372 } 373 }catch(Exception e){ 374 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 375 } 376 return ""; 377 } 378 379 380 public static boolean isPortFree(int port) { 381 ServerSocket soc = null; 382 try { 383 soc = new ServerSocket (port); 384 } catch (IOException ioe) { 385 return false; 386 } finally { 387 if (soc != null) 388 try { soc.close(); } catch (IOException ex) {} } 390 391 return true; 392 } 393 394 395 } 396 | Popular Tags |