[jboss-cvs] JBossAS SVN: r66009 - in projects/ejb3/trunk/ejb3-cache/src: main/java/org/jboss/ejb3/cache/impl and 3 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Wed Oct 10 09:57:13 EDT 2007
Author: wolfc
Date: 2007-10-10 09:57:13 -0400 (Wed, 10 Oct 2007)
New Revision: 66009
Added:
projects/ejb3/trunk/ejb3-cache/src/main/java/org/jboss/ejb3/cache/LongevityCache.java
projects/ejb3/trunk/ejb3-cache/src/main/java/org/jboss/ejb3/cache/impl/SimpleLongevityCache.java
projects/ejb3/trunk/ejb3-cache/src/test/java/org/jboss/ejb3/test/cache/invocation/
projects/ejb3/trunk/ejb3-cache/src/test/java/org/jboss/ejb3/test/cache/invocation/LongevityUnitTestCase.java
projects/ejb3/trunk/ejb3-cache/src/test/java/org/jboss/ejb3/test/cache/invocation/MockIdentifiable.java
projects/ejb3/trunk/ejb3-cache/src/test/java/org/jboss/ejb3/test/cache/invocation/MockStatefulObjectFactory.java
projects/ejb3/trunk/ejb3-cache/src/test/java/org/jboss/ejb3/test/passivation/LongevityPassivationUnitTestCase.java
Log:
LongevityCache for using objects multiple times
Added: projects/ejb3/trunk/ejb3-cache/src/main/java/org/jboss/ejb3/cache/LongevityCache.java
===================================================================
--- projects/ejb3/trunk/ejb3-cache/src/main/java/org/jboss/ejb3/cache/LongevityCache.java (rev 0)
+++ projects/ejb3/trunk/ejb3-cache/src/main/java/org/jboss/ejb3/cache/LongevityCache.java 2007-10-10 13:57:13 UTC (rev 66009)
@@ -0,0 +1,43 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.cache;
+
+/**
+ * An longevity cache keeps hold over an object for use with multiple operations.
+ *
+ * Thus where a normal cache only allows: get, release, get, release.
+ * The longevity cache allows: get, finished, get, finished, release.
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public interface LongevityCache<T extends Identifiable> extends Cache<T>
+{
+ /**
+ * Signal the finish of the current operation on the object.
+ * The object will remain in use, but is ready to be used
+ * for a new operation.
+ *
+ * @param obj
+ */
+ void finished(T obj);
+}
Property changes on: projects/ejb3/trunk/ejb3-cache/src/main/java/org/jboss/ejb3/cache/LongevityCache.java
___________________________________________________________________
Name: svn:keywords
+ Author Date Id Revision
Name: svn:eol-style
+ native
Added: projects/ejb3/trunk/ejb3-cache/src/main/java/org/jboss/ejb3/cache/impl/SimpleLongevityCache.java
===================================================================
--- projects/ejb3/trunk/ejb3-cache/src/main/java/org/jboss/ejb3/cache/impl/SimpleLongevityCache.java (rev 0)
+++ projects/ejb3/trunk/ejb3-cache/src/main/java/org/jboss/ejb3/cache/impl/SimpleLongevityCache.java 2007-10-10 13:57:13 UTC (rev 66009)
@@ -0,0 +1,148 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.cache.impl;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.ejb.NoSuchEJBException;
+
+import org.jboss.ejb3.cache.Cache;
+import org.jboss.ejb3.cache.Identifiable;
+import org.jboss.ejb3.cache.LongevityCache;
+
+/**
+ * Comment
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class SimpleLongevityCache<T extends Identifiable> implements LongevityCache<T>
+{
+ private Cache<T> delegate;
+
+ private Map<Object, Entry> cache;
+
+ private static enum State { FINISHED, IN_OPERATION };
+
+ private class Entry
+ {
+ long lastUsed;
+ T obj;
+ State state;
+
+ Entry(T obj)
+ {
+ assert obj != null : "obj is null";
+
+ this.lastUsed = System.currentTimeMillis();
+ this.obj = obj;
+ this.state = State.IN_OPERATION;
+ }
+ }
+
+ public SimpleLongevityCache(Cache<T> delegate)
+ {
+ assert delegate != null : "delegate is null";
+
+ this.delegate = delegate;
+ this.cache = new HashMap<Object, Entry>();
+ }
+
+ public void finished(T obj)
+ {
+ synchronized (cache)
+ {
+ Entry entry = cache.get(obj.getId());
+ if(entry.state != State.IN_OPERATION)
+ throw new IllegalStateException("entry " + entry + " is not in operation");
+ entry.state = State.FINISHED;
+ entry.lastUsed = System.currentTimeMillis();
+ }
+ }
+
+ public T create(Class<?>[] initTypes, Object[] initValues)
+ {
+ T obj = delegate.create(initTypes, initValues);
+ Entry entry = new Entry(obj);
+ synchronized (cache)
+ {
+ cache.put(obj.getId(), entry);
+ }
+ return obj;
+ }
+
+ public T get(Object key) throws NoSuchEJBException
+ {
+ synchronized (cache)
+ {
+ Entry entry = cache.get(key);
+ if(entry == null)
+ {
+ T obj = delegate.get(key);
+ entry = new Entry(obj);
+ cache.put(obj.getId(), entry);
+ return obj;
+ }
+ if(entry.state != State.FINISHED)
+ throw new IllegalStateException("entry " + entry + " is not finished");
+ entry.state = State.IN_OPERATION;
+ entry.lastUsed = System.currentTimeMillis();
+ return entry.obj;
+ }
+ }
+
+ public T peek(Object key) throws NoSuchEJBException
+ {
+ // This is the fastest
+ return delegate.peek(key);
+ }
+
+ public void release(T obj)
+ {
+ synchronized (cache)
+ {
+ Object key = obj.getId();
+ Entry entry = cache.get(key);
+ if(entry.state != State.FINISHED)
+ throw new IllegalStateException("entry " + entry + " is not finished");
+ delegate.release(obj);
+ cache.remove(key);
+ }
+ }
+
+ public void remove(Object key)
+ {
+ // Note that the object is not in my cache at this point.
+ delegate.remove(key);
+ }
+
+ public void start()
+ {
+ delegate.start();
+ }
+
+ public void stop()
+ {
+ delegate.stop();
+ }
+}
Property changes on: projects/ejb3/trunk/ejb3-cache/src/main/java/org/jboss/ejb3/cache/impl/SimpleLongevityCache.java
___________________________________________________________________
Name: svn:keywords
+ Author Date Id Revision
Name: svn:eol-style
+ native
Added: projects/ejb3/trunk/ejb3-cache/src/test/java/org/jboss/ejb3/test/cache/invocation/LongevityUnitTestCase.java
===================================================================
--- projects/ejb3/trunk/ejb3-cache/src/test/java/org/jboss/ejb3/test/cache/invocation/LongevityUnitTestCase.java (rev 0)
+++ projects/ejb3/trunk/ejb3-cache/src/test/java/org/jboss/ejb3/test/cache/invocation/LongevityUnitTestCase.java 2007-10-10 13:57:13 UTC (rev 66009)
@@ -0,0 +1,131 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.test.cache.invocation;
+
+import org.jboss.ejb3.cache.Cache;
+import org.jboss.ejb3.cache.LongevityCache;
+import org.jboss.ejb3.cache.StatefulObjectFactory;
+import org.jboss.ejb3.cache.impl.EntryStateCache;
+import org.jboss.ejb3.cache.impl.SimpleLongevityCache;
+
+import junit.framework.TestCase;
+
+/**
+ * The release of a bean is at tx completion. In the mean time
+ * I can have multiple invocations on a bean.
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class LongevityUnitTestCase extends TestCase
+{
+ private Cache<MockIdentifiable> delegate;
+ private LongevityCache<MockIdentifiable> cache;
+ private Object key;
+
+ /**
+ * After setUp you have a delegate, a cache and a key.
+ */
+ @Override
+ protected void setUp() throws Exception
+ {
+ super.setUp();
+
+ StatefulObjectFactory<MockIdentifiable> factory = new MockStatefulObjectFactory();
+ this.delegate = new EntryStateCache<MockIdentifiable>(factory);
+ this.cache = new SimpleLongevityCache<MockIdentifiable>(delegate);
+ cache.start();
+
+ MockIdentifiable bean = cache.create(null, null);
+ this.key = bean.getId();
+ cache.finished(bean);
+ cache.release(bean);
+ }
+
+ @Override
+ protected void tearDown() throws Exception
+ {
+ super.tearDown();
+ }
+
+ public void test1()
+ {
+ // tx.begin();
+
+ MockIdentifiable bean = cache.get(key);
+
+ // blah blah
+
+ cache.finished(bean);
+ bean = null;
+
+ // ...
+
+ // next invocation
+
+ bean = cache.get(key);
+
+ // blah blah 2
+
+ cache.finished(bean);
+
+ // tx.commit() or rollback()
+
+ cache.release(bean);
+ }
+
+ public void testDoubleGet()
+ {
+ // start
+
+ MockIdentifiable bean = cache.get(key);
+ assertNotNull(bean);
+
+ try
+ {
+ cache.get(key);
+ fail("expected IllegalStateException");
+ }
+ catch(IllegalStateException e)
+ {
+ // okay
+ }
+ }
+
+ public void testReleaseBeforeFinish()
+ {
+ // start
+
+ MockIdentifiable bean = cache.get(key);
+ assertNotNull(bean);
+
+ try
+ {
+ cache.release(bean);
+ fail("expected IllegalStateException");
+ }
+ catch(IllegalStateException e)
+ {
+ // okay
+ }
+ }
+}
Property changes on: projects/ejb3/trunk/ejb3-cache/src/test/java/org/jboss/ejb3/test/cache/invocation/LongevityUnitTestCase.java
___________________________________________________________________
Name: svn:keywords
+ Author Date Id Revision
Name: svn:eol-style
+ native
Added: projects/ejb3/trunk/ejb3-cache/src/test/java/org/jboss/ejb3/test/cache/invocation/MockIdentifiable.java
===================================================================
--- projects/ejb3/trunk/ejb3-cache/src/test/java/org/jboss/ejb3/test/cache/invocation/MockIdentifiable.java (rev 0)
+++ projects/ejb3/trunk/ejb3-cache/src/test/java/org/jboss/ejb3/test/cache/invocation/MockIdentifiable.java 2007-10-10 13:57:13 UTC (rev 66009)
@@ -0,0 +1,53 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.test.cache.invocation;
+
+import org.jboss.ejb3.cache.Identifiable;
+
+/**
+ * Comment
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class MockIdentifiable implements Identifiable
+{
+ private static volatile long currentId = 0;
+
+ private long id;
+
+ public MockIdentifiable()
+ {
+ this.id = ++currentId;
+ }
+
+ public Object getId()
+ {
+ return id;
+ }
+
+ @Override
+ public String toString()
+ {
+ return super.toString() + "{id=" + id + "}";
+ }
+}
Property changes on: projects/ejb3/trunk/ejb3-cache/src/test/java/org/jboss/ejb3/test/cache/invocation/MockIdentifiable.java
___________________________________________________________________
Name: svn:keywords
+ Author Date Id Revision
Name: svn:eol-style
+ native
Added: projects/ejb3/trunk/ejb3-cache/src/test/java/org/jboss/ejb3/test/cache/invocation/MockStatefulObjectFactory.java
===================================================================
--- projects/ejb3/trunk/ejb3-cache/src/test/java/org/jboss/ejb3/test/cache/invocation/MockStatefulObjectFactory.java (rev 0)
+++ projects/ejb3/trunk/ejb3-cache/src/test/java/org/jboss/ejb3/test/cache/invocation/MockStatefulObjectFactory.java 2007-10-10 13:57:13 UTC (rev 66009)
@@ -0,0 +1,43 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.test.cache.invocation;
+
+import org.jboss.ejb3.cache.StatefulObjectFactory;
+
+/**
+ * Comment
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class MockStatefulObjectFactory implements StatefulObjectFactory<MockIdentifiable>
+{
+ public MockIdentifiable create(Class<?>[] initTypes, Object[] initValues)
+ {
+ return new MockIdentifiable();
+ }
+
+ public void destroy(MockIdentifiable obj)
+ {
+ }
+
+}
Property changes on: projects/ejb3/trunk/ejb3-cache/src/test/java/org/jboss/ejb3/test/cache/invocation/MockStatefulObjectFactory.java
___________________________________________________________________
Name: svn:keywords
+ Author Date Id Revision
Name: svn:eol-style
+ native
Added: projects/ejb3/trunk/ejb3-cache/src/test/java/org/jboss/ejb3/test/passivation/LongevityPassivationUnitTestCase.java
===================================================================
--- projects/ejb3/trunk/ejb3-cache/src/test/java/org/jboss/ejb3/test/passivation/LongevityPassivationUnitTestCase.java (rev 0)
+++ projects/ejb3/trunk/ejb3-cache/src/test/java/org/jboss/ejb3/test/passivation/LongevityPassivationUnitTestCase.java 2007-10-10 13:57:13 UTC (rev 66009)
@@ -0,0 +1,81 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.test.passivation;
+
+import org.jboss.ejb3.cache.LongevityCache;
+import org.jboss.ejb3.cache.impl.FileObjectStore;
+import org.jboss.ejb3.cache.impl.SimpleLongevityCache;
+import org.jboss.ejb3.cache.impl.SimplePassivatingCache;
+import org.jboss.ejb3.test.cache.common.CacheTestCase;
+
+/**
+ * Test the passivation on a longevity cache.
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class LongevityPassivationUnitTestCase extends CacheTestCase
+{
+ public void test1() throws Exception
+ {
+ MockBeanContainer container = new MockBeanContainer();
+ FileObjectStore<MockBeanContext> store = new FileObjectStore<MockBeanContext>();
+ store.setStorageDirectory("./target/tmp/passivation");
+ store.start();
+ SimplePassivatingCache<MockBeanContext> delegate = new SimplePassivatingCache<MockBeanContext>(container, container, store);
+ delegate.setName("MockBeanContainer");
+ delegate.setSessionTimeout(1);
+ LongevityCache<MockBeanContext> cache = new SimpleLongevityCache<MockBeanContext>(delegate);
+ cache.start();
+
+ MockBeanContext obj = cache.create(null, null);
+ Object key = obj.getId();
+
+ cache.finished(obj);
+
+ cache.release(obj);
+ obj = null;
+
+ wait(container);
+
+ assertEquals("MockBeanContext should have been passivated", 1, container.passivations);
+
+ obj = cache.get(key);
+
+ assertEquals("MockBeanContext should have been activated", 1, container.activations);
+
+ cache.finished(obj);
+
+ sleep(3000);
+
+ assertEquals("MockBeanContext should not have been passivated", 1, container.passivations);
+
+ cache.release(obj);
+ obj = null;
+
+ wait(container);
+
+ assertEquals("MockBeanContext should have been passivated", 2, container.passivations);
+
+ cache.remove(key);
+ }
+}
Property changes on: projects/ejb3/trunk/ejb3-cache/src/test/java/org/jboss/ejb3/test/passivation/LongevityPassivationUnitTestCase.java
___________________________________________________________________
Name: svn:keywords
+ Author Date Id Revision
Name: svn:eol-style
+ native
More information about the jboss-cvs-commits
mailing list