1 19 package org.netbeans.modules.j2ee.ejbjarproject.classpath; 20 21 import java.beans.PropertyChangeEvent ; 22 import org.netbeans.spi.java.classpath.ClassPathImplementation; 23 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 24 import org.netbeans.api.java.platform.JavaPlatform; 25 import org.netbeans.api.java.platform.JavaPlatformManager; 26 import org.netbeans.api.java.platform.Specification; 27 import org.netbeans.api.java.classpath.ClassPath; 28 29 import java.beans.PropertyChangeListener ; 30 import java.beans.PropertyChangeSupport ; 31 import java.util.List ; 32 import java.util.ArrayList ; 33 import java.util.Collections ; 34 import java.util.Iterator ; 35 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 36 import org.openide.util.WeakListeners; 37 38 final class BootClassPathImplementation implements ClassPathImplementation, PropertyChangeListener { 39 40 private static final String PLATFORM_ACTIVE = "platform.active"; private static final String ANT_NAME = "platform.ant.name"; private static final String J2SE = "j2se"; 44 private final PropertyEvaluator evaluator; 45 private JavaPlatformManager platformManager; 46 private String activePlatformName; 48 private boolean isActivePlatformValid; 50 private List resourcesCache; 51 private PropertyChangeSupport support = new PropertyChangeSupport (this); 52 53 public BootClassPathImplementation(PropertyEvaluator evaluator) { 54 assert evaluator != null; 55 this.evaluator = evaluator; 56 evaluator.addPropertyChangeListener(WeakListeners.propertyChange(this, evaluator)); 57 } 58 59 public synchronized List getResources() { 60 if (this.resourcesCache == null) { 61 JavaPlatform jp = findActivePlatform (); 62 if (jp != null) { 63 ClassPath cp = jp.getBootstrapLibraries(); 65 List entries = cp.entries(); 66 ArrayList result = new ArrayList (entries.size()); 67 for (Iterator it = entries.iterator(); it.hasNext();) { 68 ClassPath.Entry entry = (ClassPath.Entry) it.next(); 69 result.add (ClassPathSupport.createResource(entry.getURL())); 70 } 71 resourcesCache = Collections.unmodifiableList (result); 72 } 73 else { 74 resourcesCache = Collections.EMPTY_LIST; 75 } 76 } 77 return this.resourcesCache; 78 } 79 80 public void addPropertyChangeListener(PropertyChangeListener listener) { 81 this.support.addPropertyChangeListener (listener); 82 } 83 84 public void removePropertyChangeListener(PropertyChangeListener listener) { 85 this.support.removePropertyChangeListener (listener); 86 } 87 88 private JavaPlatform findActivePlatform () { 89 if (this.platformManager == null) { 90 this.platformManager = JavaPlatformManager.getDefault(); 91 this.platformManager.addPropertyChangeListener(WeakListeners.propertyChange(this, this.platformManager)); 92 } 93 this.activePlatformName = evaluator.getProperty(PLATFORM_ACTIVE); 94 if (activePlatformName!=null) { 95 JavaPlatform[] installedPlatforms = this.platformManager.getInstalledPlatforms(); 96 for (int i = 0; i< installedPlatforms.length; i++) { 97 Specification spec = installedPlatforms[i].getSpecification(); 98 String antName = (String ) installedPlatforms[i].getProperties().get (ANT_NAME); 99 if (J2SE.equalsIgnoreCase(spec.getName()) 100 && activePlatformName.equals(antName)) { 101 this.isActivePlatformValid = true; 102 return installedPlatforms[i]; 103 } 104 } 105 this.isActivePlatformValid = false; 108 return null; } else { 110 return this.platformManager.getDefaultPlatform(); 112 } 113 } 114 115 public void propertyChange(PropertyChangeEvent evt) { 116 if (evt.getSource() == this.evaluator && evt.getPropertyName().equals(PLATFORM_ACTIVE)) { 117 resetCache (); 119 } 120 else if (evt.getSource() == this.platformManager && JavaPlatformManager.PROP_INSTALLED_PLATFORMS.equals(evt.getPropertyName()) && activePlatformName != null) { 121 if (this.isActivePlatformValid) { 123 JavaPlatform[] j2sePlatforms = this.platformManager.getPlatforms(null,new Specification("j2se",null)); boolean found = false; 125 for (int i=0; i< j2sePlatforms.length; i++) { 126 String antName = (String ) j2sePlatforms[i].getProperties().get("platform.ant.name"); if (antName != null && antName.equals(this.activePlatformName)) { 128 found = true; 129 } 130 } 131 if (!found) { 132 this.resetCache(); 134 } 135 } 136 else { 137 JavaPlatform[] j2sePlatforms = this.platformManager.getPlatforms(null,new Specification("j2se",null)); for (int i=0; i< j2sePlatforms.length; i++) { 139 String antName = (String ) j2sePlatforms[i].getProperties().get("platform.ant.name"); if (antName != null && antName.equals(this.activePlatformName)) { 141 this.resetCache(); 142 break; 143 } 144 } 145 } 146 147 } 148 } 149 150 153 private void resetCache () { 154 synchronized (this) { 155 resourcesCache = null; 156 } 157 support.firePropertyChange(PROP_RESOURCES, null, null); 158 } 159 160 } 161 | Popular Tags |