KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > deployment > impl > query > J2eePlatformSourceForBinaryQuery


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
20 package org.netbeans.modules.j2ee.deployment.impl.query;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import javax.swing.event.ChangeEvent JavaDoc;
31 import javax.swing.event.ChangeListener JavaDoc;
32
33 import org.netbeans.api.java.queries.SourceForBinaryQuery;
34 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
35 import org.netbeans.modules.j2ee.deployment.impl.ServerInstance;
36 import org.netbeans.modules.j2ee.deployment.impl.ServerRegistry;
37 import org.netbeans.modules.j2ee.deployment.common.api.J2eeLibraryTypeProvider;
38 import org.netbeans.modules.j2ee.deployment.plugins.api.J2eePlatformImpl;
39 import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation;
40 import org.netbeans.spi.project.libraries.LibraryImplementation;
41 import org.openide.ErrorManager;
42 import org.openide.filesystems.FileObject;
43 import org.openide.filesystems.FileStateInvalidException;
44 import org.openide.filesystems.FileUtil;
45 import org.openide.filesystems.URLMapper;
46 import org.openide.util.WeakListeners;
47
48
49 /**
50  * Finds the locations of sources for various libraries.
51  * @since 1.5
52  */

53 public class J2eePlatformSourceForBinaryQuery implements SourceForBinaryQueryImplementation {
54
55     private final Map JavaDoc/*<URL,SourceForBinaryQuery.Result>*/ cache = new HashMap JavaDoc();
56     private final Map JavaDoc/*<URL,URL>*/ normalizedURLCache = new HashMap JavaDoc();
57
58     /** Default constructor for lookup. */
59     public J2eePlatformSourceForBinaryQuery() {}
60
61     public SourceForBinaryQuery.Result findSourceRoots(URL JavaDoc binaryRoot) {
62         SourceForBinaryQuery.Result res = (SourceForBinaryQuery.Result) this.cache.get (binaryRoot);
63         if (res != null) {
64             return res;
65         }
66         boolean isNormalizedURL = isNormalizedURL(binaryRoot);
67         
68         String JavaDoc[] serverInstanceIDs = Deployment.getDefault().getServerInstanceIDs();
69         ServerRegistry servReg = ServerRegistry.getInstance();
70         for (int i=0; i < serverInstanceIDs.length; i++) {
71             ServerInstance serInst = servReg.getServerInstance(serverInstanceIDs[i]);
72             J2eePlatformImpl platformImpl = serInst.getJ2eePlatformImpl();
73             if (platformImpl == null) continue; // TODO this will be removed, when AppServPlg will be ready
74
LibraryImplementation[] libs = platformImpl.getLibraries();
75             for (int j=0; j< libs.length; j++) {
76                 String JavaDoc type = libs[j].getType ();
77                 List JavaDoc classes = libs[j].getContent(J2eeLibraryTypeProvider.VOLUME_TYPE_CLASSPATH); //NOI18N
78
for (Iterator JavaDoc it = classes.iterator(); it.hasNext();) {
79                     URL JavaDoc entry = (URL JavaDoc) it.next();
80                     URL JavaDoc normalizedEntry;
81                     if (isNormalizedURL) {
82                         normalizedEntry = getNormalizedURL(entry);
83                     }
84                     else {
85                         normalizedEntry = entry;
86                     }
87                     if (normalizedEntry != null && normalizedEntry.equals(binaryRoot)) {
88                         res = new Result(entry, libs[j]);
89                         cache.put (binaryRoot, res);
90                         return res;
91                     }
92                 }
93             }
94         }
95         return null;
96     }
97     
98     
99     private URL JavaDoc getNormalizedURL (URL JavaDoc url) {
100         //URL is already nornalized, return it
101
if (isNormalizedURL(url)) {
102             return url;
103         }
104         //Todo: Should listen on the LibrariesManager and cleanup cache
105
// in this case the search can use the cache onle and can be faster
106
// from O(n) to O(ln(n))
107
URL JavaDoc normalizedURL = (URL JavaDoc) this.normalizedURLCache.get (url);
108         if (normalizedURL == null) {
109             FileObject fo = URLMapper.findFileObject(url);
110             if (fo != null) {
111                 try {
112                     normalizedURL = fo.getURL();
113                     this.normalizedURLCache.put (url, normalizedURL);
114                 } catch (FileStateInvalidException e) {
115                     ErrorManager.getDefault().notify(e);
116                 }
117             }
118         }
119         return normalizedURL;
120     }
121     
122     /**
123      * Returns true if the given URL is file based, it is already
124      * resolved either into file URL or jar URL with file path.
125      * @param URL url
126      * @return true if the URL is normal
127      */

128     private static boolean isNormalizedURL (URL JavaDoc url) {
129         if ("jar".equals(url.getProtocol())) { //NOI18N
130
url = FileUtil.getArchiveFile(url);
131         }
132         return "file".equals(url.getProtocol()); //NOI18N
133
}
134     
135     
136     private static class Result implements SourceForBinaryQuery.Result, PropertyChangeListener JavaDoc {
137         
138         private LibraryImplementation lib;
139         private URL JavaDoc entry;
140         private ArrayList JavaDoc listeners;
141         private FileObject[] cache;
142         
143         public Result (URL JavaDoc queryFor, LibraryImplementation aLib) {
144             this.entry = queryFor;
145             this.lib = aLib;
146             this.lib.addPropertyChangeListener ((PropertyChangeListener JavaDoc)WeakListeners.create(PropertyChangeListener JavaDoc.class,this,this.lib));
147         }
148         
149         public synchronized FileObject[] getRoots () {
150             if (cache == null) {
151                 if (this.lib.getContent(J2eeLibraryTypeProvider.VOLUME_TYPE_CLASSPATH).contains (entry)) {
152                     List JavaDoc src = this.lib.getContent(J2eeLibraryTypeProvider.VOLUME_TYPE_SRC);
153                     List JavaDoc result = new ArrayList JavaDoc ();
154                     for (Iterator JavaDoc sit = src.iterator(); sit.hasNext();) {
155                         FileObject sourceRootURL = URLMapper.findFileObject((URL JavaDoc) sit.next());
156                         if (sourceRootURL!=null) {
157                             result.add (sourceRootURL);
158                         }
159                     }
160                     this.cache = (FileObject[]) result.toArray(new FileObject[result.size()]);
161                 }
162                 else {
163                     this.cache = new FileObject[0];
164                 }
165             }
166             return this.cache;
167         }
168         
169         public synchronized void addChangeListener (ChangeListener JavaDoc l) {
170             assert l != null : "Listener cannot be null"; // NOI18N
171
if (this.listeners == null) {
172                 this.listeners = new ArrayList JavaDoc ();
173             }
174             this.listeners.add (l);
175         }
176         
177         public synchronized void removeChangeListener (ChangeListener JavaDoc l) {
178             assert l != null : "Listener cannot be null"; // NOI18N
179
if (this.listeners == null) {
180                 return;
181             }
182             this.listeners.remove (l);
183         }
184         
185         public void propertyChange (PropertyChangeEvent JavaDoc event) {
186             if (LibraryImplementation.PROP_CONTENT.equals(event.getPropertyName())) {
187                 synchronized (this) {
188                     this.cache = null;
189                 }
190                 this.fireChange ();
191             }
192         }
193         
194         private void fireChange () {
195             Iterator JavaDoc it = null;
196             synchronized (this) {
197                 if (this.listeners == null) {
198                     return;
199                 }
200                 it = ((ArrayList JavaDoc)this.listeners.clone()).iterator();
201             }
202             ChangeEvent JavaDoc event = new ChangeEvent JavaDoc (this);
203             while (it.hasNext ()) {
204                 ((ChangeListener JavaDoc)it.next()).stateChanged(event);
205             }
206         }
207         
208     }
209     
210 }
211
Popular Tags