1 19 20 package org.netbeans.api.java.platform; 21 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeEvent ; 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.Collections ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.Set ; 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 44 public final class JavaPlatformManager { 45 46 49 public static final String PROP_INSTALLED_PLATFORMS="installedPlatforms"; 51 private static JavaPlatformManager instance = null; 52 53 private Lookup.Result providers; 54 private Collection lastProviders = Collections.EMPTY_SET; 55 private boolean providersValid = false; 56 private PropertyChangeListener pListener; 57 private Collection cachedPlatforms; 58 private HashSet listeners; 59 60 61 public JavaPlatformManager() { 62 } 63 64 67 public static synchronized JavaPlatformManager getDefault() { 68 if (instance == null) 69 instance = new JavaPlatformManager(); 70 71 return instance; 72 } 73 74 78 public JavaPlatform getDefaultPlatform() { 79 Collection instances = this.getProviders (); 80 for (Iterator 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 93 public synchronized JavaPlatform[] getInstalledPlatforms() { 94 if (cachedPlatforms == null) { 95 Collection instances = this.getProviders(); 96 cachedPlatforms = new HashSet (); 97 for (Iterator 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 119 public JavaPlatform[] getPlatforms (String platformDisplayName, Specification platformSpec) { 120 JavaPlatform[] platforms = getInstalledPlatforms(); 121 Collection result = new ArrayList (); 122 for (int i = 0; i < platforms.length; i++) { 123 String name = platformDisplayName == null ? null : platforms[i].getDisplayName(); Specification spec = platformSpec == null ? null : platforms[i].getSpecification(); 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 138 public synchronized void addPropertyChangeListener (PropertyChangeListener l) { 139 assert l != null : "Listener can not be null"; if (this.listeners == null) { 141 this.listeners = new HashSet (); 142 } 143 this.listeners.add (l); 144 } 145 146 150 public synchronized void removePropertyChangeListener (PropertyChangeListener l) { 151 assert l != null : "Listener can not be null"; if (this.listeners == null) { 153 return; 154 } 155 this.listeners.remove (l); 156 } 157 158 private void firePropertyChange (String property) { 159 Iterator it; 160 synchronized (this) { 161 if (this.listeners == null) { 162 return; 163 } 164 it = ((Set )this.listeners.clone()).iterator(); 165 } 166 PropertyChangeEvent event = new PropertyChangeEvent (this, property, null, null); 167 while (it.hasNext()) { 168 ((PropertyChangeListener )it.next()).propertyChange (event); 169 } 170 } 171 172 private static boolean compatible (Specification platformSpec, Specification query) { 173 String 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 covered = new HashSet (); 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 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 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 () { 226 public void propertyChange(PropertyChangeEvent evt) { 227 JavaPlatformManager.this.resetCache (false); 228 JavaPlatformManager.this.firePropertyChange(PROP_INSTALLED_PLATFORMS); 229 } 230 }; 231 } 232 Collection instances = this.providers.allInstances(); 233 Collection toAdd = new HashSet (instances); 234 toAdd.removeAll (this.lastProviders); 235 Collection toRemove = new HashSet (this.lastProviders); 236 toRemove.removeAll (instances); 237 for (Iterator it = toRemove.iterator(); it.hasNext();) { 238 JavaPlatformProvider provider = (JavaPlatformProvider) it.next (); 239 provider.removePropertyChangeListener (pListener); 240 } 241 for (Iterator 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 |