KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jlibrary > cache > impl > WorkspaceCache


1 /*
2 * jLibrary, Open Source Document Management System
3 *
4 * Copyright (c) 2003-2006, Martín Pérez Mariñán, and individual
5 * contributors as indicated by the @authors tag. See copyright.txt in the
6 * distribution for a full listing of individual contributors.
7 * All rights reserved.
8 *
9 * This is free software; you can redistribute it and/or modify it
10 * under the terms of the Modified BSD License as published by the Free
11 * Software Foundation.
12 *
13 * This software is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Modified
16 * BSD License for more details.
17 *
18 * You should have received a copy of the Modified BSD License along with
19 * this software; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the
21 * FSF site: http://www.fsf.org.
22 */

23 package org.jlibrary.cache.impl;
24
25 import java.io.ByteArrayInputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28
29 import org.apache.commons.io.IOUtils;
30 import org.apache.commons.lang.StringUtils;
31 import org.eclipse.core.resources.IFile;
32 import org.eclipse.core.resources.IFolder;
33 import org.eclipse.core.resources.IProject;
34 import org.eclipse.core.resources.IResource;
35 import org.eclipse.core.resources.IWorkspace;
36 import org.eclipse.core.resources.IWorkspaceRoot;
37 import org.eclipse.core.resources.ResourcesPlugin;
38 import org.eclipse.core.runtime.CoreException;
39 import org.eclipse.core.runtime.NullProgressMonitor;
40 import org.jlibrary.client.cache.LocalCache;
41 import org.jlibrary.client.cache.LocalCacheException;
42 import org.jlibrary.core.entities.Node;
43 import org.jlibrary.core.entities.Repository;
44 import org.jlibrary.core.util.FileUtils;
45
46
47 /**
48  * @author martin
49  *
50  * <p>This class implements the jLibrary LocalCache interface. That interface
51  * defines several callback methods used to store contents locally to allow
52  * fast access to document contents.</p>
53  *
54  * <p>This implementation uses a workspace-based file system to store the
55  * contents of the documents. Each repository will be linked with an Eclipse
56  * workspace that will be stored on the jLibrary workspace data location.
57  * Each jLibrary repository will correspond with a workspace folder and each
58  * jLibrary document and resource will correspond with a workspace file.</p>
59  *
60  * <p>Obviously, this implementation depends on the Eclipse Resources API.</p>
61  */

