KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > classpath > BootClassPathImplementation


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 package org.netbeans.modules.web.project.classpath;
20
21 import java.beans.PropertyChangeEvent JavaDoc;
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 JavaDoc;
30 import java.beans.PropertyChangeSupport JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Collections JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
36 import org.openide.util.WeakListeners;
37
38 final class BootClassPathImplementation implements ClassPathImplementation, PropertyChangeListener JavaDoc {
39
40     private static final String JavaDoc PLATFORM_ACTIVE = "platform.active"; //NOI18N
41
private static final String JavaDoc ANT_NAME = "platform.ant.name"; //NOI18N
42
private static final String JavaDoc J2SE = "j2se"; //NOI18N
43

44     private final PropertyEvaluator evaluator;
45     private JavaPlatformManager platformManager;
46     //name of project active platform
47
private String JavaDoc activePlatformName;
48     //active platform is valid (not broken reference)
49
private boolean isActivePlatformValid;
50     private List JavaDoc resourcesCache;
51     private PropertyChangeSupport JavaDoc support = new PropertyChangeSupport JavaDoc(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 JavaDoc /*<PathResourceImplementation>*/ getResources() {
60         if (this.resourcesCache == null) {
61             JavaPlatform jp = findActivePlatform ();
62             if (jp != null) {
63                 //TODO: May also listen on CP, but from Platform it should be fixed.
64
ClassPath cp = jp.getBootstrapLibraries();
65                 List JavaDoc entries = cp.entries();
66                 ArrayList JavaDoc result = new ArrayList JavaDoc (entries.size());
67                 for (Iterator JavaDoc 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 JavaDoc listener) {
81         this.support.addPropertyChangeListener (listener);
82     }
83
84     public void removePropertyChangeListener(PropertyChangeListener JavaDoc 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 JavaDoc antName = (String JavaDoc) 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             //Platform not found, return the default platform and listen
106
//on broken reference resolution
107
this.isActivePlatformValid = false;
108             return null; //this.platformManager.getDefaultPlatform ();
109
} else {
110             //Platform not set => default platform
111
return this.platformManager.getDefaultPlatform();
112         }
113     }
114     
115     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
116         if (evt.getSource() == this.evaluator && evt.getPropertyName().equals(PLATFORM_ACTIVE)) {
117             //Active platform was changed
118
resetCache ();
119         }
120         else if (evt.getSource() == this.platformManager && JavaPlatformManager.PROP_INSTALLED_PLATFORMS.equals(evt.getPropertyName()) && activePlatformName != null) {
121             //Platform definitions were changed, check if the platform was not resolved or deleted
122
if (this.isActivePlatformValid) {
123                 JavaPlatform[] j2sePlatforms = this.platformManager.getPlatforms(null,new Specification("j2se",null)); //NOI18N
124
boolean found = false;
125                 for (int i=0; i< j2sePlatforms.length; i++) {
126                     String JavaDoc antName = (String JavaDoc) j2sePlatforms[i].getProperties().get("platform.ant.name"); //NOI18N
127
if (antName != null && antName.equals(this.activePlatformName)) {
128                         found = true;
129                     }
130                 }
131                 if (!found) {
132                     //the platform was not removed
133
this.resetCache();
134                 }
135             }
136             else {
137                 JavaPlatform[] j2sePlatforms = this.platformManager.getPlatforms(null,new Specification("j2se",null)); //NOI18N
138
for (int i=0; i< j2sePlatforms.length; i++) {
139                     String JavaDoc antName = (String JavaDoc) j2sePlatforms[i].getProperties().get("platform.ant.name"); //NOI18N
140
if (antName != null && antName.equals(this.activePlatformName)) {
141                         this.resetCache();
142                         break;
143                     }
144                 }
145             }
146
147         }
148     }
149     
150     /**
151      * Resets the cache and firesPropertyChange
152      */

153     private void resetCache () {
154         synchronized (this) {
155             resourcesCache = null;
156         }
157         support.firePropertyChange(PROP_RESOURCES, null, null);
158     }
159     
160 }
161
Popular Tags