KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seplatform > platformdefinition > J2SEPlatformImpl


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.java.j2seplatform.platformdefinition;
21
22 import java.lang.ref.Reference JavaDoc;
23 import java.lang.ref.WeakReference JavaDoc;
24 import java.util.*;
25 import java.net.URL JavaDoc;
26 import java.net.MalformedURLException JavaDoc;
27 import java.io.File JavaDoc;
28
29 import org.netbeans.api.java.classpath.ClassPath;
30 import org.netbeans.api.java.platform.JavaPlatform;
31 import org.netbeans.api.java.platform.Specification;
32 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
33 import org.openide.filesystems.FileUtil;
34 import org.openide.filesystems.FileObject;
35 import org.openide.filesystems.URLMapper;
36 import org.openide.ErrorManager;
37
38 /**
39  * Implementation of the JavaPlatform API class, which serves proper
40  * bootstrap classpath information.
41  */

42 public class J2SEPlatformImpl extends JavaPlatform {
43     
44     public static final String JavaDoc PROP_ANT_NAME = "antName"; //NOI18N
45
public static final String JavaDoc PLATFORM_J2SE = "j2se"; //NOI18N
46

47     protected static final String JavaDoc PLAT_PROP_ANT_NAME="platform.ant.name"; //NOI18N
48
protected static final String JavaDoc PLAT_PROP_ARCH_FOLDER="platform.arch.folder"; //NOI18N
49
protected static final String JavaDoc SYSPROP_BOOT_CLASSPATH = "sun.boot.class.path"; // NOI18N
50
protected static final String JavaDoc SYSPROP_JAVA_CLASS_PATH = "java.class.path"; // NOI18N
51
protected static final String JavaDoc SYSPROP_JAVA_EXT_PATH = "java.ext.dirs"; //NOI18N
52

53     /**
54      * Holds the display name of the platform
55      */

56     private String JavaDoc displayName;
57     /**
58      * Holds the properties of the platform
59      */

60     private Map properties;
61
62     /**
63      * List<URL>
64      */

65     private ClassPath sources;
66
67     /**
68      * List<URL>
69      */

70     private List javadoc;
71
72     /**
73      * List<FileObject>
74      */

75     private List installFolders;
76
77     /**
78      * Holds bootstrap libraries for the platform
79      */

80     Reference JavaDoc bootstrap = new WeakReference JavaDoc(null);
81     /**
82      * Holds standard libraries of the platform
83      */

84     Reference JavaDoc standardLibs = new WeakReference JavaDoc(null);
85
86     /**
87      * Holds the specification of the platform
88      */

89     private Specification spec;
90
91     J2SEPlatformImpl (String JavaDoc dispName, List installFolders, Map initialProperties, Map sysProperties, List sources, List javadoc) {
92         super();
93         this.displayName = dispName;
94         if (installFolders != null) {
95             this.installFolders = installFolders; //No copy needed, called from this module => safe
96
}
97         else {
98             //Old version, repair
99
String JavaDoc home = (String JavaDoc) initialProperties.remove ("platform.home"); //NOI18N
100
if (home != null) {
101                 this.installFolders = new ArrayList ();
102                 StringTokenizer tk = new StringTokenizer (home, File.pathSeparator);
103                 while (tk.hasMoreTokens()) {
104                     File JavaDoc f = new File JavaDoc (tk.nextToken());
105                     try {
106                         this.installFolders.add (f.toURI().toURL());
107                     } catch (MalformedURLException JavaDoc mue) {
108                         ErrorManager.getDefault().notify (mue);
109                     }
110                 }
111             }
112             else {
113                 throw new IllegalArgumentException JavaDoc ("Invalid platform, platform must have install folder."); //NOI18N
114
}
115         }
116         this.properties = initialProperties;
117         this.sources = createClassPath(sources);
118         if (javadoc != null) {
119             this.javadoc = Collections.unmodifiableList(javadoc); //No copy needed, called from this module => safe
120
}
121         else {
122             this.javadoc = Collections.EMPTY_LIST;
123         }
124         setSystemProperties(sysProperties);
125     }
126
127     protected J2SEPlatformImpl (String JavaDoc dispName, String JavaDoc antName, List installFolders, Map initialProperties, Map sysProperties, List sources, List javadoc) {
128         this (dispName, installFolders, initialProperties, sysProperties,sources, javadoc);
129         this.properties.put (PLAT_PROP_ANT_NAME,antName);
130     }
131
132     /**
133      * @return a descriptive, human-readable name of the platform
134      */

135     public String JavaDoc getDisplayName() {
136         return displayName;
137     }
138
139     /**
140      * Alters the human-readable name of the platform
141      * @param name the new display name
142      */

143     public void setDisplayName(String JavaDoc name) {
144         this.displayName = name;
145         firePropertyChange(PROP_DISPLAY_NAME, null, null); // NOI18N
146
}
147     
148     /**
149      * Alters the human-readable name of the platform without firing
150      * events. This method is an internal contract to allow lazy creation
151      * of display name
152      * @param name the new display name
153      */

154     final protected void internalSetDisplayName (String JavaDoc name) {
155         this.displayName = name;
156     }
157
158
159     public String JavaDoc getAntName () {
160         return (String JavaDoc) this.properties.get (PLAT_PROP_ANT_NAME);
161     }
162
163     public void setAntName (String JavaDoc antName) {
164         if (antName == null || antName.length()==0) {
165             throw new IllegalArgumentException JavaDoc ();
166         }
167         this.properties.put(PLAT_PROP_ANT_NAME, antName);
168         this.firePropertyChange (PROP_ANT_NAME,null,null);
169     }
170     
171     public void setArchFolder (final String JavaDoc folder) {
172         if (folder == null || folder.length() == 0) {
173             throw new IllegalArgumentException JavaDoc ();
174         }
175         this.properties.put (PLAT_PROP_ARCH_FOLDER, folder);
176     }
177
178
179     public ClassPath getBootstrapLibraries() {
180         synchronized (this) {
181             ClassPath cp = (ClassPath) (bootstrap == null ? null : bootstrap.get());
182             if (cp != null)
183                 return cp;
184             String JavaDoc pathSpec = (String JavaDoc)getSystemProperties().get(SYSPROP_BOOT_CLASSPATH);
185             String JavaDoc extPathSpec = Util.getExtensions((String JavaDoc)getSystemProperties().get(SYSPROP_JAVA_EXT_PATH));
186             if (extPathSpec != null) {
187                 pathSpec = pathSpec + File.pathSeparator + extPathSpec;
188             }
189             cp = Util.createClassPath (pathSpec);
190             bootstrap = new WeakReference JavaDoc(cp);
191             return cp;
192         }
193     }
194
195     /**
196      * This implementation simply reads and parses `java.class.path' property and creates a ClassPath
197      * out of it.
198      * @return ClassPath that represents contents of system property java.class.path.
199      */

200     public ClassPath getStandardLibraries() {
201         synchronized (this) {
202             ClassPath cp = (ClassPath) (standardLibs == null ? null : standardLibs.get());
203             if (cp != null)
204                 return cp;
205             String JavaDoc pathSpec = (String JavaDoc)getSystemProperties().get(SYSPROP_JAVA_CLASS_PATH);
206             cp = Util.createClassPath (pathSpec);
207             standardLibs = new WeakReference JavaDoc(cp);
208             return cp;
209         }
210     }
211
212     /**
213      * Retrieves a collection of {@link org.openide.filesystems.FileObject}s of one or more folders
214      * where the Platform is installed. Typically it returns one folder, but
215      * in some cases there can be more of them.
216      */

217     public final Collection getInstallFolders() {
218         Collection result = new ArrayList ();
219         for (Iterator it = this.installFolders.iterator(); it.hasNext();) {
220             URL JavaDoc url = (URL JavaDoc) it.next ();
221             FileObject root = URLMapper.findFileObject(url);
222             if (root != null) {
223                 result.add (root);
224             }
225         }
226         return result;
227     }
228
229
230     public final FileObject findTool(final String JavaDoc toolName) {
231         String JavaDoc archFolder = (String JavaDoc) getProperties().get(PLAT_PROP_ARCH_FOLDER);
232         FileObject tool = null;
233         if (archFolder != null) {
234             tool = Util.findTool (toolName, this.getInstallFolders(), archFolder);
235         }
236         if (tool == null) {
237             tool = Util.findTool (toolName, this.getInstallFolders());
238         }
239         return tool;
240     }
241
242
243     /**
244      * Returns the location of the source of platform
245      * @return List<URL>
246      */

247     public final ClassPath getSourceFolders () {
248         return this.sources;
249     }
250
251     public final void setSourceFolders (ClassPath c) {
252         assert c != null;
253         this.sources = c;
254         this.firePropertyChange(PROP_SOURCE_FOLDER, null, null);
255     }
256
257         /**
258      * Returns the location of the Javadoc for this platform
259      * @return FileObject
260      */

261     public final List getJavadocFolders () {
262         return this.javadoc;
263     }
264
265     public final void setJavadocFolders (List c) {
266         assert c != null;
267         List safeCopy = Collections.unmodifiableList (new ArrayList (c));
268         for (Iterator it = safeCopy.iterator(); it.hasNext();) {
269             URL JavaDoc url = (URL JavaDoc) it.next ();
270             if (!"jar".equals (url.getProtocol()) && FileUtil.isArchiveFile(url)) {
271                 throw new IllegalArgumentException JavaDoc ("JavadocFolder must be a folder.");
272             }
273         }
274         this.javadoc = safeCopy;
275         this.firePropertyChange(PROP_JAVADOC_FOLDER, null, null);
276     }
277
278     public String JavaDoc getVendor() {
279         String JavaDoc s = (String JavaDoc)getSystemProperties().get("java.vm.vendor"); // NOI18N
280
return s == null ? "" : s; // NOI18N
281
}
282
283     public Specification getSpecification() {
284         if (spec == null) {
285             spec = new Specification (PLATFORM_J2SE, Util.getSpecificationVersion(this)); //NOI18N
286
}
287         return spec;
288     }
289
290     public Map getProperties() {
291         return Collections.unmodifiableMap (this.properties);
292     }
293     
294     Collection getInstallFolderURLs () {
295         return Collections.unmodifiableList(this.installFolders);
296     }
297
298
299     private static ClassPath createClassPath (List urls) {
300         List resources = new ArrayList ();
301         if (urls != null) {
302             for (Iterator it = urls.iterator(); it.hasNext();) {
303                 URL JavaDoc url = (URL JavaDoc) it.next();
304                 resources.add (ClassPathSupport.createResource (url));
305             }
306         }
307         return ClassPathSupport.createClassPath (resources);
308     }
309 }
310
Popular Tags