1 19 20 package org.netbeans.modules.j2ee.oc4j.util; 21 22 import com.sun.org.apache.xml.internal.serialize.XMLSerializer; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.FileWriter ; 26 import java.io.InputStream ; 27 import java.net.MalformedURLException ; 28 import java.net.URL ; 29 import java.net.URLClassLoader ; 30 import java.util.ArrayList ; 31 import java.util.Collection ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import javax.enterprise.deploy.spi.TargetModuleID ; 35 import javax.xml.parsers.DocumentBuilderFactory ; 36 import javax.xml.xpath.XPath ; 37 import javax.xml.xpath.XPathConstants ; 38 import javax.xml.xpath.XPathExpression ; 39 import javax.xml.xpath.XPathFactory ; 40 import org.netbeans.api.db.explorer.DatabaseException; 41 import org.netbeans.api.db.explorer.JDBCDriver; 42 import org.netbeans.api.db.explorer.JDBCDriverManager; 43 import org.netbeans.modules.j2ee.dd.api.web.DDProvider; 44 import org.netbeans.modules.j2ee.dd.api.web.Servlet; 45 import org.netbeans.modules.j2ee.dd.api.web.WebApp; 46 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties; 47 import org.netbeans.modules.j2ee.oc4j.OC4JDeploymentManager; 48 import org.netbeans.modules.j2ee.oc4j.ide.OC4JErrorManager; 49 import org.netbeans.modules.web.api.webmodule.WebModule; 50 import org.openide.DialogDisplayer; 51 import org.openide.ErrorManager; 52 import org.openide.NotifyDescriptor; 53 import org.openide.filesystems.FileObject; 54 import org.openide.filesystems.FileUtil; 55 import org.w3c.dom.DOMException ; 56 import org.w3c.dom.Document ; 57 import org.w3c.dom.Element ; 58 import org.w3c.dom.Node ; 59 import org.w3c.dom.NodeList ; 60 61 64 public class OC4JPluginUtils { 65 public static final String CONFIG_DIR = File.separator + "j2ee" + File.separator + 66 "home" + File.separator + "config"; public static final String SERVER_XML = CONFIG_DIR + File.separator + "server.xml"; public static final String SYSTEM_JAZN_DATA_XML = CONFIG_DIR + File.separator + "system-jazn-data.xml"; 70 private static Collection <String > fileRequired = new java.util.ArrayList <String >(); 72 73 static { 74 fileRequired.add("bin/oc4j"); fileRequired.add("bin/oc4j.cmd"); fileRequired.add("j2ee/home/config"); fileRequired.add("j2ee/home/config/server.xml"); fileRequired.add("j2ee/home/config/default-web-site.xml"); } 80 81 public static boolean isGoodOC4JHomeLocation(File candidate){ 82 OC4JDebug.log("org.netbeans.modules.j2ee.oc4j.util.OC4JPluginUtils", "Check location for: " + candidate); 83 if (null == candidate || 84 !candidate.exists() || 85 !candidate.canRead() || 86 !candidate.isDirectory() || 87 !hasRequiredChildren(candidate, fileRequired)) 88 return false; 89 OC4JDebug.log("org.netbeans.modules.j2ee.oc4j.util.OC4JPluginUtils", "Location is OK"); 90 return true; 91 } 92 93 public static boolean isLocalServer(InstanceProperties ip) { 94 String host = ip.getProperty(OC4JPluginProperties.PROPERTY_HOST); 95 96 if(host != null && host.equals("localhost")) 97 return true; 98 99 return false; 100 } 101 102 private static boolean hasRequiredChildren(File candidate, Collection <String > requiredChildren) { 103 if (null == candidate) 104 return false; 105 String [] children = candidate.list(); 106 if (null == children) 107 return false; 108 if (null == requiredChildren) 109 return true; 110 Iterator iter = requiredChildren.iterator(); 111 while (iter.hasNext()){ 112 String next = (String )iter.next(); 113 File test = new File (candidate.getPath()+File.separator+next); 114 if (!test.exists()) 115 return false; 116 } 117 return true; 118 } 119 120 public static int getHttpPort(String oc4jHomeLocal, String webSite) { 121 int httpPort = 8888; 122 InputStream inputStream = null; 123 Document document = null; 124 String webSiteFilePath = ""; 125 126 for (Iterator <String > it = getPathForElement(oc4jHomeLocal, "web-site").iterator(); it.hasNext();) { String webSitePath = it.next(); 128 String webSiteName = webSitePath.substring( webSitePath.lastIndexOf(File.separatorChar) + 1, webSitePath.indexOf("-web-site.xml")); if(webSiteName.equals(webSite)) 130 webSiteFilePath = webSitePath; 131 } 132 133 if(OC4JDebug.isEnabled()) { 134 OC4JDebug.log("org.netbeans.modules.j2ee.oc4j.util.OC4JPluginUtils", webSite); 135 } 136 File webSiteFile = new File (webSiteFilePath); 137 if(OC4JDebug.isEnabled()) { 138 OC4JDebug.log("org.netbeans.modules.j2ee.oc4j.util.OC4JPluginUtils", webSiteFile.getAbsolutePath()); 139 } 140 try { 141 inputStream = new FileInputStream (webSiteFile); 142 document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream); 143 Element rootElement = document.getDocumentElement(); 144 httpPort = Integer.parseInt(rootElement.getAttributes().getNamedItem("port").getNodeValue()); } catch(Exception ex) { 146 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 147 } 148 return httpPort; 149 } 150 151 public static String getHttpPort(String instanceURL) { 152 InstanceProperties ip = InstanceProperties.getInstanceProperties(instanceURL); 153 return ip.getProperty(InstanceProperties.HTTP_PORT_NUMBER); 154 } 155 156 public static int getAdminPort(String oc4jHomeLocal) { 157 int adminPort = 23791; 158 InputStream inputStream = null; 159 Document document = null; 160 String rmiFilePath = getPathForElement(oc4jHomeLocal, "rmi-config").iterator().next(); 161 File rmiFile = new File (rmiFilePath); 162 try { 163 inputStream = new FileInputStream (rmiFile); 164 document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream); 165 Element rootElement = document.getDocumentElement(); 166 adminPort = Integer.parseInt(rootElement.getAttributes().getNamedItem("port").getNodeValue()); } catch(Exception ex) { 168 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 169 } 170 return adminPort; 171 } 172 173 public static Collection <String > getWebSites(String oc4jHomeLocal) { 174 ArrayList <String > webSiteNames = new ArrayList <String >(); 175 for(String webSite : getPathForElement(oc4jHomeLocal, "web-site")) { webSiteNames.add(webSite.substring(webSite.lastIndexOf(File.separatorChar) + 1, webSite.indexOf("-web-site.xml"))); } 178 return webSiteNames; 179 } 180 181 public static Collection <String > getUsers(String oc4jHomeLocal) { 182 ArrayList <String > users = new ArrayList <String >(); 183 File xmlFile = new File (oc4jHomeLocal + File.separator + SYSTEM_JAZN_DATA_XML); 184 185 try { 186 XPathFactory factory = XPathFactory.newInstance(); 187 XPath xpath = factory.newXPath(); 188 189 InputStream inputStream = new FileInputStream (xmlFile); 190 Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream); 191 192 XPathExpression expr = xpath.compile("//role[name='oc4j-administrators']/members/member/name/text()"); 193 194 NodeList nodes = (NodeList ) expr.evaluate(document, XPathConstants.NODESET); 195 196 for(int i=0;i<nodes.getLength();i++) 197 users.add(nodes.item(i).getNodeValue()); 198 } catch(Exception ex) { 199 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 200 } 201 202 return users; 203 } 204 205 public static boolean isUserActivated(String oc4jHomeLocal, String user) { 206 File xmlFile = new File (oc4jHomeLocal + File.separator + SYSTEM_JAZN_DATA_XML); 207 208 try { 209 XPathFactory factory = XPathFactory.newInstance(); 210 XPath xpath = factory.newXPath(); 211 212 InputStream inputStream = new FileInputStream (xmlFile); 213 Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream); 214 215 XPathExpression expr = xpath.compile("//user[name='"+user+"']"); 216 217 Node node = (Node ) expr.evaluate(document, XPathConstants.NODE); 218 219 if (node.getAttributes().getNamedItem("deactivated") != null) 220 return false; 221 222 return true; 223 } catch(Exception ex) { 224 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 225 } 226 227 return false; 228 } 229 230 public static boolean activateUser(String oc4jHomeLocal, String user, String password) { 231 File xmlFile = new File (oc4jHomeLocal + File.separator + SYSTEM_JAZN_DATA_XML); 232 233 try { 234 XPathFactory factory = XPathFactory.newInstance(); 235 XPath xpath = factory.newXPath(); 236 237 InputStream inputStream = new FileInputStream (xmlFile); 238 Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream); 239 240 XPathExpression exprActivate = xpath.compile("//user[name='"+user+"']"); 241 XPathExpression exprPassword = xpath.compile("//user[name='"+user+"']/credentials/text()"); 242 243 Node nodeActivate = (Node ) exprActivate.evaluate(document, XPathConstants.NODE); 244 Node nodePassword = (Node ) exprPassword.evaluate(document, XPathConstants.NODE); 245 246 if(nodeActivate != null) { 247 try { 248 nodeActivate.getAttributes().removeNamedItem("deactivated"); 249 } catch(DOMException e) { 250 } 252 } 253 254 if(nodePassword != null) { 255 try { 256 nodePassword.setNodeValue("!"+password); 257 } catch(DOMException e) { 258 return false; 259 } 260 } 261 262 XMLSerializer serializer = new XMLSerializer(); 263 serializer.setOutputCharStream(new FileWriter (xmlFile)); 264 serializer.serialize(document); 265 266 return true; 267 } catch(Exception ex) { 268 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 269 } 270 271 return false; 272 } 273 274 public static String requestPassword(String user) { 275 OC4JPasswordInputDialog d = new OC4JPasswordInputDialog(user); 276 277 if(DialogDisplayer.getDefault().notify(d) == NotifyDescriptor.OK_OPTION) { 278 return d.getPassword(); 279 } 280 281 return null; 282 } 283 284 static Collection <String > getPathForElement(String oc4jHomeLocal, String element) { 285 InputStream inputStream = null; 286 Document document = null; 287 ArrayList <String > paths = new ArrayList <String >(); 288 File xmlFile = new File (oc4jHomeLocal + File.separator + SERVER_XML); 289 try{ 290 inputStream = new FileInputStream (xmlFile); 291 document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream); 292 NodeList elementsList = document.getElementsByTagName(element); 293 if(elementsList.getLength() == 0) { 294 return paths; 295 } else { 296 for(int i = 0; i < elementsList.getLength(); i++) { 297 Node pathAttr = elementsList.item(i).getAttributes().getNamedItem("path"); String path = pathAttr.getNodeValue(); 299 if(path.startsWith("./")) { 300 paths.add(oc4jHomeLocal + CONFIG_DIR + File.separator + path.substring(2)); 301 } else { 302 paths.add(path); 303 } 304 } 305 } 306 }catch(Exception e){ 307 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 308 } 309 return paths; 310 } 311 312 public static String getName(TargetModuleID module) { 313 String s = module.toString(); 314 s = s.substring(s.indexOf("name=")+5); 315 316 return s.substring(0, (s.indexOf(".") == -1)?(s.indexOf(",")):(s.indexOf("."))); 317 } 318 319 public static String getServerRoot(String instanceURL) { 320 InstanceProperties ip = InstanceProperties.getInstanceProperties(instanceURL); 321 String serverRoot = ip.getProperty(OC4JPluginProperties.PROPERTY_OC4J_HOME); 322 323 return serverRoot; 324 } 325 326 public static String getHostname(String instanceURL) { 327 InstanceProperties ip = InstanceProperties.getInstanceProperties(instanceURL); 328 return ip.getProperty(OC4JPluginProperties.PROPERTY_HOST); 329 } 330 331 public static void registerOracleJdbcDriver(String serverRoot) { 332 if(serverRoot == null) 333 return; 334 335 List <URL > list = new ArrayList <URL >(); 336 File serverDir = new File (serverRoot); 337 338 try{ 339 for(File file:new File (serverDir, "jdbc/lib").listFiles()) { 340 if(FileUtil.isArchiveFile(file.toURI().toURL())) 341 list.add(fileToUrl(file)); 342 } 343 } catch(MalformedURLException ex) { 344 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 345 } 346 347 URL [] urls = list.toArray(new URL [list.size()]); 348 String name = "Oracle"; 349 String clazz = "oracle.jdbc.driver.OracleDriver"; 350 351 JDBCDriver driver = JDBCDriver.create(name, name, clazz, urls); 352 353 if(JDBCDriverManager.getDefault().getDrivers(clazz).length == 0) { 354 try { 355 JDBCDriverManager.getDefault().addDriver(driver); 356 } catch(DatabaseException e) { 357 } 359 } 360 } 361 362 public static URL fileToUrl(File file) throws MalformedURLException { 363 URL url = file.toURI().toURL(); 364 if (FileUtil.isArchiveFile(url)) { 365 url = FileUtil.getArchiveRoot(url); 366 } 367 return url; 368 } 369 370 public static boolean checkClass(String clazz, OC4JDeploymentManager dm) { 371 List <URL > l = dm.getProperties().getClasses(); 373 URL [] urls = l.toArray(new URL [] {}); 374 ClassLoader c = new URLClassLoader (urls); 375 376 try { 378 Class.forName(clazz, true, c); 379 } catch (ClassNotFoundException e) { 380 OC4JErrorManager.getInstance(dm).error(clazz, e, OC4JErrorManager.GENERIC_FAILURE); 381 return false; 382 } 383 384 return true; 385 } 386 387 public static boolean isJSFInWebModule(WebModule wm) { 388 FileObject dd = wm.getDeploymentDescriptor(); 390 return (dd != null && getActionServlet(dd) != null); 391 } 392 393 public static Servlet getActionServlet(FileObject dd) { 394 if (dd == null) { 395 return null; 396 } 397 try { 398 WebApp webApp = DDProvider.getDefault().getDDRoot(dd); 399 400 return (Servlet) webApp 403 .findBeanByName("Servlet", "ServletClass", "javax.faces.webapp.FacesServlet"); } catch (java.io.IOException e) { 405 return null; 406 } 407 } 408 } | Popular Tags |