Author: epugh
Date: Wed Mar 15 08:10:48 2006
New Revision: 386104

URL: http://svn.apache.org/viewcvs?rev=386104&view=rev
Log:
Add support for caching using EHCache.

Added:
    
jakarta/turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/EHCacheService.java
    jakarta/turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/
    
jakarta/turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/DefaultEHCacheService.java
    
jakarta/turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/EHCacheTest.java
Modified:
    jakarta/turbine/fulcrum/trunk/cache/project.xml
    
jakarta/turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/GlobalCacheService.java
    jakarta/turbine/fulcrum/trunk/cache/src/test/TestComponentConfig.xml
    jakarta/turbine/fulcrum/trunk/cache/src/test/TestRoleConfig.xml
    
jakarta/turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/CacheTest.java
    jakarta/turbine/fulcrum/trunk/cache/xdocs/changes.xml
    jakarta/turbine/fulcrum/trunk/cache/xdocs/index.xml
    jakarta/turbine/fulcrum/trunk/cache/xdocs/navigation.xml

Modified: jakarta/turbine/fulcrum/trunk/cache/project.xml
URL: 
http://svn.apache.org/viewcvs/jakarta/turbine/fulcrum/trunk/cache/project.xml?rev=386104&r1=386103&r2=386104&view=diff
==============================================================================
--- jakarta/turbine/fulcrum/trunk/cache/project.xml (original)
+++ jakarta/turbine/fulcrum/trunk/cache/project.xml Wed Mar 15 08:10:48 2006
@@ -18,6 +18,18 @@
       <version>1.0.3</version>
     </dependency>
     
+    <dependency>
+      <groupId>commons-logging</groupId>
+      <artifactId>commons-logging</artifactId>
+      <version>1.0.3</version>
+    </dependency> 
+          
+    <dependency>
+      <groupId>ehcache</groupId>
+      <artifactId>ehcache</artifactId>
+      <version>1.2beta4</version>
+    </dependency>         
+    
     <!--  Needed only for testing -->
     <dependency>
       <groupId>fulcrum</groupId>

Added: 
jakarta/turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/EHCacheService.java
URL: 
http://svn.apache.org/viewcvs/jakarta/turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/EHCacheService.java?rev=386104&view=auto
==============================================================================
--- 
jakarta/turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/EHCacheService.java
 (added)
+++ 
jakarta/turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/EHCacheService.java
 Wed Mar 15 08:10:48 2006
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2001-2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */ 
+
+package org.apache.fulcrum.cache;
+
+import net.sf.ehcache.Cache;
+import net.sf.ehcache.CacheManager;
+
+
+/**
+ * @author <a href="mailto:[EMAIL PROTECTED]">Eric Pugh</a>
+ *
+ */
+public interface EHCacheService 
+{
+       /** avalon idiom */
+       public static final String ROLE = EHCacheService.class.getName();
+       
+       public CacheManager getCacheManager();
+       
+       public Cache getCache(String cache);
+       
+       
+}

Modified: 
jakarta/turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/GlobalCacheService.java
URL: 
http://svn.apache.org/viewcvs/jakarta/turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/GlobalCacheService.java?rev=386104&r1=386103&r2=386104&view=diff
==============================================================================
--- 
jakarta/turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/GlobalCacheService.java
 (original)
+++ 
jakarta/turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/GlobalCacheService.java
 Wed Mar 15 08:10:48 2006
