KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > services > cms > repository > slide > CmsContentStore


1 /*
2  * $Header: /home/cvs/jakarta-jetspeed/scratchpad/jetspeed-cms/src/java/org/apache/jetspeed/services/cms/repository/slide/CmsContentStore.java,v 1.1 2003/03/04 11:28:16 raphael Exp $
3  * $Revision: 1.1 $
4  * $Date: 2003/03/04 11:28:16 $
5  *
6  * ====================================================================
7  *
8  * The Apache Software License, Version 1.1
9  *
10  * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
11  * reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  *
17  * 1. Redistributions of source code must retain the above copyright
18  * notice, this list of conditions and the following disclaimer.
19  *
20  * 2. Redistributions in binary form must reproduce the above copyright
21  * notice, this list of conditions and the following disclaimer in
22  * the documentation and/or other materials provided with the
23  * distribution.
24  *
25  * 3. The end-user documentation included with the redistribution, if
26  * any, must include the following acknowlegement:
27  * "This product includes software developed by the
28  * Apache Software Foundation (http://www.apache.org/)."
29  * Alternately, this acknowlegement may appear in the software itself,
30  * if and wherever such third-party acknowlegements normally appear.
31  *
32  * 4. The names "The Jakarta Project", "Slide", and "Apache Software
33  * Foundation" must not be used to endorse or promote products derived
34  * from this software without prior written permission. For written
35  * permission, please contact apache@apache.org.
36  *
37  * 5. Products derived from this software may not be called "Apache"
38  * nor may "Apache" appear in their names without prior written
39  * permission of the Apache Group.
40  *
41  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  * ====================================================================
54  *
55  * This software consists of voluntary contributions made by many
56  * individuals on behalf of the Apache Software Foundation. For more
57  * information on the Apache Software Foundation, please see
58  * <http://www.apache.org/>.
59  *
60  * [Additional notices, if required by prior licensing conditions]
61  *
62  */

63
64 package org.apache.jetspeed.services.cms.repository.slide;
65
66 import java.util.Hashtable JavaDoc;
67 import java.util.Enumeration JavaDoc;
68 import java.util.Date JavaDoc;
69 import java.io.*;
70 import org.apache.slide.common.*;
71 import org.apache.slide.store.*;
72 import org.apache.slide.structure.*;
73 import org.apache.slide.content.*;
74 import org.apache.slide.util.logger.Logger;
75
76 import org.apache.jetspeed.services.resources.JetspeedResources;
77
78 /**
79  * Filesystem implementation of ContentStore .
80  *
81  * @author <a HREF="mailto:remm@apache.org">Remy Maucherat</a>
82  * @author <a HREF="mailto:christophe.lombart@skynet.be">Christophe Lombart</a>
83
84  */