62 public class WorkspaceCache implements LocalCache {
63     
64     public WorkspaceCache() {
65                 
66         super();
67         System.out.println("[WorkspaceCache] Initializing workspace cache");
68     }
69     
70     /**
71      * @see org.jlibrary.client.cache.LocalCache#clearCache()
72      */

73     public void clearCache() throws LocalCacheException {
74
75         IWorkspace workspace = ResourcesPlugin.getWorkspace();
76         IWorkspaceRoot root = workspace.getRoot();
77
78         try {
79             IProject[] projects = root.getProjects();
80             for (int i = 0; i < projects.length; i++) {
81                 projects[i].delete(true,true,null);
82             }
83         } catch (CoreException e) {
84             e.printStackTrace();
85             throw new LocalCacheException(e);
86         }
87     }
88     
89     /**
90      * @see org.jlibrary.client.cache.LocalCache#addNodeToCache(org.jlibrary.core.entities.Node, byte[])
91      */

92     public void addNodeToCache(Node node,
93                                byte[] content) throws LocalCacheException {
94
95         System.out.println("[WorkspaceCache] Adding node to cache: " + node.getName());
96         String JavaDoc repository = node.getRepository();
97         IWorkspace workspace = checkWorkspace(repository);
98         IProject project = workspace.getRoot().getProject(node.getRepository());
99         IFolder folder = checkFolder(project, node.getPath());
100         createFile(project,folder,node,content);
101     }
102
103     /**
104      * @see org.jlibrary.client.cache.LocalCache#removeNodeFromCache(org.jlibrary.core.entities.Node)
105      */

106     public void removeNodeFromCache(Node node) throws LocalCacheException {
107
108         IWorkspace workspace = ResourcesPlugin.getWorkspace();
109         IWorkspaceRoot root = workspace.getRoot();
110
111         String JavaDoc repository = node.getRepository();
112         IProject project = root.getProject(repository);
113         if (!project.exists()) {
114             return;
115         }
116         deleteFile(project,node);
117     }
118     
119
120
121     /**
122      * @see org.jlibrary.client.cache.LocalCache#getNodeContent(org.jlibrary.core.entities.Node)
123      */

124     public byte[] getNodeContent(Node node) throws LocalCacheException {
125
126         IWorkspace workspace = ResourcesPlugin.getWorkspace();
127         IWorkspaceRoot root = workspace.getRoot();
128
129         String JavaDoc repository = node.getRepository();
130         IProject project = root.getProject(repository);
131         if (!project.exists()) {
132             throw new LocalCacheException("Local project does not exist");
133         }
134         IFile file = getFile(project,node);
135
136         if (file == null) {
137             throw new LocalCacheException("The file cannot be found on local cache");
138         }
139         
140         try {
141             // Refresh file contents
142
file.refreshLocal(IResource.DEPTH_ZERO, null);
143             
144             InputStream JavaDoc stream = file.getContents();
145             byte[] contents = IOUtils.toByteArray(stream);
146             stream.close();
147             return contents;
148         } catch (CoreException e) {
149             e.printStackTrace();
150             throw new LocalCacheException(e);
151         } catch (IOException JavaDoc e) {
152             e.printStackTrace();
153             throw new LocalCacheException(e);
154         }
155     }
156     
157     /**
158      * @see org.jlibrary.client.cache.LocalCache#getNodePath(org.jlibrary.core.entities.Node)
159      */

160     public String JavaDoc getNodePath(Node node) throws LocalCacheException {
161
162         IWorkspace workspace = ResourcesPlugin.getWorkspace();
163         IWorkspaceRoot root = workspace.getRoot();
164
165         String JavaDoc repository = node.getRepository();
166         IProject project = root.getProject(repository);
167         if (!project.exists()) {
168             throw new LocalCacheException("Local project does not exist");
169         }
170         IFile file = getFile(project,node);
171         if (file == null) {
172             throw new LocalCacheException("The file cannot be found on local cache");
173         }
174         
175         return file.getLocation().toFile().getAbsolutePath();
176     }
177     
178     /**
179      * @see org.jlibrary.client.cache.LocalCache#isNodeCached(org.jlibrary.core.entities.Node)
180      */

181     public boolean isNodeCached(Node node) throws LocalCacheException {
182
183         IWorkspace workspace = ResourcesPlugin.getWorkspace();
184         IWorkspaceRoot root = workspace.getRoot();
185
186         String JavaDoc repository = node.getRepository();
187         IProject project = root.getProject(repository);
188         if (!project.exists()) {
189             return false;
190         }
191         IFile file = getFile(project,node);
192         if ((file == null) || (!file.exists())) {
193             return false;
194         }
195         return true;
196     }
197     
198     /**
199      * @see org.jlibrary.client.cache.LocalCache#clearCache(org.jlibrary.core.entities.Repository)
200      */

201     public void clearCache(Repository repository) throws LocalCacheException {
202
203         IWorkspace workspace = ResourcesPlugin.getWorkspace();
204         IWorkspaceRoot root = workspace.getRoot();
205
206         IProject project = root.getProject(repository.getId());
207         if (!project.exists()) {
208             throw new LocalCacheException("Local project does not exist");
209         }
210         try {
211             project.delete(true,true,null);
212         } catch (CoreException e) {
213             e.printStackTrace();
214             throw new LocalCacheException(e);
215         }
216     }
217     
218     private IWorkspace checkWorkspace(String JavaDoc repository) throws LocalCacheException {
219         
220         try {
221             IWorkspace workspace = ResourcesPlugin.getWorkspace();
222             IWorkspaceRoot root = workspace.getRoot();
223             IProject project = root.getProject(repository);
224             if (!project.exists()) {
225                 project.create(new NullProgressMonitor());
226             }
227             if (!project.isOpen()) {
228                 project.open(null);
229             }
230         
231             return workspace;
232         } catch (CoreException e) {
233             e.printStackTrace();
234             throw new LocalCacheException(e);
235         }
236     }
237     
238     private IFolder checkFolder(IProject project,
239                                 String JavaDoc path) throws LocalCacheException {
240
241         try {
242             IFolder folder = null;
243             
244             String JavaDoc[] splittedPath = StringUtils.split(path,"/");
245             if (splittedPath.length > 1) {
246                 for (int i = 0; i < splittedPath.length-1; i++) {
247                     String JavaDoc folderName = splittedPath[i];
248                     if (folder == null) {
249                         folder = project.getFolder(folderName);
250                     } else {
251                         folder = folder.getFolder(folderName);
252                     }
253                     if (!folder.exists()) {
254                         folder.create(IResource.NONE,true,null);
255                     }
256                 }
257             }
258
259             return folder;
260         } catch (CoreException e) {
261             e.printStackTrace();
262             throw new LocalCacheException(e);
263         }
264     }
265
266     private IFile createFile(IProject project,
267                              IFolder folder,
268                              Node node,
269                              byte[] content) throws LocalCacheException {
270
271         try {
272             String JavaDoc filename = FileUtils.getFileName(node.getPath());
273             IFile file = null;
274             if (folder == null) {
275                 file = project.getFile(filename);
276             } else {
277                 file = folder.getFile(filename);
278             }
279
280             if (!file.exists()) {
281                 InputStream JavaDoc source = new ByteArrayInputStream JavaDoc(content);
282                 file.create(source, IResource.NONE, null);
283             }
284             
285             return file;
286         } catch (CoreException e) {
287             e.printStackTrace();
288             throw new LocalCacheException(e);
289         }
290         
291     }
292     
293     private IFile getFile(IProject project,Node node) {
294
295         IFolder folder = null;
296         
297         String JavaDoc[] splittedPath = StringUtils.split(node.getPath(),"/");
298         if (splittedPath.length > 1) {
299             for (int i = 0; i < splittedPath.length-1; i++) {
300                 String JavaDoc folderName = splittedPath[i];
301                 if (folder == null) {
302                     folder = project.getFolder(folderName);
303                 } else {
304                     folder = folder.getFolder(folderName);
305                 }
306                 if (!folder.exists()) {
307                     return null;
308                 }
309             }
310         }
311         
312         String JavaDoc filename = FileUtils.getFileName(node.getPath());
313         IFile file = null;
314         if (folder == null) {
315             file = project.getFile(filename);
316         } else {
317             file = folder.getFile(filename);
318         }
319         
320         if (!file.exists()) {
321             return null;
322         }
323         
324         return file;
325     }
326     
327     private void deleteFile(IProject project,
328                             Node node) throws LocalCacheException {
329
330         IFile file = getFile(project,node);
331         if ((file == null) || !file.exists()) {
332             return;
333         }
334         try {
335             file.delete(true,null);
336         } catch (CoreException e) {
337             e.printStackTrace();
338             throw new LocalCacheException(e);
339         }
340     }
341     
342     /**
343      * Returns the file associated with the given node
344      *
345      * @param node Node to find
346      *
347      * @return IFile File associated with that node
348      */

349     public IFile getFile(Node node) throws LocalCacheException {
350
351         IWorkspace workspace = ResourcesPlugin.getWorkspace();
352         IWorkspaceRoot root = workspace.getRoot();
353
354         String JavaDoc repository = node.getRepository();
355         IProject project = root.getProject(repository);
356         if (!project.exists()) {
357             throw new LocalCacheException("Local project does not exist");
358         }
359         IFile file = getFile(project,node);
360         if (file == null) {
361             throw new LocalCacheException("The file cannot be found on local cache");
362         }
363         
364         return file;
365     }
366 }
367
Popular Tags