1 42 43 package org.jahia.services.files; 44 45 46 import java.io.File ; 47 import java.io.FileInputStream ; 48 import java.io.FileOutputStream ; 49 import java.io.IOException ; 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 65 private static Logger logger = Logger.getLogger (JahiaTextFileBaseService.class); 66 67 68 private static String serviceName = "JahiaTextFileService"; 69 70 71 private static String jahiaDataDiskPath = ""; 72 73 74 private static JahiaTextFileBaseService instance; 75 76 public static final String TEXT_FILE_CACHE = "TextFileCache"; 78 79 private static Cache cacheText; 81 82 84 protected JahiaTextFileBaseService () { 85 logger.debug ("***** Starting " + serviceName + " *****"); 86 } 87 88 89 94 public static synchronized JahiaTextFileBaseService getInstance () { 95 if (instance == null) { 96 instance = new JahiaTextFileBaseService (); 97 } 98 return instance; 99 } 100 101 102 106 public void init (SettingsBean jSettings) 107 throws JahiaInitializationException 108 { 109 JahiaTextFileBaseService.jahiaDataDiskPath = jSettings.getJahiaFilesBigTextDiskPath(); 110 File filePath = new File (JahiaTextFileBaseService.jahiaDataDiskPath); 111 if (!filePath.exists ()) { 112 logger.debug ("Creating " + JahiaTextFileBaseService.jahiaDataDiskPath + " path "); 113 filePath.mkdirs (); 114 } 115 116 121 cacheText = CacheFactory.createCache(TEXT_FILE_CACHE); 122 } 123 124 public synchronized void shutdown () 125 throws JahiaException 126 { 127 super.shutdown(); 128 129 cacheText.flush(); 131 } 132 133 134 149 public String loadBigTextValue (int jahiaID, int pageID, int fieldID, String fieldValue, 150 int versionID, int workflowState, String languageCode) 151 throws JahiaException 152 { 153 String fileName = FileUtils.getInstance().composeBigTextFileNamePart(jahiaID, pageID, fieldID, versionID, workflowState, languageCode); 154 String fullPath = FileUtils.getInstance ().composeBigTextFullPathName (jahiaDataDiskPath, fileName); 155 String result = (String )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 177 public String saveContents (int jahiaID, int pageID, int fieldID, String fieldValue, 178 int versionID, int versionStatus, String languageCode) 179 throws JahiaException 180 { 181 String fileName = FileUtils.getInstance().composeBigTextFileNamePart(jahiaID, pageID, fieldID, versionID, versionStatus, languageCode); 182 String 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 198 public String getFileName (int jahiaID, int pageID, int fieldID, int versionID, 199 int versionStatus, String languageCode) 200 throws JahiaException 201 { 202 return FileUtils.getInstance ().composeBigTextFullPathName ( 203 jahiaDataDiskPath, jahiaID, pageID, fieldID, versionID, 204 versionStatus, languageCode); 205 } 206 207 208 213 public void setName (String name) { 214 serviceName = name; 215 } 216 217 222 public boolean renameFile (int jahiaID, int pageID, int fieldID, int versionID, int versionStatus, String languageCode, 223 int njahiaID, int npageID, int nfieldID, int nversionID, int nversionStatus, String nlanguageCode) throws Exception { 224 225 String oldFileName = FileUtils.getInstance ().composeBigTextFileNamePart (jahiaID, pageID, fieldID, versionID, versionStatus, languageCode); 226 String newFileName = FileUtils.getInstance ().composeBigTextFileNamePart (njahiaID, npageID, nfieldID, nversionID, nversionStatus, nlanguageCode); 227 String oldFullPath = FileUtils.getInstance ().composeBigTextFullPathName (jahiaDataDiskPath, oldFileName); 228 String 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 FileUtils.getInstance ().deleteFile (newFullPath); 236 result = FileUtils.getInstance ().renameFile (oldFullPath, newFullPath); 237 if (result) { 238 cacheText.remove (oldFileName); 240 cacheText.remove (newFileName); 241 } 242 } 243 244 return result; 245 } 246 247 252 public boolean copyFile (int jahiaID, int pageID, int fieldID, int versionID, int versionStatus, String languageCode, 253 int njahiaID, int npageID, int nfieldID, int nversionID, int nversionStatus, String nlanguageCode) { 254 String oldFileName = FileUtils.getInstance ().composeBigTextFileNamePart (jahiaID, pageID, fieldID, versionID, versionStatus, languageCode); 255 String newFileName = FileUtils.getInstance ().composeBigTextFileNamePart (njahiaID, npageID, nfieldID, nversionID, nversionStatus, nlanguageCode); 256 String oldFullPath = FileUtils.getInstance ().composeBigTextFullPathName (jahiaDataDiskPath, oldFileName); 257 String newFullPath = FileUtils.getInstance ().composeBigTextFullPathName (jahiaDataDiskPath, newFileName); 258 logger.debug (" from File : " + oldFullPath + ", to File : " + newFullPath); 259 260 if (versionStatus < 1) { 262 File f = new File (oldFullPath); 264 if (!f.exists ()) { 265 try { 266 f.createNewFile (); 267 } catch (Throwable 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 cacheText.remove (newFileName); 276 } 277 return result; 278 } 279 280 284 public boolean deleteFile (int jahiaID, int pageID, int fieldID, int versionID, int versionStatus, String languageCode) { 285 String fileName = FileUtils.getInstance ().composeBigTextFileNamePart (jahiaID, pageID, fieldID, versionID, versionStatus, languageCode); 286 String 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 300 309 public int copySiteBigText (int siteID, String destFolder) 310 throws IOException { 311 312 File f = new File (destFolder); 313 if (!f.isDirectory () || !f.canWrite ()) { 314 return -1; 315 } 316 317 String destFolderPath = destFolder + File.separator; 318 319 f = null; 320 f = new File (jahiaDataDiskPath); 321 if (!f.isDirectory () || !f.canRead ()) { 322 return -1; 323 } 324 325 File [] files = f.listFiles (); 326 if (files.length == 0) { 327 return 0; 328 } 329 330 String siteLabel = siteID + "-"; 331 int nb = files.length; 332 File 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 (destFolderPath + files[i].getName ()); 337 try { 338 FileInputStream fileInput = new FileInputStream (files[i]); 339 FileOutputStream fileOutput = new FileOutputStream (destFile); 340 JahiaTools.copyStream (fileInput, fileOutput); 341 fileInput = null; 342 fileOutput = null; 343 nbCopy += 1; 344 } catch (java.io.FileNotFoundException fnfe) { 345 } 347 } 348 } 349 return nbCopy; 350 } 351 352 353 361 public boolean deleteSiteBigText (int siteID, JahiaUser user) 362 throws IOException { 363 364 if (!user.isAdminMember (0)) { 365 return false; 366 } 367 368 File f = new File (jahiaDataDiskPath); 369 if (!f.isDirectory () || !f.canWrite ()) { 370 return false; 371 } 372 373 File [] files = f.listFiles (); 374 375 String 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 |