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-2007 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: package org.netbeans.modules.java.api.common.classpath;
042:
043: import java.beans.PropertyChangeEvent;
044: import org.netbeans.spi.java.classpath.ClassPathImplementation;
045: import org.netbeans.spi.java.classpath.PathResourceImplementation;
046: import org.netbeans.spi.java.classpath.support.ClassPathSupport;
047: import org.netbeans.api.java.platform.JavaPlatform;
048: import org.netbeans.api.java.platform.JavaPlatformManager;
049: import java.beans.PropertyChangeListener;
050: import java.beans.PropertyChangeSupport;
051: import java.util.List;
052: import java.util.ArrayList;
053: import java.util.Collections;
054: import org.netbeans.api.java.classpath.ClassPath;
055: import org.netbeans.modules.java.api.common.util.CommonProjectUtils;
056: import org.netbeans.spi.project.support.ant.PropertyEvaluator;
057: import org.openide.util.WeakListeners;
058:
059: /**
060: * Represent a boot class path. It is possible to listen to the changes of
061: * {@link ClassPathImplementation#PROP_RESOURCES}.
062: * @author Tomas Zezula
063: */
064: public final class BootClassPathImplementation implements
065: ClassPathImplementation, PropertyChangeListener {
066:
067: private static final String PLATFORM_ACTIVE = "platform.active"; // NOI18N
068:
069: private final PropertyEvaluator evaluator;
070: private JavaPlatformManager platformManager;
071: // name of project active platform
072: private String activePlatformName;
073: // active platform is valid (not broken reference)
074: private boolean isActivePlatformValid;
075: private List<PathResourceImplementation> resourcesCache;
076: private long eventId;
077: private PropertyChangeSupport support = new PropertyChangeSupport(
078: this );
079:
080: public BootClassPathImplementation(PropertyEvaluator evaluator) {
081: assert evaluator != null;
082:
083: this .evaluator = evaluator;
084: evaluator.addPropertyChangeListener(WeakListeners
085: .propertyChange(this , evaluator));
086: }
087:
088: /**
089: * @see ClassPathImplementation#getResources()
090: */
091: public List<PathResourceImplementation> getResources() {
092: long currentId;
093: synchronized (this ) {
094: if (resourcesCache != null) {
095: return resourcesCache;
096: }
097: currentId = eventId;
098: }
099:
100: JavaPlatform jp = findActivePlatform();
101: final List<PathResourceImplementation> result = new ArrayList<PathResourceImplementation>();
102: if (jp != null) {
103: // TODO: may also listen on CP, but from Platform it should be fixed
104: final ClassPath cp = jp.getBootstrapLibraries();
105: assert cp != null : jp;
106: for (ClassPath.Entry entry : cp.entries()) {
107: result.add(ClassPathSupport.createResource(entry
108: .getURL()));
109: }
110: }
111:
112: synchronized (this ) {
113: if (currentId == eventId) {
114: if (resourcesCache == null) {
115: resourcesCache = Collections
116: .unmodifiableList(result);
117: }
118: return resourcesCache;
119: }
120: return Collections.unmodifiableList(result);
121: }
122: }
123:
124: /**
125: * Add {@link PropertyChangeListener}, see class description for more information.
126: * @param listener a listener to add.
127: */
128: public void addPropertyChangeListener(
129: PropertyChangeListener listener) {
130: support.addPropertyChangeListener(listener);
131: }
132:
133: /**
134: * Remove {@link PropertyChangeListener}, see class description for more information.
135: * @param listener a listener to remove.
136: */
137: public void removePropertyChangeListener(
138: PropertyChangeListener listener) {
139: support.removePropertyChangeListener(listener);
140: }
141:
142: /**
143: * @see PropertyChangeListener#propertyChange()
144: */
145: public void propertyChange(PropertyChangeEvent evt) {
146: if (evt.getSource() == evaluator
147: && evt.getPropertyName().equals(PLATFORM_ACTIVE)) {
148: // active platform was changed
149: resetCache();
150: } else if (evt.getSource() == platformManager
151: && JavaPlatformManager.PROP_INSTALLED_PLATFORMS
152: .equals(evt.getPropertyName())
153: && activePlatformName != null) {
154: // platform definitions were changed, check if the platform was not resolved or deleted
155: if (isActivePlatformValid) {
156: if (CommonProjectUtils
157: .getActivePlatform(activePlatformName) == null) {
158: // the platform was not removed
159: resetCache();
160: }
161: } else {
162: if (CommonProjectUtils
163: .getActivePlatform(activePlatformName) != null) {
164: resetCache();
165: }
166: }
167: }
168: }
169:
170: private JavaPlatform findActivePlatform() {
171: if (platformManager == null) {
172: platformManager = JavaPlatformManager.getDefault();
173: platformManager.addPropertyChangeListener(WeakListeners
174: .propertyChange(this , platformManager));
175: }
176: activePlatformName = evaluator.getProperty(PLATFORM_ACTIVE);
177: final JavaPlatform activePlatform = CommonProjectUtils
178: .getActivePlatform(activePlatformName);
179: isActivePlatformValid = activePlatform != null;
180: return activePlatform;
181: }
182:
183: private void resetCache() {
184: synchronized (this) {
185: resourcesCache = null;
186: eventId++;
187: }
188: support.firePropertyChange(PROP_RESOURCES, null, null);
189: }
190: }
|