Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
../../../../img/srcFileCovDistChart4.png 41% of files have more coverage
64   276   30   3,2
14   134   0,47   20
20     1,5  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  BlogManager       Line # 48 64 0% 30 66 32,7% 0.3265306
 
  (248)
 
1    /*
2    * Copyright (c) 2003-2006, Simon Brown
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    *
8    * - Redistributions of source code must retain the above copyright
9    * notice, this list of conditions and the following disclaimer.
10    *
11    * - Redistributions in binary form must reproduce the above copyright
12    * notice, this list of conditions and the following disclaimer in
13    * the documentation and/or other materials provided with the
14    * distribution.
15    *
16    * - Neither the name of Pebble nor the names of its contributors may
17    * be used to endorse or promote products derived from this software
18    * without specific prior written permission.
19    *
20    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21    * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22    * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23    * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24    * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30    * POSSIBILITY OF SUCH DAMAGE.
31    */
32    package net.sourceforge.pebble.domain;
33   
34    import net.sourceforge.pebble.PebbleContext;
35    import net.sourceforge.pebble.util.UpgradeUtilities;
36    import net.sourceforge.pebble.comparator.BlogByLastModifiedDateComparator;
37    import org.apache.commons.logging.Log;
38    import org.apache.commons.logging.LogFactory;
39   
40    import java.io.*;
41    import java.util.*;
42   
43    /**
44    * A singleton to manage the active blog.
45    *
46    * @author Simon Brown
47    */
 
