001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.gsfpath.api.platform;
043:
044: import java.beans.PropertyChangeListener;
045: import java.beans.PropertyChangeEvent;
046: import java.beans.PropertyChangeSupport;
047: import java.util.ArrayList;
048: import java.util.Arrays;
049: import java.util.Collection;
050: import java.util.Collections;
051: import java.util.HashSet;
052: import org.netbeans.modules.gsfpath.platform.FallbackDefaultJavaPlatform;
053: import org.netbeans.modules.gsfpath.platform.JavaPlatformProvider;
054: import org.openide.util.Lookup;
055: import org.openide.util.LookupListener;
056: import org.openide.util.LookupEvent;
057: import org.openide.modules.SpecificationVersion;
058:
059: /**
060: * JavaPlatformManager provides access to list of installed Java Platforms in the system. It can enumerate them,
061: * assign serializable IDs to their instances. It also defines a `default' platform, which represents NetBeans'
062: * own runtime environment.
063: *
064: * @author Radko Najman, Svata Dedic, Tomas Zezula
065: */
066: public final class JavaPlatformManager {
067:
068: /**
069: * Property name of the installedPlatforms property
070: */
071: public static final String PROP_INSTALLED_PLATFORMS = "installedPlatforms"; //NOI18N
072:
073: private static JavaPlatformManager instance = null;
074:
075: private Lookup.Result<JavaPlatformProvider> providers;
076: private Collection<? extends JavaPlatformProvider> lastProviders = Collections
077: .emptySet();
078: private boolean providersValid = false;
079: private PropertyChangeListener pListener;
080: private Collection<JavaPlatform> cachedPlatforms;
081: private final PropertyChangeSupport pcs = new PropertyChangeSupport(
082: this );
083:
084: /** Creates a new instance of JavaPlatformManager */
085: public JavaPlatformManager() {
086: }
087:
088: /** Gets an instance of JavaPlatformManager. It the instance doesn't exist it will be created.
089: * @return the instance of JavaPlatformManager
090: */
091: public static synchronized JavaPlatformManager getDefault() {
092: if (instance == null)
093: instance = new JavaPlatformManager();
094:
095: return instance;
096: }
097:
098: /**
099: * Returns default platform. The platform the IDE is running on.
100: * @return the default platform (never null as of org.netbeans.modules.gsfpath.platform/1 1.9)
101: */
102: public JavaPlatform getDefaultPlatform() {
103: for (JavaPlatformProvider provider : getProviders()) {
104: JavaPlatform defaultPlatform = provider
105: .getDefaultPlatform();
106: if (defaultPlatform != null) {
107: return defaultPlatform;
108: }
109: }
110: return new FallbackDefaultJavaPlatform();
111: }
112:
113: /** Gets an array of JavaPlatfrom objects.
114: * @return the array of java platform definitions.
115: */
116: public synchronized JavaPlatform[] getInstalledPlatforms() {
117: if (cachedPlatforms == null) {
118: cachedPlatforms = new HashSet<JavaPlatform>();
119: for (JavaPlatformProvider provider : getProviders()) {
120: cachedPlatforms.addAll(Arrays.asList(provider
121: .getInstalledPlatforms()));
122: }
123: }
124: return cachedPlatforms.toArray(new JavaPlatform[cachedPlatforms
125: .size()]);
126: }
127:
128: /**
129: * Returns platform given by display name and/or specification.
130: * @param platformDisplayName display name of platform or null for any name.
131: * @param platformSpec Specification of platform or null for platform of any type, in the specification null means all.
132: * Specification with null profiles means none or any profile.
133: * Specification with Profile(null,null) means any profile but at least 1.
134: * For example Specification ("CLDC", new Profile[] { new Profile("MIMDP",null), new Profile(null,null)})
135: * matches all CLDC platforms with MIDP profile of any versions and any additional profile.
136: * @return JavaPlatform[], never returns null, may return empty array when no platform matches given
137: * query.
138: */
139: public JavaPlatform[] getPlatforms(String platformDisplayName,
140: Specification platformSpec) {
141: Collection<JavaPlatform> result = new ArrayList<JavaPlatform>();
142: for (JavaPlatform platform : getInstalledPlatforms()) {
143: String name = platformDisplayName == null ? null : platform
144: .getDisplayName(); //Don't ask for display name when not needed
145: Specification spec = platformSpec == null ? null : platform
146: .getSpecification(); //Don't ask for platform spec when not needed
147: if ((platformDisplayName == null || name
148: .equalsIgnoreCase(platformDisplayName))
149: && (platformSpec == null || compatible(spec,
150: platformSpec))) {
151: result.add(platform);
152: }
153: }
154: return result.toArray(new JavaPlatform[result.size()]);
155: }
156:
157: /**
158: * Adds PropertyChangeListener to the JavaPlatformManager, the listener is notified
159: * when the platform is added,removed or modified.
160: * @param l the listener, can not be null
161: */
162: public void addPropertyChangeListener(PropertyChangeListener l) {
163: assert l != null : "Listener can not be null"; //NOI18N
164: pcs.addPropertyChangeListener(l);
165: }
166:
167: /**
168: * Removes PropertyChangeListener to the JavaPlatformManager.
169: * @param l the listener, can not be null
170: */
171: public void removePropertyChangeListener(PropertyChangeListener l) {
172: assert l != null : "Listener can not be null"; //NOI18N
173: pcs.removePropertyChangeListener(l);
174: }
175:
176: private void firePropertyChange(String property) {
177: pcs.firePropertyChange(property, null, null);
178: }
179:
180: private static boolean compatible(Specification platformSpec,
181: Specification query) {
182: String name = query.getName();
183: SpecificationVersion version = query.getVersion();
184: return ((name == null || name.equalsIgnoreCase(platformSpec
185: .getName()))
186: && (version == null || version.equals(platformSpec
187: .getVersion())) && compatibleProfiles(
188: platformSpec.getProfiles(), query.getProfiles()));
189: }
190:
191: private static boolean compatibleProfiles(
192: Profile[] platformProfiles, Profile[] query) {
193: if (query == null) {
194: return true;
195: } else if (platformProfiles == null) {
196: return false;
197: } else {
198: Collection<Profile> covered = new HashSet<Profile>();
199: for (Profile pattern : query) {
200: boolean found = false;
201: for (Profile p : platformProfiles) {
202: if (compatibleProfile(p, pattern)) {
203: found = true;
204: covered.add(p);
205: }
206: }
207: if (!found) {
208: return false;
209: }
210: }
211: return covered.size() == platformProfiles.length;
212: }
213: }
214:
215: private static boolean compatibleProfile(Profile platformProfile,
216: Profile query) {
217: String name = query.getName();
218: SpecificationVersion version = query.getVersion();
219: return ((name == null || name.equals(platformProfile.getName())) && (version == null || version
220: .equals(platformProfile.getVersion())));
221: }
222:
223: private synchronized Collection<? extends JavaPlatformProvider> getProviders() {
224: if (!this .providersValid) {
225: if (this .providers == null) {
226: this .providers = Lookup.getDefault().lookupResult(
227: JavaPlatformProvider.class);
228: this .providers.addLookupListener(new LookupListener() {
229: public void resultChanged(LookupEvent ev) {
230: resetCache(true);
231: JavaPlatformManager.this
232: .firePropertyChange(PROP_INSTALLED_PLATFORMS);
233: }
234: });
235: }
236: if (this .pListener == null) {
237: this .pListener = new PropertyChangeListener() {
238: public void propertyChange(PropertyChangeEvent evt) {
239: JavaPlatformManager.this .resetCache(false);
240: JavaPlatformManager.this
241: .firePropertyChange(PROP_INSTALLED_PLATFORMS);
242: }
243: };
244: }
245: Collection<? extends JavaPlatformProvider> instances = this .providers
246: .allInstances();
247: Collection<JavaPlatformProvider> toAdd = new HashSet<JavaPlatformProvider>(
248: instances);
249: toAdd.removeAll(this .lastProviders);
250: Collection<JavaPlatformProvider> toRemove = new HashSet<JavaPlatformProvider>(
251: this .lastProviders);
252: toRemove.removeAll(instances);
253: for (JavaPlatformProvider provider : toRemove) {
254: provider.removePropertyChangeListener(pListener);
255: }
256: for (JavaPlatformProvider provider : toAdd) {
257: provider.addPropertyChangeListener(pListener);
258: }
259: this .lastProviders = instances;
260: providersValid = true;
261: }
262: return this .lastProviders;
263: }
264:
265: private synchronized void resetCache(boolean resetProviders) {
266: JavaPlatformManager.this.cachedPlatforms = null;
267: this.providersValid &= !resetProviders;
268: }
269:
270: }
|