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.spi.java.project.classpath.support;
043:
044: import java.beans.PropertyChangeEvent;
045: import java.beans.PropertyChangeListener;
046: import java.beans.PropertyChangeSupport;
047: import java.io.File;
048: import java.net.MalformedURLException;
049: import java.net.URL;
050: import java.util.ArrayList;
051: import java.util.Arrays;
052: import java.util.Collections;
053: import java.util.List;
054: import java.util.concurrent.atomic.AtomicBoolean;
055: import org.netbeans.api.project.ProjectManager;
056: import org.netbeans.spi.java.classpath.ClassPathImplementation;
057: import org.netbeans.spi.java.classpath.PathResourceImplementation;
058: import org.netbeans.spi.java.classpath.support.ClassPathSupport;
059: import org.netbeans.spi.project.support.ant.PropertyEvaluator;
060: import org.netbeans.spi.project.support.ant.PropertyUtils;
061: import org.openide.ErrorManager;
062: import org.openide.filesystems.FileUtil;
063: import org.openide.util.WeakListeners;
064:
065: /**
066: * Implementation of a single classpath that is derived from list of Ant properties.
067: */
068: final class ProjectClassPathImplementation implements
069: ClassPathImplementation, PropertyChangeListener, Runnable {
070:
071: private final PropertyChangeSupport support = new PropertyChangeSupport(
072: this );
073: private final File projectFolder;
074: private List<PathResourceImplementation> resources;
075: private final PropertyEvaluator evaluator;
076: private AtomicBoolean dirty = new AtomicBoolean();
077: private final List<String> propertyNames;
078:
079: /**
080: * Construct the implementation.
081: * @param projectFolder the folder containing the project, used to resolve relative paths
082: * @param propertyNames the names of an Ant properties which will supply the classpath
083: * @param evaluator a property evaluator used to find the value of the classpath
084: */
085: public ProjectClassPathImplementation(File projectFolder,
086: String[] propertyNames, PropertyEvaluator evaluator) {
087: assert projectFolder != null && propertyNames != null
088: && evaluator != null;
089: this .projectFolder = projectFolder;
090: this .evaluator = evaluator;
091: this .propertyNames = Arrays.asList(propertyNames);
092: this .resources = this .getPath();
093: evaluator.addPropertyChangeListener(WeakListeners
094: .propertyChange(this , evaluator));
095: }
096:
097: public synchronized List<PathResourceImplementation> getResources() {
098: assert this .resources != null;
099: return this .resources;
100: }
101:
102: public void addPropertyChangeListener(
103: PropertyChangeListener listener) {
104: support.addPropertyChangeListener(listener);
105: }
106:
107: public void removePropertyChangeListener(
108: PropertyChangeListener listener) {
109: support.removePropertyChangeListener(listener);
110: }
111:
112: public void propertyChange(PropertyChangeEvent evt) {
113: String prop = evt.getPropertyName();
114: if (prop != null
115: && !propertyNames.contains(evt.getPropertyName())) {
116: // Not interesting to us.
117: return;
118: }
119: // Coalesce changes; can come in fast after huge CP changes (#47910):
120: if (!dirty.getAndSet(true)) {
121: ProjectManager.mutex().postReadRequest(this );
122: }
123: }
124:
125: public void run() {
126: dirty.set(false);
127: List<PathResourceImplementation> newRoots = getPath();
128: boolean fire = false;
129: synchronized (this ) {
130: if (!this .resources.equals(newRoots)) {
131: this .resources = newRoots;
132: fire = true;
133: }
134: }
135: if (fire) {
136: support.firePropertyChange(PROP_RESOURCES, null, null);
137: }
138: }
139:
140: private List<PathResourceImplementation> getPath() {
141: List<PathResourceImplementation> result = new ArrayList<PathResourceImplementation>();
142: for (String p : propertyNames) {
143: String prop = evaluator.getProperty(p);
144: if (prop != null) {
145: for (String piece : PropertyUtils.tokenizePath(prop)) {
146: File f = PropertyUtils.resolveFile(
147: this .projectFolder, piece);
148: try {
149: URL entry = f.toURI().toURL();
150: if (FileUtil.isArchiveFile(entry)
151: || (f.isFile() && f.length() < 4)) { //XXX: Not yet closed archive file
152: entry = FileUtil.getArchiveRoot(entry);
153: } else if (!f.exists()) {
154: // if file does not exist (e.g. build/classes folder
155: // was not created yet) then corresponding File will
156: // not be ended with slash. Fix that.
157: assert !entry.toExternalForm()
158: .endsWith("/") : f; // NOI18N
159: entry = new URL(entry.toExternalForm()
160: + "/"); // NOI18N
161: } else if (f.isFile()) {
162: ErrorManager
163: .getDefault()
164: .log(
165: ErrorManager.ERROR,
166: "ProjectClassPathImplementation: file: "
167: + f
168: .getAbsolutePath()
169: + " is not a valid archive file."); //NOI18N
170: continue;
171: }
172: result.add(ClassPathSupport
173: .createResource(entry));
174: } catch (MalformedURLException mue) {
175: assert false : mue;
176: }
177: }
178: }
179: }
180: return Collections.unmodifiableList(result);
181: }
182:
183: }
|