1 19 20 package org.netbeans.core.registry; 21 22 import org.netbeans.spi.registry.BasicContext; 23 import org.netbeans.core.registry.ConvertorBinding; 24 import org.netbeans.core.registry.InstanceBinding; 25 import org.openide.filesystems.FileObject; 26 import org.openide.ErrorManager; 27 28 import java.io.IOException ; 29 import java.util.Collection ; 30 import java.util.Collections ; 31 import java.util.HashSet ; 32 import java.util.Set ; 33 import java.lang.ref.WeakReference ; 34 35 48 abstract class ObjectBinding { 49 private static final ErrorManager err = ErrorManager.getDefault().getInstance("org.netbeans.core.registry"); private static final Reader[] READERS = 51 new Reader[]{InstanceBinding.READER, ShadowBinding.READER, 52 ObjectRefBinding.READER, SettingBinding.READER, 53 ConvertorBinding.READER, XMLBasedBinding.READER}; 54 55 private static final Writer[] WRITERS = 56 new Writer[]{ObjectRefBinding.WRITER, ConvertorBinding.WRITER, 57 SettingBinding.WRITER}; 58 59 private FileObject file; 60 private WeakReference cachedInstance = null; 61 62 63 ObjectBinding(FileObject fo) { 64 this.file = fo; 65 } 66 67 70 BasicContext getContext () { 71 return null; 72 } 73 74 public static Collection getFileExtensions() { 75 Set s = new HashSet (READERS.length); 76 for (int i = 0; i < READERS.length; i++) { 77 Reader reader = READERS[i]; 78 s.add(reader.getFileExtension()); 79 } 80 return Collections.unmodifiableCollection(s); 81 } 82 83 public static ObjectBinding get(BasicContext ctx, FileObject fo) { 84 if (fo == null || !fo.isValid() || fo.isFolder()) return null; 85 86 Reader r = getReader(fo); 87 88 if (r == null) { 89 ErrorManager errorManager = getErrorManager(); 90 if (errorManager.isLoggable(ErrorManager.INFORMATIONAL)) { 91 errorManager.log("File " + fo + " was parsed, but it is not a .settings file nor namespace-aware XML. It will be ignored."); 92 } 93 } 94 95 return (r != null) ? r.getObjectBinding(ctx, fo) : null; 96 } 97 98 public void delete () throws IOException { 99 FileObject file = getFile(); 100 if (file != null) 101 file.delete(); 102 } 103 104 public ObjectBinding write(final Object object) throws IOException { 105 Writer w = getWriter(object); 106 if (w != null) { 107 w.write(getFile(), object); 108 } 109 110 Reader r = (w != null) ? getReader(getFile()) : null; 111 ObjectBinding retVal = null; 112 if (r != null) { 113 retVal = r.getObjectBinding(getContext(),getFile ()); 114 retVal.cachedInstance = new WeakReference (object); 115 } 116 return retVal; 117 } 118 119 public static ObjectBinding write(BasicContext ctx, FileObject folder, String bindingName, Object object) throws IOException { 120 Writer w = getWriter(object); 121 FileObject fo = null; 122 if (w != null) { 123 fo = w.write(folder, bindingName, object); 124 } 125 126 Reader r = (fo != null) ? getReader(fo) : null; 127 ObjectBinding retVal = null; 128 if (r != null) { 129 retVal = r.getObjectBinding(ctx,fo); 130 retVal.cachedInstance = new WeakReference (object); 131 } 132 return retVal; 133 } 134 135 140 public Object getModuleDescriptor() { 141 return getDefaultModuleDescriptor(); 142 } 143 144 private static Object getDefaultModuleDescriptor() { 145 return new Object () { 146 public boolean equals(Object obj) { 147 return false; 148 } 149 }; 150 } 151 152 153 private static Reader getReader(FileObject fo) { 154 for (int i = 0; i < READERS.length; i++) { 155 if (READERS[i].canRead(fo)) { 156 return READERS[i]; 157 } 158 } 159 return null; 160 } 161 162 private static Writer getWriter(Object obj) { 163 for (int i = 0; i < WRITERS.length; i++) { 164 if (WRITERS[i].canWrite(obj)) { 165 return WRITERS[i]; 166 } 167 168 } 169 return null; 170 } 171 172 175 public final FileObject getFile() { 176 return file; 177 } 178 179 180 public String getBindingName() { 181 return file.getName(); 182 } 183 184 public final Object getObject() throws IOException { 185 Object retVal = null; 186 if (cachedInstance == null || (retVal = cachedInstance.get()) == null) { 187 retVal = createInstance(); 188 cachedInstance = new WeakReference (retVal); 189 } 190 191 return retVal; 192 } 193 194 195 public boolean isBinding(String bindingName) { 196 return getBindingName().equals(bindingName); 197 } 198 199 abstract public Object createInstance() throws IOException ; 200 201 abstract public boolean isEnabled(); 202 203 public static final ErrorManager getErrorManager() { 204 return err; 205 } 206 207 abstract static class Reader { 208 abstract boolean canRead(FileObject fo); 209 210 abstract String getFileExtension(); 211 212 abstract ObjectBinding getObjectBinding(BasicContext ctx, FileObject fo); 213 } 214 215 abstract static class Writer { 216 abstract boolean canWrite(Object obj); 217 218 abstract void write(FileObject fo, Object obj) throws IOException ; 219 220 abstract String getFileExtension(); 221 222 final FileObject write(FileObject folder, String bindingName, Object obj) throws IOException { 223 FileObject fo; 224 synchronized (this) { 225 fo = folder.getFileObject(bindingName, getFileExtension()); 226 if (fo == null) { 227 fo = folder.createData(bindingName, getFileExtension()); 228 } 229 } 230 this.write(fo, obj); 231 return fo; 232 } 233 } 234 } 235 | Popular Tags |