Coverage Report - net.sourceforge.pebble.domain.BlogManager
 
Classes in this File Line Coverage Branch Coverage Complexity
BlogManager
32%
27/83
12%
3/24
1.7
 
 1  
 /*
 2  
  * Copyright (c) 2003-2011, 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  4
   private static Log log = LogFactory.getLog(BlogManager.class);
 52  
 
 53  
   /** the singleton instance of this class */
 54  4
   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  4
   private Map<String,Blog> blogs = new HashMap<String,Blog>();
 61  
 
 62  4
   private boolean multiBlog = false;
 63  
 
 64  
   /**
 65  
    * Creates a new instance - private constructor for the singleton pattern.
 66  
    */
 67  4
   private BlogManager() {
 68  4
   }
 69  
 
 70  
   /**
 71  
    * Gets the singleton instance of this class.
 72  
    *
 73  
    * @return    the singleton BlogManager instance
 74  
    */
 75  
   public static BlogManager getInstance() {
 76  9524
     return instance;
 77  
   }
 78  
 
 79  
   /**
 80  
    * Gets the default blog.
 81  
    *
 82  
    * @return    the active Blog instance
 83  
    */
 84  
   public Blog getBlog() {
 85  12
     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  
   public Blog getBlog(String id) {
 98  336
     return blogs.get(id);
 99  
   }
 100  
 
 101  
   /**
 102  
    * Configures this instance to manage the blog(s) in the specified directory.
 103  
    */
 104  
   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  0
     } else {
 124  
       // start the default blog only
 125  0
       startBlog(defaultBlog.getAbsolutePath(), DEFAULT_BLOG);      
 126  
     }
 127  0
   }
 128  
 
 129  
   public void stopBlogs() {
 130  0
     for (Blog blog : blogs.values()) {
 131  0
       stopBlog(blog);
 132  
     }
 133  0
   }
 134  
 
 135  
   private void stopBlog(Blog blog) {
 136  0
     blog.stop();
 137  0
   }
 138  
 
 139  
   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  0
   }
 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  
   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  
     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  0
     } catch (Exception e) {
 174  0
       log.error("Exception encountered", e);
 175  0
     }
 176  
 
 177  
     try {
 178  0
       if (blogVersion == null || !blogVersion.equals(currentVersion)) {
 179  0
         UpgradeUtilities.upgradeBlog(blog, blogVersion, currentVersion);
 180  
 
 181  0
         if (currentVersion != null) {
 182  0
           BufferedWriter writer = new BufferedWriter(new FileWriter(versionFile));
 183  0
           writer.write(currentVersion);
 184  0
           writer.close();
 185  
         }
 186  
 
 187  
         // now that the upgrade is complete, reload the blog
 188  0
         reloadBlog(blog);
 189  
       }
 190  0
     } catch (Exception e) {
 191  0
       log.error("Exception encountered", e);
 192  0
     }
 193  0
   }
 194  
 
 195  
   public void addBlog(String blogId) {
 196  0
     File file = new File(getBlogsDirectory(), blogId);
 197  0
     file.mkdirs();
 198  0
     startBlog(file.getAbsolutePath(), blogId);
 199  0
   }
 200  
 
 201  
   /**
 202  
    * Determines whether this blog manager supports multiple blogs.
 203  
    *
 204  
    * @return  true if multiple blogs are supported, false otherwise
 205  
    */
 206  
   public boolean isMultiBlog() {
 207  1272
     return this.multiBlog;
 208  
   }
 209  
 
 210  
   public void setMultiBlog(boolean multiBlog) {
 211  2552
     this.multiBlog = multiBlog;
 212  2552
   }
 213  
 
 214  
   public void addBlog(Blog blog) {
 215  2688
     blogs.put(blog.getId(), blog);
 216  2688
   }
 217  
 
 218  
   public void removeAllBlogs() {
 219  2552
     blogs = new HashMap<String,Blog>();
 220  2552
   }
 221  
 
 222  
   /**
 223  
    * Gets all blogs that are currently being managed.
 224  
    *
 225  
    * @return  a Collection of Blog instances
 226  
    */
 227  
   public Collection<Blog> getBlogs() {
 228  12
     List<Blog> sortedBlogs = new ArrayList<Blog>(blogs.values());
 229  12
     Collections.sort(sortedBlogs, new BlogByLastModifiedDateComparator());
 230  12
     return sortedBlogs;
 231  
   }
 232  
 
 233  
   /**
 234  
    * Gets the number of blogs that are currently being managed.
 235  
    *
 236  
    * @return the number of managed blogs
 237  
    */
 238  
   public int getNumberOfBlogs() {
 239  4
           return blogs.size();
 240  
   }
 241  
   
 242  
   
 243  
   /**
 244  
    * Gets all blogs that are currently being managed and are
 245  
    * to be included in aggregated pages and feeds.
 246  
    *
 247  
    * @return  a List of Blog instances
 248  
    */
 249  
   public List<Blog> getPublicBlogs() {
 250  20
     List<Blog> list = new ArrayList<Blog>();
 251  20
     for (Blog blog : blogs.values()) {
 252  40
       if (blog.isPublic()) {
 253  40
         list.add(blog);
 254  
       }
 255  
     }
 256  
 
 257  20
     return list;
 258  
   }
 259  
 
 260  
   /**
 261  
    * Determines whether there is a blog with the specified ID.
 262  
    *
 263  
    * @param id    the blog ID
 264  
    * @return  true if a blog with the specified ID exists, false otherwise
 265  
    */
 266  
   public boolean hasBlog(String id) {
 267  32
     return blogs.containsKey(id);
 268  
   }
 269  
 
 270  
   public MultiBlog getMultiBlog() {
 271  20
     return new MultiBlog(PebbleContext.getInstance().getConfiguration().getDataDirectory());
 272  
   }
 273  
 
 274  
   private File getBlogsDirectory() {
 275  0
     return new File(PebbleContext.getInstance().getConfiguration().getDataDirectory(), "blogs");
 276  
   }
 277  
 
 278  
 }