KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > deployment > devmodules > api > J2eePlatform


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
20 package org.netbeans.modules.j2ee.deployment.devmodules.api;
21
22 import java.awt.Image JavaDoc;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import java.io.File JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Set JavaDoc;
32 import org.netbeans.api.java.platform.JavaPlatform;
33 import org.netbeans.modules.j2ee.deployment.impl.ServerInstance;
34 import org.netbeans.modules.j2ee.deployment.plugins.api.J2eePlatformImpl;
35 import org.netbeans.modules.j2ee.deployment.common.api.J2eeLibraryTypeProvider;
36 import org.netbeans.spi.project.libraries.LibraryImplementation;
37 import org.openide.filesystems.FileObject;
38 import org.openide.filesystems.FileUtil;
39 import org.openide.filesystems.URLMapper;
40 import org.openide.util.Utilities;
41
42
43 /**
44  * J2eePlatform describes the target environment J2EE applications are build against
45  * and subsequently deployed to. Each server instance defines its own J2EE platform.
46  *
47  * @author Stepan Herold
48  * @since 1.5
49  */

50 public final class J2eePlatform {
51     
52     /** Display name property */
53     public static final String JavaDoc PROP_DISPLAY_NAME = "displayName"; //NOI18N
54
/** Libraries property */
55     public static final String JavaDoc PROP_LIBRARIES = "libraries"; //NOI18N
56
/** Classpath property */
57     public static final String JavaDoc PROP_CLASSPATH = "classpath"; //NOI18N
58
/** Platform roots property */
59     public static final String JavaDoc PROP_PLATFORM_ROOTS = "platformRoots"; //NOI18N
60

61     
62     /**
63      * Constant for the application runtime tool. The standard properties defined
64      * for this tool are as follows {@link #TOOL_PROP_MAIN_CLASS},
65      * {@link #TOOL_PROP_MAIN_CLASS_ARGS}, {@link #TOOL_PROP_JVM_OPTS}
66      * @since 1.16
67      */

68     public static final String JavaDoc TOOL_APP_CLIENT_RUNTIME = "appClientRuntime"; // NOI18N
69

70     /**
71      * Constant for the JSR109 tool.
72      * @since 1.16
73      */

74     public static final String JavaDoc TOOL_JSR109 = "jsr109"; // NOI18N
75

76     /**
77      * Constant for the WSCOMPILE tool.
78      * @since 1.16
79      */

80     public static final String JavaDoc TOOL_WSCOMPILE = "wscompile"; // NOI18N
81

82     /**
83      * Constant for the WSIMPORT tool.
84      * @since 1.16
85      */

86     public static final String JavaDoc TOOL_WSIMPORT = "wsimport"; // NOI18N
87

88     /**
89      * Constant for the WSGEN tool.
90      * @since 1.16
91      */

92     public static final String JavaDoc TOOL_WSGEN = "wsgen"; // NOI18N
93

94     /**
95      * Constant for the WSIT tool.
96      * @since 1.16
97      */

98     public static final String JavaDoc TOOL_WSIT = "wsit"; // NOI18N
99

100     /**
101      * Constant for the JWSDP tool.
102      * @since 1.16
103      */

104     public static final String JavaDoc TOOL_JWSDP = "jwsdp"; // NOI18N
105

106     /**
107      * Constant for the KEYSTORE tool.
108      * @since 1.16
109      */

110     public static final String JavaDoc TOOL_KEYSTORE = "keystore"; // NOI18N
111

112     /**
113      * Constant for the KEYSTORE_CLIENT tool.
114      * @since 1.16
115      */

116     public static final String JavaDoc TOOL_KEYSTORE_CLIENT = "keystoreClient"; // NOI18N
117

118     /**
119      * Constant for the TRUSTSTORE tool.
120      * @since 1.16
121      */

122     public static final String JavaDoc TOOL_TRUSTSTORE = "truststore"; // NOI18N
123

124     /**
125      * Constant for the TRUSTSTORE_CLIENT tool.
126      * @since 1.16
127      */

128     public static final String JavaDoc TOOL_TRUSTSTORE_CLIENT = "truststoreClient"; // NOI18N
129

130     /**
131      * Constant for the main class tool property.
132      * @since 1.16
133      */

134     public static final String JavaDoc TOOL_PROP_MAIN_CLASS = "main.class"; // NOI18N
135

136     /**
137      * Constant for the main class arguments tool property.
138      * @since 1.16
139      */

140     public static final String JavaDoc TOOL_PROP_MAIN_CLASS_ARGS = "main.class.args"; // NOI18N
141

142     /**
143      * Constant for the JVM options tool property.
144      * @since 1.16
145      */

146     public static final String JavaDoc TOOL_PROP_JVM_OPTS = "jvm.opts"; // NOI18N
147

148     /**
149      * Constant for the distribution archive client property. Some of the tool
150      * property values may refer to this property.
151      * @since 1.16
152      */

153     public static final String JavaDoc CLIENT_PROP_DIST_ARCHIVE = "client.dist.archive"; // NOI18N
154

155     private static final String JavaDoc DEFAULT_ICON = "org/netbeans/modules/j2ee/deployment/impl/ui/resources/Servers.png"; // NOI18N
156

157     private J2eePlatformImpl impl;
158     private File JavaDoc[] classpathCache;
159     private String JavaDoc currentClasspath;
160     private ServerInstance serverInstance;
161     
162     // listens to libraries content changes
163
private PropertyChangeListener JavaDoc librariesChangeListener = new PropertyChangeListener JavaDoc() {
164         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
165             if (evt.getPropertyName().equals(LibraryImplementation.PROP_CONTENT)) {
166                 classpathCache = null;
167                 String JavaDoc newClassPath = getClasspathAsString();
168                 if (currentClasspath == null || !currentClasspath.equals(newClassPath)) {
169                     currentClasspath = newClassPath;
170                     impl.firePropertyChange(PROP_CLASSPATH, null, null);
171                 }
172             }
173         }
174     };
175     
176     /**
177      * Creates a new instance of J2eePlatform.
178      *
179      * @param aImpl instance of <code>J2eePlatformImpl</code>.
180      */

