KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > files > JahiaTextFileBaseService


1 /*
2  * ____.
3  * __/\ ______| |__/\. _______
4  * __ .____| | \ | +----+ \
5  * _______| /--| | | - \ _ | : - \_________
6  * \\______: :---| : : | : | \________>
7  * |__\---\_____________:______: :____|____:_____\
8  * /_____|
9  *
10  * . . . i n j a h i a w e t r u s t . . .
11  *
12  *
13  *
14  * ----- BEGIN LICENSE BLOCK -----
15  * Version: JCSL 1.0
16  *
17  * The contents of this file are subject to the Jahia Community Source License
18  * 1.0 or later (the "License"); you may not use this file except in
19  * compliance with the License. You may obtain a copy of the License at
20  * http://www.jahia.org/license
21  *
22  * Software distributed under the License is distributed on an "AS IS" basis,
23  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
24  * for the rights, obligations and limitations governing use of the contents
25  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
26  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
27  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
28  *
29  * The Shared Modifications are Jahia View Helper.
30  *
31  * The Developer of the Shared Modifications is Jahia Solution S�rl.
32  * Portions created by the Initial Developer are Copyright (C) 2002 by the
33  * Initial Developer. All Rights Reserved.
34  *
35  * Contributor(s):
36  * 18-NOV-2000, Xo3 SA, Eric Vassalli : Initial version
37  * 06-JAN-2001, Xo3 SA, David Jilly
38  * 09-AUG-2003, Jahia Solutions Sarl, Fulco Houkes
39  *
40  * ----- END LICENSE BLOCK -----
41  */