@@ -25,6 +25,10 @@
 
 /**
  * GlobalCacheService interface.
+ * 
+ * @todo This GlobalCacheService should be usable by both the 
DefaultEHCacheService
+ * and the DefaultGlobalCache.   The CacheObject class from GCS and the 
Element class
+ * from EHCS are very similar.
  *
  * @author <a href="mailto:[EMAIL PROTECTED]">Dave Bryson</a>
  * @author <a href="mailto:[EMAIL PROTECTED]">Peter Courcoux</a>

Added: 
jakarta/turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/DefaultEHCacheService.java
URL: 
http://svn.apache.org/viewcvs/jakarta/turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/DefaultEHCacheService.java?rev=386104&view=auto
==============================================================================
--- 
jakarta/turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/DefaultEHCacheService.java
 (added)
+++ 
jakarta/turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/DefaultEHCacheService.java
 Wed Mar 15 08:10:48 2006
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2001-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.fulcrum.cache.impl;
+
+import net.sf.ehcache.Cache;
+import net.sf.ehcache.CacheManager;
+
+import org.apache.avalon.framework.activity.Disposable;
+import org.apache.avalon.framework.activity.Initializable;
+import org.apache.avalon.framework.service.ServiceException;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.avalon.framework.service.Serviceable;
+import org.apache.avalon.framework.thread.ThreadSafe;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.fulcrum.cache.EHCacheService;
+
+/**
+ * Default implementation of EHCacheService
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Eric Pugh </a>
+ *
+ */
+public class DefaultEHCacheService implements EHCacheService, Serviceable, 
Disposable, Initializable,
+        ThreadSafe {
+
+    protected Log logger = 
LogFactory.getLog(DefaultEHCacheService.class.getName());
+
+    private CacheManager cacheManager;
+    
+    public CacheManager getCacheManager(){
+               return cacheManager;
+    }
+    
+    public Cache getCache(String cacheName){
+               return cacheManager.getCache(cacheName);
+    }
+
+
+    /**
+     * @see 
org.apache.avalon.framework.component.Composable#compose(org.apache.avalon.framework.component.ComponentManager)
+     */
+    public void service(ServiceManager manager) throws ServiceException {
+
+
+    }
+
+    /**
+     * @see org.apache.avalon.framework.activity.Initializable#initialize()
+     */
+    public void initialize() throws Exception {
+        
+        cacheManager = CacheManager.create();
+        logger.debug("EHCacheService started!");
+    }
+
+    /**
+     * @see org.apache.avalon.framework.activity.Disposable#dispose()
+     */
+    public void dispose() {
+               cacheManager.shutdown();
+               cacheManager = null;
+           logger.debug("EHCacheService stopped!");
+           
+    }
+
+   
+}
\ No newline at end of file

Modified: jakarta/turbine/fulcrum/trunk/cache/src/test/TestComponentConfig.xml
URL: 
http://svn.apache.org/viewcvs/jakarta/turbine/fulcrum/trunk/cache/src/test/TestComponentConfig.xml?rev=386104&r1=386103&r2=386104&view=diff
==============================================================================
--- jakarta/turbine/fulcrum/trunk/cache/src/test/TestComponentConfig.xml 
(original)
+++ jakarta/turbine/fulcrum/trunk/cache/src/test/TestComponentConfig.xml Wed 
Mar 15 08:10:48 2006
@@ -1,3 +1,4 @@
 <componentConfig>
     <cache cacheInitialSize="20" cacheCheckFrequency="5"/>
+    <ehcache/>
 </componentConfig>

Modified: jakarta/turbine/fulcrum/trunk/cache/src/test/TestRoleConfig.xml
URL: 
http://svn.apache.org/viewcvs/jakarta/turbine/fulcrum/trunk/cache/src/test/TestRoleConfig.xml?rev=386104&r1=386103&r2=386104&view=diff
==============================================================================
--- jakarta/turbine/fulcrum/trunk/cache/src/test/TestRoleConfig.xml (original)
+++ jakarta/turbine/fulcrum/trunk/cache/src/test/TestRoleConfig.xml Wed Mar 15 
08:10:48 2006
@@ -4,4 +4,9 @@
         name="org.apache.fulcrum.cache.GlobalCacheService"
         shorthand="cache"
         default-class="org.apache.fulcrum.cache.DefaultGlobalCacheService"/>
+        
+    <role
+        name="org.apache.fulcrum.cache.EHCacheService"
+        shorthand="ehcache"
+        default-class="org.apache.fulcrum.cache.impl.DefaultEHCacheService"/>  
      
 </role-list>

Modified: 
jakarta/turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/CacheTest.java
URL: 
http://svn.apache.org/viewcvs/jakarta/turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/CacheTest.java?rev=386104&r1=386103&r2=386104&view=diff
==============================================================================
--- 
jakarta/turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/CacheTest.java
 (original)
