KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > weblogic9 > WLPluginProperties


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.j2ee.weblogic9;
20
21 import java.io.BufferedInputStream JavaDoc;
22 import java.io.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.FileNotFoundException JavaDoc;
26 import java.io.FileReader JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.StringReader JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.LinkedList JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Properties JavaDoc;
34 import java.util.StringTokenizer JavaDoc;
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 JavaDoc;
46 import org.w3c.dom.NamedNodeMap JavaDoc;
47 import org.w3c.dom.Node JavaDoc;
48 import org.w3c.dom.NodeList JavaDoc;
49 import org.xml.sax.InputSource JavaDoc;
50
51 /**
52  * Plugin Properties Singleton class
53  * @author Ivan Sidorkin
54  */

55 public class WLPluginProperties {
56     
57     private static final boolean verboseRegistration =
58             System.getProperty("netbeans.weblogic.registration") != null;
59     
60     // additional properties that are stored in the InstancePropeties object
61
public static final String JavaDoc SERVER_ROOT_ATTR = "serverRoot"; // NOI18N
62
public static final String JavaDoc DOMAIN_ROOT_ATTR = "domainRoot"; // NOI18N
63
public static final String JavaDoc IS_LOCAL_ATTR = "isLocal"; // NOI18N
64
public static final String JavaDoc HOST_ATTR = "host"; // NOI18N
65
public static final String JavaDoc PORT_ATTR = "port"; // NOI18N
66
public static final String JavaDoc DEBUGGER_PORT_ATTR = "debuggerPort"; // NOI18N
67

68     private static WLPluginProperties pluginProperties = null;
69     private String JavaDoc 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     /** Creates a new instance of */
82     private WLPluginProperties() {
83         java.io.InputStream JavaDoc inStream = null;
84         try {
85             try {
86                 propertiesFile = getPropertiesFile();
87                 if (null != propertiesFile)
88                     inStream = propertiesFile.getInputStream();
89             } catch (java.io.FileNotFoundException JavaDoc e) {
90                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
91             } catch (java.io.IOException JavaDoc 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 JavaDoc e) {
99             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
100         }
101         
102     }
103     
104     void loadPluginProperties(java.io.InputStream JavaDoc inStream) {
105         Properties JavaDoc inProps = new Properties JavaDoc();
106         if (null != inStream)
107             try {
108                 inProps.load(inStream);
109             } catch (java.io.IOException JavaDoc e) {
110                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
111             }
112         String JavaDoc loc = inProps.getProperty(INSTALL_ROOT_KEY);
113         if (loc!=null){// try to get the default value
114
setInstallLocation(loc);
115         }
116     }
117     
118     private static final String JavaDoc INSTALL_ROOT_KEY = "installRoot"; // NOI18N
119

120     
121     private FileObject propertiesFile = null;
122     
123     private FileObject getPropertiesFile() throws java.io.IOException JavaDoc {
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"); // NOI18N
129
if (null == retVal) {
130                 retVal = dir.createData("weblogic","properties"); //NOI18N
131
}
132         }
133         return retVal;
134     }
135     
136     
137     public void saveProperties(){
138         Properties JavaDoc outProp = new Properties JavaDoc();
139         String JavaDoc installRoot = getInstallLocation();
140         if (installRoot != null)
141             outProp.setProperty(INSTALL_ROOT_KEY, installRoot);
142         
143         FileLock l = null;
144         java.io.OutputStream JavaDoc 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 JavaDoc 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 JavaDoc e) {
162             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
163         }
164     }
165     
166     //temporary fix of #65456
167
//TODO domains file should be detected automatically upon nodemanager.properties content;
168
//same problem also in ServerPropertiesPanel.getRegisteredDomains()
169
public static final String JavaDoc DOMAIN_LIST = "common/nodemanager/nodemanager.domains"; // NOI18N
170

