1 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 ; 30 import java.io.File ; 31 import java.io.IOException ; 32 import java.util.Enumeration ; 33 import org.openide.util.Exceptions; 34 35 44 public class Attributes extends DefaultAttributes { 45 public static String ATTRNAME = "attributes.xml"; 46 private static final String USERDIR = "netbeans.user"; private static final String LOCATION = "var"; 49 private static DefaultAttributes sharedUserAttributes; 50 51 private final String attributePrefix; 52 private AbstractFileSystem.List list; 53 private static final boolean BACKWARD_COMPATIBILITY = true; 54 private static File rootForAttributes; 55 56 57 public Attributes(File 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 preparePrefix(File fileSystemRoot) { 70 fileSystemRoot = FileUtil.normalizeFile(fileSystemRoot); 71 String rootPath = fileSystemRoot.getAbsolutePath().replace('\\', '/'); 72 return ((Utilities.isWindows () || (Utilities.getOperatingSystem () == Utilities.OS_OS2))) ? rootPath.toLowerCase() : rootPath; 73 } 74 75 public static File getRootForAttributes() { 76 synchronized (ExLocalFileSystem.class) { 77 if (rootForAttributes == null) { 78 String userDir = System.getProperty(USERDIR); 79 80 if (userDir != null) { 81 rootForAttributes = new File (userDir, LOCATION); 82 } else { 83 rootForAttributes = new File (System.getProperty("java.io.tmpdir")); File tmpAttrs = new File (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 101 public String [] children(String f) { 102 return list.children(f); 103 } 104 105 110 public Object readAttribute(String name, String attrName) { 111 final String translatedName = translateName(name); 112 Object 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 129 public void writeAttribute(String name, String attrName, Object value) 130 throws IOException { 131 getPreferedAttributes().writeAttribute(translateName(name), attrName, value); 132 } 133 134 138 public synchronized Enumeration attributes(String name) { 139 Enumeration 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 copyAllToUserDir(String name, Enumeration attributeNames) { 147 148 if (attributeNames != null && attributeNames.hasMoreElements() && isBackwardCompatible()) { 149 final String translatedName = translateName(name); 150 151 while (attributeNames.hasMoreElements()) { 152 String attrName = (String ) attributeNames.nextElement(); 153 Object value = super.readAttribute(name, attrName); 154 try { 155 getPreferedAttributes().writeAttribute(translatedName, attrName, value); 156 } catch (IOException e) { 157 Exceptions.printStackTrace(e); 158 } 159 } 160 super.deleteAttributes(name); 161 attributeNames = getPreferedAttributes().attributes(translatedName); 162 } 163 return attributeNames; 164 } 165 166 171 public synchronized void renameAttributes(String oldName, String newName) { 172 if (isBackwardCompatible()) copyAllToUserDir(oldName, super.attributes(oldName)); 173 getPreferedAttributes().renameAttributes(translateName(oldName), translateName(newName)); 174 } 175 176 180 public synchronized void deleteAttributes(String name) { 181 if (isBackwardCompatible()) super.deleteAttributes(name); 182 getPreferedAttributes().deleteAttributes(translateName(name)); 183 } 184 185 186 private String translateName(String name) { 187 return (attributePrefix.endsWith("/"))? attributePrefix+"/"+name: attributePrefix+name; } 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 e) { 197 Exceptions.printStackTrace(e); 198 } catch (IOException 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 |