KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > jboss4 > util > JBProperties


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.jboss4.util;
20
21 import java.io.BufferedInputStream JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.FileNotFoundException JavaDoc;
25 import java.io.FilenameFilter JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.net.MalformedURLException JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Enumeration JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Properties JavaDoc;
34 import java.util.logging.Level JavaDoc;
35 import java.util.logging.Logger JavaDoc;
36 import org.netbeans.api.java.platform.JavaPlatform;
37 import org.netbeans.api.java.platform.JavaPlatformManager;
38 import org.netbeans.api.java.platform.Specification;
39 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
40 import org.netbeans.modules.j2ee.jboss4.JBDeploymentManager;
41 import org.netbeans.modules.j2ee.jboss4.customizer.CustomizerSupport;
42 import org.openide.ErrorManager;
43 import org.openide.filesystems.FileUtil;
44 import org.openide.modules.InstalledFileLocator;
45 import org.openide.util.NbCollections;
46
47 /**
48  * Helper class that makes it easier to access and set JBoss instance properties.
49  *
50  * @author sherold
51  */

52 public class JBProperties {
53     
54     /** Java platform property which is used as a java platform ID */
55     public static final String JavaDoc PLAT_PROP_ANT_NAME = "platform.ant.name"; //NOI18N
56

57     // properties
58
public static final String JavaDoc PROP_PROXY_ENABLED = "proxy_enabled"; // NOI18N
59
private static final String JavaDoc PROP_JAVA_PLATFORM = "java_platform"; // NOI18N
60
private static final String JavaDoc PROP_JAVA_OPTS = "java_opts"; // NOI18N
61
private static final String JavaDoc PROP_SOURCES = "sources"; // NOI18N
62
private static final String JavaDoc PROP_JAVADOCS = "javadocs"; // NOI18N
63
private static final String JavaDoc PROP_SERVER_DIR = "server-dir"; // NOI18N
64
private static final String JavaDoc PROP_ROOT_DIR = "root-dir"; // NOI18N
65

66     // default values
67
private static final String JavaDoc DEF_VALUE_JAVA_OPTS = ""; // NOI18N
68
private static final boolean DEF_VALUE_PROXY_ENABLED = true;
69     
70     private final InstanceProperties ip;
71     private final JBDeploymentManager manager;
72     
73     // credentials initialized with default values
74
private String JavaDoc username = "admin"; // NOI18N
75
private String JavaDoc password = "admin"; // NOI18N
76

77     /** timestamp of the jmx-console-users.properties file when it was parsed for the last time */
78     private long updateCredentialsTimestamp;
79     
80     private static final Logger JavaDoc LOGGER = Logger.getLogger(JBProperties.class.getName());
81     
82     
83     /** Creates a new instance of JBProperties */
84     public JBProperties(JBDeploymentManager manager) {
85         this.manager = manager;
86         ip = manager.getInstanceProperties();
87     }
88     
89     public boolean supportsJavaEE5ejb3() {
90         return new File JavaDoc(getServerDir(), "deploy/ejb3.deployer").exists() || // JBoss 4 // NOI18N
91
new File JavaDoc(getServerDir(), "deployers/ejb3.deployer").exists(); // JBoss 5 // NOI18N
92
}
93
94     public boolean supportsJavaEE5web() {
95         return new File JavaDoc(getServerDir(), "deployers/jbossweb.deployer").exists(); // JBoss 5 // NOI18N
96
}
97     
98     public File JavaDoc getServerDir() {
99         return new File JavaDoc(ip.getProperty(PROP_SERVER_DIR));
100     }
101     
102     public File JavaDoc getRootDir() {
103         return new File JavaDoc(ip.getProperty(PROP_ROOT_DIR));
104     }
105     
106     public boolean getProxyEnabled() {
107         String JavaDoc val = ip.getProperty(PROP_PROXY_ENABLED);
108         return val != null ? Boolean.valueOf(val).booleanValue()
109                            : DEF_VALUE_PROXY_ENABLED;
110     }
111     
112     public void setProxyEnabled(boolean enabled) {
113         ip.setProperty(PROP_PROXY_ENABLED, Boolean.toString(enabled));
114     }
115     
116     public JavaPlatform getJavaPlatform() {
117         String JavaDoc currentJvm = ip.getProperty(PROP_JAVA_PLATFORM);
118         JavaPlatformManager jpm = JavaPlatformManager.getDefault();
119         JavaPlatform[] installedPlatforms = jpm.getPlatforms(null, new Specification("J2SE", null)); // NOI18N
120
for (int i = 0; i < installedPlatforms.length; i++) {
121             String JavaDoc platformName = (String JavaDoc)installedPlatforms[i].getProperties().get(PLAT_PROP_ANT_NAME);
122             if (platformName != null && platformName.equals(currentJvm)) {
123                 return installedPlatforms[i];
124             }
125         }
126         // return default platform if none was set
127
return jpm.getDefaultPlatform();
128     }
129     
130     public void setJavaPlatform(JavaPlatform javaPlatform) {
131         ip.setProperty(PROP_JAVA_PLATFORM, (String JavaDoc)javaPlatform.getProperties().get(PLAT_PROP_ANT_NAME));
132     }
133     
134     public String JavaDoc getJavaOpts() {
135         String JavaDoc val = ip.getProperty(PROP_JAVA_OPTS);
136         return val != null ? val
137                            : DEF_VALUE_JAVA_OPTS;
138     }
139     
140     public void setJavaOpts(String JavaDoc javaOpts) {
141         ip.setProperty(PROP_JAVA_OPTS, javaOpts);
142     }
143     
144     public List JavaDoc<URL JavaDoc> getClasses() {
145         List JavaDoc<URL JavaDoc> list = new ArrayList JavaDoc<URL JavaDoc>();
146         try {
147             File JavaDoc rootDir = getRootDir();
148             File JavaDoc serverDir = getServerDir();
149             list.add(fileToUrl(new File JavaDoc(rootDir, "client/jboss-j2ee.jar"))); // NOI18N
150

151             File JavaDoc wsClientLib = new File JavaDoc(rootDir, "client/jbossws-client.jar"); // NOI18N
152
if (wsClientLib.exists()) {
153                 list.add(fileToUrl(wsClientLib));
154             }
155
156             addFiles(new File JavaDoc(rootDir, "lib"), list); //NOI18N
157
addFiles(new File JavaDoc(serverDir, "/lib"), list); //NOI18N
158
if (supportsJavaEE5ejb3()) {
159                 File JavaDoc ejb3deployer = new File JavaDoc(serverDir, "/deploy/ejb3.deployer/"); // NOI18N
160
if (ejb3deployer.exists()) {
161                     addFiles(ejb3deployer, list);
162                 }
163                 else
164                 if ((ejb3deployer = new File JavaDoc(serverDir, "/deployers/ejb3.deployer/")).exists()) { // NOI18N
165
addFiles(ejb3deployer, list);
166                 }
167             }
168             
169             File JavaDoc jsfAPI = new File JavaDoc(serverDir, "/deploy/jbossweb-tomcat55.sar/jsf-libs/myfaces-api.jar"); // NOI18N
170
if (jsfAPI.exists()) {
171                 try {
172                     list.add(fileToUrl(jsfAPI));
173                 } catch (MalformedURLException JavaDoc e) {
174                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
175                 }
176             }
177             else
178             if ((jsfAPI = new File JavaDoc(serverDir, "/deployers/jbossweb.deployer/jsf-libs/jsf-api.jar")).exists()) { // NOI18N
179
try {
180                     list.add(fileToUrl(jsfAPI));
181                 } catch (MalformedURLException JavaDoc e) {
182                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
183                 }
184             }
185             
186             File JavaDoc jsfIMPL = new File JavaDoc(serverDir, "/deploy/jbossweb-tomcat55.sar/jsf-libs/myfaces-impl.jar"); // NOI18N
187
if (jsfIMPL.exists()) {
188                 try {
189                     list.add(fileToUrl(jsfIMPL));
190                 } catch (MalformedURLException JavaDoc e) {
191                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
192                 }
193             }
194             else
195             if ((jsfIMPL = new File JavaDoc(serverDir, "/deployers/jbossweb.deployer/jsf-libs/jsf-impl.jar")).exists()) { // NOI18N
196
try {
197                     list.add(fileToUrl(jsfIMPL));
198                 } catch (MalformedURLException JavaDoc e) {
199                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
200                 }
201             }
202         } catch (MalformedURLException JavaDoc e) {
203             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
204         }
205         return list;
206     }
207     
208     private static class FF implements FilenameFilter JavaDoc {
209         public boolean accept(File JavaDoc dir, String JavaDoc name) {
210             return name.endsWith(".jar") || new File JavaDoc(dir, name).isDirectory(); // NOI18N
211
}
212     }
213             
214     private void addFiles(File JavaDoc folder, List JavaDoc l) {
215         File JavaDoc files [] = folder.listFiles(new FF());
216         if (files == null)
217             return;
218         for (int i = 0; i < files.length; i++) {
219             if (files [i].isDirectory()) {
220                 addFiles(files [i], l);
221             } else {
222                 try {
223                     l.add(fileToUrl(files [i]));
224                 } catch (MalformedURLException JavaDoc e) {
225                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
226                }
227             }
228        }
229     }
230     
231     public List JavaDoc<URL JavaDoc> getSources() {
232         String JavaDoc path = ip.getProperty(PROP_SOURCES);
233         if (path == null) {
234             return new ArrayList JavaDoc<URL JavaDoc>();
235         }
236         return CustomizerSupport.tokenizePath(path);
237     }
238                                                                                                                                                                            
239     public void setSources(List JavaDoc<URL JavaDoc> path) {
240         ip.setProperty(PROP_SOURCES, CustomizerSupport.buildPath(path));
241         manager.getJBPlatform().notifyLibrariesChanged();
242     }
243     
244     public List JavaDoc<URL JavaDoc> getJavadocs() {
245         String JavaDoc path = ip.getProperty(PROP_JAVADOCS);
246         if (path == null) {
247             ArrayList JavaDoc<URL JavaDoc> list = new ArrayList JavaDoc<URL JavaDoc>();
248             try {
249                 File JavaDoc j2eeDoc = InstalledFileLocator.getDefault().locate("docs/javaee5-doc-api.zip", null, false); // NOI18N
250
if (j2eeDoc != null) {
251                     list.add(fileToUrl(j2eeDoc));
252                 }
253             } catch (MalformedURLException JavaDoc e) {
254                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
255             }
256             return list;
257         }
258         return CustomizerSupport.tokenizePath(path);
259     }
260                                                                                                                                                                            
261     public void setJavadocs(List JavaDoc<URL JavaDoc> path) {
262         ip.setProperty(PROP_JAVADOCS, CustomizerSupport.buildPath(path));
263         manager.getJBPlatform().notifyLibrariesChanged();
264     }
265     
266     public synchronized String JavaDoc getUsername() {
267         updateCredentials();
268         return username;
269     }
270     
271     public synchronized String JavaDoc getPassword() {
272         updateCredentials();
273         return password;
274     }
275     
276     // private helper methods -------------------------------------------------
277

278     private synchronized void updateCredentials() {
279         File JavaDoc usersPropFile = new File JavaDoc(getServerDir(), "/conf/props/jmx-console-users.properties");
280         long lastModified = usersPropFile.lastModified();
281         if (lastModified == updateCredentialsTimestamp) {
282             LOGGER.log(Level.FINER, "Credentials are up-to-date.");
283             return;
284         }
285         Properties JavaDoc usersProps = new Properties JavaDoc();
286         try {
287             InputStream JavaDoc is = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(usersPropFile));
288             try {
289                 usersProps.load(is);
290             } finally {
291                 is.close();
292             }
293         } catch (FileNotFoundException JavaDoc e) {
294             LOGGER.log(Level.WARNING, usersPropFile + " not found.", e);
295             return;
296         } catch (IOException JavaDoc e) {
297             LOGGER.log(Level.WARNING, "Error while reading " + usersPropFile, e);
298             return;
299         }
300
301         Enumeration JavaDoc<String JavaDoc> names = NbCollections.checkedEnumerationByFilter(usersProps.propertyNames(), String JavaDoc.class, false);
302         if (names.hasMoreElements()) {
303             username = names.nextElement();
304             password = usersProps.getProperty(username);
305         }
306         
307         updateCredentialsTimestamp = lastModified;
308     }
309     
310     /** Return URL representation of the specified file. */
311     private static URL JavaDoc fileToUrl(File JavaDoc file) throws MalformedURLException JavaDoc {
312         URL JavaDoc url = file.toURI().toURL();
313         if (FileUtil.isArchiveFile(url)) {
314             url = FileUtil.getArchiveRoot(url);
315         }
316         return url;
317     }
318 }
319
Popular Tags