Coverage Report - net.sourceforge.pebble.ContentCache
 
Classes in this File Line Coverage Branch Coverage Complexity
ContentCache
63%
21/33
50%
2/4
1.167
 
 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;
 33  
 
 34  
 import org.apache.commons.logging.Log;
 35  
 import org.apache.commons.logging.LogFactory;
 36  
 
 37  
 import java.util.Date;
 38  
 import java.util.Properties;
 39  
 import java.io.InputStream;
 40  
 import java.io.IOException;
 41  
 import java.net.URL;
 42  
 
 43  
 import net.sourceforge.pebble.util.RelativeDate;
 44  
 import net.sourceforge.pebble.domain.*;
 45  
 import net.sf.ehcache.Element;
 46  
 import net.sf.ehcache.Cache;
 47  
 import net.sf.ehcache.CacheManager;
 48  
 
 49  
 /**
 50  
  * A wrapper for a cache used to store blog entries and static pages.
 51  
  *
 52  
  * @author    Simon Brown
 53  
  */
 54  
 public class ContentCache {
 55  
 
 56  4
   private static final ContentCache instance = new ContentCache();
 57  
 
 58  
   /** the log used by this class */
 59  4
   private static Log log = LogFactory.getLog(ContentCache.class);
 60  
 
 61  
   private Cache cache;
 62  
 
 63  4
   private ContentCache() {
 64  4
     URL url = BlogService.class.getResource("/ehcache.xml");
 65  4
     CacheManager cacheManager = new CacheManager(url);
 66  4
     cache = cacheManager.getCache("contentCache");
 67  
 
 68  
     // size the cache (number of blogs * max elements in memory configured in the ehcache.xml file)
 69  
     // Fix: Previously the number of blogs was calculated through blogManager.getBlogs().getSize() which
 70  
     // caused the blog to load and access the Cache that is just now being initialized.
 71  
     // This lead to NPE because instance is not yet set to this instance.
 72  4
     cache.getCacheConfiguration().setMaxElementsInMemory(cache.getCacheConfiguration().getMaxElementsInMemory() * BlogManager.getInstance().getNumberOfBlogs());
 73  4
   }
 74  
 
 75  
   public static ContentCache getInstance() {
 76  3200
     return instance;
 77  
   }
 78  
 
 79  
   public synchronized void putBlogEntry(BlogEntry blogEntry) {
 80  608
     Element element = new Element(getCompositeKeyForBlogEntry(blogEntry), blogEntry);
 81  608
     cache.put(element);
 82  608
   }
 83  
 
 84  
   public synchronized BlogEntry getBlogEntry(Blog blog, String blogEntryId) {
 85  2132
     BlogEntry blogEntry = null;
 86  2132
     Element element = cache.get(getCompositeKeyForBlogEntry(blog, blogEntryId));
 87  2132
     if (element != null) {
 88  852
       blogEntry = (BlogEntry)element.getValue(); 
 89  
     }
 90  
 
 91  2132
     return blogEntry;
 92  
   }
 93  
 
 94  
   public synchronized void removeBlogEntry(BlogEntry blogEntry) {
 95  784
     cache.remove(getCompositeKeyForBlogEntry(blogEntry));
 96  784
   }
 97  
 
 98  
   private String getCompositeKeyForBlogEntry(BlogEntry blogEntry) {
 99  1392
     return getCompositeKeyForBlogEntry(blogEntry.getBlog(), blogEntry.getId());
 100  
   }
 101  
 
 102  
   private String getCompositeKeyForBlogEntry(Blog blog, String blogEntryId) {
 103  3524
     return blog.getId() + "/blogEntry/" + blogEntryId;
 104  
   }
 105  
 
 106  
   public synchronized void putStaticPage(StaticPage staticPage) {
 107  0
     Element element = new Element(getCompositeKeyForStaticPage(staticPage), staticPage);
 108  0
     cache.put(element);
 109  0
   }
 110  
 
 111  
   public synchronized StaticPage getStaticPage(Blog blog, String staticPageId) {
 112  0
     StaticPage staticPage = null;
 113  0
     Element element = cache.get(getCompositeKeyForStaticPage(blog, staticPageId));
 114  0
     if (element != null) {
 115  0
       staticPage = (StaticPage)element.getValue();
 116  
     }
 117  
 
 118  0
     return staticPage;
 119  
   }
 120  
 
 121  
   public synchronized void removeStaticPage(StaticPage staticPage) {
 122  0
     cache.remove(getCompositeKeyForStaticPage(staticPage));
 123  0
   }
 124  
 
 125  
   private String getCompositeKeyForStaticPage(StaticPage staticPage) {
 126  0
     return getCompositeKeyForStaticPage(staticPage.getBlog(), staticPage.getId());
 127  
   }
 128  
 
 129  
   private String getCompositeKeyForStaticPage(Blog blog, String staticPageId) {
 130  0
     return blog.getId() + "/staticPage/" + staticPageId;
 131  
   }
 132  
 
 133  
 }