~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to openide/masterfs/src/org/netbeans/modules/masterfs/providers/Attributes.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
 
 
43
package org.netbeans.modules.masterfs.providers;
 
44
 
 
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;
 
50
 
 
51
import java.beans.PropertyVetoException;
 
52
import java.io.File;
 
53
import java.io.IOException;
 
54
import java.util.Enumeration;
 
55
import org.openide.util.Exceptions;
 
56
 
 
57
/**
 
58
 * Implementation of DefaultAttributes that should be shared by all
 
59
 * filesystems that are mounted into MasterFileSystem.
 
60
 * 
 
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. 
 
64
 *  
 
65
 */
 
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
 
70
 
 
71
    private static DefaultAttributes sharedUserAttributes;
 
72
 
 
73
    private final String attributePrefix;
 
74
    private AbstractFileSystem.List list;
 
75
    private static final boolean BACKWARD_COMPATIBILITY = false;
 
76
    private static File rootForAttributes;
 
77
 
 
78
 
 
79
    public Attributes(File mountPoint, AbstractFileSystem.Info info, AbstractFileSystem.Change change, AbstractFileSystem.List list) {
 
80
        super(info, change, list);
 
81
        this.list = list;
 
82
        this.attributePrefix = preparePrefix(mountPoint);
 
83
    }
 
84
 
 
85
    public Attributes(AbstractFileSystem.Info info, AbstractFileSystem.Change change, AbstractFileSystem.List list) {
 
86
        super(info, change, list);
 
87
        this.list = list;
 
88
        this.attributePrefix = "";
 
89
    }
 
90
    
 
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;
 
95
    }
 
96
 
 
97
    public static File getRootForAttributes() {
 
98
        synchronized (ExLocalFileSystem.class) {
 
99
            if (rootForAttributes == null) {
 
100
                String userDir = System.getProperty(USERDIR);
 
101
                                 
 
102
                if (userDir != null) {
 
103
                    rootForAttributes = new File(userDir, LOCATION);
 
104
                } else {
 
105
                    rootForAttributes = new File(System.getProperty("java.io.tmpdir"));//NOI18N
 
106
                    File tmpAttrs = new File (rootForAttributes, ATTRNAME);
 
107
                    if (tmpAttrs.exists()) {
 
108
                        tmpAttrs.delete();   
 
109
                    }
 
110
                    tmpAttrs.deleteOnExit();
 
111
                }
 
112
                
 
113
                
 
114
                if (!rootForAttributes.exists()) {
 
115
                    rootForAttributes.mkdirs();
 
116
                }
 
117
            }
 
118
        }
 
119
        return rootForAttributes;
 
120
    }
 
121
 
 
122
    /** isn't filtered anymore as it was in DefaultAttributes*/
 
123
    public String[] children(String f) {
 
124
        return list.children(f);
 
125
    }
 
126
 
 
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)
 
131
    */
 
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);
 
140
            }
 
141
        }
 
142
        return retVal;
 
143
    }
 
144
 
 
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}.
 
150
    */
 
151
    public void writeAttribute(String name, String attrName, Object value)
 
152
            throws IOException {
 
153
        getPreferedAttributes().writeAttribute(translateName(name), attrName, value);
 
154
    }
 
155
 
 
156
    /* Get all file attribute names for the file.
 
157
    * @param name the file
 
158
    * @return enumeration of keys (as strings)
 
159
    */
 
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));
 
164
        }
 
165
        return retVal;
 
166
    }
 
167
 
 
168
    private Enumeration copyAllToUserDir(String name, Enumeration attributeNames) {
 
169
        
 
170
        if (attributeNames != null && attributeNames.hasMoreElements() && isBackwardCompatible()) {
 
171
            final String translatedName = translateName(name);
 
172
            
 
173
            while (attributeNames.hasMoreElements()) {
 
174
                String attrName = (String) attributeNames.nextElement();
 
175
                Object value = super.readAttribute(name, attrName);
 
176
                try {
 
177
                    getPreferedAttributes().writeAttribute(translatedName, attrName, value);
 
178
                } catch (IOException e) {
 
179
                    Exceptions.printStackTrace(e);
 
180
                }
 
181
            }
 
182
            super.deleteAttributes(name);
 
183
            attributeNames = getPreferedAttributes().attributes(translatedName);
 
184
        }
 
185
        return attributeNames;
 
186
    }
 
187
 
 
188
    /* Called when a file is renamed, to appropriatelly update its attributes.
 
189
    * <p>
 
190
    * @param oldName old name of the file
 
191
    * @param newName new name of the file
 
192
    */
 
193
    public synchronized void renameAttributes(String oldName, String newName) {
 
194
        if (isBackwardCompatible()) copyAllToUserDir(oldName, super.attributes(oldName));
 
195
        getPreferedAttributes().renameAttributes(translateName(oldName), translateName(newName));
 
196
    }
 
197
 
 
198
    /* Called when a file is deleted to also delete its attributes.
 
199
    *
 
200
    * @param name name of the file
 
201
    */
 
202
    public synchronized void deleteAttributes(String name) {
 
203
        if (isBackwardCompatible()) super.deleteAttributes(name);
 
204
        getPreferedAttributes().deleteAttributes(translateName(name));
 
205
    }
 
206
 
 
207
    /** adds prefix: systemName of FileSystem */
 
208
    private String translateName(String name) {
 
209
        return (attributePrefix.endsWith("/"))? attributePrefix+"/"+name: attributePrefix+name; // NOI18N
 
210
    }
 
211
    
 
212
    private DefaultAttributes getPreferedAttributes() {
 
213
        synchronized (Attributes.class) {
 
214
            if (sharedUserAttributes == null) {
 
215
                ExLocalFileSystem exLFs = null;
 
216
                try {
 
217
                    exLFs = ExLocalFileSystem.getInstance(getRootForAttributes());                    
 
218
                } catch (PropertyVetoException e) {
 
219
                    Exceptions.printStackTrace(e);
 
220
                } catch (IOException e) {
 
221
                    Exceptions.printStackTrace(e);
 
222
                }
 
223
                sharedUserAttributes = exLFs.getAttributes();
 
224
            }
 
225
        }
 
226
 
 
227
        assert sharedUserAttributes != null;
 
228
        return (sharedUserAttributes != null) ? sharedUserAttributes : this;
 
229
    }
 
230
 
 
231
    private boolean isBackwardCompatible() {
 
232
        return BACKWARD_COMPATIBILITY && (getPreferedAttributes() != this);
 
233
    }
 
234
}