KickJava   Java API By Example, From Geeks To Geeks.

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


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.openide.ErrorManager;
34 import org.openide.filesystems.URLMapper;
35 import org.openide.filesystems.FileObject;
36 import org.openide.filesystems.FileStateInvalidException;
37 import org.openide.filesystems.FileUtil;
38 import org.netbeans.api.java.queries.JavadocForBinaryQuery;
39 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
40 import org.netbeans.modules.j2ee.deployment.impl.ServerInstance;
41 import org.netbeans.modules.j2ee.deployment.impl.ServerRegistry;
42 import org.netbeans.modules.j2ee.deployment.common.api.J2eeLibraryTypeProvider;
43 import org.netbeans.modules.j2ee.deployment.plugins.api.J2eePlatformImpl;
44 import org.netbeans.spi.java.queries.JavadocForBinaryQueryImplementation;
45 import org.netbeans.spi.project.libraries.LibraryImplementation;
46 import org.openide.util.WeakListeners;
47
48
49 /**
50  * Implementation of Javadoc query for the library.
51  * @since 1.5
52  */

53 public class J2eePlatformJavadocForBinaryQuery implements JavadocForBinaryQueryImplementation {
54     
55     private static int MAX_DEPTH = 3;
56     private final Map JavaDoc/*<URL,URL>*/ normalizedURLCache = new HashMap JavaDoc();
57
58     /** Default constructor for lookup. */
59     public J2eePlatformJavadocForBinaryQuery() {
60     }
61
62     public JavadocForBinaryQuery.Result findJavadoc(final URL JavaDoc b) {
63         class R implements JavadocForBinaryQuery.Result, PropertyChangeListener JavaDoc {
64
65             private LibraryImplementation lib;
66             private ArrayList JavaDoc listeners;
67             private URL JavaDoc[] cachedRoots;
68             
69
70             public R (LibraryImplementation lib) {
71                 this.lib = lib;
72                 this.lib.addPropertyChangeListener (WeakListeners.propertyChange(this,this.lib));
73             }
74
75             public synchronized URL JavaDoc[] getRoots() {
76                 if (this.cachedRoots == null) {
77                     List JavaDoc l = lib.getContent(J2eeLibraryTypeProvider.VOLUME_TYPE_JAVADOC);
78                     List JavaDoc result = new ArrayList JavaDoc ();
79                     for (Iterator JavaDoc it = l.iterator(); it.hasNext();) {
80                         URL JavaDoc u = (URL JavaDoc) it.next ();
81                         result.add (getIndexFolder(u));
82                     }
83                     this.cachedRoots = (URL JavaDoc[])result.toArray(new URL JavaDoc[result.size()]);
84                 }
85                 return this.cachedRoots;
86             }
87             
88             public synchronized void addChangeListener(ChangeListener JavaDoc l) {
89                 assert l != null : "Listener can not be null";
90                 if (this.listeners == null) {
91                     this.listeners = new ArrayList JavaDoc ();
92                 }
93                 this.listeners.add (l);
94             }
95             
96             public synchronized void removeChangeListener(ChangeListener JavaDoc l) {
97                 assert l != null : "Listener can not be null";
98                 if (this.listeners == null) {
99                     return;
100                 }
101                 this.listeners.remove (l);
102             }
103             
104             public void propertyChange (PropertyChangeEvent JavaDoc event) {
105                 if (LibraryImplementation.PROP_CONTENT.equals(event.getPropertyName())) {
106                     synchronized (this) {
107                         this.cachedRoots = null;
108                     }
109                     this.fireChange ();
110                 }
111             }
112             
113             private void fireChange () {
114                 Iterator JavaDoc it = null;
115                 synchronized (this) {
116                     if (this.listeners == null) {
117                         return;
118                     }
119                     it = ((ArrayList JavaDoc)this.listeners.clone()).iterator ();
120                 }
121                 ChangeEvent JavaDoc event = new ChangeEvent JavaDoc (this);
122                 while (it.hasNext()) {
123                     ((ChangeListener JavaDoc)it.next()).stateChanged(event);
124                 }
125             }
126         }
127
128         boolean isNormalizedURL = isNormalizedURL(b);
129         
130         String JavaDoc[] serverInstanceIDs = Deployment.getDefault().getServerInstanceIDs();
131         ServerRegistry servReg = ServerRegistry.getInstance();
132         for (int i=0; i < serverInstanceIDs.length; i++) {
133             ServerInstance serInst = servReg.getServerInstance(serverInstanceIDs[i]);
134             J2eePlatformImpl platformImpl = serInst.getJ2eePlatformImpl();
135             if (platformImpl == null) continue; // TODO this will be removed, when AppServPlg will be ready
136
LibraryImplementation[] libs = platformImpl.getLibraries();
137             for (int j=0; j<libs.length; j++) {
138                 List JavaDoc jars = libs[j].getContent(J2eeLibraryTypeProvider.VOLUME_TYPE_CLASSPATH);
139                 Iterator JavaDoc it = jars.iterator();
140                 while (it.hasNext()) {
141                     URL JavaDoc entry = (URL JavaDoc)it.next();
142                     URL JavaDoc normalizedEntry;
143                     if (isNormalizedURL) {
144                         normalizedEntry = getNormalizedURL(entry);
145                     }
146                     else {
147                         normalizedEntry = entry;
148                     }
149                     if (normalizedEntry != null && normalizedEntry.equals(b)) {
150                         return new R(libs[j]);
151                     }
152                 }
153             }
154         }
155         return null;
156     }
157
158     private URL JavaDoc getNormalizedURL (URL JavaDoc url) {
159         //URL is already nornalized, return it
160
if (isNormalizedURL(url)) {
161             return url;
162         }
163         //Todo: Should listen on the LibrariesManager and cleanup cache
164
// in this case the search can use the cache onle and can be faster
165
// from O(n) to O(ln(n))
166
URL JavaDoc normalizedURL = (URL JavaDoc) this.normalizedURLCache.get (url);
167         if (normalizedURL == null) {
168             FileObject fo = URLMapper.findFileObject(url);
169             if (fo != null) {
170                 try {
171                     normalizedURL = fo.getURL();
172                     this.normalizedURLCache.put (url, normalizedURL);
173                 } catch (FileStateInvalidException e) {
174                     ErrorManager.getDefault().notify(e);
175                 }
176             }
177         }
178         return normalizedURL;
179     }
180
181     /**
182      * Returns true if the given URL is file based, it is already
183      * resolved either into file URL or jar URL with file path.
184      * @param URL url
185      * @return true if the URL is normal
186      */

187     private static boolean isNormalizedURL (URL JavaDoc url) {
188         if ("jar".equals(url.getProtocol())) { //NOI18N
189
url = FileUtil.getArchiveFile(url);
190         }
191         return "file".equals(url.getProtocol()); //NOI18N
192
}
193
194     /**
195      * Search for the actual root of the Javadoc containing the index-all.html or
196      * index-files. In case when it is not able to find it, it returns the given Javadoc folder/file.
197      * @param URL Javadoc folder/file
198      * @return URL either the URL of folder containg the index or the given parameter if the index was not found.
199      */

200     private static URL JavaDoc getIndexFolder (URL JavaDoc rootURL) {
201         if (rootURL == null) {
202             return null;
203         }
204         FileObject root = URLMapper.findFileObject(rootURL);
205         if (root == null) {
206             return rootURL;
207         }
208         FileObject result = findIndexFolder (root,1);
209         try {
210             return result == null ? rootURL : result.getURL();
211         } catch (FileStateInvalidException e) {
212             ErrorManager.getDefault().notify(e);
213             return rootURL;
214         }
215     }
216     
217     private static FileObject findIndexFolder (FileObject fo, int depth) {
218         if (depth > MAX_DEPTH) {
219             return null;
220         }
221         if (fo.getFileObject("index-files",null)!=null || fo.getFileObject("index-all.html",null)!=null) { //NOI18N
222
return fo;
223         }
224         FileObject[] children = fo.getChildren();
225         for (int i=0; i< children.length; i++) {
226             if (children[i].isFolder()) {
227                 FileObject result = findIndexFolder(children[i], depth+1);
228                 if (result != null) {
229                     return result;
230                 }
231             }
232         }
233         return null;
234     }
235 }
236
Popular Tags