KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.*;
23 import java.io.*;
24 import java.net.URL JavaDoc;
25 import java.util.*;
26
27 import org.openide.filesystems.*;
28 import org.openide.util.Enumerations;
29 import org.openide.util.Lookup;
30 import org.openide.util.Utilities;
31
32 /** Read-only, fixed-structure filesystem.
33  * Derived from the ManifestFileSystem of yore (sandwich branch).
34  * @author Jesse Glick, Jaroslav Tulach
35  */

36 public class FixedFileSystem extends AbstractFileSystem implements /*ManifestSection.Iterator,*/
37     AbstractFileSystem.List, AbstractFileSystem.Change, AbstractFileSystem.Attr, AbstractFileSystem.Info {
38     // XXX svuid
39

40     /** The default instance used in the system.
41      */

42     static FixedFileSystem deflt = null;
43     /** Get the default fixed filesystem for the system.
44      * @return the default instance
45      */

46     public static FixedFileSystem getDefault () {
47         return deflt;
48     }
49
50     /** Represents one file/instance. */
51     public static class Instance implements Serializable {
52         // XXX svuid
53
/** if true, this is a folder, if false, a file
54          */

55         final boolean folder;
56         /** a known MIME type, or null to guess
57          */

58         final String JavaDoc contentType;
59         /** raw byte array of file contents; may be null
60          */

61         final byte[] contents;
62         /** special display name, or null if unimportant
63          */

64         final String JavaDoc displayName;
65         /** special icon, or null if unimportant
66          */

67         final URL JavaDoc icon;
68         /** beanName class name of bean or null (FFS tries to get icon from its beaninfo)
69          */

70         final String JavaDoc beanName;
71         
72         Map<String JavaDoc, Object JavaDoc> attributes; // name -> value attributes (null OK)
73
/** Make a new wrapper for file data.
74          * @param folder true if should be a folder rather than a file
75          * @param contentType MIME type or null
76          * @param contents raw contents or null
77          * @param displayName special display name, or null
78          * @param icon special icon, or null
79          */

80         public Instance (boolean folder, String JavaDoc contentType, byte[] contents, String JavaDoc displayName, URL JavaDoc icon) {
81             this.folder = folder;
82             this.contentType = contentType;
83             this.contents = contents;
84             this.displayName = displayName;
85             this.icon = icon;
86             beanName = null;
87         }
88         
89         /** Make a new wrapper for file data.
90          * @param folder true if should be a folder rather than a file
91          * @param contentType MIME type or null
92          * @param contents raw contents or null
93          * @param displayName special display name, or null
94          * @param beanName class name of bean or null (FFS tries to get icon from its beaninfo)
95          */

96         public Instance (boolean folder, String JavaDoc contentType, byte[] contents, String JavaDoc displayName, String JavaDoc beanName) {
97             this.folder = folder;
98             this.contentType = contentType;
99             this.contents = contents;
100             this.displayName = displayName;
101             this.beanName = beanName;
102             icon = null;
103         }
104         
105         public void writeAttribute(String JavaDoc name, Object JavaDoc value) {
106             if (attributes == null) {
107                 attributes = new HashMap<String JavaDoc, Object JavaDoc>();
108             }
109             attributes.put(name, value);
110         }
111     }
112
113     /** Creator for which is set as attribute, but its returned
114      * value from getter is the one returne from its createValue method. */

115     public interface AttributeCreator {
116         public Object JavaDoc createValue(FixedFileSystem thisFS,
117         String JavaDoc thisFilePath, String JavaDoc thisAttr);
118     }
119
120
121     
122     /** last time somebody added new order */
123     //private static long orderDate = System.currentTimeMillis ();
124

125     /** set of instances by name */
126     private final Map<String JavaDoc, Instance> instances = new HashMap<String JavaDoc, Instance> ();
127     
128     /* order. set of (Ord) objects */
129     //private final Set order = new HashSet (7); // Set<Ord>
130
/* recomputed attributes of orders */
131     //private Set orderAttributes; // Set<String>
132
/* time when order for this FS has been computed */
133     //private long orderComputed = System.currentTimeMillis ();
134

135     /** Display name of the file system.
136      */

137     private String JavaDoc displayName/*, moduleName*/;
138
139     /** Time of last change to any contained file.
140      */

141     private transient long date = System.currentTimeMillis ();
142
143     @SuppressWarnings JavaDoc("deprecation")
144     private void _setSystemName(String JavaDoc s) throws PropertyVetoException {
145         setSystemName(s);
146     }
147     
148     /** Creates the filesystem with no instances.
149      * @param name system name of the filesystem
150      * @param displayName display name of the filesystem
151      */

152     public FixedFileSystem (String JavaDoc name, String JavaDoc displayName/*, String moduleName*/) {
153         list = this;
154         change = this;
155         info = this;
156         attr = this;
157         try {
158             _setSystemName (name);
159         } catch (PropertyVetoException pve) {
160             throw new InternalError JavaDoc (pve.toString ());
161         }
162         this.displayName = displayName;
163         //this.moduleName = moduleName;
164
}
165
166     /** Does the filesystem actually contain anything?
167      * @return true if so
168      */

169     public boolean isNonTrivial () {
170         return ! instances.isEmpty ();
171     }
172
173     /** Get the stored display name.
174      * @return the display name
175      */

176     public String JavaDoc getDisplayName () {
177         return displayName;
178     }
179
180     /** Is this filesystem read-only?
181      * @return true, it always is
182      */

183     public boolean isReadOnly () {
184         return true;
185     }
186
187     /** Marker for an automatically-generated folder.
188      */

189     private static final Instance autoFolder = new Instance (true, null, null, null, (String JavaDoc) null);
190     /** Add a file to the filesystem.
191      * Fires changes as needed.
192      * @param path the resource path to add (for a file or folder)
193      * @param inst data for the file to be added
194      */

195     public void add (String JavaDoc path, Instance inst) {
196         if (path.length () == 0) return;
197         //System.err.println("FFS.add: " + path);
198
String JavaDoc folder;
199         int idx = path.lastIndexOf ('/');
200         if (idx == -1) {
201             folder = ""; // NOI18N
202
} else {
203             folder = path.substring (0, idx);
204         }
205         add (folder, autoFolder);
206         boolean nue;
207         synchronized (instances) {
208             Object JavaDoc old = instances.get (path);
209             if (inst == old) return;
210             nue = (old == null);
211             if (nue) instances.put (path, inst);
212         }
213         refreshResource (folder, false);
214         if (! nue) refreshResource (path, false);
215         date = System.currentTimeMillis ();
216     }
217     
218     /** Remove a file from the filesystem.
219      * Fires changes as needed.
220      * @param path the resource path to remove
221      */

222     public void remove (String JavaDoc path) {
223         //System.err.println("FFS.remove: " + path);
224
synchronized (instances) {
225             if (instances.remove (path) == null) {
226                 return;
227             }
228         }
229         // XXX remove empty parents too??
230
int idx = path.lastIndexOf ('/');
231         if (idx != -1) {
232             //System.err.println("FFS.refresh: " + path.substring (0, idx));
233
refreshResource (path.substring (0, idx), false);
234         } else {
235             //System.err.println("FFS.refresh: root");
236
refreshResource ("", false); // NOI18N
237
}
238     }
239     
240     /** Retrieve file instance
241      * @param path the resource path
242      * @return file instance or <code>null</code>
243      */

244     public FixedFileSystem.Instance get (String JavaDoc path) {
245         return instances.get(path);
246     }
247
248     /** Order the file behind others defaults in the folder.
249      * @param folder folder containing file
250      * @param file fullpath to file
251      */

252     public void addDefault (String JavaDoc folder, String JavaDoc file) {
253         Instance inst = instances.get(folder);
254         if (inst == null) {
255             inst = new Instance(true, null, null, null, (String JavaDoc) null);
256             add(folder, inst);
257         }
258         String JavaDoc attrName = "OpenIDE-Folder-Order"; // NOI18N
259
String JavaDoc attrValue = null;
260         Map attrs = inst.attributes;
261         if (attrs != null) {
262             attrValue = (String JavaDoc) attrs.get(attrName);
263         }
264         
265         if (attrValue == null) {
266             attrValue = file;
267         } else {
268             attrValue += '/' + file;
269         }
270         inst.writeAttribute(attrName, attrValue);
271     }
272     
273     /** For debugging only.
274      * @return a string representation
275      */

276     @SuppressWarnings JavaDoc("deprecation")
277     public String JavaDoc toString () {
278         return "FixedFileSystem[" + getSystemName () + "]" + instances.keySet (); // NOI18N
279
}
280     
281     // For the benefit of SystemFileSystem:
282
/** Maybe provide a localized display name.
283      * @param resource resource path
284      * @return a localized display name, or null
285      */

286     String JavaDoc annotateName (String JavaDoc resource) {
287         synchronized (instances) {
288             Instance inst = instances.get(resource);
289             return (inst == null) ? null : inst.displayName;
290         }
291     }
292     /** Maybe provide a special icon.
293      * @param resource resource path to check
294      * @return an icon, or null
295      */

296     java.awt.Image JavaDoc annotateIcon (String JavaDoc resource) {
297         Instance inst;
298         synchronized (instances) {
299             inst = instances.get(resource);
300         }
301         
302         if (inst == null) return null;
303         if (inst.icon != null) {
304             return java.awt.Toolkit.getDefaultToolkit ().getImage (inst.icon);
305         }
306         
307         if (inst.beanName == null) return null;
308         
309         try {
310             Class JavaDoc<?> clazz = Lookup.getDefault().lookup(ClassLoader JavaDoc.class).loadClass(inst.beanName);
311             return Utilities.getBeanInfo(clazz).getIcon(BeanInfo.ICON_COLOR_16x16);
312         } catch (Exception JavaDoc ex) {
313             return null;
314         }
315     }
316     
317     /*
318     public synchronized void processLoader (
319         ManifestSection.LoaderSection ls
320     ) throws InstantiationException {
321         // Singleton class -> store configurable default instance.
322         DataLoader l = ls.getLoader ();
323         String clazz = l.getClass ().getName ();
324         String fileName = clazz.replace ('.', '-') + ".xml"; // NOI18N
325         String path = "Settings/Loaders/" + fileName; // NOI18N
326         Instance inst = createXMLInstance (clazz);
327         
328         String[] installBefore = ls.getInstallBefore ();
329         String[] installAfter = ls.getInstallAfter ();
330
331         if (installBefore != null) {
332             for (int i = 0; i < installBefore.length; i++) {
333                 order.add (new Ord (fileName, installBefore[i]));
334                 orderDate = System.currentTimeMillis ();
335             }
336         }
337         
338         if (installAfter != null) {
339             for (int i = 0; i < installAfter.length; i++) {
340                 order.add (new Ord (installAfter[i], fileName));
341                 orderDate = System.currentTimeMillis ();
342             }
343         }
344         
345         add (path, inst);
346     }
347     */

348
349     // --------- List ----------
350

351     /** Get names of children.
352      * @param f resource path of parent
353      * @return list of child names taken from instance map
354      */

355     public String JavaDoc[] children (String JavaDoc f) {
356         synchronized (instances) {
357             ArrayList<String JavaDoc> l = new ArrayList<String JavaDoc> (10);
358             for (String JavaDoc path : instances.keySet()) {
359                 if (f.length () == 0) {
360                     // Look for children of root folder.
361
if (path.lastIndexOf ('/') == -1)
362                         l.add (path);
363                 } else {
364                     // Children of some subfolder.
365
if (path.startsWith (f + '/') && path.lastIndexOf ('/') == f.length ())
366                         l.add (path.substring (f.length () + 1));
367                 }
368             }
369             //System.err.println("FSS.children: " + f + " -> " + l);
370
return l.toArray (new String JavaDoc[l.size ()]);
371         }
372     }
373
374     // --------- Change ---------
375

376     /** Cannot create folders.
377      * @param name resource path
378      * @throws IOException always
379      */

380     public void createFolder (String JavaDoc name) throws java.io.IOException JavaDoc {
381         throw new IOException ("unsupported"); // NOI18N
382
}
383
384     /** Cannot create files.
385      * @param name resource path
386      * @throws IOException always
387      */

388     public void createData (String JavaDoc name) throws IOException {
389         throw new IOException ("unsupported"); // NOI18N
390
}
391
392     /** Cannot rename.
393      * @param oldName old resource path
394      * @param newName new resource path
395      * @throws IOException always
396      */

397     public void rename (String JavaDoc oldName, String JavaDoc newName) throws IOException {
398         throw new IOException ("unsupported"); // NOI18N
399
}
400
401     /** Cannot delete.
402      * @param name resource path
403      * @throws IOException always
404      */

405     public void delete (String JavaDoc name) throws IOException {
406         throw new IOException ("unsupported"); // NOI18N
407
}
408
409     // -------- Attr -------
410

411     /** Currently no attributes are supported.
412      * @param name resource path
413      * @param attrName attribute name
414      * @return currently nothing
415      */

416     public Object JavaDoc readAttribute (String JavaDoc name, String JavaDoc attrName) {
417         if ("OpenIDE-Folder-SortMode".equals (attrName)) { // NOI18N
418
return "O"; // NOI18N
419
}
420         
421         /*
422         if (! "Settings/Loaders".equals (name)) {
423             return null;
424         }
425         
426         
427         Set s = orderAttributes;
428         return s == null || !s.contains (attrName) ? null : Boolean.TRUE;
429         */

430         
431         Instance inst = instances.get(name);
432         if (inst == null) {
433             return null;
434         }
435         Map m = inst.attributes;
436         if (m == null) {
437             return null;
438         }
439         
440         Object JavaDoc ret = m.get (attrName);
441         if(ret instanceof AttributeCreator) {
442             return ((AttributeCreator)ret).createValue(this, name, attrName);
443         }
444         
445         return ret;
446
447         //return null;
448
}
449
450     /** Cannot write attributes.
451      * @param name resource path
452      * @param attrName attribute name
453      * @param value new value
454      * @throws IOException always
455      */

456     public void writeAttribute (String JavaDoc name, String JavaDoc attrName, Object JavaDoc value) throws IOException {
457         throw new IOException ("unsupported"); // NOI18N
458
}
459
460     /** Currently no attributes are supported.
461      * @param name resource path
462      * @return list of attribute names (none currently)
463      */

464     public synchronized Enumeration<String JavaDoc> attributes (String JavaDoc name) {
465         Instance inst = instances.get(name);
466         if (inst == null) return Enumerations.<String JavaDoc>empty();
467         Map<String JavaDoc, Object JavaDoc> m = inst.attributes;
468         if (m == null) return Enumerations.<String JavaDoc>empty();
469         return Collections.enumeration(m.keySet());
470
471     }
472
473     /** Do nothing.
474      * @param oldName old resource path
475      * @param newName new resource path
476      */

477     public void renameAttributes (String JavaDoc oldName, String JavaDoc newName) {
478         // do nothing
479
}
480
481     /** Do nothing.
482      * @param name resource path
483      */

484     public void deleteAttributes (String JavaDoc name) {
485         // do nothing
486
}
487
488     // --------- Info --------
489

490     /** Currently has just one modification time for the
491      * whole filesystem.
492      * @param name resource path
493      * @return general date
494      */

495     public Date lastModified (String JavaDoc name) {
496         return new Date (date);
497     }
498
499     /** Is this a folder?
500      * @param name resource path
501      * @return true if folder, false for file
502      */

503     public boolean folder (String JavaDoc name) {
504         synchronized (instances) {
505             Instance inst = instances.get(name);
506             if (inst == null) return false;
507             return inst.folder;
508         }
509     }
510
511     /** Never permit writing.
512      * @param name resource path
513      * @return always false
514      */

515     public boolean readOnly (String JavaDoc name) {
516         return true;
517     }
518
519     /** Return predetermined MIME types where possible.
520      * @param name resource path
521      * @return a special MIME type, or null usually
522      */

523     public String JavaDoc mimeType (String JavaDoc name) {
524         synchronized (instances) {
525             Instance inst = instances.get(name);
526             return (inst == null) ? null : inst.contentType;
527         }
528     }
529
530     /** Get content length.
531      * @param name resource path
532      * @return size of content in bytes
533      */

534     public long size (String JavaDoc name) {
535         synchronized (instances) {
536             Instance inst = instances.get(name);
537             if (inst == null) return 0L;
538             if (inst.folder) return 0L;
539             if (inst.contents == null) return 0L;
540             return inst.contents.length;
541         }
542     }
543
544     /** Open contents.
545      * @param name resource path
546      * @throws FileNotFoundException where applicable
547      * @return a stream reading from the fixed byte array
548      */

549     public InputStream inputStream (String JavaDoc name) throws FileNotFoundException {
550         Instance inst;
551         synchronized (instances) {
552             inst = instances.get(name);
553         }
554         if (inst == null) throw new FileNotFoundException (name);
555         if (inst.folder) throw new FileNotFoundException (name);
556         byte[] contents = inst.contents;
557         if (contents == null) contents = new byte[0];
558         return new ByteArrayInputStream (contents);
559     }
560
561     /** Cannot write contents.
562      * @param name resource path
563      * @throws IOException always
564      * @return wouldn't
565      */

566     public OutputStream outputStream (String JavaDoc name) throws IOException {
567         throw new IOException ("unsupported"); // NOI18N
568
}
569
570     /** Cannot lock files.
571      * @param name resource path
572      * @throws IOException don't bother
573      */

574     public void lock (String JavaDoc name) throws IOException {
575         throw new IOException ("unsupported"); // NOI18N
576
}
577
578     /** Cannot lock files.
579      * @param name resource path
580      */

581     public void unlock (String JavaDoc name) {
582         // do nothing
583
}
584
585     /** Ignore importance status.
586      * @param name resource path
587      */

588     public void markUnimportant (String JavaDoc name) {
589         // do nothing
590
}
591     
592     /* A special inner class to hold ordering dependency between two
593      * data loaders.
594      * /
595     private static final class Ord extends Object {
596         /* either name of class (does not end with .xml) or
597          * a filename
598          * /
599         private String first;
600         private String second;
601         
602         /* Constructor.
603          * /
604         public Ord (String repr1, String repr2) {
605             first = repr1;
606             second = repr2;
607         }
608         
609         /* Fills a map with attributes.
610          * @param map the map to add attributes to
611          * /
612         public String attribute () {
613             first = extract (first);
614             second = extract (second);
615             
616             
617             String f = convert (first);
618             String s = convert (second);
619             
620             return f + '<' + s;
621         }
622         
623         
624         /* Convert an object to a string.
625          * @param s class or file name
626          * /
627         private static String convert (String s) {
628             if (s.indexOf ('-') >= 0) {
629                 // already
630                 return s;
631             }
632             
633             // simple check, if the name of reference ends with object
634             // then we actually want a loader/if such loader has not been
635             // found yet => use simple replace of XXXLoader instead of XXXObject
636             if (s.endsWith("Object")) { // NOI18N
637                 s = s.substring (0, s.length() - 6) + "Loader"; // NOI18N
638             }
639             
640             try {
641                 Class clazz = Class.forName(
642                     s, false, TopManager.getDefault().currentClassLoader()
643                 );
644                 DataLoader dl = (DataLoader)DataLoader.findObject(clazz, false);
645                 if (dl != null) {
646                 org.openide.loaders.DataObject dataObj = somethingWith(dl);
647                     if (dataObj!= null) {
648                         FileObject fo = dataObj.getPrimaryFile ();
649                         s = fo.getName () + '.' + fo.getExt();
650                     }
651                 }
652             } catch (ClassNotFoundException ex) {
653                 ex.printStackTrace();
654             }
655             
656             return s;
657         }
658         
659         /* Extracts the name of a loader from string (name of representation
660          * class) or Class, the class of the loader.
661          * @return the replace for the object
662          * /
663         private static String extract (String obj) {
664             if (obj.indexOf ('-') > 0) {
665                 return obj;
666             }
667             
668             String reprClassName = (String)obj;
669             
670             try {
671                 Class clazz = Class.forName(
672                     reprClassName, false, TopManager.getDefault().currentClassLoader()
673                 );
674                 
675                 
676                 Enumeration en = TopManager.getDefault ().getLoaderPool().allLoaders();
677                 while (en.hasMoreElements()) {
678                     DataLoader dl = (DataLoader)en.nextElement();
679                     if (clazz.isAssignableFrom (dl.getRepresentationClass())) {
680                         // found fine, add an attribute
681                         return convert (dl.getClass ().getName ());
682                     }
683                 }
684             } catch (ClassNotFoundException ex) {
685             }
686             
687             // no replace found
688             return obj;
689         }
690     }*/

691 }
692
Popular Tags