KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > platform > queries > PlatformSourceForBinaryQuery


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.java.platform.queries;
20
21 import java.beans.PropertyChangeEvent JavaDoc;
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import javax.swing.event.ChangeEvent JavaDoc;
30 import javax.swing.event.ChangeListener JavaDoc;
31 import org.openide.filesystems.FileObject;
32 import org.openide.filesystems.FileUtil;
33 import org.openide.filesystems.URLMapper;
34 import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation;
35 import org.netbeans.api.java.classpath.ClassPath;
36 import org.netbeans.api.java.platform.JavaPlatformManager;
37 import org.netbeans.api.java.platform.JavaPlatform;
38 import org.netbeans.api.java.queries.SourceForBinaryQuery;
39 import org.openide.util.Exceptions;
40 import org.openide.util.WeakListeners;
41
42
43 /**
44  * This implementation of the SourceForBinaryQueryImplementation
45  * provides sources for the active platform and project libraries
46  */

47
48 public class PlatformSourceForBinaryQuery implements SourceForBinaryQueryImplementation {
49     
50     private static final String JavaDoc JAR_FILE = "jar:file:"; //NOI18N
51
private static final String JavaDoc RTJAR_PATH = "/jre/lib/rt.jar!/"; //NOI18N
52
private static final String JavaDoc SRC_ZIP = "/src.zip"; //NOI18N
53

54     private Map JavaDoc/*<URL, SourceForBinaryQuery.Result>*/ cache = new HashMap JavaDoc ();
55
56     public PlatformSourceForBinaryQuery () {
57     }
58
59     /**
60      * Tries to locate the source root for given classpath root.
61      * @param binaryRoot the URL of a classpath root (platform supports file and jar protocol)
62      * @return FileObject[], never returns null
63      */

64     public SourceForBinaryQuery.Result findSourceRoots(URL JavaDoc binaryRoot) {
65         SourceForBinaryQuery.Result res = (SourceForBinaryQuery.Result) this.cache.get (binaryRoot);
66         if (res != null) {
67             return res;
68         }
69         JavaPlatformManager mgr = JavaPlatformManager.getDefault();
70         JavaPlatform[] platforms = mgr.getInstalledPlatforms();
71         for (int i=0; i< platforms.length; i++) {
72             ClassPath cp = platforms[i].getBootstrapLibraries();
73             for (Iterator JavaDoc it = cp.entries().iterator(); it.hasNext();) {
74                 ClassPath.Entry entry = (ClassPath.Entry) it.next();
75                 if (entry.getURL().equals (binaryRoot)) {
76                     res = new Result (platforms[i]);
77                     this.cache.put (binaryRoot, res);
78                     return res;
79                 }
80             }
81         }
82         String JavaDoc binaryRootS = binaryRoot.toExternalForm();
83         if (binaryRootS.startsWith(JAR_FILE)) {
84             if (binaryRootS.endsWith(RTJAR_PATH)) {
85                 //Unregistered platform
86
String JavaDoc srcZipS = binaryRootS.substring(4,binaryRootS.length() - RTJAR_PATH.length()) + SRC_ZIP;
87                 try {
88                     URL JavaDoc srcZip = FileUtil.getArchiveRoot(new URL JavaDoc(srcZipS));
89                     FileObject fo = URLMapper.findFileObject(srcZip);
90                     if (fo != null) {
91                         return new UnregisteredPlatformResult (fo);
92                     }
93                 } catch (MalformedURLException JavaDoc mue) {
94                     Exceptions.printStackTrace(mue);
95                 }
96             }
97         }
98         return null;
99     }
100     
101     private static class Result implements SourceForBinaryQuery.Result, PropertyChangeListener JavaDoc {
102                         
103         private JavaPlatform platform;
104         private ArrayList JavaDoc listeners;
105                         
106         public Result (JavaPlatform platform) {
107             this.platform = platform;
108             this.platform.addPropertyChangeListener ((PropertyChangeListener JavaDoc)WeakListeners.create(PropertyChangeListener JavaDoc.class,this,this.platform));
109         }
110                         
111         public FileObject[] getRoots () { //No need for caching, platforms does.
112
ClassPath sources = this.platform.getSourceFolders();
113             return sources.getRoots();
114         }
115                         
116         public synchronized void addChangeListener (ChangeListener JavaDoc l) {
117             assert l != null : "Listener can not be null"; //NOI18N
118
if (this.listeners == null) {
119                 this.listeners = new ArrayList JavaDoc ();
120             }
121             this.listeners.add (l);
122         }
123                         
124         public synchronized void removeChangeListener (ChangeListener JavaDoc l) {
125             assert l != null : "Listener can not be null"; //NOI18N
126
if (this.listeners == null) {
127                 return;
128             }
129             this.listeners.remove (l);
130         }
131         
132         public void propertyChange (PropertyChangeEvent JavaDoc event) {
133             if (JavaPlatform.PROP_SOURCE_FOLDER.equals(event.getPropertyName())) {
134                 this.fireChange ();
135             }
136         }
137         
138         private void fireChange () {
139             Iterator JavaDoc it = null;
140             synchronized (this) {
141                 if (this.listeners == null) {
142                     return;
143                 }
144                 it = ((ArrayList JavaDoc)this.listeners.clone()).iterator ();
145             }
146             ChangeEvent JavaDoc event = new ChangeEvent JavaDoc (this);
147             while (it.hasNext()) {
148                 ((ChangeListener JavaDoc)it.next()).stateChanged(event);
149             }
150         }
151     }
152     
153     private static class UnregisteredPlatformResult implements SourceForBinaryQuery.Result {
154         
155         private FileObject srcRoot;
156         
157         private UnregisteredPlatformResult (FileObject fo) {
158             assert fo != null;
159             srcRoot = fo;
160         }
161     
162         public FileObject[] getRoots() {
163             return srcRoot.isValid() ? new FileObject[] {srcRoot} : new FileObject[0];
164         }
165         
166         public void addChangeListener(ChangeListener JavaDoc l) {
167             //Not supported, no listening.
168
}
169         
170         public void removeChangeListener(ChangeListener JavaDoc l) {
171             //Not supported, no listening.
172
}
173 }}
174
175
Popular Tags