KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > projectapi > SimpleFileOwnerQueryImplementation


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.projectapi;
21
22 import java.io.IOException JavaDoc;
23 import java.lang.ref.Reference JavaDoc;
24 import java.lang.ref.WeakReference JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26 import java.net.URI JavaDoc;
27 import java.net.URISyntaxException JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.LinkedList JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Set JavaDoc;
34 import java.util.WeakHashMap JavaDoc;
35 import org.netbeans.api.project.Project;
36 import org.netbeans.api.project.ProjectManager;
37 import org.netbeans.spi.project.FileOwnerQueryImplementation;
38 import org.openide.ErrorManager;
39 import org.openide.filesystems.FileObject;
40 import org.openide.filesystems.FileStateInvalidException;
41 import org.openide.filesystems.URLMapper;
42 import org.openide.util.Utilities;
43 import org.openide.util.WeakSet;
44
45 /**
46  * Finds a project by searching the directory tree.
47  * @author Jesse Glick
48  */

49 public class SimpleFileOwnerQueryImplementation implements FileOwnerQueryImplementation {
50     
51     /** Do nothing */
52     public SimpleFileOwnerQueryImplementation() {}
53     
54     public Project getOwner(URI JavaDoc fileURI) {
55         // Try to find a FileObject for it.
56
URI JavaDoc test = fileURI;
57         FileObject file;
58         do {
59             file = uri2FileObject(test);
60             test = goUp(test);
61         } while (file == null && test != null);
62         if (file == null) {
63             return null;
64         }
65         return getOwner(file);
66     }
67     
68     private final Set JavaDoc<FileObject> warnedAboutBrokenProjects = new WeakSet<FileObject>();
69         
70     public Project getOwner(FileObject f) {
71         while (f != null) {
72             if (f.isFolder()) {
73                 Project p;
74                 try {
75                     p = ProjectManager.getDefault().findProject(f);
76                 } catch (IOException JavaDoc e) {
77                     // There is a project here, but we cannot load it...
78
if (warnedAboutBrokenProjects.add(f)) { // #60416
79
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
80                     }
81                     return null;
82                 }
83                 if (p != null) {
84                     return p;
85                 }
86             }
87             
88             if (!externalOwners.isEmpty()) {
89                 Reference JavaDoc<FileObject> externalOwnersReference = externalOwners.get(fileObject2URI(f));
90
91                 if (externalOwnersReference != null) {
92                     FileObject externalOwner = externalOwnersReference.get();
93
94                     if (externalOwner != null) {
95                         try {
96                             // Note: will be null if there is no such project.
97
return ProjectManager.getDefault().findProject(externalOwner);
98                         } catch (IOException JavaDoc e) {
99                             // There is a project there, but we cannot load it...
100
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
101                             return null;
102                         }
103                     }
104                 }
105             }
106             f = f.getParent();
107         }
108         return null;
109     }
110     
111     /**
112      * Map from external source roots to the owning project directories.
113      */

114     private static final Map JavaDoc<URI JavaDoc,Reference JavaDoc<FileObject>> externalOwners =
115         Collections.synchronizedMap(new WeakHashMap JavaDoc<URI JavaDoc,Reference JavaDoc<FileObject>>());
116     private static final Map JavaDoc<FileObject,Collection JavaDoc<URI JavaDoc>> project2External =
117         Collections.synchronizedMap(new WeakHashMap JavaDoc<FileObject,Collection JavaDoc<URI JavaDoc>>());
118     
119     /** @see FileOwnerQuery#reset */
120     public static void reset() {
121         externalOwners.clear();
122     }
123     
124     private static URI JavaDoc fileObject2URI(FileObject f) {
125         try {
126             return URI.create(f.getURL().toString());
127         } catch (FileStateInvalidException e) {
128             throw (IllegalArgumentException JavaDoc) new IllegalArgumentException JavaDoc(e.toString()).initCause(e);
129         }
130     }
131     
132     /** @see FileOwnerQuery#markExternalOwner */
133     public static void markExternalOwnerTransient(FileObject root, Project owner) {
134         markExternalOwnerTransient(fileObject2URI(root), owner);
135     }
136     
137     /** @see FileOwnerQuery#markExternalOwner */
138     public static void markExternalOwnerTransient(URI JavaDoc root, Project owner) {
139         if (owner != null) {
140             externalOwners.put(root, new WeakReference JavaDoc<FileObject>(owner.getProjectDirectory()));
141             synchronized (project2External) {
142                 FileObject prjDir = owner.getProjectDirectory();
143                 Collection JavaDoc<URI JavaDoc> roots = project2External.get (prjDir);
144                 if (roots == null) {
145                     roots = new LinkedList JavaDoc<URI JavaDoc>();
146                     project2External.put(prjDir, roots);
147                 }
148                 roots.add (root);
149             }
150         } else {
151             Reference JavaDoc<FileObject> ownerReference = externalOwners.remove(root);
152             
153             if (ownerReference != null) {
154                 FileObject ownerFO = ownerReference.get();
155                 
156                 if (ownerFO != null) {
157                     synchronized (project2External) {
158                         Collection JavaDoc<URI JavaDoc> roots = project2External.get(ownerFO);
159                         if (roots != null) {
160                             roots.remove(root);
161                             if (roots.size() == 0) {
162                                 project2External.remove(ownerFO);
163                             }
164                         }
165                     }
166                 }
167             }
168         }
169     }
170     
171     private static FileObject uri2FileObject(URI JavaDoc u) {
172         URL JavaDoc url;
173         try {
174             url = u.toURL();
175         } catch (MalformedURLException JavaDoc e) {
176             e.printStackTrace();
177             assert false : u;
178             return null;
179         }
180         return URLMapper.findFileObject(url);
181     }
182     
183     private static URI JavaDoc goUp(URI JavaDoc u) {
184         assert u.isAbsolute() : u;
185         assert u.getFragment() == null : u;
186         assert u.getQuery() == null : u;
187         // XXX isn't there any easier way to do this?
188
// Using getPath in the new path does not work; nbfs: URLs break. (#39613)
189
// On the other hand, nbfs: URLs are not really used any more, so do we care?
190
String JavaDoc path = u.getPath();
191         if (path == null || path.equals("/")) { // NOI18N
192
return null;
193         }
194         String JavaDoc us = u.toString();
195         if (us.endsWith("/")) { // NOI18N
196
us = us.substring(0, us.length() - 1);
197             assert path.endsWith("/"); // NOI18N
198
path = path.substring(0, path.length() - 1);
199         }
200         int idx = us.lastIndexOf('/');
201         assert idx != -1 : path;
202         if (path.lastIndexOf('/') == 0) {
203             us = us.substring(0, idx + 1);
204         } else {
205             us = us.substring(0, idx);
206         }
207         URI JavaDoc nue;
208         try {
209             nue = new URI JavaDoc(us);
210         } catch (URISyntaxException JavaDoc e) {
211             throw new AssertionError JavaDoc(e);
212         }
213         if (Utilities.isWindows()) {
214             String JavaDoc pth = nue.getPath();
215             // check that path is not "/C:" or "/"
216
if ((pth.length() == 3 && pth.endsWith(":")) ||
217                 (pth.length() == 1 && pth.endsWith("/"))) {
218                 return null;
219             }
220         }
221         assert nue.isAbsolute() : nue;
222         assert u.toString().startsWith(nue.toString()) : "not a parent: " + nue + " of " + u;
223         return nue;
224     }
225     
226 }
227
Popular Tags