181     private J2eePlatform(ServerInstance aServerInstance, J2eePlatformImpl aImpl) {
182         impl = aImpl;
183         serverInstance = aServerInstance;
184         // listens to libraries changes
185
addPropertyChangeListener(new PropertyChangeListener JavaDoc() {
186             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
187                 if (evt.getPropertyName().equals(PROP_LIBRARIES)) {
188                     LibraryImplementation[] libs = getLibraries();
189                     for (int i = 0; i < libs.length; i++) {
190                         libs[i].removePropertyChangeListener(librariesChangeListener);
191                         libs[i].addPropertyChangeListener(librariesChangeListener);
192                     }
193                     
194                     classpathCache = null;
195                     String JavaDoc newClassPath = getClasspathAsString();
196                     if (currentClasspath == null || !currentClasspath.equals(newClassPath)) {
197                         currentClasspath = newClassPath;
198                         impl.firePropertyChange(PROP_CLASSPATH, null, null);
199                     }
200                 }
201             }
202         });
203         LibraryImplementation[] libs = getLibraries();
204         for (int i = 0; i < libs.length; i++) {
205             libs[i].addPropertyChangeListener(librariesChangeListener);
206         }
207         currentClasspath = getClasspathAsString();
208     }
209     
210     static J2eePlatform create(ServerInstance serInst) {
211         J2eePlatform result = serInst.getJ2eePlatform();
212         if (result == null) {
213             J2eePlatformImpl platformImpl = serInst.getJ2eePlatformImpl();
214             if (platformImpl != null) {
215                 result = new J2eePlatform(serInst, platformImpl);
216                 serInst.setJ2eePlatform(result);
217             }
218         }
219         return result;
220     }
221     
222     /**
223      * Return classpath entries.
224      *
225      * @return classpath entries.
226      */

227     public File JavaDoc[] getClasspathEntries() {
228         if (classpathCache == null) {
229             LibraryImplementation[] libraries = impl.getLibraries();
230             List JavaDoc/*<String>*/ classpath = new ArrayList JavaDoc();
231             for (int i = 0; i < libraries.length; i++) {
232                 List JavaDoc classpathList = libraries[i].getContent(J2eeLibraryTypeProvider.VOLUME_TYPE_CLASSPATH);
233                 for (Iterator JavaDoc iter = classpathList.iterator(); iter.hasNext();) {
234                     URL JavaDoc url = (URL JavaDoc)iter.next();
235                     if ("jar".equals(url.getProtocol())) { //NOI18N
236
url = FileUtil.getArchiveFile(url);
237                     }
238                     FileObject fo = URLMapper.findFileObject(url);
239                     if (fo != null) {
240                         File JavaDoc f = FileUtil.toFile(fo);
241                         if (f != null) {
242                             classpath.add(f);
243                         }
244                     }
245                 }
246             }
247             classpathCache = (File JavaDoc[])classpath.toArray(new File JavaDoc[classpath.size()]);
248         }
249         return classpathCache;
250     }
251     
252     /**
253      * Return classpath for the specified tool. Use the tool constants declared
254      * in this class.
255      *
256      * @param toolName tool name, for example {@link #TOOL_APP_CLIENT_RUNTIME}.
257      * @return classpath for the specified tool.
258      */

259     public File JavaDoc[] getToolClasspathEntries(String JavaDoc toolName) {
260         return impl.getToolClasspathEntries(toolName);
261     }
262     
263     /**
264      * Returns the property value for the specified tool.
265      * <p>
266      * The property value uses Ant property format and therefore may contain
267      * references to another properties defined either by the client of this API
268      * or by the tool itself.
269      * <p>
270      * The properties the client may be requited to define are as follows
271      * {@link #CLIENT_PROP_DIST_ARCHIVE}
272      *
273      * @param toolName tool name, for example {@link #TOOL_APP_CLIENT_RUNTIME}.
274      * @param propertyName tool property name, for example {@link #TOOL_PROP_MAIN_CLASS}.
275      *
276      * @return property value or null, if the property is not defined for the
277      * specified tool.
278      *
279      * @since 1.16
280      */