+++ 
jakarta/turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/CacheTest.java
 Wed Mar 15 08:10:48 2006
@@ -220,12 +220,10 @@
     public void testCacheFlush() throws Exception
     {
         String testString = new String("This is a test");
-        Object retrievedObject = null;
         CachedObject cacheObject = null;
         // Create and add Object that expires in 1 turbine Refresh + 1 millis 
         cacheObject = new CachedObject(testString, (getCacheRefresh() * 5) + 
1);
         assertNotNull("Failed to create a cachable object", cacheObject);
-        long addTime = System.currentTimeMillis();
         globalCache.addObject(cacheKey, cacheObject);
         // 1 Refresh
         Thread.sleep(getCacheRefresh() + 1);
@@ -270,7 +268,6 @@
      */
     public void testRefreshableObject() throws Exception
     {
-        String testString = new String("This is a test");
         Object retrievedObject = null;
         RefreshableCachedObject cacheObject = null;
         // Create and add Object that expires in TEST_EXPIRETIME millis.
@@ -378,7 +375,6 @@
             LOG.warn("Running testRefreshableTimeToLive test due to property " 
+ SKIP_TESTS_KEY + " being false.");
         }
 
-        String testString = new String("This is a test");
         Object retrievedObject = null;
         RefreshableCachedObject cacheObject = null;
         // Create and add Object that expires in TEST_EXPIRETIME millis.

Added: 
jakarta/turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/EHCacheTest.java
URL: 
http://svn.apache.org/viewcvs/jakarta/turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/EHCacheTest.java?rev=386104&view=auto
==============================================================================
--- 
jakarta/turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/EHCacheTest.java
 (added)
+++ 
jakarta/turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/EHCacheTest.java
 Wed Mar 15 08:10:48 2006
@@ -0,0 +1,194 @@
+
+/*
+ * Copyright 2000-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License")
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.fulcrum.cache;
+
+import net.sf.ehcache.Cache;
+import net.sf.ehcache.Element;
+
+import org.apache.avalon.framework.component.ComponentException;
+import org.apache.fulcrum.testcontainer.BaseUnitTest;
+/**
+ * EHCacheTest
+ *
+ * @author <a href="[EMAIL PROTECTED]">Eric Pugh</a> 
+ * @version $Id: CacheTest.java 223198 2004-11-09 08:30:41Z epugh $
+ */
+public class EHCacheTest extends BaseUnitTest
+{
+    
+    private EHCacheService ehCacheService = null;
+    private static final String cacheKey = "CacheKey";
+    private static final String cacheKey_2 = "CacheKey_2";
+    
+
+    /**
+     * Defines the testcase name for JUnit.
+     *
+     * @param name the testcase's name.
+     */
+    public EHCacheTest(String name)
+    {
+        super(name);
+    }
+
+    protected void setUp() throws Exception
+    {
+        super.setUp();
+        try
+        {
+            ehCacheService = (EHCacheService) this.lookup(EHCacheService.ROLE);
+        }
+        catch (ComponentException e)
+        {
+            e.printStackTrace();
+            fail(e.getMessage());
+        }
+    }
+    /**
+     * Simple test that verify an object can be created and deleted.
+     * @throws Exception
+     */
+    public void testSimpleAddGetCacheObject() throws Exception
+    {
+               Cache ehCache = new Cache("test", 1, true, false, 5, 2);
+               ehCacheService.getCacheManager().addCache(ehCache);
+        String testString = new String("This is a test");
+        Object retrievedObject = null;
+        Element cacheObject1 = null;
+        // Create object
+        cacheObject1 = new Element(cacheKey,testString);
+        assertNotNull("Failed to create a cachable object 1", cacheObject1);
+        // Add object to cache
+        ehCache.put(cacheObject1);
+        // Get object from cache
+        retrievedObject = ehCache.get(cacheKey);
+        assertNotNull("Did not retrieved a cached object 1", retrievedObject);
+        assertTrue("Did not retrieved a correct, expected cached object 1", 
retrievedObject == cacheObject1);
+        // Remove object from cache
+        ehCache.remove(cacheKey);
+        // Verify object removed from cache
+        retrievedObject = null;
+        cacheObject1 = null;
+        try
+        {
+            retrievedObject = ehCache.get(cacheKey);
+            assertNull(
+                "Retrieved the deleted cached object 1 and did not get 
expected ObjectExpiredException",
+                retrievedObject);
+        }
+   
+        catch (Exception e)
+        {
+            throw e;
+        }
+        // Remove object from cache that does NOT exist in the cache
+        ehCache.remove(cacheKey);
+    }
+    /**
+     * Simple test that adds, retrieves, and deletes 2 object.
+     *
+     * @throws Exception
+     */
+    public void test2ObjectAddGetCachedObject() throws Exception
+    {
+               Cache ehCache = new Cache("test2", 1, true, false, 5, 2);
+               ehCacheService.getCacheManager().addCache(ehCache);
+        String testString = new String("This is a test");
+        Object retrievedObject = null;
+        Element cacheObject1 = null;
+        Element cacheObject2 = null;
+        // Create and add Object #1
+        cacheObject1 = new Element(cacheKey,testString);
+        assertNotNull("Failed to create a cachable object 1", cacheObject1);
+        ehCache.put(cacheObject1);
+        retrievedObject = ehCache.get(cacheKey);
+        assertNotNull("Did not retrieved a cached object 1", retrievedObject);
+        assertEquals("Did not retrieved correct cached object", cacheObject1, 
retrievedObject);
+        // Create and add Object #2
+        cacheObject2 = new Element(cacheKey_2,testString);
+        assertNotNull("Failed to create a cachable object 2", cacheObject2);
+        ehCache.put(cacheObject2);
+        retrievedObject = ehCache.get(cacheKey_2);
+        assertNotNull("Did not retrieved a cached object 2", retrievedObject);
+        assertEquals("Did not retrieved correct cached object 2", 
cacheObject2, retrievedObject);
+        // Get object #1
+        retrievedObject = ehCache.get(cacheKey);
+        assertNotNull("Did not retrieved a cached object 1. Attempt #2", 
retrievedObject);
+        assertEquals("Did not retrieved correct cached object 1. Attempt #2", 
cacheObject1, retrievedObject);
+        // Get object #1
+        retrievedObject = ehCache.get(cacheKey);
+        assertNotNull("Did not retrieved a cached object 1. Attempt #3", 
retrievedObject);
+        assertEquals("Did not retrieved correct cached object 1. Attempt #3", 
cacheObject1, retrievedObject);
+        // Get object #2
+        retrievedObject = ehCache.get(cacheKey_2);
+        assertNotNull("Did not retrieved a cached object 2. Attempt #2", 
retrievedObject);
+        assertEquals("Did not retrieved correct cached object 2 Attempt #2", 
cacheObject2, retrievedObject);
+        // Remove objects
+        ehCache.remove(cacheKey);
+        ehCache.remove(cacheKey_2);
+    }
+    
+    /**
+     * Verify that an object expiration
+     * when it now longer exists in cache.
+     *
+     * @throws Exception
+     */
+    public void testObjectExpiration() throws Exception
+    {
+               Cache ehCache = new Cache("expire", 1, true, false, 2, 2);
+               ehCacheService.getCacheManager().addCache(ehCache);
+        String testString = new String("This is a test");
+        Object retrievedObject = null;
+        Element cacheObject = null;
+        // Create and add Object that expires in 1000 millis (1 second)
+        cacheObject = new Element(cacheKey,testString);
+        assertNotNull("Failed to create a cachable object", cacheObject);
+        ehCache.put(cacheObject);
+        // Try to get un-expired object
+        try
+        {
+            retrievedObject = null;
+            retrievedObject = ehCache.get(cacheKey);
+            assertNotNull("Did not retrieved a cached object", 
retrievedObject);
+            assertEquals("Did not retrieved correct cached object", 
cacheObject, retrievedObject);
+        }
+   
+        catch (Exception e)
+        {
+            throw e;
+        }
+        // Sleep 3000 Millis (3 seconds)
+        Thread.sleep(3000);
+        // Try to get expired object
+        try
+        {
+            retrievedObject = null;
+            retrievedObject = ehCache.get(cacheKey);
+            assertNull(
+                "Retrieved the expired cached object  and did not get expected 
ObjectExpiredException",
+                retrievedObject);
+        }
+        catch (Exception e)
+        {
+            throw e;
+        }
+        // Remove objects
+        ehCache.remove(cacheKey);
+    }   
+}

Modified: jakarta/turbine/fulcrum/trunk/cache/xdocs/changes.xml
URL: 
http://svn.apache.org/viewcvs/jakarta/turbine/fulcrum/trunk/cache/xdocs/changes.xml?rev=386104&r1=386103&r2=386104&view=diff
==============================================================================
--- jakarta/turbine/fulcrum/trunk/cache/xdocs/changes.xml (original)
+++ jakarta/turbine/fulcrum/trunk/cache/xdocs/changes.xml Wed Mar 15 08:10:48 
2006
@@ -2,10 +2,15 @@
 <document>
   <properties>
     <title>Fulcrum Cache</title>
-    <author email="[EMAIL PROTECTED]">Eric Pugh</author>
+    <author email="[EMAIL PROTECTED]">Eric Pugh</author>
   </properties>
 
   <body>
+    <release version="1.1" date="">
+     <action dev="epugh" type="add">
+        Add new EHCacheService based on EHCache from 
http://ehcache.sourceforge.net/
+      </action>    
+    </release>
     <release version="1.0.5" date="2004-11-24">
      <action dev="epugh" type="remove">
         Remove CacheServiceFacade.  It was a code smell.

Modified: jakarta/turbine/fulcrum/trunk/cache/xdocs/index.xml
URL: 
http://svn.apache.org/viewcvs/jakarta/turbine/fulcrum/trunk/cache/xdocs/index.xml?rev=386104&r1=386103&r2=386104&view=diff
==============================================================================
--- jakarta/turbine/fulcrum/trunk/cache/xdocs/index.xml (original)
+++ jakarta/turbine/fulcrum/trunk/cache/xdocs/index.xml Wed Mar 15 08:10:48 2006
@@ -11,6 +11,11 @@
 
   <section name="Overview">
     <p>
+      <b>NEW:</b> There are now two caches, GlobalCacheService and 
EHCacheService.  GlobalCacheService
+      was built on internal foundations and EHCacheService was built on the 
EHCache project from
+      <a href="http://ehcache.sourceforge.net/";>ehcache.sourceforge.net</a>
+    </p>
+    <p>
      This Service functions as a Global Cache.  A global cache is a good
      place to store items that you may need to access often but don't
      necessarily need (or want) to fetch from the database everytime.  A
@@ -28,7 +33,36 @@
     </p>    
   </section>
   
-<section name="Configuration">
+<section name="Configuration for EHCacheService">
+
+    <p>
+      First, here is the role configuration.
+    </p>
+
+    <source>
+    <![CDATA[
+        <role
+            name="org.apache.fulcrum.cache.EHCacheService"
+            shorthand="ehcache"
+            
default-class="org.apache.fulcrum.cache.impl.DefaultEHCacheService"/>
+    ]]>
+    </source>
+
+      <p>
+        And here is the configuration:
+      </p>
+    <source>
+
+    <![CDATA[
+ <ehcache>   
+          
+    </ehcache>
+    ]]>
+    </source>
+
+  </section>
+    
+<section name="Configuration for GlobalCacheService">
 
     <p>
       First, here is the role configuration.

Modified: jakarta/turbine/fulcrum/trunk/cache/xdocs/navigation.xml
URL: 
http://svn.apache.org/viewcvs/jakarta/turbine/fulcrum/trunk/cache/xdocs/navigation.xml?rev=386104&r1=386103&r2=386104&view=diff
==============================================================================
--- jakarta/turbine/fulcrum/trunk/cache/xdocs/navigation.xml (original)
+++ jakarta/turbine/fulcrum/trunk/cache/xdocs/navigation.xml Wed Mar 15 
08:10:48 2006
@@ -9,6 +9,8 @@
             href="http://jakarta.apache.org/turbine/"/>
       <item name="Fulcrum"
             href="http://jakarta.apache.org/turbine/fulcrum/"/>
+      <item name="EHCache"
+            href="http://ehcache.sourceforge.net/"/>               
     </links>
 
     <menu name="Overview">



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to