KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openedit > repository > filesystem > FileRepository


1 /*
2  * Created on Jan 12, 2005
3  */

4 package org.openedit.repository.filesystem;
5
6 import java.io.File JavaDoc;
7 import java.io.InputStream JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.List JavaDoc;
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 /**
20  * @author cburkey
21  *
22  */

23 public class FileRepository implements Repository
24 {
25     private static final Log log = LogFactory.getLog(FileRepository.class);
26     protected File JavaDoc fieldRootDirectory;
27     protected FileUtils fieldFileUtils;
28
29     /**
30      *
31      */

32     public FileRepository()
33     {
34     }
35     public FileRepository(File JavaDoc inRoot)
36     {
37         setRootDirectory(inRoot);
38     }
39     public void setRootDirectory(File JavaDoc inRoot)
40     {
41         fieldRootDirectory = inRoot;
42     }
43     /* (non-javadoc)
44      * @see com.einnovation.repository.Repository#get(java.lang.String)
45      */

46     public ContentItem get(String JavaDoc 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 JavaDoc inPath) throws RepositoryException
55     {
56         if ( log.isDebugEnabled() )
57         {
58             log.debug("reading:" + inPath);
59         }
60         return createContentItem(inPath);
61     }
62
63     /* (non-javadoc)
64      * @see com.einnovation.repository.Repository#put(com.einnovation.repository.ContentItem)
65      */

66     public void put(ContentItem inContent) throws RepositoryException
67     {
68         if ( log.isDebugEnabled() )
69         {
70             log.debug("saving:" + inContent.getPath());
71         }
72
73         String JavaDoc path = inContent.getPath();
74         File JavaDoc 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 JavaDoc getFile( ContentItem inPath )
91     {
92         if( inPath instanceof FileItem)
93         {
94             FileItem f = (FileItem)inPath;
95             return f.getFile();
96         }
97         return new File JavaDoc( getRootDirectory(), inPath.getPath() );
98     }
99
100     /* (non-javadoc)
101      * @see com.einnovation.repository.Repository#copy(com.einnovation.repository.ContentItem, com.einnovation.repository.ContentItem)
102      */

103     public void copy(ContentItem inSource, ContentItem inDestination) throws RepositoryException
104     {
105         File JavaDoc sourceFile = getFile( inSource.getPath() );
106         File JavaDoc destination = null;
107
108         if (sourceFile.isDirectory())
109         {
110             destination = getFile( inDestination.getPath() );
111             destination.mkdirs();
112         }
113         else
114         {
115             //why was I doing this? delete it
116
//String destContentPath = PathUtilities.extractPagePath( inDestination.getPath() ) + "."
117
// + PathUtilities.extractPageType( inSource.getPath() );
118

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 JavaDoc todelete = getFile( inSource.getPath() );
130         File JavaDoc toMove = getFile( inDestination.getPath() );
131         moveFiles(todelete, toMove);
132     }
133
134     /* (non-javadoc)
135      * @see com.einnovation.repository.Repository#remove(com.einnovation.repository.ContentItem)
136      */

137     public void remove(ContentItem inContentItem) throws RepositoryException
138     {
139         File JavaDoc todelete = getFile( inContentItem.getPath() );
140
141         deleteAll(todelete);
142     }
143
144     /* (non-javadoc)
145      * @see com.einnovation.repository.Repository#getContentItems(java.lang.String)
146      */

147     public List JavaDoc getVersions(String JavaDoc inPath) throws RepositoryException
148     {
149         List JavaDoc list = new ArrayList JavaDoc(1);
150         list.add(createContentItem(inPath));
151         return list;
152     }
153     
154     protected ContentItem createContentItem( String JavaDoc 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 JavaDoc 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 JavaDoc getFile( String JavaDoc inPath )
176     {
177         return new File JavaDoc( getRootDirectory(), inPath );
178     }
179
180     public File JavaDoc getRootDirectory()
181     {
182         return fieldRootDirectory;
183     }
184
185     protected void copyFiles( File JavaDoc source, File JavaDoc destination ) throws RepositoryException
186     {
187         try
188         {
189             getFileUtils().copyFiles( source, destination );
190         }
191         catch( Exception JavaDoc e )
192         {
193             throw new RepositoryException( e );
194         }
195     }
196     protected void moveFiles( File JavaDoc source, File JavaDoc 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 JavaDoc 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; //else we would end up saving it onto itself
217
}
218         File JavaDoc file = getFile( inContentItem.getPath() );
219         InputStream JavaDoc 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 JavaDoc e )
229         {
230             throw new RepositoryException(
231                     "Error writing content for path " + inContentItem.getPath(), e );
232         }
233     }
234     public boolean doesExist(String JavaDoc inPath) throws RepositoryException
235     {
236         File JavaDoc file = getFile( inPath );
237
238         return file.exists();
239     }
240     public ContentItem getLastVersion(String JavaDoc inPath) throws RepositoryException
241     {
242         return createContentItem(inPath);
243     }
244     public List JavaDoc getChildrenNames(String JavaDoc inParent) throws RepositoryException
245     {
246         if( inParent.endsWith("/"))
247         {
248             inParent = inParent.substring(0,inParent.length() - 1);
249         }
250         List JavaDoc children = new ArrayList JavaDoc();
251         File JavaDoc file = getFile( inParent );
252         File JavaDoc[] all = file.listFiles();
253         if( all != null)
254         {
255             for (int i = 0; i < all.length; i++)
256             {
257                 String JavaDoc name = inParent + "/" + all[i].getName();
258                 children.add(name);
259             }
260         }
261         return children;
262     }
263     public void deleteOldVersions(String JavaDoc inPath) throws RepositoryException
264     {
265     
266     
267     }
268     
269     
270 }
271
Popular Tags