281     public String JavaDoc getToolProperty(String JavaDoc toolName, String JavaDoc propertyName) {
282         return impl.getToolProperty(toolName, propertyName);
283     }
284     
285     /**
286      * Specifies whether a tool of the given name is supported by this platform.
287      * Use the tool constants declared in this class.
288      *
289      * @param toolName tool name, for example {@link #TOOL_APP_CLIENT_RUNTIME}.
290      * @return <code>true</code> if platform supports tool of the given name,
291      * <code>false</code> otherwise.
292      */

293     public boolean isToolSupported(String JavaDoc toolName) {
294         return impl.isToolSupported(toolName);
295     }
296     
297     // this will be made public and will return Library
298
private LibraryImplementation[] getLibraries() {
299         return impl.getLibraries();
300     }
301     
302     /**
303      * Return platform's display name.
304      *
305      * @return platform's display name.
306      */

307     public String JavaDoc getDisplayName() {
308         // return impl.getDisplayName();
309
// AB: for now return server instance's display name
310
return serverInstance.getDisplayName();
311     }
312     
313     /**
314      * Return platform's icon.
315      *
316      * @return platform's icon.
317      * @since 1.6
318      */

319     public Image JavaDoc getIcon() {
320         Image JavaDoc result = impl.getIcon();
321         if (result == null)
322             result = Utilities.loadImage(DEFAULT_ICON);
323         
324         return result;
325     }
326     
327     /**
328      * Return platform's root directories. This will be mostly server's installation
329      * directory.
330      *
331      * @return platform's root directories.
332      */

333     public File JavaDoc[] getPlatformRoots() {
334         return impl.getPlatformRoots();
335     }
336     
337     /**
338      * Return a list of supported J2EE specification versions. Use J2EE specification
339      * versions defined in the {@link org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule}
340      * class.
341      *
342      * @return list of supported J2EE specification versions.
343      */

344     public Set JavaDoc/*<String>*/ getSupportedSpecVersions() {
345         return impl.getSupportedSpecVersions();
346     }
347     
348     /**
349      * Return a list of supported J2EE specification versions for
350      * a given module type.
351      *
352      * @param moduleType one of the constants defined in
353      * {@link org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule}
354      * @return list of supported J2EE specification versions.
355      */

356     public Set JavaDoc <String JavaDoc> getSupportedSpecVersions(Object JavaDoc moduleType) {
357         return impl.getSupportedSpecVersions(moduleType);
358     }
359     
360     /**
361      * Return a list of supported J2EE module types. Use module types defined in the
362      * {@link org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule}
363      * class.
364      *
365      * @return list of supported J2EE module types.
366      */

367     public Set JavaDoc/*<Object>*/ getSupportedModuleTypes() {
368         return impl.getSupportedModuleTypes();
369     }
370     
371     /**
372      * Return a set of J2SE platform versions this J2EE platform can run with.
373      * Versions should be specified as strings i.g. ("1.3", "1.4", etc.)
374      *
375      * @return set of J2SE platform versions this J2EE platform can run with.
376      *
377      * @since 1.9
378      */

379     public Set JavaDoc getSupportedJavaPlatformVersions() {
380         return impl.getSupportedJavaPlatformVersions();
381     }
382     
383     /**
384      * Is profiling supported by this J2EE platform?
385      *
386      * @return true, if profiling is supported, false otherwise.
387      *
388      * @since 1.9
389      */

390     public boolean supportsProfiling() {
391         return true;
392     }
393     
394     /**
395      * Return server J2SE platform or null if the platform is unknown, not
396      * registered in the IDE.
397      *
398      * @return server J2SE platform or null if the platform is unknown, not
399      * registered in the IDE.
400      *
401      * @since 1.9
402      */

403     public JavaPlatform getJavaPlatform() {
404         return impl.getJavaPlatform();
405     }
406     
407     /**
408      * Register a listener which will be notified when some of the platform's properties
409      * change.
410      *
411      * @param l listener which should be added.
412      */

413     public void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
414         impl.addPropertyChangeListener(l);
415     }
416     
417     /**
418      * Remove a listener registered previously.
419      *
420      * @param l listener which should be removed.
421      */

422     public void removePropertyChangeListener(PropertyChangeListener JavaDoc l) {
423         impl.removePropertyChangeListener(l);
424     }
425     
426     public String JavaDoc toString() {
427         return impl.getDisplayName() + " [" + getClasspathAsString() + "]"; // NOI18N
428
}
429     
430     private String JavaDoc getClasspathAsString() {
431         File JavaDoc[] classpathEntr = getClasspathEntries();
432         StringBuffer JavaDoc classpath = new StringBuffer JavaDoc();
433         final String JavaDoc PATH_SEPARATOR = System.getProperty("path.separator"); // NOI18N
434
for (int i = 0; i < classpathEntr.length; i++) {
435             classpath.append(classpathEntr[i].getAbsolutePath());
436             if (i + 1 < classpathEntr.length) {
437                 classpath.append(PATH_SEPARATOR);
438             }
439         }
440         return classpath.toString();
441     }
442 }
443
Popular Tags