Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
../../../img/srcFileCovDistChart6.png 36% of files have more coverage
25   133   14   2,08
4   68   0,56   12
12     1,17  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  ContentCache       Line # 54 25 0% 14 17 58,5% 0.58536583
 
  (216)
 
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;
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    private static final ContentCache instance = new ContentCache();
57   
58    /** the log used by this class */
59    private static Log log = LogFactory.getLog(ContentCache.class);
60   
61    private Cache cache;
62   
 
63  2 toggle private ContentCache() {
64  2 URL url = BlogService.class.getResource("/ehcache.xml");
65  2 CacheManager cacheManager = new CacheManager(url);
66  2 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  2 cache.getCacheConfiguration().setMaxElementsInMemory(cache.getCacheConfiguration().getMaxElementsInMemory() * BlogManager.getInstance().getNumberOfBlogs());
73    }
74   
 
75  1294 toggle public static ContentCache getInstance() {
76  1294 return instance;
77    }
78   
 
79  284 toggle public synchronized void putBlogEntry(BlogEntry blogEntry) {
80  284 Element element = new Element(getCompositeKeyForBlogEntry(blogEntry), blogEntry);
81  284 cache.put(element);
82    }
83   
 
84  790 toggle public synchronized BlogEntry getBlogEntry(Blog blog, String blogEntryId) {
85  790 BlogEntry blogEntry = null;
86  790 Element element = cache.get(getCompositeKeyForBlogEntry(blog, blogEntryId));
87  790 if (element != null) {
88  190 blogEntry = (BlogEntry)element.getValue();
89    }
90   
91  790 return blogEntry;
92    }
93   
 
94  372 toggle public synchronized void removeBlogEntry(BlogEntry blogEntry) {
95  372 cache.remove(getCompositeKeyForBlogEntry(blogEntry));
96    }
97   
 
98  656 toggle private String getCompositeKeyForBlogEntry(BlogEntry blogEntry) {
99  656 return getCompositeKeyForBlogEntry(blogEntry.getBlog(), blogEntry.getId());
100    }
101   
 
102  1446 toggle private String getCompositeKeyForBlogEntry(Blog blog, String blogEntryId) {
103  1446 return blog.getId() + "/blogEntry/" + blogEntryId;
104    }
105   
 
106  0 toggle public synchronized void putStaticPage(StaticPage staticPage) {
107  0 Element element = new Element(getCompositeKeyForStaticPage(staticPage), staticPage);
108  0 cache.put(element);
109    }
110   
 
111  0 toggle 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  0 toggle public synchronized void removeStaticPage(StaticPage staticPage) {
122  0 cache.remove(getCompositeKeyForStaticPage(staticPage));
123    }
124   
 
125  0 toggle private String getCompositeKeyForStaticPage(StaticPage staticPage) {
126  0 return getCompositeKeyForStaticPage(staticPage.getBlog(), staticPage.getId());
127    }
128   
 
129  0 toggle private String getCompositeKeyForStaticPage(Blog blog, String staticPageId) {
130  0 return blog.getId() + "/staticPage/" + staticPageId;
131    }
132   
133    }