contentcache.java

### File Source: contentcache.java

             /*
        P/P   *  Method: net.sourceforge.pebble.ContentCache__static_init
              *
              *  Preconditions:
              *    net/sourceforge/pebble/domain/BlogManager.instance != null
              *    net/sourceforge/pebble/domain/BlogManager.instance.blogs != null
              *
              *  Presumptions:
              *    net.sf.ehcache.CacheManager:getCache(...)@66 != null
              *
              *  Postconditions:
              *    instance == &new ContentCache(ContentCache__static_init#1)
              *    init'ed(log)
              *    new ContentCache(ContentCache__static_init#1) num objects == 1
              *    instance.cache != null
              */
         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.*;
             /*
        P/P   *  Method: void net.sourceforge.pebble.ContentCache()
              *
              *  Preconditions:
              *    net/sourceforge/pebble/domain/BlogManager.instance != null
              *    net/sourceforge/pebble/domain/BlogManager.instance.blogs != null
              *
              *  Presumptions:
              *    net.sf.ehcache.Cache:getCacheConfiguration(...)@72 != null
              *    net.sf.ehcache.CacheManager:getCache(...)@66 != null
              *    net.sf.ehcache.config.CacheConfiguration:getMaxElementsInMemory(...)@72*getNumberOfBlogs(...)@72 in range
              *    net.sf.ehcache.config.CacheConfiguration:getMaxElementsInMemory(...)@72*java.util.Map:size(...)@237 in -231..232-1
              *
              *  Postconditions:
              *    (soft) this.cache != null
              */
        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    private ContentCache() {
        64      URL url = BlogService.class.getResource("/ehcache.xml");
        65      CacheManager cacheManager = new CacheManager(url);
        66      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      cache.getCacheConfiguration().setMaxElementsInMemory(cache.getCacheConfiguration().getMaxElementsInMemory() * BlogManager.getInstance().getNumberOfBlogs());
        73    }
        74
               /*
        P/P     *  Method: ContentCache getInstance()
                *
                *  Postconditions:
                *    return_value == &new ContentCache(ContentCache__static_init#1)
                */
        75    public static ContentCache getInstance() {
        76      return instance;
        77    }
        78
        79    public synchronized void putBlogEntry(BlogEntry blogEntry) {
                 /*
        P/P       *  Method: void putBlogEntry(BlogEntry)
                  *
                  *  Preconditions:
                  *    blogEntry != null
                  *    blogEntry.blog != null
                  *    init'ed(blogEntry.blog.id)
                  *    init'ed(blogEntry.id)
                  *    this.cache != null
                  */
        80      Element element = new Element(getCompositeKeyForBlogEntry(blogEntry), blogEntry);
        81      cache.put(element);
        82    }
        83
        84    public synchronized BlogEntry getBlogEntry(Blog blog, String blogEntryId) {
        85      BlogEntry blogEntry = null;
                 /*
        P/P       *  Method: BlogEntry getBlogEntry(Blog, String)
                  *
                  *  Preconditions:
                  *    blog != null
                  *    init'ed(blog.id)
                  *    this.cache != null
                  *
                  *  Postconditions:
                  *    init'ed(return_value)
                  *
                  *  Test Vectors:
                  *    net.sf.ehcache.Cache:get(...)@86: Addr_Set{null}, Inverse{null}
                  */
        86      Element element = cache.get(getCompositeKeyForBlogEntry(blog, blogEntryId));
        87      if (element != null) {
        88        blogEntry = (BlogEntry)element.getValue();
        89      }
        90
        91      return blogEntry;
        92    }
        93
        94    public synchronized void removeBlogEntry(BlogEntry blogEntry) {
                 /*
        P/P       *  Method: void removeBlogEntry(BlogEntry)
                  *
                  *  Preconditions:
                  *    blogEntry != null
                  *    blogEntry.blog != null
                  *    init'ed(blogEntry.blog.id)
                  *    init'ed(blogEntry.id)
                  *    this.cache != null
                  */
        95      cache.remove(getCompositeKeyForBlogEntry(blogEntry));
        96    }
        97
               /*
        P/P     *  Method: String getCompositeKeyForBlogEntry(BlogEntry)
                *
                *  Preconditions:
                *    blogEntry != null
                *    blogEntry.blog != null
                *    init'ed(blogEntry.blog.id)
                *    init'ed(blogEntry.id)
                *
                *  Postconditions:
                *    return_value != null
                */
        98    private String getCompositeKeyForBlogEntry(BlogEntry blogEntry) {
        99      return getCompositeKeyForBlogEntry(blogEntry.getBlog(), blogEntry.getId());
       100    }
       101
               /*
        P/P     *  Method: String getCompositeKeyForBlogEntry(Blog, String)
                *
                *  Preconditions:
                *    blog != null
                *    init'ed(blog.id)
                *
                *  Postconditions:
                *    return_value != null
                */
       102    private String getCompositeKeyForBlogEntry(Blog blog, String blogEntryId) {
       103      return blog.getId() + "/blogEntry/" + blogEntryId;
       104    }
       105
       106    public synchronized void putStaticPage(StaticPage staticPage) {
                 /*
        P/P       *  Method: void putStaticPage(StaticPage)
                  *
                  *  Preconditions:
                  *    staticPage != null
                  *    staticPage.blog != null
                  *    init'ed(staticPage.blog.id)
                  *    init'ed(staticPage.id)
                  *    this.cache != null
                  */
       107      Element element = new Element(getCompositeKeyForStaticPage(staticPage), staticPage);
       108      cache.put(element);
       109    }
       110
       111    public synchronized StaticPage getStaticPage(Blog blog, String staticPageId) {
       112      StaticPage staticPage = null;
                 /*
        P/P       *  Method: StaticPage getStaticPage(Blog, String)
                  *
                  *  Preconditions:
                  *    blog != null
                  *    init'ed(blog.id)
                  *    this.cache != null
                  *
                  *  Postconditions:
                  *    init'ed(return_value)
                  *
                  *  Test Vectors:
                  *    net.sf.ehcache.Cache:get(...)@113: Addr_Set{null}, Inverse{null}
                  */
       113      Element element = cache.get(getCompositeKeyForStaticPage(blog, staticPageId));
       114      if (element != null) {
       115        staticPage = (StaticPage)element.getValue();
       116      }
       117
       118      return staticPage;
       119    }
       120
       121    public synchronized void removeStaticPage(StaticPage staticPage) {
                 /*
        P/P       *  Method: void removeStaticPage(StaticPage)
                  *
                  *  Preconditions:
                  *    staticPage != null
                  *    staticPage.blog != null
                  *    init'ed(staticPage.blog.id)
                  *    init'ed(staticPage.id)
                  *    this.cache != null
                  */
       122      cache.remove(getCompositeKeyForStaticPage(staticPage));
       123    }
       124
               /*
        P/P     *  Method: String getCompositeKeyForStaticPage(StaticPage)
                *
                *  Preconditions:
                *    staticPage != null
                *    staticPage.blog != null
                *    init'ed(staticPage.blog.id)
                *    init'ed(staticPage.id)
                *
                *  Postconditions:
                *    return_value != null
                */
       125    private String getCompositeKeyForStaticPage(StaticPage staticPage) {
       126      return getCompositeKeyForStaticPage(staticPage.getBlog(), staticPage.getId());
       127    }
       128
               /*
        P/P     *  Method: String getCompositeKeyForStaticPage(Blog, String)
                *
                *  Preconditions:
                *    blog != null
                *    init'ed(blog.id)
                *
                *  Postconditions:
                *    return_value != null
                */
       129    private String getCompositeKeyForStaticPage(Blog blog, String staticPageId) {
       130      return blog.getId() + "/staticPage/" + staticPageId;
       131    }
       132
       133  }

SofCheck Inspector Build Version : 2.22510

contentcache.java | 2010-Jun-25 19:40:34
contentcache.class | 2010-Jul-19 20:23:40