View Javadoc

1   package net.sourceforge.blogentis.storage;
2   
3   //-----------------------------------------------------------------------
4   //Blogentis - a blog publishing platform.
5   //Copyright (C) 2004 Tassos Bassoukos <abassouk@gmail.com>
6   //
7   //This library is free software; you can redistribute it and/or
8   //modify it under the terms of the GNU Lesser General Public
9   //License as published by the Free Software Foundation; either
10  //version 2.1 of the License, or (at your option) any later version.
11  //
12  //This library is distributed in the hope that it will be useful,
13  //but WITHOUT ANY WARRANTY; without even the implied warranty of
14  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  //Lesser General Public License for more details.
16  //
17  //You should have received a copy of the GNU Lesser General Public
18  //License along with this library; if not, write to the Free Software
19  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  //-----------------------------------------------------------------------
21  //
22  //$Id: FileRetrieverService.java,v 1.1 2004/10/22 17:34:11 tassos Exp $
23  //
24  
25  import java.util.ArrayList;
26  import java.util.Collections;
27  import java.util.HashMap;
28  import java.util.Iterator;
29  import java.util.List;
30  import java.util.Set;
31  
32  import net.sourceforge.blogentis.om.Blog;
33  import net.sourceforge.blogentis.slide.SlideService;
34  import net.sourceforge.blogentis.turbine.BlogRunDataService;
35  
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  import org.apache.turbine.services.InitializationException;
39  import org.apache.turbine.services.TurbineBaseService;
40  import org.apache.turbine.services.TurbineServices;
41  import org.apache.turbine.util.RunData;
42  
43  public class FileRetrieverService
44          extends TurbineBaseService {
45      private static Log log = LogFactory.getLog(FileRetrieverService.class);
46  
47      public static final String SERVICE_NAME = "FileRetrieverService";
48  
49      public static FileRetrieverService getInstance() {
50          return (FileRetrieverService)TurbineServices.getInstance()
51                  .getService(SERVICE_NAME);
52      }
53  
54      private AbstractFileResource locateFile(Blog blog, String file) {
55          file = file.replaceFirst("^/", "").replaceAll("//+", "/");
56          if (file.indexOf('/') < 0)
57              return null;
58          return SlideService.getInstance()
59                  .getFileIfExists(BlogRunDataService.getCurrentRunData(), blog,
60                                   file);
61      }
62  
63      public AbstractFileResource getFile(Blog blog, String file) {
64          if (blog != null) {
65              AbstractFileResource tf = locateFile(blog, file);
66              if (tf != null)
67                  return tf;
68          }
69          return new LocalFileResource(file);
70      }
71  
72      public boolean exists(Blog blog, String file) {
73          return getFile(blog, file).exists();
74      }
75  
76      public long getFileLastModified(Blog blog, String file) {
77          return getFile(blog, file).getLastModified().getTime();
78      }
79  
80      public void init()
81              throws InitializationException {
82          super.init();
83          setInit(true);
84      }
85  
86      public List getTemplatePathList(RunData data, Blog b, String dirPath,
87                                      FileResourceFilter filter) {
88  
89          ArrayList list = new ArrayList();
90          if (!dirPath.startsWith("/"))
91              dirPath = "/" + dirPath;
92          Set s = SlideService.getInstance().getAllChildren(data, b, dirPath,
93                                                            filter);
94          s.addAll(LocalFileResource.getAllFiles(dirPath, filter));
95          list.addAll(s);
96          Collections.sort(list);
97          return list;
98      }
99  
100     /***
101      * Retrieve a tree of TemplateFolders based on
102      * 
103      * @param data
104      *            the RunData of the current request
105      * @param b
106      *            the Blog from which to extyract the storage tree.
107      * @param dirPath
108      *            the path to start, "/" if to search all files.
109      * @param filter
110      *            the filter to use for the retrieval, or null if all the files
111      *            are requested.
112      * @return a TemplateFolder the represents the root of the tree.
113      */
114     public TemplateFolder getTemplateList(RunData data, Blog b, String dirPath,
115                                           FileResourceFilter filter) {
116         List list = getTemplatePathList(data, b, dirPath, filter);
117         HashMap dirMap = new HashMap();
118         TemplateFolder root = new TemplateFolder(dirPath);
119         dirMap.put(dirPath, root);
120 
121         for(Iterator i = list.iterator(); i.hasNext();) {
122             String path = (String)i.next();
123             AbstractFileResource afr = getFile(b, path);
124             String parent;
125             if (path.lastIndexOf('/') <= 0 || !path.startsWith(dirPath))
126                 parent = null;
127             else
128                 parent = path.substring(0, path.lastIndexOf('/'));
129 
130             TemplateFolder parentFolder = null;
131             if (parent != null)
132                 parentFolder = (TemplateFolder)dirMap.get(parent);
133             if (parentFolder == null)
134                 parentFolder = root;
135 
136             if (afr.isDirectory()) {
137                 if (dirMap.containsKey(path))
138                     continue;
139                 TemplateFolder tf = new TemplateFolder(path);
140                 dirMap.put(path, tf);
141                 parentFolder.directories.add(tf);
142             } else {
143                 parentFolder.files.add(afr);
144             }
145         }
146         return root;
147     }
148 }