1 19 package org.netbeans.modules.j2ee.weblogic9; 20 21 import java.io.BufferedInputStream ; 22 import java.io.BufferedReader ; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.FileNotFoundException ; 26 import java.io.FileReader ; 27 import java.io.IOException ; 28 import java.io.StringReader ; 29 import java.util.Collection ; 30 import java.util.Iterator ; 31 import java.util.LinkedList ; 32 import java.util.List ; 33 import java.util.Properties ; 34 import java.util.StringTokenizer ; 35 import org.netbeans.api.java.platform.JavaPlatformManager; 36 import org.openide.filesystems.FileLock; 37 import org.openide.filesystems.FileObject; 38 import org.openide.filesystems.FileSystem; 39 import org.openide.filesystems.Repository; 40 import org.openide.ErrorManager; 41 import org.openide.modules.SpecificationVersion; 42 import org.openide.util.NbBundle; 43 import org.openide.util.Utilities; 44 import org.openide.xml.XMLUtil; 45 import org.w3c.dom.Document ; 46 import org.w3c.dom.NamedNodeMap ; 47 import org.w3c.dom.Node ; 48 import org.w3c.dom.NodeList ; 49 import org.xml.sax.InputSource ; 50 51 55 public class WLPluginProperties { 56 57 private static final boolean verboseRegistration = 58 System.getProperty("netbeans.weblogic.registration") != null; 59 60 public static final String SERVER_ROOT_ATTR = "serverRoot"; public static final String DOMAIN_ROOT_ATTR = "domainRoot"; public static final String IS_LOCAL_ATTR = "isLocal"; public static final String HOST_ATTR = "host"; public static final String PORT_ATTR = "port"; public static final String DEBUGGER_PORT_ATTR = "debuggerPort"; 68 private static WLPluginProperties pluginProperties = null; 69 private String installLocation; 70 71 72 public static WLPluginProperties getInstance(){ 73 if(pluginProperties==null){ 74 pluginProperties = new WLPluginProperties(); 75 } 76 return pluginProperties; 77 } 78 79 80 81 82 private WLPluginProperties() { 83 java.io.InputStream inStream = null; 84 try { 85 try { 86 propertiesFile = getPropertiesFile(); 87 if (null != propertiesFile) 88 inStream = propertiesFile.getInputStream(); 89 } catch (java.io.FileNotFoundException e) { 90 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 91 } catch (java.io.IOException e) { 92 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 93 } finally { 94 loadPluginProperties(inStream); 95 if (null != inStream) 96 inStream.close(); 97 } 98 } catch (java.io.IOException e) { 99 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 100 } 101 102 } 103 104 void loadPluginProperties(java.io.InputStream inStream) { 105 Properties inProps = new Properties (); 106 if (null != inStream) 107 try { 108 inProps.load(inStream); 109 } catch (java.io.IOException e) { 110 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 111 } 112 String loc = inProps.getProperty(INSTALL_ROOT_KEY); 113 if (loc!=null){ setInstallLocation(loc); 115 } 116 } 117 118 private static final String INSTALL_ROOT_KEY = "installRoot"; 120 121 private FileObject propertiesFile = null; 122 123 private FileObject getPropertiesFile() throws java.io.IOException { 124 FileSystem fs = Repository.getDefault().getDefaultFileSystem(); 125 FileObject dir = fs.findResource("J2EE"); 126 FileObject retVal = null; 127 if (null != dir) { 128 retVal = dir.getFileObject("weblogic","properties"); if (null == retVal) { 130 retVal = dir.createData("weblogic","properties"); } 132 } 133 return retVal; 134 } 135 136 137 public void saveProperties(){ 138 Properties outProp = new Properties (); 139 String installRoot = getInstallLocation(); 140 if (installRoot != null) 141 outProp.setProperty(INSTALL_ROOT_KEY, installRoot); 142 143 FileLock l = null; 144 java.io.OutputStream outStream = null; 145 try { 146 if (null != propertiesFile) { 147 try { 148 l = propertiesFile.lock(); 149 outStream = propertiesFile.getOutputStream(l); 150 if (null != outStream) 151 outProp.store(outStream, ""); 152 } catch (java.io.IOException e) { 153 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 154 } finally { 155 if (null != outStream) 156 outStream.close(); 157 if (null != l) 158 l.releaseLock(); 159 } 160 } 161 } catch (java.io.IOException e) { 162 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 163 } 164 } 165 166 public static final String DOMAIN_LIST = "common/nodemanager/nodemanager.domains"; 171 public static boolean domainListExists(File candidate) { 172 if (null == candidate || 173 !candidate.exists() || 174 !candidate.canRead() || 175 !candidate.isDirectory() || 176 !new File (candidate.getPath() + File.separator + DOMAIN_LIST).exists()) { 177 return false; 178 } 179 return true; 180 } 181 182 private static Collection fileColl = new java.util.ArrayList (); 183 184 static { 185 fileColl.add("common"); fileColl.add("javelin"); fileColl.add("uninstall"); fileColl.add("common/bin"); fileColl.add("server/lib/weblogic.jar"); } 191 192 public static boolean isGoodServerLocation(File candidate){ 193 if (null == candidate || 194 !candidate.exists() || 195 !candidate.canRead() || 196 !candidate.isDirectory() || 197 !hasRequiredChildren(candidate, fileColl)) { 198 return false; 199 } 200 return true; 201 } 202 203 210 public static boolean isSupportedVersion(File serverRoot) { 211 List <File > registryFiles = findRegistryFiles(serverRoot); 212 for (File registryFile : registryFiles) { 213 if (testRegistryFile(serverRoot, registryFile)) { 214 return true; 215 } 216 } 217 218 return false; 219 } 220 221 224 private static List <File > findRegistryFiles(File serverRoot) { 225 List <File > registryList = new LinkedList <File >(); 226 227 List <String > beaHomesList = findBeaHomes(); 228 for (String beaHome : beaHomesList) { 229 File registryFile = new File (beaHome + File.separator + "registry.xml"); registryList.add(registryFile); 231 } 232 233 return registryList; 234 } 235 236 239 private static boolean testRegistryFile(File serverRoot, File registryFile) { 240 try { 241 InputSource input = new InputSource (new BufferedInputStream (new FileInputStream (registryFile))); 242 Document doc = XMLUtil.parse(input, false, false, null, null); 243 NodeList releaseNodes = doc.getElementsByTagName("release"); for (int i = 0; i < releaseNodes.getLength(); i++) { 245 Node releaseNode = releaseNodes.item(i); 246 NamedNodeMap releaseNodeAttributes = releaseNode.getAttributes(); 247 String level = releaseNodeAttributes.getNamedItem("level").getNodeValue(); String installDir = releaseNodeAttributes.getNamedItem("InstallDir").getNodeValue(); String installDirCanonical = new File (installDir).getCanonicalPath(); 250 if (level != null && level.startsWith("9.") && installDirCanonical.equals(serverRoot.getCanonicalPath())) { 251 return true; 252 } 253 } 254 } catch (Exception ex) { 255 if (verboseRegistration) { 256 String msg = NbBundle.getMessage(WLPluginProperties.class, "ERR_READING_REGISTRY_FILE", registryFile.getPath()); 257 ErrorManager.getDefault().annotate(ex, msg); 258 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 259 } 260 } 261 262 return false; 263 264 } 265 266 private static List <String > findBeaHomes() { 267 List <String > beaHomesList = new LinkedList <String >(); 268 String dir = ""; 269 if (Utilities.isUnix()) { 270 dir = System.getProperty("user.home", ""); } 272 else 273 if (Utilities.isWindows()) { 274 String systemDrive = System.getenv("SystemDrive"); if (systemDrive == null) { 276 systemDrive = "C:"; } 278 dir = systemDrive; 279 } 280 File beaHomeList = new File (dir + File.separator + "bea" + File.separator + "beahomelist"); try { 282 BufferedReader br = null; 283 try { 284 br = new BufferedReader (new FileReader (beaHomeList)); 285 String list = br.readLine(); 286 if (list != null) { 287 StringTokenizer st = new StringTokenizer (list, ";"); while (st.hasMoreTokens()) { 289 beaHomesList.add(st.nextToken()); 290 } 291 } 292 } 293 finally { 294 if (br != null) { 295 br.close(); 296 } 297 } 298 } catch (Exception ex) { 299 String msg = NbBundle.getMessage(WLPluginProperties.class, "ERR_READING_BEAHOMELIST", beaHomeList.getPath()); 300 ErrorManager.getDefault().annotate(ex, msg); 301 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 302 } 303 304 return beaHomesList; 305 } 306 307 private static boolean hasRequiredChildren(File candidate, Collection requiredChildren) { 308 if (null == candidate) 309 return false; 310 String [] children = candidate.list(); 311 if (null == children) 312 return false; 313 if (null == requiredChildren) 314 return true; 315 Iterator iter = requiredChildren.iterator(); 316 while (iter.hasNext()){ 317 String next = (String )iter.next(); 318 File test = new File (candidate.getPath()+File.separator+next); 319 if (!test.exists()) 320 return false; 321 } 322 return true; 323 } 324 325 public boolean isCurrentServerLocationValid(){ 326 if (getInstallLocation()!=null) 327 return (isGoodServerLocation(new File (getInstallLocation()))); 328 else 329 return false; 330 } 331 332 333 public void setInstallLocation(String installLocation){ 334 if ( installLocation.endsWith("/") || installLocation.endsWith("\\") ){ 335 installLocation = installLocation.substring(0, installLocation.length() - 1 ); 336 } 337 338 this.installLocation = installLocation; 339 } 341 342 public String getInstallLocation(){ 343 return this.installLocation; 344 } 345 346 private static final String J2SE_PLATFORM_VERSION_15 = "1.5"; private static final String J2SE_PLATFORM_VERSION_16 = "1.6"; 349 public static boolean runningOnCorrectJdk() { 350 SpecificationVersion defPlatVersion = JavaPlatformManager.getDefault().getDefaultPlatform().getSpecification().getVersion(); 351 if (J2SE_PLATFORM_VERSION_15.equals(defPlatVersion.toString()) || 353 J2SE_PLATFORM_VERSION_16.equals(defPlatVersion.toString())) 354 return true; 355 return false; 356 } 357 } 358 | Popular Tags |