KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > masterfs > filebasedfs > FileBasedFileSystem


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.masterfs.filebasedfs;
21
22 import java.io.File JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29 import org.netbeans.modules.masterfs.filebasedfs.fileobjects.FileObjectFactory;
30 import org.netbeans.modules.masterfs.filebasedfs.utils.FileInfo;
31 import org.openide.filesystems.FileObject;
32 import org.openide.filesystems.FileSystem;
33 import org.openide.filesystems.FileUtil;
34 import org.openide.util.actions.SystemAction;
35
36 /**
37  * @author Radek Matous
38  */

39 public final class FileBasedFileSystem extends FileSystem {
40     private static Map JavaDoc allInstances = new HashMap JavaDoc();
41     private final FileObjectFactory factory;
42
43     //only for tests purposes
44
public static void reinitForTests() {
45         FileBasedFileSystem.allInstances = new HashMap JavaDoc();
46     }
47     
48     public static FileBasedFileSystem getInstance(final File JavaDoc file) {
49         FileBasedFileSystem retVal;
50         final FileInfo fInfo = new FileInfo(file);
51         final FileInfo rootInfo = fInfo.getRoot();
52
53         synchronized (FileBasedFileSystem.allInstances) {
54             final File JavaDoc rootFile = rootInfo.getFile();
55             retVal = (FileBasedFileSystem) FileBasedFileSystem.allInstances.get(rootFile);
56             if (retVal == null) {
57                 if (rootInfo.isConvertibleToFileObject()) {
58                     retVal = new FileBasedFileSystem(rootFile);
59                     FileBasedFileSystem.allInstances.put(rootFile, retVal);
60                 }
61             }
62         }
63         return retVal;
64     }
65     
66     public static final FileObject getFileObject(final File JavaDoc file) {
67         FileBasedFileSystem fs = getInstance(file);
68         return (fs != null) ? fs.findFileObject(file) : null;
69     }
70     
71
72     static Collection JavaDoc getInstances() {
73         synchronized (FileBasedFileSystem.allInstances) {
74             return new ArrayList JavaDoc(allInstances.values());
75         }
76     }
77     
78     static int getSize () {
79         synchronized (FileBasedFileSystem.allInstances) {
80             return allInstances.size();
81         }
82     }
83     
84     private FileBasedFileSystem(final File JavaDoc rootFile) {
85         this.factory = FileObjectFactory.getInstance(new FileInfo(rootFile));
86     }
87
88     public final org.openide.filesystems.FileObject findResource(final String JavaDoc name) {
89         File JavaDoc f = new File JavaDoc(name);
90         assert f.getAbsolutePath().replace('\\', '/').equals(name.replace('\\', '/')) : name + " versus " + f.getAbsolutePath();
91         return findFileObject(f);
92     }
93
94     public final FileObject findFileObject(final File JavaDoc f) {
95         return findFileObject(new FileInfo (f));
96     }
97     
98     public final FileObject findFileObject(final FileInfo fInfo) {
99         File JavaDoc f = fInfo.getFile();
100         boolean issue45485 = fInfo.isWindows() && f.getName().endsWith(".");//NOI18N
101
if (issue45485) {
102             File JavaDoc f2 = FileUtil.normalizeFile(f);
103             issue45485 = !f2.getName().endsWith(".");
104             if (issue45485) return null;
105         }
106         final FileObject retVal = (getFactory().findFileObject(fInfo));
107         return (retVal != null && retVal.isValid()) ? retVal : null;
108     }
109
110     public final org.openide.filesystems.FileObject getRoot() {
111         return getFactory().getRoot();
112     }
113
114     public final String JavaDoc getDisplayName() {
115         return getFactory().getRoot().getRealRoot().getPath();
116     }
117
118     public final SystemAction[] getActions() {
119         return new SystemAction[] {};
120     }
121
122     public final SystemAction[] getActions(final Set JavaDoc/*<FileObject>*/ foSet) {
123         return new SystemAction[] {};
124
125     }
126
127     public final void refresh(final boolean expected) {
128         Statistics.StopWatch stopWatch = Statistics.getStopWatch(Statistics.REFRESH_FS);
129         stopWatch.start();
130         
131         getFactory().refreshAll(expected);
132         stopWatch.stop();
133     
134         // print refresh stats unconditionally in trunk
135
Logger.getLogger("org.netbeans.modules.masterfs.REFRESH").fine(
136             "FS.refresh statistics (" + Statistics.fileObjects() + "FileObjects):\n " +
137             Statistics.REFRESH_FS.toString() + "\n " +
138             Statistics.LISTENERS_CALLS.toString() + "\n " +
139             Statistics.REFRESH_FOLDER.toString() + "\n " +
140             Statistics.REFRESH_FILE.toString() + "\n"
141         );
142
143         Statistics.REFRESH_FS.reset();
144         Statistics.LISTENERS_CALLS.reset();
145         Statistics.REFRESH_FOLDER.reset();
146         Statistics.REFRESH_FILE.reset();
147     }
148
149     public final boolean isReadOnly() {
150         return false;
151     }
152
153     public final String JavaDoc toString() {
154         return getDisplayName();
155     }
156
157     public final FileObjectFactory getFactory() {
158         return factory;
159     }
160 }
161
Popular Tags