1 4 package org.openedit.repository.filesystem; 5 6 import java.io.File ; 7 import java.io.InputStream ; 8 import java.util.ArrayList ; 9 import java.util.List ; 10 11 import org.apache.commons.logging.Log; 12 import org.apache.commons.logging.LogFactory; 13 import org.openedit.repository.ContentItem; 14 import org.openedit.repository.Repository; 15 import org.openedit.repository.RepositoryException; 16 17 import com.openedit.util.FileUtils; 18 19 23 public class FileRepository implements Repository 24 { 25 private static final Log log = LogFactory.getLog(FileRepository.class); 26 protected File fieldRootDirectory; 27 protected FileUtils fieldFileUtils; 28 29 32 public FileRepository() 33 { 34 } 35 public FileRepository(File inRoot) 36 { 37 setRootDirectory(inRoot); 38 } 39 public void setRootDirectory(File inRoot) 40 { 41 fieldRootDirectory = inRoot; 42 } 43 46 public ContentItem get(String inPath) throws RepositoryException 47 { 48 if ( log.isDebugEnabled() ) 49 { 50 log.debug("reading:" + inPath); 51 } 52 return createContentItem(inPath); 53 } 54 public ContentItem getStub(String inPath) throws RepositoryException 55 { 56 if ( log.isDebugEnabled() ) 57 { 58 log.debug("reading:" + inPath); 59 } 60 return createContentItem(inPath); 61 } 62 63 66 public void put(ContentItem inContent) throws RepositoryException 67 { 68 if ( log.isDebugEnabled() ) 69 { 70 log.debug("saving:" + inContent.getPath()); 71 } 72 73 String path = inContent.getPath(); 74 File file = getFile( path ); 75 if (file.isDirectory()) 76 { 77 throw new RepositoryException( "Error attempting to write content to a " 78 + "folder instead of a file for path " + path ); 79 } 80 if (path.endsWith("/") && !file.exists()) 81 { 82 file.mkdirs(); 83 } 84 else 85 { 86 writeContent( inContent ); 87 } 88 } 89 90 protected File getFile( ContentItem inPath ) 91 { 92 if( inPath instanceof FileItem) 93 { 94 FileItem f = (FileItem)inPath; 95 return f.getFile(); 96 } 97 return new File ( getRootDirectory(), inPath.getPath() ); 98 } 99 100 103 public void copy(ContentItem inSource, ContentItem inDestination) throws RepositoryException 104 { 105 File sourceFile = getFile( inSource.getPath() ); 106 File destination = null; 107 108 if (sourceFile.isDirectory()) 109 { 110 destination = getFile( inDestination.getPath() ); 111 destination.mkdirs(); 112 } 113 else 114 { 115 119 destination = getFile( inDestination.getPath() ); 120 destination.getParentFile().mkdirs(); 121 } 122 123 copyFiles( sourceFile, destination ); 124 125 } 126 127 public void move( ContentItem inSource, ContentItem inDestination ) throws RepositoryException 128 { 129 File todelete = getFile( inSource.getPath() ); 130 File toMove = getFile( inDestination.getPath() ); 131 moveFiles(todelete, toMove); 132 } 133 134 137 public void remove(ContentItem inContentItem) throws RepositoryException 138 { 139 File todelete = getFile( inContentItem.getPath() ); 140 141 deleteAll(todelete); 142 } 143 144 147 public List getVersions(String inPath) throws RepositoryException 148 { 149 List list = new ArrayList (1); 150 list.add(createContentItem(inPath)); 151 return list; 152 } 153 154 protected ContentItem createContentItem( String inPath ) 155 { 156 FileItem contentItem = new FileItem(); 157 contentItem.setPath( inPath ); 158 contentItem.setFile( getFile( inPath ) ); 159 return contentItem; 160 } 161 protected void deleteAll(File todelete) 162 { 163 getFileUtils().deleteAll( todelete ); 164 } 165 166 public FileUtils getFileUtils() 167 { 168 if (fieldFileUtils == null) 169 { 170 fieldFileUtils = new FileUtils(); 171 } 172 return fieldFileUtils; 173 } 174 175 protected File getFile( String inPath ) 176 { 177 return new File ( getRootDirectory(), inPath ); 178 } 179 180 public File getRootDirectory() 181 { 182 return fieldRootDirectory; 183 } 184 185 protected void copyFiles( File source, File destination ) throws RepositoryException 186 { 187 try 188 { 189 getFileUtils().copyFiles( source, destination ); 190 } 191 catch( Exception e ) 192 { 193 throw new RepositoryException( e ); 194 } 195 } 196 protected void moveFiles( File source, File destination ) throws RepositoryException 197 { 198 try 199 { 200 if ( !source.renameTo(destination)) 201 { 202 getFileUtils().copyFiles( source, destination ); 203 getFileUtils().deleteAll(source); 204 } 205 } 206 catch( Exception e ) 207 { 208 throw new RepositoryException( e ); 209 } 210 } 211 212 protected void writeContent(ContentItem inContentItem) throws RepositoryException 213 { 214 if ( inContentItem instanceof FileItem ) 215 { 216 return; } 218 File file = getFile( inContentItem.getPath() ); 219 InputStream in = inContentItem.getInputStream(); 220 if ( in == null) 221 { 222 throw new RepositoryException("Content item did not have an input stream " + inContentItem.getPath()); 223 } 224 try 225 { 226 getFileUtils().writeFile( in, file ); 227 } 228 catch( Exception e ) 229 { 230 throw new RepositoryException( 231 "Error writing content for path " + inContentItem.getPath(), e ); 232 } 233 } 234 public boolean doesExist(String inPath) throws RepositoryException 235 { 236 File file = getFile( inPath ); 237 238 return file.exists(); 239 } 240 public ContentItem getLastVersion(String inPath) throws RepositoryException 241 { 242 return createContentItem(inPath); 243 } 244 public List getChildrenNames(String inParent) throws RepositoryException 245 { 246 if( inParent.endsWith("/")) 247 { 248 inParent = inParent.substring(0,inParent.length() - 1); 249 } 250 List children = new ArrayList (); 251 File file = getFile( inParent ); 252 File [] all = file.listFiles(); 253 if( all != null) 254 { 255 for (int i = 0; i < all.length; i++) 256 { 257 String name = inParent + "/" + all[i].getName(); 258 children.add(name); 259 } 260 } 261 return children; 262 } 263 public void deleteOldVersions(String inPath) throws RepositoryException 264 { 265 266 267 } 268 269 270 } 271 | Popular Tags |