1 17 package org.alfresco.repo.content.filestore; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.util.Date ; 22 import java.util.HashSet ; 23 import java.util.Set ; 24 25 import org.alfresco.error.AlfrescoRuntimeException; 26 import org.alfresco.repo.content.AbstractContentStore; 27 import org.alfresco.service.cmr.repository.ContentIOException; 28 import org.alfresco.service.cmr.repository.ContentReader; 29 import org.alfresco.service.cmr.repository.ContentWriter; 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 33 41 public class FileContentStore extends AbstractContentStore 42 { 43 private static final Log logger = LogFactory.getLog(FileContentStore.class); 44 45 private File rootDirectory; 46 private String rootAbsolutePath; 47 private boolean allowRandomAccess; 48 49 53 public FileContentStore(String rootDirectoryStr) 54 { 55 rootDirectory = new File (rootDirectoryStr); 56 if (!rootDirectory.exists()) 57 { 58 if (!rootDirectory.mkdirs()) 59 { 60 throw new ContentIOException("Failed to create store root: " + rootDirectory, null); 61 } 62 } 63 rootDirectory = rootDirectory.getAbsoluteFile(); 64 rootAbsolutePath = rootDirectory.getAbsolutePath(); 65 allowRandomAccess = true; 66 } 67 68 public String toString() 69 { 70 StringBuilder sb = new StringBuilder (36); 71 sb.append("FileContentStore") 72 .append("[ root=").append(rootDirectory) 73 .append("]"); 74 return sb.toString(); 75 } 76 77 88 public void setAllowRandomAccess(boolean allowRandomAccess) 89 { 90 this.allowRandomAccess = allowRandomAccess; 91 } 92 93 99 private File createNewFile() throws IOException 100 { 101 String contentUrl = createNewUrl(); 102 return createNewFile(contentUrl); 103 } 104 105 117 public File createNewFile(String newContentUrl) throws IOException 118 { 119 File file = makeFile(newContentUrl); 120 121 File dir = file.getParentFile(); 123 if (!dir.exists()) 124 { 125 dir.mkdirs(); 126 } 127 128 boolean created = file.createNewFile(); 130 if (!created) 131 { 132 throw new ContentIOException( 133 "When specifying a URL for new content, the URL may not be in use already. \n" + 134 " store: " + this + "\n" + 135 " new URL: " + newContentUrl); 136 } 137 138 return file; 140 } 141 142 150 private String makeContentUrl(File file) 151 { 152 String path = file.getAbsolutePath(); 153 if (!path.startsWith(rootAbsolutePath)) 155 { 156 throw new AlfrescoRuntimeException( 157 "File does not fall below the store's root: \n" + 158 " file: " + file + "\n" + 159 " store: " + this); 160 } 161 int index = rootAbsolutePath.length(); 163 if (path.charAt(index) == File.separatorChar) 164 { 165 index++; 166 } 167 String url = AbstractContentStore.STORE_PROTOCOL + path.substring(index); 169 url = url.replace('\\', '/'); 171 return url; 173 } 174 175 185 private File makeFile(String contentUrl) 186 { 187 String relativeUrl = getRelativePart(contentUrl); 189 File file = new File (rootDirectory, relativeUrl); 191 return file; 193 } 194 195 198 @Override 199 public boolean exists(String contentUrl) throws ContentIOException 200 { 201 File file = makeFile(contentUrl); 202 return file.exists(); 203 } 204 205 209 public ContentReader getReader(String contentUrl) 210 { 211 try 212 { 213 File file = makeFile(contentUrl); 214 FileContentReader reader = new FileContentReader(file, contentUrl); 215 reader.setAllowRandomAccess(allowRandomAccess); 216 217 if (logger.isDebugEnabled()) 219 { 220 logger.debug("Created content reader: \n" + 221 " url: " + contentUrl + "\n" + 222 " file: " + file + "\n" + 223 " reader: " + reader); 224 } 225 return reader; 226 } 227 catch (Throwable e) 228 { 229 throw new ContentIOException("Failed to get reader for URL: " + contentUrl, e); 230 } 231 } 232 233 236 public ContentWriter getWriter(ContentReader existingContentReader, String newContentUrl) 237 { 238 try 239 { 240 File file = null; 241 String contentUrl = null; 242 if (newContentUrl == null) { 244 file = createNewFile(); 246 contentUrl = makeContentUrl(file); 248 } 249 else { 251 file = createNewFile(newContentUrl); 252 contentUrl = newContentUrl; 253 } 254 FileContentWriter writer = new FileContentWriter(file, contentUrl, existingContentReader); 256 writer.setAllowRandomAccess(allowRandomAccess); 257 258 if (logger.isDebugEnabled()) 260 { 261 logger.debug("Created content writer: \n" + 262 " writer: " + writer); 263 } 264 return writer; 265 } 266 catch (IOException e) 267 { 268 throw new ContentIOException("Failed to get writer", e); 269 } 270 } 271 272 public Set <String > getUrls(Date createdAfter, Date createdBefore) 273 { 274 Set <String > contentUrls = new HashSet <String >(1000); 276 getUrls(rootDirectory, contentUrls, createdAfter, createdBefore); 277 if (logger.isDebugEnabled()) 279 { 280 logger.debug("Listed all content URLS: \n" + 281 " store: " + this + "\n" + 282 " count: " + contentUrls.size()); 283 } 284 return contentUrls; 285 } 286 287 294 private void getUrls(File directory, Set <String > contentUrls, Date createdAfter, Date createdBefore) 295 { 296 File [] files = directory.listFiles(); 297 if (files == null) 298 { 299 throw new ContentIOException("Failed list files in folder: " + directory); 301 } 302 for (File file : files) 303 { 304 if (file.isDirectory()) 305 { 306 getUrls(file, contentUrls, createdAfter, createdBefore); 308 } 309 else 310 { 311 long lastModified = file.lastModified(); 313 if (createdAfter != null && lastModified < createdAfter.getTime()) 314 { 315 continue; 317 } 318 else if (createdBefore != null && lastModified > createdBefore.getTime()) 319 { 320 continue; 322 } 323 String contentUrl = makeContentUrl(file); 325 contentUrls.add(contentUrl); 326 } 327 } 328 } 329 330 334 public boolean delete(String contentUrl) throws ContentIOException 335 { 336 File file = makeFile(contentUrl); 338 boolean deleted = false; 339 if (!file.exists()) 340 { 341 deleted = true; 342 } 343 else 344 { 345 deleted = file.delete(); 346 } 347 348 if (logger.isDebugEnabled()) 350 { 351 logger.debug("Delete content directly: \n" + 352 " store: " + this + "\n" + 353 " url: " + contentUrl); 354 } 355 return deleted; 356 } 357 } 358 | Popular Tags |