[jboss-cvs] JBossAS SVN: r79458 - in trunk/spring-int: src/main/org/jboss/spring and 1 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Tue Oct 14 07:35:10 EDT 2008
Author: alesj
Date: 2008-10-14 07:35:10 -0400 (Tue, 14 Oct 2008)
New Revision: 79458
Added:
trunk/spring-int/src/main/org/jboss/spring/cluster/
trunk/spring-int/src/main/org/jboss/spring/cluster/CacheLookup.java
trunk/spring-int/src/main/org/jboss/spring/cluster/CachePostProcessor.java
trunk/spring-int/src/main/org/jboss/spring/cluster/CacheScope.java
Modified:
trunk/spring-int/build.xml
trunk/spring-int/pom.xml
Log:
[JBAS-6088]; add simple Spring bean cache / cluster scope.
Modified: trunk/spring-int/build.xml
===================================================================
--- trunk/spring-int/build.xml 2008-10-14 11:05:35 UTC (rev 79457)
+++ trunk/spring-int/build.xml 2008-10-14 11:35:10 UTC (rev 79458)
@@ -102,6 +102,7 @@
<path refid="jboss.microcontainer.classpath"/>
<path refid="jboss.jboss.vfs.classpath"/>
<path refid="jboss.jboss.javaee.classpath"/>
+ <path refid="jboss.cache.jbosscache.pojo.classpath"/>
</path>
<!-- Where source files live -->
Modified: trunk/spring-int/pom.xml
===================================================================
--- trunk/spring-int/pom.xml 2008-10-14 11:05:35 UTC (rev 79457)
+++ trunk/spring-int/pom.xml 2008-10-14 11:35:10 UTC (rev 79458)
@@ -99,6 +99,20 @@
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.jboss.cache</groupId>
+ <artifactId>jbosscache-pojo</artifactId>
+ <exclusions>
+ <exclusion>
+ <groupId>jboss</groupId>
+ <artifactId>jboss-j2ee</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>org.jboss.aop</groupId>
+ <artifactId>jboss-aop</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
</dependencies>
</project>
\ No newline at end of file
Added: trunk/spring-int/src/main/org/jboss/spring/cluster/CacheLookup.java
===================================================================
--- trunk/spring-int/src/main/org/jboss/spring/cluster/CacheLookup.java (rev 0)
+++ trunk/spring-int/src/main/org/jboss/spring/cluster/CacheLookup.java 2008-10-14 11:35:10 UTC (rev 79458)
@@ -0,0 +1,108 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt 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.spring.cluster;
+
+import org.jboss.cache.CacheException;
+import org.jboss.cache.pojo.PojoCache;
+
+/**
+ * Cache lookup helper class.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public abstract class CacheLookup
+{
+ private String rootName = "/pojos/";
+ protected PojoCache pojoCache;
+
+ protected CacheLookup(PojoCache pojoCache)
+ {
+ this.pojoCache = pojoCache;
+ }
+
+ /**
+ * Get the object out of pojo cache.
+ *
+ * @param name the name to look for
+ * @return cached object or null for some cache exception
+ */
+ protected Object get(String name)
+ {
+ try
+ {
+ return pojoCache.find(rootName + name);
+ }
+ catch (CacheException e)
+ {
+ System.err.println("Exception getting object from PojoCache:" + e);
+ }
+ return null;
+ }
+
+ /**
+ * Put the object into cache.
+ *
+ * @param name the name to put it under
+ * @param object the object to put
+ * @return result of cache put
+ */
+ protected Object put(String name, Object object)
+ {
+ try
+ {
+ return pojoCache.attach(rootName + name, object);
+ }
+ catch (CacheException e)
+ {
+ throw new IllegalArgumentException("Unable to put object to PojoCache: " + e);
+ }
+ }
+
+ /**
+ * Remove object with name param from cache.
+ *
+ * @param name the object's name
+ * @return removed object or null for any cache error
+ */
+ public Object remove(String name)
+ {
+ try
+ {
+ return pojoCache.detach(rootName + name);
+ }
+ catch (CacheException e)
+ {
+ System.err.println("Exception removing object from PojoCache:" + e);
+ return null;
+ }
+ }
+
+ /**
+ * Set the cache root name.
+ *
+ * @param rootName the root name
+ */
+ public void setRootName(String rootName)
+ {
+ this.rootName = rootName;
+ }
+}
Added: trunk/spring-int/src/main/org/jboss/spring/cluster/CachePostProcessor.java
===================================================================
--- trunk/spring-int/src/main/org/jboss/spring/cluster/CachePostProcessor.java (rev 0)
+++ trunk/spring-int/src/main/org/jboss/spring/cluster/CachePostProcessor.java 2008-10-14 11:35:10 UTC (rev 79458)
@@ -0,0 +1,94 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt 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.spring.cluster;
+
+import java.lang.annotation.Annotation;
+
+import org.jboss.cache.pojo.PojoCache;
+import org.jboss.cache.pojo.annotation.Replicable;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
+import org.springframework.beans.factory.config.BeanPostProcessor;
+import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
+
+/**
+ * Pojo cache / cluster post processor.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class CachePostProcessor extends CacheLookup implements BeanFactoryPostProcessor, BeanPostProcessor
+{
+ private String scopeName = "cache";
+
+ public CachePostProcessor(PojoCache pojoCache)
+ {
+ super(pojoCache);
+ }
+
+ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException
+ {
+ beanFactory.registerScope(scopeName, new CacheScope(pojoCache));
+ }
+
+ public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException
+ {
+ Class<?> beanClass = bean.getClass();
+ if (beanClass.isAnnotationPresent(getMarkerAnnotation()))
+ {
+ Object result = get(beanName);
+ if (result != null)
+ {
+ return result;
+ }
+ else
+ {
+ return put(beanName, bean);
+ }
+ }
+ return bean;
+ }
+
+ public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException
+ {
+ return bean;
+ }
+
+ /**
+ * Get the bean cache / cluster marker annotation.
+ *
+ * @return the cache / cluster marker annotation
+ */
+ public Class<? extends Annotation> getMarkerAnnotation()
+ {
+ return Replicable.class;
+ }
+
+ /**
+ * Set scope name.
+ *
+ * @param scopeName the scope name
+ */
+ public void setScopeName(String scopeName)
+ {
+ this.scopeName = scopeName;
+ }
+}
Added: trunk/spring-int/src/main/org/jboss/spring/cluster/CacheScope.java
===================================================================
--- trunk/spring-int/src/main/org/jboss/spring/cluster/CacheScope.java (rev 0)
+++ trunk/spring-int/src/main/org/jboss/spring/cluster/CacheScope.java 2008-10-14 11:35:10 UTC (rev 79458)
@@ -0,0 +1,57 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt 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.spring.cluster;
+
+import org.jboss.cache.pojo.PojoCache;
+import org.springframework.beans.factory.ObjectFactory;
+import org.springframework.beans.factory.config.Scope;
+
+/**
+ * Pojo cache scope.
+ *
+ * It enables plain spring beans to be clustered
+ * via JBoss Pojo cache.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class CacheScope extends CacheLookup implements Scope
+{
+ public CacheScope(PojoCache pojoCache)
+ {
+ super(pojoCache);
+ }
+
+ public String getConversationId()
+ {
+ return null;
+ }
+
+ public Object get(String name, ObjectFactory objectFactory)
+ {
+ Object result = get(name);
+ return (result != null) ? result : put(name, objectFactory.getObject());
+ }
+
+ public void registerDestructionCallback(String string, Runnable runnable)
+ {
+ }
+}
More information about the jboss-cvs-commits
mailing list