KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > platform > JavaPlatformManager


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.api.java.platform;
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Set JavaDoc;
30 import org.netbeans.modules.java.platform.FallbackDefaultJavaPlatform;
31 import org.netbeans.modules.java.platform.JavaPlatformProvider;
32 import org.openide.util.Lookup;
33 import org.openide.util.LookupListener;
34 import org.openide.util.LookupEvent;
35 import org.openide.modules.SpecificationVersion;
36
37 /**
38  * JavaPlatformManager provides access to list of installed Java Platforms in the system. It can enumerate them,
39  * assign serializable IDs to their instances. It also defines a `default' platform, which represents NetBeans'
40  * own runtime environment.
41  *
42  * @author Radko Najman, Svata Dedic, Tomas Zezula
43  */

44 public final class JavaPlatformManager {
45
46     /**
47      * Property name of the installedPlatforms property
48      */

49     public static final String JavaDoc PROP_INSTALLED_PLATFORMS="installedPlatforms"; //NOI18N
50

51     private static JavaPlatformManager instance = null;
52
53     private Lookup.Result/*<JavaPlatformProvider>*/ providers;
54     private Collection JavaDoc/*<JavaPlatformProvider>*/ lastProviders = Collections.EMPTY_SET;
55     private boolean providersValid = false;
56     private PropertyChangeListener JavaDoc pListener;
57     private Collection JavaDoc/*<JavaPlatform>*/ cachedPlatforms;
58     private HashSet JavaDoc/*<PropertyChangeListener>*/ listeners;
59
60     /** Creates a new instance of JavaPlatformManager */
61     public JavaPlatformManager() {
62     }
63
64     /** Gets an instance of JavaPlatformManager. It the instance doesn't exist it will be created.
65      * @return the instance of JavaPlatformManager
66      */

67     public static synchronized JavaPlatformManager getDefault() {
68         if (instance == null)
69             instance = new JavaPlatformManager();
70
71         return instance;
72     }
73
74     /**
75      * Returns default platform. The platform the IDE is running on.
76      * @return the default platform (never null as of org.netbeans.modules.java.platform/1 1.9)
77      */

78     public JavaPlatform getDefaultPlatform() {
79         Collection JavaDoc/*<JavaPlatformProvider>*/ instances = this.getProviders ();
80         for (Iterator JavaDoc it = instances.iterator(); it.hasNext();) {
81             JavaPlatformProvider provider = (JavaPlatformProvider) it.next();
82             JavaPlatform defaultPlatform = provider.getDefaultPlatform ();
83             if (defaultPlatform!=null) {
84                 return defaultPlatform;
85             }
86         }
87         return new FallbackDefaultJavaPlatform();
88     }
89
90     /** Gets an array of JavaPlatfrom objects.
91      * @return the array of java platform definitions.
92      */

93     public synchronized JavaPlatform[] getInstalledPlatforms() {
94         if (cachedPlatforms == null) {
95             Collection JavaDoc/*<JavaPlatformProvider>*/ instances = this.getProviders();
96             cachedPlatforms = new HashSet JavaDoc ();
97             for (Iterator JavaDoc it = instances.iterator(); it.hasNext(); ) {
98                 JavaPlatformProvider provider = (JavaPlatformProvider) it.next ();
99                 JavaPlatform[] platforms = provider.getInstalledPlatforms();
100                 for (int i = 0; i < platforms.length; i++) {
101                     cachedPlatforms.add (platforms[i]);
102                 }
103             }
104         }
105         return (JavaPlatform[]) cachedPlatforms.toArray(new JavaPlatform[cachedPlatforms.size()]);
106     }
107
108     /**
109      * Returns platform given by display name and/or specification.
110      * @param platformDisplayName display name of platform or null for any name.
111      * @param platformSpec Specification of platform or null for platform of any type, in the specification null means all.
112      * Specification with null profiles means none or any profile.
113      * Specification with Profile(null,null) means any profile but at least 1.
114      * For example Specification ("CLDC", new Profile[] { new Profile("MIMDP",null), new Profile(null,null)})
115      * matches all CLDC platforms with MIDP profile of any versions and any additional profile.
116      * @return JavaPlatform[], never returns null, may return empty array when no platform matches given
117      * query.
118      */

119     public JavaPlatform[] getPlatforms (String JavaDoc platformDisplayName, Specification platformSpec) {
120         JavaPlatform[] platforms = getInstalledPlatforms();
121         Collection JavaDoc/*<JavaPlatform>*/ result = new ArrayList JavaDoc ();
122         for (int i = 0; i < platforms.length; i++) {
123             String JavaDoc name = platformDisplayName == null ? null : platforms[i].getDisplayName(); //Don't ask for display name when not needed
124
Specification spec = platformSpec == null ? null : platforms[i].getSpecification(); //Don't ask for platform spec when not needed
125
if ((platformDisplayName==null || name.equalsIgnoreCase(platformDisplayName)) &&
126                 (platformSpec == null || compatible (spec, platformSpec))) {
127                 result.add(platforms[i]);
128             }
129         }
130         return (JavaPlatform[]) result.toArray(new JavaPlatform[result.size()]);
131     }
132
133     /**
134      * Adds PropertyChangeListener to the JavaPlatformManager, the listener is notified
135      * when the platform is added,removed or modified.
136      * @param l the listener, can not be null
137      */

138     public synchronized void addPropertyChangeListener (PropertyChangeListener JavaDoc l) {
139         assert l != null : "Listener can not be null"; //NOI18N
140
if (this.listeners == null) {
141             this.listeners = new HashSet JavaDoc ();
142         }
143         this.listeners.add (l);
144     }
145
146     /**
147      * Removes PropertyChangeListener to the JavaPlatformManager.
148      * @param l the listener, can not be null
149      */

150     public synchronized void removePropertyChangeListener (PropertyChangeListener JavaDoc l) {
151         assert l != null : "Listener can not be null"; //NOI18N
152
if (this.listeners == null) {
153             return;
154         }
155         this.listeners.remove (l);
156     }
157
158     private void firePropertyChange (String JavaDoc property) {
159         Iterator JavaDoc it;
160         synchronized (this) {
161             if (this.listeners == null) {
162                 return;
163             }
164             it = ((Set JavaDoc)this.listeners.clone()).iterator();
165         }
166         PropertyChangeEvent JavaDoc event = new PropertyChangeEvent JavaDoc (this, property, null, null);
167         while (it.hasNext()) {
168             ((PropertyChangeListener JavaDoc)it.next()).propertyChange (event);
169         }
170     }
171
172     private static boolean compatible (Specification platformSpec, Specification query) {
173         String JavaDoc name = query.getName();
174         SpecificationVersion version = query.getVersion();
175         return ((name == null || name.equalsIgnoreCase (platformSpec.getName())) &&
176             (version == null || version.equals (platformSpec.getVersion())) &&
177             compatibleProfiles (platformSpec.getProfiles(), query.getProfiles()));
178     }
179
180     private static boolean compatibleProfiles (Profile[] platformProfiles, Profile[] query) {
181         if (query == null) {
182             return true;
183         }
184         else if (platformProfiles == null) {
185             return false;
186         }
187         else {
188             Collection JavaDoc/*<Profile>*/ covered = new HashSet JavaDoc ();
189             for (int i=0; i<query.length; i++) {
190                 Profile pattern = query[i];
191                 boolean found = false;
192                 for (int j = 0; j< platformProfiles.length; j++) {
193                     if (compatibleProfile(platformProfiles[j],pattern)) {
194                         found = true;
195                         covered.add (platformProfiles[j]);
196                     }
197                 }
198                 if (!found) {
199                     return false;
200                 }
201             }
202             return covered.size() == platformProfiles.length;
203         }
204     }
205
206     private static boolean compatibleProfile (Profile platformProfile, Profile query) {
207         String JavaDoc name = query.getName();
208         SpecificationVersion version = query.getVersion();
209         return ((name == null || name.equals (platformProfile.getName())) &&
210                (version == null || version.equals (platformProfile.getVersion())));
211     }
212
213     private synchronized Collection JavaDoc/*<JavaPlatformProvider>*/ getProviders () {
214         if (!this.providersValid) {
215             if (this.providers == null) {
216                 this.providers = Lookup.getDefault().lookup(new Lookup.Template(JavaPlatformProvider.class));
217                 this.providers.addLookupListener (new LookupListener () {
218                     public void resultChanged(LookupEvent ev) {
219                         resetCache (true);
220                         JavaPlatformManager.this.firePropertyChange(PROP_INSTALLED_PLATFORMS);
221                     }
222                 });
223             }
224             if (this.pListener == null ) {
225                 this.pListener = new PropertyChangeListener JavaDoc() {
226                     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
227                         JavaPlatformManager.this.resetCache (false);
228                         JavaPlatformManager.this.firePropertyChange(PROP_INSTALLED_PLATFORMS);
229                     }
230                 };
231             }
232             Collection JavaDoc/*<JavaPlatformProvider>*/ instances = this.providers.allInstances();
233             Collection JavaDoc/*<JavaPlatformProvider>*/ toAdd = new HashSet JavaDoc(instances);
234             toAdd.removeAll (this.lastProviders);
235             Collection JavaDoc/*<JavaPlatformProvider>*/ toRemove = new HashSet JavaDoc(this.lastProviders);
236             toRemove.removeAll (instances);
237             for (Iterator JavaDoc it = toRemove.iterator(); it.hasNext();) {
238                 JavaPlatformProvider provider = (JavaPlatformProvider) it.next ();
239                 provider.removePropertyChangeListener (pListener);
240             }
241             for (Iterator JavaDoc it = toAdd.iterator(); it.hasNext();) {
242                 JavaPlatformProvider provider = (JavaPlatformProvider) it.next ();
243                 provider.addPropertyChangeListener (pListener);
244             }
245             this.lastProviders = instances;
246             providersValid = true;
247         }
248         return this.lastProviders;
249     }
250
251
252     private synchronized void resetCache (boolean resetProviders) {
253         JavaPlatformManager.this.cachedPlatforms = null;
254         this.providersValid &= !resetProviders;
255     }
256
257 }
258
Popular Tags