85 public class CmsContentStore extends AbstractService
86     implements ContentStore {
87     
88     
89     // -------------------------------------------------------------- Constants
90

91     
92     public static final int BUFFER_SIZE = 2048;
93     public static final String JavaDoc CHARACTER_ENCODING = "8859_1";
94     
95     
96     // ----------------------------------------------------- Instance Variables
97

98     
99     /**
100      * Path from which files are stored.
101      */

102     private String JavaDoc rootpath;
103     
104     
105     /**
106      * True if versioning.
107      */

108     private boolean version = true;
109     
110     /**
111      * True if content written from a prior server run should be removed.
112      * WARNING: setting this option to true will remove all files, located
113      * under the RootPath location!
114      */

115     private boolean removePriorContent = false;
116     
117     /**
118      * Default rootpath is the current directory
119      */

120     private final String JavaDoc DEFAULT_ROOTPATH=".";
121     
122     // ---------------------------------------------------- ServiceImpl Methods
123

124     
125     /**
126      * Read parameters.
127      *
128      * @param parameters Hashtable containing the parameters' name
129      * and associated value
130      */

131     public synchronized void setParameters(Hashtable JavaDoc parameters)
132         throws ServiceParameterErrorException,
133         ServiceParameterMissingException {
134         // A parameter will tell were serialization files are.
135
rootpath = JetspeedResources.getString("services.CmsService.slide.content.rootpath");
136         if (rootpath == null) {
137             // Default is that files are stored starting in the
138
// current directory
139
rootpath = DEFAULT_ROOTPATH;
140         }
141         String JavaDoc versionValue = (String JavaDoc) parameters.get("services.CmsService.slide.content.version");
142         if (versionValue != null) {
143             version = Boolean.valueOf(versionValue).booleanValue();
144         }
145         String JavaDoc removePriorContentValue = (String JavaDoc) parameters.get("services.CmsService.slide.content.resetBeforeStarting");
146         if (removePriorContentValue != null) {
147             removePriorContent = removePriorContentValue.equalsIgnoreCase("true");
148         }
149             
150     }
151     
152     
153     /**
154      * Connects to store.
155      *
156      * @exception VersionException
157      */

158     public synchronized void connect()
159         throws ServiceConnectionFailedException {
160     }
161     
162     
163     /**
164      * Disconnects from content store.
165      *
166      * @exception ServiceDisconnectionFailedException
167      */

168     public synchronized void disconnect()
169         throws ServiceDisconnectionFailedException {
170     }
171     
172     
173     /**
174      * Initializes content store.
175      *
176      * @exception ServiceInitializationFailedException Throws an exception
177      * if the store has already been initialized before
178      */

179     public synchronized void initialize(NamespaceAccessToken token)
180         throws ServiceInitializationFailedException {
181         try {
182             // remove prior content specified at rootPath
183
if ((removePriorContent) && (!DEFAULT_ROOTPATH.equals(rootpath)))
184                 reset();
185             File baseDir = new File(rootpath);
186             token.getLogger().log("FileContentStore rootpath: " + baseDir.getAbsolutePath() ,Logger.INFO);
187             baseDir.mkdirs();
188         } catch (Exception JavaDoc e) {
189             e.printStackTrace();
190             throw new ServiceInitializationFailedException(this, e);
191         }
192     }
193     
194     
195     /**
196      * Deletes content store.
197      *
198      * @exception ServiceResetFailedException
199      */

200     public void reset()
201         throws ServiceResetFailedException {
202         String JavaDoc[] filter = new String JavaDoc[1];
203         filter[0] = "*.*";
204         new slidestore.reference.fileDeleter.DeleterWalker(rootpath, filter );
205     }
206     
207     
208     /**
209      * This function tells whether or not the service is connected.
210      *
211      * @return boolean true if we are connected
212      * @exception ServiceAccessException Service access error
213      */

214     public boolean isConnected()
215         throws ServiceAccessException {
216         return true;
217     }
218     
219     
220     /**
221      * Does nothing here.
222      *
223      * @exception ServiceAccessException Unspecified service access error
224      */

225     public synchronized void commit()
226         throws ServiceAccessException {
227     }
228     
229     
230     // --------------------------------------------------- ContentStore Methods
231

232     
233     /**
234      * Retrive revision content.
235      *
236      * @param uri Uri
237      * @param revisionNumber Node revision number
238      */

239     public NodeRevisionContent retrieveRevisionContent
240         (Uri uri, NodeRevisionDescriptor revisionDescriptor)
241         throws ServiceAccessException, RevisionNotFoundException {
242         NodeRevisionContent result = null;
243         String JavaDoc revisionUri = null;
244         if (version)
245             revisionUri = uri.toString() + "_"
246                 + revisionDescriptor.getRevisionNumber();
247         else
248             revisionUri = uri.toString();
249         try {
250             File file = new File(rootpath + revisionUri);
251             FileInputStream is = new FileInputStream(file);
252             InputStreamReader reader = new InputStreamReader
253                 (is, CHARACTER_ENCODING);
254             result = new NodeRevisionContent();
255             result.setContent(reader);
256             result.setContent(is);
257         } catch (FileNotFoundException e) {
258             throw new RevisionNotFoundException
259                 (uri.toString(),
260                  revisionDescriptor.getRevisionNumber());
261         } catch (IOException e) {
262             e.printStackTrace();
263             throw new ServiceAccessException(this, e.getMessage());
264         }
265         return result;
266
267     }
268     
269     
270     /**
271      * Create a new revision
272      *
273      * @param uri Uri
274      * @param revisionDescriptor Node revision descriptor
275      * @param revisionContent Node revision content
276      */

277     public void createRevisionContent
278         (Uri uri, NodeRevisionDescriptor revisionDescriptor,
279          NodeRevisionContent revisionContent)
280         throws ServiceAccessException, RevisionAlreadyExistException {
281         String JavaDoc revisionUri = null;
282         if (version)
283             revisionUri = uri.toString() + "_"
284                 + revisionDescriptor.getRevisionNumber();
285         else
286             revisionUri = uri.toString();
287         
288         try {
289             File file = new File(rootpath + revisionUri);
290             File parentFile = new File(file.getParent());
291             if ((parentFile != null) && (!parentFile.exists())) {
292                 parentFile.mkdirs();
293             }
294             
295             boolean created = !file.exists();
296             if (!created) {
297                 throw new RevisionAlreadyExistException
298                     (uri.toString(), revisionDescriptor.getRevisionNumber());
299             }
300             
301             InputStream is = revisionContent.streamContent();
302             
303             if (is != null) {
304                 OutputStream os = new FileOutputStream(file);
305                 // We copy 8 ko with each read
306
byte[] buffer = new byte[BUFFER_SIZE];
307                 long position = 0;
308                 long contentLength = revisionDescriptor.getContentLength();
309                 
310                 while (true) {
311                     int nChar = is.read(buffer);
312                     if (nChar == -1) {
313                         break;
314                     }
315                     os.write(buffer, 0, nChar);
316                     position = position + nChar;
317                 }
318                 os.close();
319                 is.close();
320                 
321                 if (contentLength != -1) {
322                     if (position != contentLength) {
323             // set content length so that repository is consistent
324
revisionDescriptor.setContentLength(position);
325                         if (position < contentLength) {
326                             // Not enough bytes read !!!
327
throw new IOException("Not enough bytes read");
328                         }
329                         if (position > contentLength) {
330                             // Not enough bytes read !!!
331
throw new IOException("Too many bytes read");
332                         }
333                         // FIXME : Delete the file
334
}
335                 } else {
336                     revisionDescriptor.setContentLength(position);
337                 }
338                 
339             } else {
340             }
341             
342         } catch (IOException e) {
343             throw new ServiceAccessException(this, e.getMessage());
344         } catch(RevisionAlreadyExistException e) {
345             throw e; // we do NOT want this caught by next clause.
346
} catch (Exception JavaDoc e) {
347             e.printStackTrace();
348             throw new ServiceAccessException(this, e.getMessage());
349         }
350     }
351     
352     
353     /**
354      * Modify the latest revision of an object.
355      *
356      * @param uri Uri
357      * @param revisionDescriptor Node revision descriptor
358      * @param revisionContent Node revision content
359      */

360     public void storeRevisionContent
361         (Uri uri, NodeRevisionDescriptor revisionDescriptor,
362          NodeRevisionContent revisionContent)
363         throws ServiceAccessException, RevisionNotFoundException {
364         String JavaDoc revisionUri = null;
365         if (version)
366             revisionUri = uri.toString() + "_"
367                 + revisionDescriptor.getRevisionNumber();
368         else
369             revisionUri = uri.toString();
370         try {
371             File file = new File(rootpath + revisionUri);
372             
373             InputStream is = revisionContent.streamContent();
374             
375             if (is != null) {
376                 OutputStream os = null;
377                 try {
378                     os = new FileOutputStream(file);
379                 } catch (FileNotFoundException ex) {
380                     // Try to create the parent directory and try again
381
File parentFile = new File(file.getParent());
382                     if ((parentFile != null) && (!parentFile.exists())) {
383                         parentFile.mkdirs();
384                     }
385                     os = new FileOutputStream(file);
386                 }
387                 // We copy 8 ko with each read
388
byte[] buffer = new byte[BUFFER_SIZE];
389                 long position = 0;
390                 long contentLength = revisionDescriptor.getContentLength();
391                 
392                 while (true) {
393                     int nChar = is.read(buffer);
394                     if (nChar == -1) {
395                         break;
396                     }
397                     os.write(buffer, 0, nChar);
398                     position = position + nChar;
399                 }
400                 os.close();
401                 is.close();
402                 
403                 if (contentLength != -1) {
404                     if (position != contentLength) {
405                         if (position < contentLength) {
406                             // Not enough bytes read !!!
407
throw new IOException("Not enough bytes read");
408                         }
409                         if (position > contentLength) {
410                             // Not enough bytes read !!!
411
throw new IOException("Too many bytes read");
412                         }
413                         // FIXME : Delete the file
414
}
415                 } else {
416                     revisionDescriptor.setContentLength(position);
417                 }
418                 
419             } else {
420             }
421             
422         } catch (FileNotFoundException e) {
423             throw new RevisionNotFoundException
424                 (uri.toString(), revisionDescriptor.getRevisionNumber());
425         } catch (IOException e) {
426             e.printStackTrace();
427             throw new ServiceAccessException(this, e.getMessage());
428         }
429     }
430     
431     
432     /**
433      * Remove revision.
434      *
435      * @param uri Uri
436      * @param revisionNumber Node revision number
437      */

438     public void removeRevisionContent(Uri uri,
439                                       NodeRevisionDescriptor revisionDescriptor)
440         throws ServiceAccessException {
441         String JavaDoc revisionUri = null;
442         if (version)
443             revisionUri = uri.toString() + "_" + revisionDescriptor.getRevisionNumber();
444         else
445             revisionUri = uri.toString();
446         
447         try {
448             File file = new File(rootpath + revisionUri);
449             boolean deleted = file.delete();
450             File parentFile = new File(file.getParent());
451             if (parentFile != null) {
452                 parentFile.delete();
453             }
454         } catch (Exception JavaDoc e) {
455             e.printStackTrace();
456             throw new ServiceAccessException(this, e.getMessage());
457         }
458     }
459     
460 }
461
Popular Tags