42
43 package org.jahia.services.files;
44
45
46 import java.io.File JavaDoc;
47 import java.io.FileInputStream JavaDoc;
48 import java.io.FileOutputStream JavaDoc;
49 import java.io.IOException JavaDoc;
50
51 import org.apache.log4j.Logger;
52 import org.jahia.exceptions.JahiaException;
53 import org.jahia.exceptions.JahiaInitializationException;
54 import org.jahia.services.cache.Cache;
55 import org.jahia.services.cache.CacheFactory;
56 import org.jahia.services.usermanager.JahiaUser;
57 import org.jahia.settings.SettingsBean;
58 import org.jahia.utils.FileUtils;
59 import org.jahia.utils.JahiaTools;
60
61
62 public class JahiaTextFileBaseService extends JahiaTextFileService {
63
64     /** logging */
65     private static Logger logger = Logger.getLogger (JahiaTextFileBaseService.class);
66
67     /** the service name */
68     private static String JavaDoc serviceName = "JahiaTextFileService";
69
70     /** the disk path where to store the data */
71     private static String JavaDoc jahiaDataDiskPath = "";
72
73     /** the service unique instance */
74     private static JahiaTextFileBaseService instance;
75
76     // the Text File cache name.
77
public static final String JavaDoc TEXT_FILE_CACHE = "TextFileCache";
78     /** the service cache */
79     private static Cache cacheText; // filename -> content
80

81
82     /** Default constructor, creates a new <code>JahiaTextFileBaseService</code> instance.
83      */

84     protected JahiaTextFileBaseService () {
85         logger.debug ("***** Starting " + serviceName + " *****");
86     }
87
88
89     /**
90      * Return the unique instance of this class.
91      *
92      * @return the unique instance of this class.
93      */

94     public static synchronized JahiaTextFileBaseService getInstance () {
95         if (instance == null) {
96             instance = new JahiaTextFileBaseService ();
97         }
98         return instance;
99     }
100
101
102     /** Initializes the service with the settings passed as argument.
103      *
104      * @param jSettings the service initialization settings
105      */

106     public void init (SettingsBean jSettings)
107             throws JahiaInitializationException
108     {
109         JahiaTextFileBaseService.jahiaDataDiskPath = jSettings.getJahiaFilesBigTextDiskPath();
110         File JavaDoc filePath = new File JavaDoc (JahiaTextFileBaseService.jahiaDataDiskPath);
111         if (!filePath.exists ()) {
112             logger.debug ("Creating " + JahiaTextFileBaseService.jahiaDataDiskPath + " path ");
113             filePath.mkdirs ();
114         }
115
116         /**
117          * Warning we only use the file name part here because we had problems
118          * with cache synchronization in clusters that didn't use a 100% similar
119          * "slashing" scheme and this was causing sync problems.
120          */

121         cacheText = CacheFactory.createCache(TEXT_FILE_CACHE);
122     }
123
124     public synchronized void shutdown ()
125         throws JahiaException
126     {
127         super.shutdown();
128
129         // flush the cache
130
cacheText.flush();
131     }
132
133
134     /**
135      * Load a big text file content and set its value.
136      *
137      * @param jahiaID The site ID referenced by the big text.
138      * @param pageID The page ID referenced by the big text.
139      * @param fieldID The big text field ID.
140      * @param fieldValue The default big text value if file does not exists.
141      * @param versionID The big text version ID.
142      * @param workflowState The big text workflow state.
143      * @param languageCode The big text language code.
144      * @return
145      * The big text value (aka file content) as a String.
146      * @throws JahiaException
147      * when a general exception occured.
148      */

149     public String JavaDoc loadBigTextValue (int jahiaID, int pageID, int fieldID, String JavaDoc fieldValue,
150                                     int versionID, int workflowState, String JavaDoc languageCode)
151             throws JahiaException
152     {
153         String JavaDoc fileName = FileUtils.getInstance().composeBigTextFileNamePart(jahiaID, pageID, fieldID, versionID, workflowState, languageCode);
154         String JavaDoc fullPath = FileUtils.getInstance ().composeBigTextFullPathName (jahiaDataDiskPath, fileName);
155         String JavaDoc result = (String JavaDoc)cacheText.get (fileName);
156         if (result == null) {
157             if (FileUtils.getInstance ().fileExists (fullPath)) {
158                 result = FileUtils.getInstance ().readFile (fullPath, languageCode);
159             } else {
160                 result = fieldValue;
161             }
162             if (!cacheText.containsKey(fileName)){
163                 cacheText.put (fileName, result);
164             }
165         }
166         logger.debug ("File : " + fullPath + ", value : " + result);
167
168         return result;
169     }
170
171     /**
172      * saveContents
173      * EV 18.11.2000
174      * EV 18.11.2000 returns the file name
175      *
176      */

177     public String JavaDoc saveContents (int jahiaID, int pageID, int fieldID, String JavaDoc fieldValue,
178                                 int versionID, int versionStatus, String JavaDoc languageCode)
179             throws JahiaException
180     {
181         String JavaDoc fileName = FileUtils.getInstance().composeBigTextFileNamePart(jahiaID, pageID, fieldID, versionID, versionStatus, languageCode);
182         String JavaDoc fullPath = FileUtils.getInstance ().composeBigTextFullPathName (
183                 jahiaDataDiskPath, fileName);
184
185         logger.debug ("File : " + fullPath + ", value : " + fieldValue);
186         FileUtils.getInstance ().writeFile (fullPath, fieldValue, languageCode);
187         cacheText.put (fileName, fieldValue);
188         return fileName;
189     }
190
191
192     /**
193      * getFileName
194      * YG 29.08.2001
195      * returns the file name
196      *
197      */

198     public String JavaDoc getFileName (int jahiaID, int pageID, int fieldID, int versionID,
199                                int versionStatus, String JavaDoc languageCode)
200             throws JahiaException
201     {
202         return FileUtils.getInstance ().composeBigTextFullPathName (
203                 jahiaDataDiskPath, jahiaID, pageID, fieldID, versionID,
204                 versionStatus, languageCode);
205     }
206
207
208     /**
209      * set the service name
210      *
211      * @param name the service name to be set
212      */

213     public void setName (String JavaDoc name) {
214         serviceName = name;
215     }
216
217     /**
218      * renames a file
219      * first half of the parameters are describing the old file, other half the new one.
220      * @return true if it worked
221      */

222     public boolean renameFile (int jahiaID, int pageID, int fieldID, int versionID, int versionStatus, String JavaDoc languageCode,
223                                int njahiaID, int npageID, int nfieldID, int nversionID, int nversionStatus, String JavaDoc nlanguageCode) throws Exception JavaDoc {
224
225         String JavaDoc oldFileName = FileUtils.getInstance ().composeBigTextFileNamePart (jahiaID, pageID, fieldID, versionID, versionStatus, languageCode);
226         String JavaDoc newFileName = FileUtils.getInstance ().composeBigTextFileNamePart (njahiaID, npageID, nfieldID, nversionID, nversionStatus, nlanguageCode);
227         String JavaDoc oldFullPath = FileUtils.getInstance ().composeBigTextFullPathName (jahiaDataDiskPath, oldFileName);
228         String JavaDoc newFullPath = FileUtils.getInstance ().composeBigTextFullPathName (jahiaDataDiskPath, newFileName);
229
230         logger.debug (" from File : " + oldFullPath + ", to File : " + newFullPath);
231         boolean result = true;
232         if (oldFullPath != null && newFullPath != null
233                 && !oldFullPath.equals (newFullPath)) {
234             // First delete the new file if exists
235
FileUtils.getInstance ().deleteFile (newFullPath);
236             result = FileUtils.getInstance ().renameFile (oldFullPath, newFullPath);
237             if (result) {
238                 // let's synchronize cache
239
cacheText.remove (oldFileName);
240                 cacheText.remove (newFileName);
241             }
242         }
243
244         return result;
245     }
246
247     /**
248      * copy a file
249      * first half of the parameters are describing the old file, other half the new one.
250      * @return true if it worked
251      */

252     public boolean copyFile (int jahiaID, int pageID, int fieldID, int versionID, int versionStatus, String JavaDoc languageCode,
253                              int njahiaID, int npageID, int nfieldID, int nversionID, int nversionStatus, String JavaDoc nlanguageCode) {
254         String JavaDoc oldFileName = FileUtils.getInstance ().composeBigTextFileNamePart (jahiaID, pageID, fieldID, versionID, versionStatus, languageCode);
255         String JavaDoc newFileName = FileUtils.getInstance ().composeBigTextFileNamePart (njahiaID, npageID, nfieldID, nversionID, nversionStatus, nlanguageCode);
256         String JavaDoc oldFullPath = FileUtils.getInstance ().composeBigTextFullPathName (jahiaDataDiskPath, oldFileName);
257         String JavaDoc newFullPath = FileUtils.getInstance ().composeBigTextFullPathName (jahiaDataDiskPath, newFileName);
258         logger.debug (" from File : " + oldFullPath + ", to File : " + newFullPath);
259
260         // test if the oldFileName exist or not, if not, create an empty file
261
if (versionStatus < 1) {
262             // does the archive file exist ? Create an empty one else.
263
File JavaDoc f = new File JavaDoc (oldFullPath);
264             if (!f.exists ()) {
265                 try {
266                     f.createNewFile ();
267                 } catch (Throwable JavaDoc t) {
268                     logger.debug (" the old file doesn't exist and an exception occured when trying to create an empty one " + oldFullPath + ", to File : ", t);
269                 }
270             }
271         }
272         boolean result = FileUtils.getInstance ().copyFile (oldFullPath, newFullPath);
273         if (result) {
274             // let's synchronize cache
275
cacheText.remove (newFileName);
276         }
277         return result;
278     }
279
280     /**
281      * delete a file
282      * @return true if it worked
283      */

284     public boolean deleteFile (int jahiaID, int pageID, int fieldID, int versionID, int versionStatus, String JavaDoc languageCode) {
285         String JavaDoc fileName = FileUtils.getInstance ().composeBigTextFileNamePart (jahiaID, pageID, fieldID, versionID, versionStatus, languageCode);
286         String JavaDoc fullPath = FileUtils.getInstance ().composeBigTextFullPathName (jahiaDataDiskPath, fileName);
287
288         logger.debug (" File : " + fullPath);
289
290         boolean result = FileUtils.getInstance ().deleteFile (fullPath);
291         if (result) {
292             cacheText.remove(fileName);
293         }
294         return result;
295     }
296
297
298 ///////////////////////////////// CacheIO objects ///////////////////////////////////////
299

300     // NK
301
/**
302      * Copy all big text of a site in a gived folder
303      * This folder must be a valid folder
304      *
305      * @param siteID the id of the requested site
306      * @param destFolder the destination Folder
307      * @return the number of duplicated files, -1 on error
308      */

309     public int copySiteBigText (int siteID, String JavaDoc destFolder)
310             throws IOException JavaDoc {
311
312         File JavaDoc f = new File JavaDoc (destFolder);
313         if (!f.isDirectory () || !f.canWrite ()) {
314             return -1;
315         }
316
317         String JavaDoc destFolderPath = destFolder + File.separator;
318
319         f = null;
320         f = new File JavaDoc (jahiaDataDiskPath);
321         if (!f.isDirectory () || !f.canRead ()) {
322             return -1;
323         }
324
325         File JavaDoc[] files = f.listFiles ();
326         if (files.length == 0) {
327             return 0;
328         }
329
330         String JavaDoc siteLabel = siteID + "-";
331         int nb = files.length;
332         File JavaDoc destFile = null;
333         int nbCopy = 0;
334         for (int i = 0; i < nb; i++) {
335             if (files[i].getName ().startsWith (siteLabel)) {
336                 destFile = new File JavaDoc (destFolderPath + files[i].getName ());
337                 try {
338                     FileInputStream JavaDoc fileInput = new FileInputStream JavaDoc (files[i]);
339                     FileOutputStream JavaDoc fileOutput = new FileOutputStream JavaDoc (destFile);
340                     JahiaTools.copyStream (fileInput, fileOutput);
341                     fileInput = null;
342                     fileOutput = null;
343                     nbCopy += 1;
344                 } catch (java.io.FileNotFoundException JavaDoc fnfe) {
345                     //
346
}
347             }
348         }
349         return nbCopy;
350     }
351
352
353     // NK
354
/**
355      * Delete all big text files of a site
356      *
357      * @param siteID the id of the requested site
358      * @param user the user must be a root user
359      * @return false on error
360      */

361     public boolean deleteSiteBigText (int siteID, JahiaUser user)
362             throws IOException JavaDoc {
363
364         if (!user.isAdminMember (0)) {
365             return false;
366         }
367
368         File JavaDoc f = new File JavaDoc (jahiaDataDiskPath);
369         if (!f.isDirectory () || !f.canWrite ()) {
370             return false;
371         }
372
373         File JavaDoc[] files = f.listFiles ();
374
375         String JavaDoc siteLabel = siteID + "-";
376         int nb = files.length;
377         for (int i = 0; i < nb; i++) {
378             if (files[i].getName ().startsWith (siteLabel)) {
379                 files[i].delete ();
380             }
381         }
382         return true;
383     }
384
385 }
386
Popular Tags