48    public class BlogManager {
49   
50    /** the log used by this class */
51    private static Log log = LogFactory.getLog(BlogManager.class);
52   
53    /** the singleton instance of this class */
54    private static BlogManager instance = new BlogManager();
55   
56    private static final String THEMES_PATH = "themes";
57    private static final String DEFAULT_BLOG = "default";
58   
59    /** the blogs that are currently being managed */
60    private Map<String,Blog> blogs = new HashMap<String,Blog>();
61   
62    private boolean multiBlog = false;
63   
64    /**
65    * Creates a new instance - private constructor for the singleton pattern.
66    */
 
67  2 toggle private BlogManager() {
68    }
69   
70    /**
71    * Gets the singleton instance of this class.
72    *
73    * @return the singleton BlogManager instance
74    */
 
75  4646 toggle public static BlogManager getInstance() {
76  4646 return instance;
77    }
78   
79    /**
80    * Gets the default blog.
81    *
82    * @return the active Blog instance
83    */
 
84  6 toggle public Blog getBlog() {
85  6 return getBlog(DEFAULT_BLOG);
86    }
87   
88    /**
89    * Gets a named blog. If running in single user mode then this method
90    * returns the currently active blog. If running in multi-user mode,
91    * this method returns the named blog from the overall composite
92    * blog.
93    *
94    * @param id the blog ID
95    * @return a Blog instance
96    */
 
97  168 toggle public Blog getBlog(String id) {
98  168 return blogs.get(id);
99    }
100   
101    /**
102    * Configures this instance to manage the blog(s) in the specified directory.
103    */
 
104  0 toggle public void startBlogs() {
105  0 File blogsDirectory = getBlogsDirectory();
106  0 File defaultBlog = new File(blogsDirectory, DEFAULT_BLOG);
107   
108  0 if (!blogsDirectory.exists()) {
109  0 log.info("Pebble blog directory does not exist - creating");
110  0 defaultBlog.mkdirs();
111    }
112   
113  0 if (isMultiBlog()) {
114    // find all directories and set them up as blogs
115  0 File files[] = getBlogsDirectory().listFiles();
116  0 if (files != null) {
117  0 for (File file : files) {
118  0 if (file.isDirectory()) {
119  0 startBlog(file.getAbsolutePath(), file.getName());
120    }
121    }
122    }
123    } else {
124    // start the default blog only
125  0 startBlog(defaultBlog.getAbsolutePath(), DEFAULT_BLOG);
126    }
127    }
128   
 
129  0 toggle public void stopBlogs() {
130  0 for (Blog blog : blogs.values()) {
131  0 stopBlog(blog);
132    }
133    }
134   
 
135  0 toggle private void stopBlog(Blog blog) {
136  0 blog.stop();
137    }
138   
 
139  0 toggle public void reloadBlog(Blog blog) {
140  0 stopBlog(blog);
141   
142  0 File f = new File(getBlogsDirectory(), blog.getId());
143  0 startBlog(f.getAbsolutePath(), blog.getId());
144    }
145   
146    /**
147    * Loads a blog that is a part of a larger composite blog.
148    *
149    * @param blogDir the blog.dir for the blog
150    * @param blogId the ID for the blog
151    */
 
152  0 toggle private void startBlog(String blogDir, String blogId) {
153  0 Blog blog = new Blog(blogDir);
154  0 blog.setId(blogId);
155   
156  0 File pathToLiveThemes = new File(PebbleContext.getInstance().getWebApplicationRoot(), THEMES_PATH);
157  0 Theme theme = new Theme(blog, "user-" + blogId, pathToLiveThemes.getAbsolutePath());
158  0 blog.setEditableTheme(theme);
159   
160  0 blog.start();
161  0 blogs.put(blog.getId(), blog);
162   
163    // which version are we at and do we need to upgrade?
164  0 File versionFile = new File(blogDir, "pebble.version");
165  0 String blogVersion = null;
166  0 String currentVersion = PebbleContext.getInstance().getBuildVersion();
167  0 try {
168  0 if (versionFile.exists()) {
169  0 BufferedReader reader = new BufferedReader(new FileReader(versionFile));
170  0 blogVersion = reader.readLine();
171  0 reader.close();
172    }
173    } catch (Exception e) {
174  0 log.error(e);
175    }
176   
177  0 try {
178  0 if (blogVersion == null || !blogVersion.equals(currentVersion)) {
179  0 UpgradeUtilities.upgradeBlog(blog, blogVersion, currentVersion);
180   
181  0 BufferedWriter writer = new BufferedWriter(new FileWriter(versionFile));
182  0 writer.write(currentVersion);
183  0 writer.close();
184   
185    // now that the upgrade is complete, reload the blog
186  0 reloadBlog(blog);
187    }
188    } catch (Exception e) {
189  0 log.error(e);
190    }
191    }
192   
 
193  0 toggle public void addBlog(String blogId) {
194  0 File file = new File(getBlogsDirectory(), blogId);
195  0 file.mkdirs();
196  0 startBlog(file.getAbsolutePath(), blogId);
197    }
198   
199    /**
200    * Determines whether this blog manager supports multiple blogs.
201    *
202    * @return true if multiple blogs are supported, false otherwise
203    */
 
204  598 toggle public boolean isMultiBlog() {
205  598 return this.multiBlog;
206    }
207   
 
208  1250 toggle public void setMultiBlog(boolean multiBlog) {
209  1250 this.multiBlog = multiBlog;
210    }
211   
 
212  1318 toggle public void addBlog(Blog blog) {
213  1318 blogs.put(blog.getId(), blog);
214    }
215   
 
216  1250 toggle public void removeAllBlogs() {
217  1250 blogs = new HashMap<String,Blog>();
218    }
219   
220    /**
221    * Gets all blogs that are currently being managed.
222    *
223    * @return a Collection of Blog instances
224    */
 
225  6 toggle public Collection<Blog> getBlogs() {
226  6 List<Blog> sortedBlogs = new ArrayList<Blog>(blogs.values());
227  6 Collections.sort(sortedBlogs, new BlogByLastModifiedDateComparator());
228  6 return sortedBlogs;
229    }
230   
231    /**
232    * Gets the number of blogs that are currently being managed.
233    *
234    * @return the number of managed blogs
235    */
 
236  2 toggle public int getNumberOfBlogs() {
237  2 return blogs.size();
238    }
239   
240   
241    /**
242    * Gets all blogs that are currently being managed and are
243    * to be included in aggregated pages and feeds.
244    *
245    * @return a List of Blog instances
246    */
 
247  10 toggle public List<Blog> getPublicBlogs() {
248  10 List<Blog> list = new ArrayList<Blog>();
249  10 for (Blog blog : blogs.values()) {
250  20 if (blog.isPublic()) {
251  20 list.add(blog);
252    }
253    }
254   
255  10 return list;
256    }
257   
258    /**
259    * Determines whether there is a blog with the specified ID.
260    *
261    * @param id the blog ID
262    * @return true if a blog with the specified ID exists, false otherwise
263    */
 
264  16 toggle public boolean hasBlog(String id) {
265  16 return blogs.containsKey(id);
266    }
267   
 
268  10 toggle public MultiBlog getMultiBlog() {
269  10 return new MultiBlog(PebbleContext.getInstance().getConfiguration().getDataDirectory());
270    }
271   
 
272  0 toggle private File getBlogsDirectory() {
273  0 return new File(PebbleContext.getInstance().getConfiguration().getDataDirectory(), "blogs");
274    }
275   
276    }