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