KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > startup > layers > SystemFileSystem


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.core.startup.layers;
21
22 import java.awt.Image JavaDoc;
23 import java.awt.Toolkit JavaDoc;
24 import java.beans.BeanInfo JavaDoc;
25 import java.beans.PropertyVetoException JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.NotSerializableException JavaDoc;
29 import java.io.ObjectStreamException JavaDoc;
30 import java.io.Serializable JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.MissingResourceException JavaDoc;
35 import java.util.ResourceBundle JavaDoc;
36 import java.util.Set JavaDoc;
37 import java.util.logging.Level JavaDoc;
38 import org.openide.filesystems.FileObject;
39 import org.openide.filesystems.FileStatusEvent;
40 import org.openide.filesystems.FileSystem;
41 import org.openide.filesystems.FileUtil;
42 import org.openide.filesystems.LocalFileSystem;
43 import org.openide.filesystems.MultiFileSystem;
44 import org.openide.filesystems.Repository;
45 import org.openide.util.NbBundle;
46
47 /** The system FileSystem - represents system files under $NETBEANS_HOME/system.
48 *
49 * @author Jan Jancura, Ian Formanek, Petr Hamernik
50 */

51 public final class SystemFileSystem extends MultiFileSystem implements FileSystem.Status {
52     // Must be public for BeanInfo to work: #11186.
53

54     /** generated Serialized Version UID */
55     static final long serialVersionUID = -7761052280240991668L;
56
57     /** system name of this filesystem */
58     private static final String JavaDoc SYSTEM_NAME = "SystemFileSystem"; // NOI18N
59

60     /** name of file attribute with localizing bundle */
61     private static final String JavaDoc ATTR_BUNDLE = "SystemFileSystem.localizingBundle"; // NOI18N
62

63     /** name of file attribute with URL to 16x16 color icon */
64     private static final String JavaDoc ATTR_ICON_16 = "SystemFileSystem.icon"; // NOI18N
65
/** name of file attribute with URL to 32x32 color icon */
66     private static final String JavaDoc ATTR_ICON_32 = "SystemFileSystem.icon32"; // NOI18N
67

68     /** user fs */
69     private ModuleLayeredFileSystem user;
70     /** home fs */
71     private ModuleLayeredFileSystem home;
72
73     /** @param fss list of file systems to delegate to
74     */

75     @SuppressWarnings JavaDoc("deprecation")
76     private SystemFileSystem (FileSystem[] fss) throws PropertyVetoException JavaDoc {
77         super (fss);
78         user = (ModuleLayeredFileSystem) fss[0];
79         home = fss.length > 2 ? (ModuleLayeredFileSystem) fss[1] : null;
80
81         setSystemName (SYSTEM_NAME);
82         setHidden (true);
83     }
84
85
86     /** Name of the system */
87     public String JavaDoc getDisplayName () {
88         return NbBundle.getMessage(SystemFileSystem.class, "CTL_SystemFileSystem"); // NOI18N
89
}
90     
91     /** Getter for the instalation layer filesystem.
92      * May be null if there is none such.
93     */

94     public ModuleLayeredFileSystem getInstallationLayer () {
95         return home;
96     }
97     
98     /** Getter for the user layer filesystem.
99     */

100     public ModuleLayeredFileSystem getUserLayer () {
101         return user;
102     }
103     
104     /** Changes layers to provided values.
105      * @param arr the new layers
106      * @throws IllegalArgumentException if there is an overlap
107      */

108     public final void setLayers (FileSystem[] arr) throws IllegalArgumentException JavaDoc {
109         Set JavaDoc<FileSystem> s = new HashSet JavaDoc<FileSystem> ();
110         for (int i = 0; i < arr.length; i++)
111             if (s.contains (arr[i]))
112                 throw new IllegalArgumentException JavaDoc ("Overlap in filesystem layers"); // NOI18N
113
else
114                 s.add (arr[i]);
115
116         // create own internal copy of passed filesystems
117
setDelegates(arr.clone());
118         firePropertyChange ("layers", null, null); // NOI18N
119
}
120     
121     /** Getter for the array of filesystems that are currently used
122     * in the IDE.
123     *
124     * @return array of filesystems
125     */

126     public FileSystem[] getLayers() {
127         // don't return reference to internal buffer
128
return getDelegates().clone();
129     }
130
131     protected FileSystem createWritableOnForRename (String JavaDoc oldName, String JavaDoc newName) throws IOException JavaDoc {
132         return createWritableOn (oldName);
133     }
134     
135     protected FileSystem createWritableOn (String JavaDoc name) throws IOException JavaDoc {
136         FileSystem[] fss = getDelegates ();
137         for (int index = 0; index < fss.length; index++) {
138             if (! fss[index].isReadOnly ())
139                 return fss[index];
140         }
141         // Can really happen if invoked from e.g. org.netbeans.core.Plain.
142
throw new IOException JavaDoc("No writable filesystems in our delegates"); // NOI18N
143
}
144     
145     protected java.util.Set JavaDoc createLocksOn (String JavaDoc name) throws IOException JavaDoc {
146         LocalFileSystemEx.potentialLock (name);
147         return super.createLocksOn (name);
148     }
149     
150     /** This filesystem cannot be removed from pool, it is persistent.
151     */

152     @Deprecated JavaDoc
153     public boolean isPersistent () {
154         return true;
155     }
156
157     public FileSystem.Status getStatus () {
158         return this;
159     }
160
161     /** Annotate name
162     */

163     public String JavaDoc annotateName (String JavaDoc s, Set JavaDoc set) {
164
165         // Look for a localized file name.
166
// Note: all files in the set are checked. But please only place the attribute
167
// on the primary file, and use this primary file name as the bundle key.
168
Iterator JavaDoc it = set.iterator ();
169         while (it.hasNext ()) {
170             // annotate a name
171
FileObject fo = (FileObject) it.next ();
172
173             String JavaDoc bundleName = (String JavaDoc)fo.getAttribute (ATTR_BUNDLE); // NOI18N
174
if (bundleName != null) {
175                 try {
176                     bundleName = org.openide.util.Utilities.translate(bundleName);
177                     ResourceBundle JavaDoc b = NbBundle.getBundle(bundleName);
178                     try {
179                         return b.getString (fo.getPath());
180                     } catch (MissingResourceException JavaDoc ex) {
181                         // ignore--normal
182
}
183                 } catch (MissingResourceException JavaDoc ex) {
184                     ModuleLayeredFileSystem.err.log(
185                         Level.WARNING,
186                         "Computing display name for " + fo, ex); // NOI18N
187
// ignore
188
}
189             }
190             
191             String JavaDoc fixedName = FixedFileSystem.deflt.annotateName(fo.getPath());
192             if (fixedName != null) return fixedName;
193         }
194
195         return s;
196     }
197
198     /** Annotate icon
199     */

200     public Image JavaDoc annotateIcon (Image JavaDoc im, int type, Set JavaDoc s) {
201         String JavaDoc attr;
202         if (type == BeanInfo.ICON_COLOR_16x16) {
203             attr = ATTR_ICON_16;
204         } else if (type == BeanInfo.ICON_COLOR_32x32) {
205             attr = ATTR_ICON_32;
206         } else {
207             // mono icons not supported
208
return im;
209         }
210         Iterator JavaDoc it = s.iterator ();
211         while (it.hasNext ()) {
212             FileObject fo = (FileObject) it.next ();
213             Object JavaDoc value = fo.getAttribute (attr);
214             if (value != null) {
215                 if (value instanceof URL JavaDoc) {
216                     return Toolkit.getDefaultToolkit ().getImage ((URL JavaDoc) value);
217                 } else if (value instanceof Image JavaDoc) {
218                     // #18832
219
return (Image JavaDoc)value;
220                 } else {
221                     ModuleLayeredFileSystem.err.warning("Attribute " + attr + " on " + fo + " expected to be a URL or Image; was: " + value);
222                 }
223             }
224             Image JavaDoc anntIm = FixedFileSystem.deflt.annotateIcon(fo.getPath());
225             if (anntIm != null) {
226                 return anntIm;
227             }
228         }
229         return im;
230     }
231
232     /** Initializes and creates new repository. This repository's system fs is
233     * based on the content of ${HOME_DIR}/system and ${USER_DIR}/system directories
234     *
235     * @param userDir directory where user can write, or null to do it in memory
236     * @param homeDir directory where netbeans has been installed, user need not have write access, or null if none
237     * @param extradirs 0+ additional directories to use like homeDir
238     * @return repository
239     * @exception PropertyVetoException if something fails
240     */

241     static SystemFileSystem create (File JavaDoc userDir, File JavaDoc homeDir, File JavaDoc[] extradirs)
242     throws java.beans.PropertyVetoException JavaDoc, IOException JavaDoc {
243         FileSystem user;
244         LocalFileSystem home;
245
246         if (userDir != null) {
247             // only one file system
248
if (!userDir.exists ()) {
249                 userDir.mkdirs ();
250             }
251             LocalFileSystem l = new LocalFileSystemEx ( true );
252             l.setRootDirectory (userDir);
253             user = l;
254         } else {
255             // use some replacement
256
user = FileUtil.createMemoryFileSystem ();
257         }
258
259         if (homeDir == null) {
260             home = null;
261         } else {
262             home = new LocalFileSystemEx ();
263             home.setRootDirectory (homeDir);
264             home.setReadOnly (true);
265         }
266         LocalFileSystem[] extras = new LocalFileSystem[extradirs.length];
267         for (int i = 0; i < extradirs.length; i++) {
268             extras[i] = new LocalFileSystemEx();
269             extras[i].setRootDirectory(extradirs[i]);
270             extras[i].setReadOnly(true);
271         }
272
273         FileSystem[] arr = new FileSystem[home == null ? 2 : 3];
274         arr[0] = new ModuleLayeredFileSystem(user, new FileSystem[0], null);
275         if (home != null) {
276             File JavaDoc cachedir = new File JavaDoc(new File JavaDoc (userDir.getParentFile(), "var"), "cache"); // NOI18N
277
arr[1] = new ModuleLayeredFileSystem(home, extras, cachedir);
278         }
279         FixedFileSystem.deflt = new FixedFileSystem
280             ("org.netbeans.core.projects.FixedFileSystem", "Automatic Manifest Installation"); // NOI18N
281
arr[home == null ? 1 : 2] = FixedFileSystem.deflt;
282
283         return new SystemFileSystem (arr);
284     }
285
286     /** Notification that a file has migrated from one file system
287     * to another. Usually when somebody writes to file on readonly file
288     * system and the file has to be copied to write one.
289     * <P>
290     * This method allows subclasses to fire for example FileSystem.PROP_STATUS
291     * change to notify that annotation of this file should change.
292     *
293     * @param fo file object that change its actual file system
294     */

295     protected void notifyMigration (FileObject fo) {
296         fireFileStatusChanged (new FileStatusEvent (this, fo, false, true));
297     }
298
299     // --- SAFETY ---
300
private Object JavaDoc writeReplace () throws ObjectStreamException JavaDoc {
301         new NotSerializableException JavaDoc ("WARNING - SystemFileSystem is not designed to be serialized").printStackTrace (); // NOI18N
302
return new SingletonSerializer ();
303     }
304     
305     private static final class SingletonSerializer extends Object JavaDoc implements Serializable JavaDoc {
306         private static final long serialVersionUID = 6436781994611L;
307         SingletonSerializer() {}
308         private Object JavaDoc readResolve () throws ObjectStreamException JavaDoc {
309             return Repository.getDefault().getDefaultFileSystem ();
310         }
311     }
312     // --- SAFETY ---
313

314 }
315
Popular Tags