Author: manik.surtani(a)jboss.com
Date: 2008-04-15 11:11:00 -0400 (Tue, 15 Apr 2008)
New Revision: 5570
Modified:
core/trunk/src/main/java/org/jboss/cache/factories/ComponentRegistry.java
core/trunk/src/main/java/org/jboss/cache/interceptors/CallInterceptor.java
core/trunk/src/test/java/org/jboss/cache/loader/CacheLoaderPurgingTest.java
Log:
Updated construction sequence
Modified: core/trunk/src/main/java/org/jboss/cache/factories/ComponentRegistry.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/factories/ComponentRegistry.java 2008-04-15
13:48:32 UTC (rev 5569)
+++ core/trunk/src/main/java/org/jboss/cache/factories/ComponentRegistry.java 2008-04-15
15:11:00 UTC (rev 5570)
@@ -9,8 +9,16 @@
import org.jboss.cache.config.ConfigurationException;
import org.jboss.cache.config.RuntimeConfig;
import static org.jboss.cache.factories.ComponentRegistry.State.*;
-import org.jboss.cache.factories.annotations.*;
-import org.jboss.cache.invocation.*;
+import org.jboss.cache.factories.annotations.CacheInjectionMethods;
+import org.jboss.cache.factories.annotations.ComponentName;
+import org.jboss.cache.factories.annotations.DefaultFactoryFor;
+import org.jboss.cache.factories.annotations.Destroy;
+import org.jboss.cache.factories.annotations.Inject;
+import org.jboss.cache.factories.annotations.Start;
+import org.jboss.cache.factories.annotations.Stop;
+import org.jboss.cache.invocation.CacheData;
+import org.jboss.cache.invocation.CacheLifecycleManager;
+import org.jboss.cache.invocation.InterceptorChain;
import org.jboss.cache.util.BeanUtils;
import org.jboss.cache.util.reflect.CachedMethod;
import org.jboss.cache.util.reflect.ReflectionUtil;
@@ -18,7 +26,12 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
-import java.util.*;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
/**
* A registry where components which have been created are stored. Components are stored
as singletons, registered under
@@ -263,6 +276,11 @@
return getOrCreateComponent(null, componentClass);
}
+ public <T> T getOrCreateComponent(String componentName, Class<T>
componentClass)
+ {
+ return getOrCreateComponent(componentName, componentClass, CONSTRUCTED);
+ }
+
/**
* Retrieves a named component if one exists, and if not, attempts to find a factory
capable of constructing the component
* (factories annotated with the {@link
org.jboss.cache.factories.annotations.DefaultFactoryFor} annotation that is capable
@@ -279,10 +297,11 @@
*
* @param componentName name of component to be created. If null, uses the fully
qualified class name as component name.
* @param componentClass type of component to be retrieved. Should not be null.
+ * @param state state to move component to
* @return a fully wired component instance, or null if one cannot be found or
constructed.
* @throws ConfigurationException if there is a problem with consructing or wiring the
instance.
*/
- public <T> T getOrCreateComponent(String componentName, Class<T>
componentClass)
+ public <T> T getOrCreateComponent(String componentName, Class<T>
componentClass, State state)
{
T component = getComponent(componentName == null ? componentClass.getName() :
componentName, componentClass);
@@ -313,6 +332,10 @@
registerNullComponent(componentNameToUse, componentClass);
}
}
+
+ // guarantee state
+// Component c = componentLookup.get(componentName);
+// if (c != null) c.changeState(state);
return component;
}
@@ -421,7 +444,13 @@
public void unregisterComponent(String name)
{
Component c = componentLookup.remove(name);
- if (c != null) c.changeState(c.state == STARTED ? STOPPED : CONSTRUCTED);
+ if (c != null)
+ {
+ c.changeState(c.state == STARTED ? STOPPED : CONSTRUCTED);
+
+ c.dependencies.clear();
+ c.dependencyFor.clear();
+ }
}
/**
@@ -494,7 +523,7 @@
for (int i = 0; i < parameterTypes.length; i++)
{
- parameters[i] = getComponent(componentsToInject.get(i).name,
parameterTypes[i]);
+ parameters[i] = getOrCreateComponent(componentsToInject.get(i).name,
parameterTypes[i]);
}
Method reflectMethod = method.getMethod();
@@ -639,7 +668,7 @@
cr.changeState(CONSTRUCTED);
bootstrap = new Bootstrap((ClassLoader) deployerClassLoader.instance,
(InterceptorChain) interceptorChain.instance,
- (CacheLifecycleManager)lifeCycleManager.instance,
(CacheData)cacheData.instance, (CacheSPI) spi.instance,
+ (CacheLifecycleManager) lifeCycleManager.instance, (CacheData)
cacheData.instance, (CacheSPI) spi.instance,
(ComponentRegistry) cr.instance, (Configuration) conf.instance);
overallState = null;
@@ -678,10 +707,11 @@
bootstrap.bootstrap();
}
- for (Component c : componentLookup.values())
- {
- c.changeState(state);
- }
+ // copy the component lookup CHM to prevent concurrent modification exceptions
later on, if components need to be
+ // constructed and added to the system on the fly.
+ Set<Component> components = new
HashSet<Component>(componentLookup.values());
+ for (Component c : components) c.changeState(state);
+
overallState = state;
}
@@ -805,23 +835,25 @@
{
if (d != null)
{
- if (d.instance == null)
+
+ //if (d.instance == null)
+ //{
+ // this is a "hollow" component that has not been constructed
yet. Another "constructed" version probably exists in the
+ // componentLookup. Make sure we replace this.
+ // always lookup from the component registry!!
+ Component c = componentLookup.get(d.name);
+ if (increase)
{
- // this is a "hollow" component that has not been
constructed yet. Another "constructed" version probably exists in the
- // componentLookup. Make sure we replace this.
- Component c = componentLookup.get(d.name);
- if (increase)
- {
- dependencies.remove(d);
- dependencies.add(c);
- }
- else
- {
- dependencyFor.remove(d);
- dependencies.add(c);
- }
- d = c;
+ dependencies.remove(d);
+ dependencies.add(c);
}
+ else
+ {
+ dependencyFor.remove(d);
+ dependencies.add(c);
+ }
+ d = c;
+ //}
if (d != null)
{
@@ -858,7 +890,7 @@
destroy();
break;
case CONSTRUCTED:
- // nothing to do here.
+ // do nothing
}
state = newState;
Modified: core/trunk/src/main/java/org/jboss/cache/interceptors/CallInterceptor.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/interceptors/CallInterceptor.java 2008-04-15
13:48:32 UTC (rev 5569)
+++ core/trunk/src/main/java/org/jboss/cache/interceptors/CallInterceptor.java 2008-04-15
15:11:00 UTC (rev 5570)
@@ -14,8 +14,8 @@
import org.jboss.cache.config.Configuration;
import org.jboss.cache.config.Option;
import org.jboss.cache.factories.annotations.Inject;
+import org.jboss.cache.factories.annotations.Start;
import org.jboss.cache.interceptors.base.ChainedInterceptor;
-import org.jboss.cache.loader.CacheLoaderManager;
import org.jboss.cache.transaction.GlobalTransaction;
import org.jboss.cache.transaction.TransactionTable;
@@ -35,19 +35,27 @@
public class CallInterceptor extends ChainedInterceptor
{
- private CacheLoaderManager cacheLoaderManager;
+ // private CacheLoaderManager cacheLoaderManager;
private TransactionTable transactionTable;
private Configuration configuration;
+ private boolean cacheLoadingEnabled;
@Inject
- public void injectDependencies(CacheLoaderManager cacheLoaderManager, TransactionTable
transactionTable,
+ public void injectDependencies(TransactionTable transactionTable,
Configuration configuration)
{
- this.cacheLoaderManager = cacheLoaderManager;
+// this.cacheLoaderManager = cacheLoaderManager;
this.transactionTable = transactionTable;
this.configuration = configuration;
}
+ @Start
+ public void startInterceptor()
+ {
+ cacheLoadingEnabled = configuration.getCacheLoaderConfig() != null &&
+ configuration.getCacheLoaderConfig().getFirstCacheLoaderConfig() != null;
+ }
+
public Object handlePrepareCommand(InvocationContext ctx, PrepareCommand command)
throws Throwable
{
if (trace) log.trace("Suppressing invocation of method
handlePrepareCommand.");
@@ -152,8 +160,8 @@
}
// todo: consolidate cache loader and regular modification lists!!
- if (cacheLoaderManager != null)
- transactionTable.addCacheLoaderModification(gtx, command);
+// if (cacheLoaderManager != null)
+ if (cacheLoadingEnabled) transactionTable.addCacheLoaderModification(gtx,
command);
}
}
return result;
Modified: core/trunk/src/test/java/org/jboss/cache/loader/CacheLoaderPurgingTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/loader/CacheLoaderPurgingTest.java 2008-04-15
13:48:32 UTC (rev 5569)
+++ core/trunk/src/test/java/org/jboss/cache/loader/CacheLoaderPurgingTest.java 2008-04-15
15:11:00 UTC (rev 5570)
@@ -65,7 +65,7 @@
public void testSingleLoaderPurge() throws Exception
{
- cache = (CacheSPI<Object, Object>) new
DefaultCacheFactory().createCache(false);
+ cache = (CacheSPI<Object, Object>) new DefaultCacheFactory<Object,
Object>().createCache(false);
Configuration c = cache.getConfiguration();
c.setCacheLoaderConfig(getSingleCacheLoaderConfig("",
DummySharedInMemoryCacheLoader.class.getName(), "", false, false, false,
true));
cache.start();