KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > masterfs > providers > Attributes


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
21 package org.netbeans.modules.masterfs.providers;
22
23 import org.netbeans.modules.masterfs.ExLocalFileSystem;
24 import org.openide.util.Utilities;
25 import org.openide.filesystems.AbstractFileSystem;
26 import org.openide.filesystems.DefaultAttributes;
27 import org.openide.filesystems.FileUtil;
28
29 import java.beans.PropertyVetoException JavaDoc;
30 import java.io.File JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import org.openide.util.Exceptions;
34
35 /**
36  * Implementation of DefaultAttributes that should be shared by all
37  * filesystems that are mounted into MasterFileSystem.
38  *
39  * Ensures that .nbattrs file is stored in netbeans.user/var/cache/attribs/.nbattrs.
40  * There exist just one file for all attributes. This implemenation is supposed
41  * to provide backward compatibility.
42  *
43  */

44 public class Attributes extends DefaultAttributes {
45     public static String JavaDoc ATTRNAME = "attributes.xml";
46     private static final String JavaDoc USERDIR = "netbeans.user";//NOI18N
47
private static final String JavaDoc LOCATION = "var";//NOI18N
48

49     private static DefaultAttributes sharedUserAttributes;
50
51     private final String JavaDoc attributePrefix;
52     private AbstractFileSystem.List list;
53     private static final boolean BACKWARD_COMPATIBILITY = true;
54     private static File JavaDoc rootForAttributes;
55
56
57     public Attributes(File JavaDoc mountPoint, AbstractFileSystem.Info info, AbstractFileSystem.Change change, AbstractFileSystem.List list) {
58         super(info, change, list);
59         this.list = list;
60         this.attributePrefix = preparePrefix(mountPoint);
61     }
62
63     public Attributes(AbstractFileSystem.Info info, AbstractFileSystem.Change change, AbstractFileSystem.List list) {
64         super(info, change, list);
65         this.list = list;
66         this.attributePrefix = "";
67     }
68     
69     private String JavaDoc preparePrefix(File JavaDoc fileSystemRoot) {
70         fileSystemRoot = FileUtil.normalizeFile(fileSystemRoot);
71         String JavaDoc rootPath = fileSystemRoot.getAbsolutePath().replace('\\', '/');
72         return ((Utilities.isWindows () || (Utilities.getOperatingSystem () == Utilities.OS_OS2))) ? rootPath.toLowerCase() : rootPath;
73     }
74
75     public static File JavaDoc getRootForAttributes() {
76         synchronized (ExLocalFileSystem.class) {
77             if (rootForAttributes == null) {
78                 String JavaDoc userDir = System.getProperty(USERDIR);
79                                  
80                 if (userDir != null) {
81                     rootForAttributes = new File JavaDoc(userDir, LOCATION);
82                 } else {
83                     rootForAttributes = new File JavaDoc(System.getProperty("java.io.tmpdir"));//NOI18N
84
File JavaDoc tmpAttrs = new File JavaDoc (rootForAttributes, ATTRNAME);
85                     if (tmpAttrs.exists()) {
86                         tmpAttrs.delete();
87                     }
88                     tmpAttrs.deleteOnExit();
89                 }
90                 
91                 
92                 if (!rootForAttributes.exists()) {
93                     rootForAttributes.mkdirs();
94                 }
95             }
96         }
97         return rootForAttributes;
98     }
99
100     /** isn't filtered anymore as it was in DefaultAttributes*/
101     public String JavaDoc[] children(String JavaDoc f) {
102         return list.children(f);
103     }
104
105     /* Get the file attribute with the specified name.
106     * @param name the file
107     * @param attrName name of the attribute
108     * @return appropriate (serializable) value or <CODE>null</CODE> if the attribute is unset (or could not be properly restored for some reason)
109     */

110     public Object JavaDoc readAttribute(String JavaDoc name, String JavaDoc attrName) {
111         final String JavaDoc translatedName = translateName(name);
112         Object JavaDoc retVal = getPreferedAttributes().readAttribute(translatedName, attrName);
113         if (retVal == null && isBackwardCompatible()) {
114             retVal = super.readAttribute(name, attrName);
115             if (retVal != null) {
116                 copyAllToUserDir(name, super.attributes(name));
117                 retVal = getPreferedAttributes().readAttribute(translatedName, attrName);
118             }
119         }
120         return retVal;
121     }
122
123     /* Set the file attribute with the specified name.
124     * @param name the file
125     * @param attrName name of the attribute
126     * @param value new value or <code>null</code> to clear the attribute. Must be serializable, although particular filesystems may or may not use serialization to store attribute values.
127     * @exception IOException if the attribute cannot be set. If serialization is used to store it, this may in fact be a subclass such as {@link NotSerializableException}.
128     */

129     public void writeAttribute(String JavaDoc name, String JavaDoc attrName, Object JavaDoc value)
130             throws IOException JavaDoc {
131         getPreferedAttributes().writeAttribute(translateName(name), attrName, value);
132     }
133
134     /* Get all file attribute names for the file.
135     * @param name the file
136     * @return enumeration of keys (as strings)
137     */

138     public synchronized Enumeration JavaDoc attributes(String JavaDoc name) {
139         Enumeration JavaDoc retVal = getPreferedAttributes().attributes(translateName(name));
140         if ((retVal == null || !retVal.hasMoreElements()) && isBackwardCompatible()) {
141             retVal = copyAllToUserDir(name, super.attributes(name));
142         }
143         return retVal;
144     }
145
146     private Enumeration JavaDoc copyAllToUserDir(String JavaDoc name, Enumeration JavaDoc attributeNames) {
147         
148         if (attributeNames != null && attributeNames.hasMoreElements() && isBackwardCompatible()) {
149             final String JavaDoc translatedName = translateName(name);
150             
151             while (attributeNames.hasMoreElements()) {
152                 String JavaDoc attrName = (String JavaDoc) attributeNames.nextElement();
153                 Object JavaDoc value = super.readAttribute(name, attrName);
154                 try {
155                     getPreferedAttributes().writeAttribute(translatedName, attrName, value);
156                 } catch (IOException JavaDoc e) {
157                     Exceptions.printStackTrace(e);
158                 }
159             }
160             super.deleteAttributes(name);
161             attributeNames = getPreferedAttributes().attributes(translatedName);
162         }
163         return attributeNames;
164     }
165
166     /* Called when a file is renamed, to appropriatelly update its attributes.
167     * <p>
168     * @param oldName old name of the file
169     * @param newName new name of the file
170     */

171     public synchronized void renameAttributes(String JavaDoc oldName, String JavaDoc newName) {
172         if (isBackwardCompatible()) copyAllToUserDir(oldName, super.attributes(oldName));
173         getPreferedAttributes().renameAttributes(translateName(oldName), translateName(newName));
174     }
175
176     /* Called when a file is deleted to also delete its attributes.
177     *
178     * @param name name of the file
179     */

180     public synchronized void deleteAttributes(String JavaDoc name) {
181         if (isBackwardCompatible()) super.deleteAttributes(name);
182         getPreferedAttributes().deleteAttributes(translateName(name));
183     }
184
185     /** adds prefix: systemName of FileSystem */
186     private String JavaDoc translateName(String JavaDoc name) {
187         return (attributePrefix.endsWith("/"))? attributePrefix+"/"+name: attributePrefix+name; // NOI18N
188
}
189     
190     private DefaultAttributes getPreferedAttributes() {
191         synchronized (Attributes.class) {
192             if (sharedUserAttributes == null) {
193                 ExLocalFileSystem exLFs = null;
194                 try {
195                     exLFs = ExLocalFileSystem.getInstance(getRootForAttributes());
196                 } catch (PropertyVetoException JavaDoc e) {
197                     Exceptions.printStackTrace(e);
198                 } catch (IOException JavaDoc e) {
199                     Exceptions.printStackTrace(e);
200                 }
201                 sharedUserAttributes = exLFs.getAttributes();
202             }
203         }
204
205         assert sharedUserAttributes != null;
206         return (sharedUserAttributes != null) ? sharedUserAttributes : this;
207     }
208
209     private boolean isBackwardCompatible() {
210         return BACKWARD_COMPATIBILITY && (getPreferedAttributes() != this);
211     }
212 }
213
Popular Tags