2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
43
package org.netbeans.modules.masterfs.providers;
45
import org.netbeans.modules.masterfs.ExLocalFileSystem;
46
import org.openide.util.Utilities;
47
import org.openide.filesystems.AbstractFileSystem;
48
import org.openide.filesystems.DefaultAttributes;
49
import org.openide.filesystems.FileUtil;
51
import java.beans.PropertyVetoException;
53
import java.io.IOException;
54
import java.util.Enumeration;
55
import org.openide.util.Exceptions;
58
* Implementation of DefaultAttributes that should be shared by all
59
* filesystems that are mounted into MasterFileSystem.
61
* Ensures that .nbattrs file is stored in netbeans.user/var/cache/attribs/.nbattrs.
62
* There exist just one file for all attributes. This implemenation is supposed
63
* to provide backward compatibility.
66
public class Attributes extends DefaultAttributes {
67
public static String ATTRNAME = "attributes.xml";
68
private static final String USERDIR = "netbeans.user";//NOI18N
69
private static final String LOCATION = "var";//NOI18N
71
private static DefaultAttributes sharedUserAttributes;
73
private final String attributePrefix;
74
private AbstractFileSystem.List list;
75
private static final boolean BACKWARD_COMPATIBILITY = false;
76
private static File rootForAttributes;
79
public Attributes(File mountPoint, AbstractFileSystem.Info info, AbstractFileSystem.Change change, AbstractFileSystem.List list) {
80
super(info, change, list);
82
this.attributePrefix = preparePrefix(mountPoint);
85
public Attributes(AbstractFileSystem.Info info, AbstractFileSystem.Change change, AbstractFileSystem.List list) {
86
super(info, change, list);
88
this.attributePrefix = "";
91
private String preparePrefix(File fileSystemRoot) {
92
fileSystemRoot = FileUtil.normalizeFile(fileSystemRoot);
93
String rootPath = fileSystemRoot.getAbsolutePath().replace('\\', '/');
94
return ((Utilities.isWindows () || (Utilities.getOperatingSystem () == Utilities.OS_OS2))) ? rootPath.toLowerCase() : rootPath;
97
public static File getRootForAttributes() {
98
synchronized (ExLocalFileSystem.class) {
99
if (rootForAttributes == null) {
100
String userDir = System.getProperty(USERDIR);
102
if (userDir != null) {
103
rootForAttributes = new File(userDir, LOCATION);
105
rootForAttributes = new File(System.getProperty("java.io.tmpdir"));//NOI18N
106
File tmpAttrs = new File (rootForAttributes, ATTRNAME);
107
if (tmpAttrs.exists()) {
110
tmpAttrs.deleteOnExit();
114
if (!rootForAttributes.exists()) {
115
rootForAttributes.mkdirs();
119
return rootForAttributes;
122
/** isn't filtered anymore as it was in DefaultAttributes*/
123
public String[] children(String f) {
124
return list.children(f);
127
/* Get the file attribute with the specified name.
128
* @param name the file
129
* @param attrName name of the attribute
130
* @return appropriate (serializable) value or <CODE>null</CODE> if the attribute is unset (or could not be properly restored for some reason)
132
public Object readAttribute(String name, String attrName) {
133
final String translatedName = translateName(name);
134
Object retVal = getPreferedAttributes().readAttribute(translatedName, attrName);
135
if (retVal == null && isBackwardCompatible()) {
136
retVal = super.readAttribute(name, attrName);
137
if (retVal != null) {
138
copyAllToUserDir(name, super.attributes(name));
139
retVal = getPreferedAttributes().readAttribute(translatedName, attrName);
145
/* Set the file attribute with the specified name.
146
* @param name the file
147
* @param attrName name of the attribute
148
* @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.
149
* @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}.
151
public void writeAttribute(String name, String attrName, Object value)
153
getPreferedAttributes().writeAttribute(translateName(name), attrName, value);
156
/* Get all file attribute names for the file.
157
* @param name the file
158
* @return enumeration of keys (as strings)
160
public synchronized Enumeration attributes(String name) {
161
Enumeration retVal = getPreferedAttributes().attributes(translateName(name));
162
if ((retVal == null || !retVal.hasMoreElements()) && isBackwardCompatible()) {
163
retVal = copyAllToUserDir(name, super.attributes(name));
168
private Enumeration copyAllToUserDir(String name, Enumeration attributeNames) {
170
if (attributeNames != null && attributeNames.hasMoreElements() && isBackwardCompatible()) {
171
final String translatedName = translateName(name);
173
while (attributeNames.hasMoreElements()) {
174
String attrName = (String) attributeNames.nextElement();
175
Object value = super.readAttribute(name, attrName);
177
getPreferedAttributes().writeAttribute(translatedName, attrName, value);
178
} catch (IOException e) {
179
Exceptions.printStackTrace(e);
182
super.deleteAttributes(name);
183
attributeNames = getPreferedAttributes().attributes(translatedName);
185
return attributeNames;
188
/* Called when a file is renamed, to appropriatelly update its attributes.
190
* @param oldName old name of the file
191
* @param newName new name of the file
193
public synchronized void renameAttributes(String oldName, String newName) {
194
if (isBackwardCompatible()) copyAllToUserDir(oldName, super.attributes(oldName));
195
getPreferedAttributes().renameAttributes(translateName(oldName), translateName(newName));
198
/* Called when a file is deleted to also delete its attributes.
200
* @param name name of the file
202
public synchronized void deleteAttributes(String name) {
203
if (isBackwardCompatible()) super.deleteAttributes(name);
204
getPreferedAttributes().deleteAttributes(translateName(name));
207
/** adds prefix: systemName of FileSystem */
208
private String translateName(String name) {
209
return (attributePrefix.endsWith("/"))? attributePrefix+"/"+name: attributePrefix+name; // NOI18N
212
private DefaultAttributes getPreferedAttributes() {
213
synchronized (Attributes.class) {
214
if (sharedUserAttributes == null) {
215
ExLocalFileSystem exLFs = null;
217
exLFs = ExLocalFileSystem.getInstance(getRootForAttributes());
218
} catch (PropertyVetoException e) {
219
Exceptions.printStackTrace(e);
220
} catch (IOException e) {
221
Exceptions.printStackTrace(e);
223
sharedUserAttributes = exLFs.getAttributes();
227
assert sharedUserAttributes != null;
228
return (sharedUserAttributes != null) ? sharedUserAttributes : this;
231
private boolean isBackwardCompatible() {
232
return BACKWARD_COMPATIBILITY && (getPreferedAttributes() != this);