171     public static boolean domainListExists(File JavaDoc candidate) {
172         if (null == candidate ||
173                 !candidate.exists() ||
174                 !candidate.canRead() ||
175                 !candidate.isDirectory() ||
176                 !new File JavaDoc(candidate.getPath() + File.separator + DOMAIN_LIST).exists()) {
177             return false;
178         }
179         return true;
180     }
181     
182     private static Collection JavaDoc fileColl = new java.util.ArrayList JavaDoc();
183     
184     static {
185         fileColl.add("common"); // NOI18N
186
fileColl.add("javelin"); // NOI18N
187
fileColl.add("uninstall"); // NOI18N
188
fileColl.add("common/bin"); // NOI18N
189
fileColl.add("server/lib/weblogic.jar"); // NOI18N
190
}
191     
192     public static boolean isGoodServerLocation(File JavaDoc 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     /**
204      * Checks whether license.bea file contains at least one occurence of version 9.0 or 9.1.
205      *
206      * The method is rather heurestic than exact way how to detect the version
207      * because we are not able to decide which version belongs to the directory specified by the user
208      * in case of more than one license-group tag occurences (several WL servers in one BEA home).
209      */

210     public static boolean isSupportedVersion(File JavaDoc serverRoot) {
211         List JavaDoc<File JavaDoc> registryFiles = findRegistryFiles(serverRoot);
212         for (File JavaDoc registryFile : registryFiles) {
213             if (testRegistryFile(serverRoot, registryFile)) {
214                 return true;
215             }
216         }
217         
218         return false;
219     }
220     
221     /**
222      * @return registry.xml from all BEA homes
223      */

224     private static List JavaDoc<File JavaDoc> findRegistryFiles(File JavaDoc serverRoot) {
225         List JavaDoc<File JavaDoc> registryList = new LinkedList JavaDoc<File JavaDoc>();
226         
227         List JavaDoc<String JavaDoc> beaHomesList = findBeaHomes();
228         for (String JavaDoc beaHome : beaHomesList) {
229             File JavaDoc registryFile = new File JavaDoc(beaHome + File.separator + "registry.xml"); // NOI18N
230
registryList.add(registryFile);
231         }
232         
233         return registryList;
234     }
235     
236     /**
237      * @return true if the given registry contains server version 9.x on the given server root path
238      */

239     private static boolean testRegistryFile(File JavaDoc serverRoot, File JavaDoc registryFile) {
240         try {
241             InputSource JavaDoc input = new InputSource JavaDoc(new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(registryFile)));
242             Document JavaDoc doc = XMLUtil.parse(input, false, false, null, null);
243             NodeList JavaDoc releaseNodes = doc.getElementsByTagName("release"); // NOI18N
244
for (int i = 0; i < releaseNodes.getLength(); i++) {
245                 Node JavaDoc releaseNode = releaseNodes.item(i);
246                 NamedNodeMap JavaDoc releaseNodeAttributes = releaseNode.getAttributes();
247                 String JavaDoc level = releaseNodeAttributes.getNamedItem("level").getNodeValue(); // NOI18N
248
String JavaDoc installDir = releaseNodeAttributes.getNamedItem("InstallDir").getNodeValue(); // NOI18N
249
String JavaDoc installDirCanonical = new File JavaDoc(installDir).getCanonicalPath();
250                 if (level != null && level.startsWith("9.") && installDirCanonical.equals(serverRoot.getCanonicalPath())) {
251                     return true;
252                 }
253             }
254         } catch (Exception JavaDoc ex) {
255             if (verboseRegistration) {
256                 String JavaDoc 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 JavaDoc<String JavaDoc> findBeaHomes() {
267         List JavaDoc<String JavaDoc> beaHomesList = new LinkedList JavaDoc<String JavaDoc>();
268         String JavaDoc dir = "";
269         if (Utilities.isUnix()) {
270             dir = System.getProperty("user.home", ""); // NOI18N
271
}
272         else
273         if (Utilities.isWindows()) {
274             String JavaDoc systemDrive = System.getenv("SystemDrive"); // NOI18N
275
if (systemDrive == null) {
276                 systemDrive = "C:"; // NOI18N
277
}
278             dir = systemDrive;
279         }
280         File JavaDoc beaHomeList = new File JavaDoc(dir + File.separator + "bea" + File.separator + "beahomelist"); // NOI18N
281
try {
282             BufferedReader JavaDoc br = null;
283             try {
284                 br = new BufferedReader JavaDoc(new FileReader JavaDoc(beaHomeList));
285                 String JavaDoc list = br.readLine();
286                 if (list != null) {
287                     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(list, ";"); // NOI18N
288
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 JavaDoc ex) {
299             String JavaDoc 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 JavaDoc candidate, Collection JavaDoc requiredChildren) {
308         if (null == candidate)
309             return false;
310         String JavaDoc[] children = candidate.list();
311         if (null == children)
312             return false;
313         if (null == requiredChildren)
314             return true;
315         Iterator JavaDoc iter = requiredChildren.iterator();
316         while (iter.hasNext()){
317             String JavaDoc next = (String JavaDoc)iter.next();
318             File JavaDoc test = new File JavaDoc(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 JavaDoc(getInstallLocation())));
328         else
329             return false;
330     }
331     
332     
333     public void setInstallLocation(String JavaDoc installLocation){
334         if ( installLocation.endsWith("/") || installLocation.endsWith("\\") ){
335             installLocation = installLocation.substring(0, installLocation.length() - 1 );
336         }
337         
338         this.installLocation = installLocation;
339 // WLDeploymentFactory.resetWLClassLoader(installLocation);
340
}
341     
342     public String JavaDoc getInstallLocation(){
343         return this.installLocation;
344     }
345     
346     private static final String JavaDoc J2SE_PLATFORM_VERSION_15 = "1.5"; // NOI18N
347
private static final String JavaDoc J2SE_PLATFORM_VERSION_16 = "1.6"; // NOI18N
348

349     public static boolean runningOnCorrectJdk() {
350         SpecificationVersion defPlatVersion = JavaPlatformManager.getDefault().getDefaultPlatform().getSpecification().getVersion();
351         // test just JDK 1.5 and 1.6 for now, because WL 9.x requires it. Future releases may come with another requirements.
352
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