KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > properties > IndexedStoreWrapper


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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     /* constants */
27     private static final String JavaDoc INDEX_NAME = "index"; //$NON-NLS-1$
28

29     public IndexedStoreWrapper(IPath location) {
30         this.location = location;
31     }
32
33     private void open() throws CoreException {
34         try {
35             String JavaDoc name = location.toOSString();
36             store = IndexedStore.find(name);
37             if (store == null) {
38                 store = new IndexedStore();
39                 store.open(name);
40             }
41         } catch (Exception JavaDoc e) {
42             String JavaDoc 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         // Rename the problematic store for future analysis.
51
java.io.File JavaDoc file = location.toFile();
52         file.renameTo(location.addFileExtension("001").toFile()); //$NON-NLS-1$
53
file.delete();
54         if (!file.exists()) {
55             try {
56                 open();
57             } catch (CoreException e) {
58                 //failed again, null the store to make sure we
59
//don't attempt to access an invalid store.
60
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 JavaDoc e) {
72             String JavaDoc 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 JavaDoc e) {
85             String JavaDoc 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             //failed to open -- copy store elsewhere and create a new one
97
recreate();
98             if (store == null) {
99                 String JavaDoc 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 JavaDoc e) {
110             String JavaDoc 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 JavaDoc 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             //just rethrow
127
throw e;
128         } catch (Exception JavaDoc e) {
129             problem = e;
130             return null;
131         } finally {
132             if (problem != null) {
133                 String JavaDoc 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 JavaDoc getObjectAsString(ObjectID id) throws CoreException {
141         try {
142             return getStore().getObjectAsString(id);
143         } catch (Exception JavaDoc e) {
144             String JavaDoc 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 JavaDoc e) {
159             String JavaDoc 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 JavaDoc s) throws CoreException {
165         try {
166             return getStore().createObject(s);
167         } catch (Exception JavaDoc e) {
168             String JavaDoc 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 JavaDoc e) {
177             String JavaDoc 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 JavaDoc e) {
186             String JavaDoc 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 JavaDoc e) {
195             String JavaDoc message = NLS.bind(CompatibilityMessages.indexed_couldNotRead, location.toOSString());
196             throw new ResourceException(IResourceStatus.FAILED_READ_LOCAL, location, message, e);
197         }
198     }
199
200     /**
201      * Something has gone wrong. Make a best effort at copying the file
202      * elsewhere and creating a new one. Log exceptions.
203      */

204     public synchronized void reset() {
205         try {
206             recreate();
207         } catch (CoreException e) {
208             Policy.log(e.getStatus());
209         }
210     }
211 }
212
Popular Tags