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.modules.mobility.project.queries;
043:
044: import java.io.File;
045: import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation;
046: import org.netbeans.spi.project.support.ant.AntProjectHelper;
047: import org.openide.ErrorManager;
048: import org.openide.filesystems.FileObject;
049: import java.net.URL;
050: import java.net.MalformedURLException;
051: import java.util.ArrayList;
052: import java.util.Arrays;
053: import java.util.Collections;
054: import java.util.HashSet;
055: import java.util.Set;
056: import org.netbeans.api.java.queries.SourceForBinaryQuery;
057: import org.netbeans.api.project.ProjectUtils;
058: import org.netbeans.modules.mobility.project.J2MEProjectUtils;
059: import org.netbeans.modules.mobility.project.J2MEProject;
060: import org.netbeans.modules.mobility.project.ui.CyclicDependencyWarningPanel;
061: import org.netbeans.spi.project.support.ant.PropertyUtils;
062: import org.netbeans.spi.project.support.ant.AntProjectListener;
063: import org.netbeans.spi.project.support.ant.AntProjectEvent;
064: import org.openide.filesystems.URLMapper;
065: import org.openide.util.WeakListeners;
066:
067: import javax.swing.event.ChangeListener;
068: import javax.swing.event.ChangeEvent;
069: import org.openide.util.RequestProcessor;
070:
071: public class CompiledSourceForBinaryQuery implements
072: SourceForBinaryQueryImplementation {
073:
074: protected J2MEProject project;
075: protected final Set<Thread> threads;
076: protected final AntProjectHelper helper;
077:
078: public CompiledSourceForBinaryQuery(J2MEProject project,
079: AntProjectHelper helper) {
080: this .project = project;
081: this .helper = helper;
082: this .threads = Collections
083: .synchronizedSet(new HashSet<Thread>());
084: }
085:
086: public SourceForBinaryQuery.Result findSourceRoots(
087: final URL binaryRoot) {
088:
089: class R implements SourceForBinaryQuery.Result,
090: AntProjectListener {
091:
092: ArrayList<ChangeListener> listeners = new ArrayList<ChangeListener>();
093: private FileObject[] cache = null;
094: final private AntProjectListener antProjectListener;
095: final private transient Object lock = new Object();
096:
097: public R(AntProjectHelper helper) {
098: antProjectListener = WeakListeners.create(
099: AntProjectListener.class, this , helper);
100: helper.addAntProjectListener(antProjectListener);
101: }
102:
103: public FileObject[] getRoots() {
104: FileObject[] fo = cache;
105: if (fo == null) {
106: fo = createRoots();
107: synchronized (lock) {
108: if (cache != null)
109: cache = fo;
110: }
111: }
112: return fo;
113: }
114:
115: private FileObject[] createRoots() {
116: final ArrayList<FileObject> roots = new ArrayList<FileObject>();
117: try {
118: final URL projectRoot = URLMapper.findURL(helper
119: .getProjectDirectory(), URLMapper.EXTERNAL);
120: URL distRoot = helper.resolveFile("dist").toURI()
121: .toURL(); //NOI18N
122: URL buildRoot = helper.resolveFile("build").toURI()
123: .toURL(); //NOI18N
124: if (J2MEProjectUtils.isParentOf(distRoot,
125: binaryRoot)
126: || J2MEProjectUtils.isParentOf(buildRoot,
127: binaryRoot)) {
128: final String srcPath = helper
129: .getStandardPropertyEvaluator()
130: .getProperty("src.dir"); //NOI18N
131: final FileObject src = srcPath == null ? null
132: : helper.resolveFileObject(srcPath);
133: if (src != null)
134: roots.add(src);
135: final String cfg = J2MEProjectUtils
136: .detectConfiguration(projectRoot,
137: binaryRoot);
138: String path = J2MEProjectUtils
139: .evaluateProperty(helper,
140: "libs.classpath", cfg); //NOI18N
141: if (path != null)
142: path = helper.resolvePath(path);
143: if (path != null) {
144: final String p[] = PropertyUtils
145: .tokenizePath(path);
146: for (int i = 0; i < p.length; i++)
147: try {
148: final URL url = J2MEProjectUtils
149: .wrapJar(new File(p[i])
150: .toURI().toURL());
151: if (url != null
152: && !J2MEProjectUtils
153: .isParentOf(
154: projectRoot,
155: url)) {
156: if (threads.contains(Thread
157: .currentThread())) {
158: CyclicDependencyWarningPanel
159: .showWarning(ProjectUtils
160: .getInformation(
161: project)
162: .getDisplayName());
163: return new FileObject[0];
164: }
165: try {
166: threads.add(Thread
167: .currentThread());
168: final SourceForBinaryQuery.Result result = SourceForBinaryQuery
169: .findSourceRoots(url);
170: if (result != null)
171: roots
172: .addAll(Arrays
173: .asList(result
174: .getRoots()));
175: } finally {
176: threads.remove(Thread
177: .currentThread());
178: }
179: }
180: } catch (MalformedURLException mue) {
181: }
182: }
183: }
184: } catch (MalformedURLException mue) {
185: ErrorManager.getDefault().notify(mue);
186: }
187: return roots.toArray(new FileObject[roots.size()]);
188: }
189:
190: public void addChangeListener(ChangeListener l) {
191: synchronized (listeners) {
192: listeners.add(l);
193: }
194: }
195:
196: public void removeChangeListener(ChangeListener l) {
197: synchronized (listeners) {
198: listeners.remove(l);
199: }
200: }
201:
202: private void fireChanged() {
203: RequestProcessor.getDefault().post(new Runnable() {
204: public void run() {
205: synchronized (listeners) {
206: final ChangeEvent event = new ChangeEvent(
207: this );
208: for (int i = 0; i < listeners.size(); i++) {
209: final ChangeListener changeListener = listeners
210: .get(i);
211: changeListener.stateChanged(event);
212: }
213: }
214: }
215: });
216: }
217:
218: public void configurationXmlChanged(
219: @SuppressWarnings("unused")
220: AntProjectEvent event) {
221: }
222:
223: public void propertiesChanged(@SuppressWarnings("unused")
224: AntProjectEvent event) {
225: synchronized (lock) {
226: cache = null;
227: }
228: fireChanged();
229: }
230:
231: }
232:
233: try {
234: URL distRoot = helper.resolveFile("dist").toURI().toURL(); //NOI18N
235: URL buildRoot = helper.resolveFile("build").toURI().toURL(); //NOI18N
236: if (J2MEProjectUtils.isParentOf(distRoot, binaryRoot)
237: || J2MEProjectUtils.isParentOf(buildRoot,
238: binaryRoot))
239: return new R(helper);
240: } catch (MalformedURLException mue) {
241: }
242: return null;
243: }
244:
245: }
|