1 11 package org.eclipse.core.internal.properties; 12 13 import org.eclipse.core.internal.indexing.*; 14 import org.eclipse.core.internal.resources.*; 15 import org.eclipse.core.internal.utils.Policy; 16 import org.eclipse.core.resources.IResourceStatus; 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IPath; 19 import org.eclipse.osgi.util.NLS; 20 21 public class IndexedStoreWrapper { 22 23 private IndexedStore store; 24 private IPath location; 25 26 27 private static final String INDEX_NAME = "index"; 29 public IndexedStoreWrapper(IPath location) { 30 this.location = location; 31 } 32 33 private void open() throws CoreException { 34 try { 35 String name = location.toOSString(); 36 store = IndexedStore.find(name); 37 if (store == null) { 38 store = new IndexedStore(); 39 store.open(name); 40 } 41 } catch (Exception e) { 42 String message = NLS.bind(CompatibilityMessages.indexed_couldNotOpen, location.toOSString()); 43 ResourceStatus status = new ResourceStatus(IResourceStatus.FAILED_WRITE_LOCAL, location, message, e); 44 throw new CoreException(status); 45 } 46 } 47 48 private void recreate() throws CoreException { 49 close(); 50 java.io.File file = location.toFile(); 52 file.renameTo(location.addFileExtension("001").toFile()); file.delete(); 54 if (!file.exists()) { 55 try { 56 open(); 57 } catch (CoreException e) { 58 store = null; 61 throw e; 62 } 63 } 64 } 65 66 public synchronized void close() { 67 if (store == null) 68 return; 69 try { 70 store.close(); 71 } catch (Exception e) { 72 String message = NLS.bind(CompatibilityMessages.indexed_couldNotClose, location.toOSString()); 73 Policy.log(new ResourceStatus(IResourceStatus.FAILED_WRITE_LOCAL, location, message, e)); 74 } finally { 75 store = null; 76 } 77 } 78 79 public synchronized void commit() throws CoreException { 80 if (store == null) 81 return; 82 try { 83 store.commit(); 84 } catch (Exception e) { 85 String message = NLS.bind(CompatibilityMessages.indexed_couldNotCommit, location.toOSString()); 86 ResourceStatus status = new ResourceStatus(IResourceStatus.FAILED_WRITE_LOCAL, location, message, e); 87 throw new ResourceException(status); 88 } 89 } 90 91 private void create() throws CoreException { 92 try { 93 open(); 94 } catch (CoreException e) { 95 Policy.log(e.getStatus()); 96 recreate(); 98 if (store == null) { 99 String message = NLS.bind(CompatibilityMessages.indexed_couldNotCreate, location.toOSString()); 100 ResourceStatus status = new ResourceStatus(IResourceStatus.FAILED_WRITE_LOCAL, location, message, null); 101 throw new ResourceException(status); 102 } 103 } 104 } 105 106 private Index createIndex() throws CoreException { 107 try { 108 return getStore().createIndex(INDEX_NAME); 109 } catch (Exception e) { 110 String message = NLS.bind(CompatibilityMessages.indexed_couldNotCreateIndex, location.toOSString()); 111 ResourceStatus status = new ResourceStatus(IResourceStatus.FAILED_WRITE_LOCAL, location, message, e); 112 throw new ResourceException(status); 113 } 114 } 115 116 public synchronized Index getIndex() throws CoreException { 117 Exception problem = null; 118 try { 119 return getStore().getIndex(INDEX_NAME); 120 } catch (IndexedStoreException e) { 121 if (e.id == IndexedStoreException.IndexNotFound) 122 return createIndex(); 123 problem = e; 124 return null; 125 } catch (CoreException e) { 126 throw e; 128 } catch (Exception e) { 129 problem = e; 130 return null; 131 } finally { 132 if (problem != null) { 133 String message = NLS.bind(CompatibilityMessages.indexed_couldNotGetIndex, location.toOSString()); 134 ResourceStatus status = new ResourceStatus(IResourceStatus.FAILED_READ_LOCAL, location, message, problem); 135 throw new ResourceException(status); 136 } 137 } 138 } 139 140 public synchronized String getObjectAsString(ObjectID id) throws CoreException { 141 try { 142 return getStore().getObjectAsString(id); 143 } catch (Exception e) { 144 String message = NLS.bind(CompatibilityMessages.indexed_couldNotRead, location.toOSString()); 145 throw new ResourceException(IResourceStatus.FAILED_READ_LOCAL, location, message, e); 146 } 147 } 148 149 private IndexedStore getStore() throws CoreException { 150 if (store == null) 151 create(); 152 return store; 153 } 154 155 public synchronized IndexCursor getCursor() throws CoreException { 156 try { 157 return getIndex().open(); 158 } catch (Exception e) { 159 String message = NLS.bind(CompatibilityMessages.indexed_couldNotCreateCursor, location.toOSString()); 160 throw new ResourceException(IResourceStatus.FAILED_READ_LOCAL, location, message, e); 161 } 162 } 163 164 public synchronized ObjectID createObject(String s) throws CoreException { 165 try { 166 return getStore().createObject(s); 167 } catch (Exception e) { 168 String message = NLS.bind(CompatibilityMessages.indexed_couldNotWrite, location.toOSString()); 169 throw new ResourceException(IResourceStatus.FAILED_WRITE_LOCAL, location, message, e); 170 } 171 } 172 173 public synchronized ObjectID createObject(byte[] b) throws CoreException { 174 try { 175 return getStore().createObject(b); 176 } catch (Exception e) { 177 String message = NLS.bind(CompatibilityMessages.indexed_couldNotWrite, location.toOSString()); 178 throw new ResourceException(IResourceStatus.FAILED_WRITE_LOCAL, location, message, e); 179 } 180 } 181 182 public synchronized void removeObject(ObjectID id) throws CoreException { 183 try { 184 getStore().removeObject(id); 185 } catch (Exception e) { 186 String message = NLS.bind(CompatibilityMessages.indexed_couldNotDelete, location.toOSString()); 187 throw new ResourceException(IResourceStatus.FAILED_DELETE_LOCAL, location, message, e); 188 } 189 } 190 191 public synchronized byte[] getObject(ObjectID id) throws CoreException { 192 try { 193 return getStore().getObject(id); 194 } catch (Exception e) { 195 String message = NLS.bind(CompatibilityMessages.indexed_couldNotRead, location.toOSString()); 196 throw new ResourceException(IResourceStatus.FAILED_READ_LOCAL, location, message, e); 197 } 198 } 199 200 204 public synchronized void reset() { 205 try { 206 recreate(); 207 } catch (CoreException e) { 208 Policy.log(e.getStatus()); 209 } 210 } 211 } 212 | Popular Tags |