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: package org.netbeans.modules.ruby.railsprojects.classpath;
042:
043: import java.beans.PropertyChangeEvent;
044: import java.io.File;
045: import java.net.MalformedURLException;
046: import java.util.List;
047: import java.util.ArrayList;
048: import java.util.Collections;
049: import java.beans.PropertyChangeListener;
050: import java.beans.PropertyChangeSupport;
051: import java.net.URL;
052: import org.netbeans.modules.ruby.railsprojects.SourceRoots;
053: import org.netbeans.modules.ruby.spi.project.support.rake.PropertyEvaluator;
054: import org.netbeans.modules.ruby.spi.project.support.rake.RakeProjectHelper;
055: import org.netbeans.modules.gsfpath.spi.classpath.ClassPathImplementation;
056: import org.netbeans.modules.gsfpath.spi.classpath.PathResourceImplementation;
057: import org.netbeans.modules.gsfpath.spi.classpath.support.ClassPathSupport;
058: import org.openide.ErrorManager;
059:
060: /**
061: * Implementation of a single classpath that is derived from one Ant property.
062: */
063: final class SourcePathImplementation implements
064: ClassPathImplementation, PropertyChangeListener {
065:
066: private static final String PROP_BUILD_DIR = "build.dir"; //NOI18N
067:
068: private final PropertyChangeSupport support = new PropertyChangeSupport(
069: this );
070: private List<PathResourceImplementation> resources;
071: private final SourceRoots sourceRoots;
072: private final RakeProjectHelper projectHelper;
073: private final PropertyEvaluator evaluator;
074: private String urlString;
075:
076: /**
077: * Construct the implementation.
078: * @param sourceRoots used to get the roots information and events
079: */
080: public SourcePathImplementation(SourceRoots sourceRoots) {
081: assert sourceRoots != null;
082: this .sourceRoots = sourceRoots;
083: this .projectHelper = null;
084: this .evaluator = null;
085: sourceRoots.addPropertyChangeListener(this );
086: }
087:
088: /**
089: * Construct the implementation.
090: * @param sourceRoots used to get the roots information and events
091: * @param projectHelper used to obtain the project root
092: */
093: public SourcePathImplementation(SourceRoots sourceRoots,
094: RakeProjectHelper projectHelper, PropertyEvaluator evaluator) {
095: assert sourceRoots != null && projectHelper != null
096: && evaluator != null;
097: this .sourceRoots = sourceRoots;
098: sourceRoots.addPropertyChangeListener(this );
099: this .projectHelper = projectHelper;
100: this .evaluator = evaluator;
101: evaluator.addPropertyChangeListener(this );
102: }
103:
104: public List<PathResourceImplementation> getResources() {
105: synchronized (this ) {
106: if (this .resources != null) {
107: return this .resources;
108: }
109: }
110: URL[] roots = sourceRoots.getRootURLs();
111: synchronized (this ) {
112: if (this .resources == null) {
113: List<PathResourceImplementation> result = new ArrayList<PathResourceImplementation>(
114: roots.length);
115: for (URL root : roots) {
116: urlString = root.toExternalForm();
117: if (urlString.endsWith("/vendor/")) {
118: continue;
119: }
120: // Issue #123178 - GoldSpike copies ALL relevant files into a new directory, WEB-INF,
121: // which we don't want to scan!
122: if (urlString.endsWith("/WEB-INF/")) {
123: continue;
124: }
125: result.add(ClassPathSupport.createResource(root));
126: }
127: // adds java artifacts generated by wscompile and wsimport to resources to be available for code completion
128: if (projectHelper != null) {
129: try {
130: String buildDir = this .evaluator
131: .getProperty(PROP_BUILD_DIR);
132: if (buildDir != null) {
133: // generated/wsclient
134: File f = new File(this .projectHelper
135: .resolveFile(buildDir),
136: "generated/wsclient"); //NOI18N
137: URL url = f.toURI().toURL();
138: if (!f.exists()) { //NOI18N
139: assert !url.toExternalForm().endsWith(
140: "/"); //NOI18N
141: url = new URL(
142: url.toExternalForm() + '/'); //NOI18N
143: }
144: result.add(ClassPathSupport
145: .createResource(url));
146:
147: // generated/wsimport/client
148: f = new File(projectHelper
149: .resolveFile(buildDir),
150: "generated/wsimport/client"); //NOI18N
151: url = f.toURI().toURL();
152: if (!f.exists()) { //NOI18N
153: assert !url.toExternalForm().endsWith(
154: "/"); //NOI18N
155: url = new URL(
156: url.toExternalForm() + '/'); //NOI18N
157: }
158: result.add(ClassPathSupport
159: .createResource(url));
160: }
161: } catch (MalformedURLException ex) {
162: ErrorManager.getDefault().notify(ex);
163: }
164: }
165: this .resources = Collections.unmodifiableList(result);
166: }
167: return this .resources;
168: }
169: }
170:
171: public void addPropertyChangeListener(
172: PropertyChangeListener listener) {
173: support.addPropertyChangeListener(listener);
174: }
175:
176: public void removePropertyChangeListener(
177: PropertyChangeListener listener) {
178: support.removePropertyChangeListener(listener);
179: }
180:
181: public void propertyChange(PropertyChangeEvent evt) {
182: if (SourceRoots.PROP_ROOTS.equals(evt.getPropertyName())) {
183: synchronized (this) {
184: this.resources = null;
185: }
186: this.support.firePropertyChange(PROP_RESOURCES, null, null);
187: } else if (this.evaluator != null
188: && evt.getSource() == this.evaluator
189: && (evt.getPropertyName() == null || PROP_BUILD_DIR
190: .equals(evt.getPropertyName()))) {
191: synchronized (this) {
192: this.resources = null;
193: }
194: this.support.firePropertyChange(PROP_RESOURCES, null, null);
195: }
196: }
197:
198: }
|