Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package org.infinispan.factories;
import org.infinispan.IllegalLifecycleStateException;
import org.infinispan.commons.CacheConfigurationException;
import org.infinispan.commons.CacheException;
import org.infinispan.commons.api.Lifecycle;
import org.infinispan.commons.util.ReflectionUtil;
import org.infinispan.commons.util.Util;
import org.infinispan.configuration.cache.Configuration;
import org.infinispan.factories.annotations.DefaultFactoryFor;
import org.infinispan.factories.annotations.Inject;
import org.infinispan.factories.annotations.SurvivesRestarts;
import org.infinispan.factories.components.ComponentMetadata;
import org.infinispan.factories.components.ComponentMetadataRepo;
import org.infinispan.factories.scopes.Scope;
import org.infinispan.factories.scopes.Scopes;
import org.infinispan.lifecycle.ComponentStatus;
import org.infinispan.util.TimeService;
import org.infinispan.util.logging.Log;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Stack;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import static org.infinispan.commons.util.ReflectionUtil.invokeAccessibly;
/**
* A registry where components which have been created are stored. Components are stored as singletons, registered
* under a specific name.
*
* Components can be retrieved from the registry using {@link #getComponent(Class)}.
*
* Components can be registered using {@link #registerComponent(Object, Class)}, which will cause any dependencies to be
* wired in as well. Components that need to be created as a result of wiring will be done using {@link
* #getOrCreateComponent(Class)}, which will look up the default factory for the component type (factories annotated
* with the appropriate {@link DefaultFactoryFor} annotation.
*
* Default factories are treated as components too and will need to be wired before being used.
*
* The registry can exist in one of several states, as defined by the {@link org.infinispan.lifecycle.ComponentStatus}
* enumeration. In terms of the cache, state changes in the following manner:
INSTANTIATED - when first
* constructed
CONSTRUCTED - when created using the DefaultCacheFactory
STARTED - when {@link
* org.infinispan.Cache#start()} is called
STOPPED - when {@link org.infinispan.Cache#stop()} is called
*
*
* Cache configuration can only be changed and will only be re-injected if the cache is not in the {@link
* org.infinispan.lifecycle.ComponentStatus#RUNNING} state.
*
* Thread Safety: instances of {@link GlobalComponentRegistry} can be concurrently updated so all
* the write operations are serialized through class intrinsic lock.
*
* @author Manik Surtani
* @author Galder Zamarreño
* @since 4.0
*/
@SurvivesRestarts
@Scope(Scopes.NAMED_CACHE)
public abstract class AbstractComponentRegistry implements Lifecycle, Cloneable {
private final boolean trace = getLog().isTraceEnabled();
private static final String DEPENDENCIES_ENABLE_JVMOPTION = "infinispan.debugDependencies";
/**
* Set the system property
infinispan.debugDependencies
to
true
to enable some extra information to
* errors generated by the component factory.
*/
public static final boolean DEBUG_DEPENDENCIES = Boolean.getBoolean(DEPENDENCIES_ENABLE_JVMOPTION);
private Stack debugStack = DEBUG_DEPENDENCIES ? new Stack() : null;
/**
* Contains class definitions of component factories that can be used to construct certain components
*/
// private Map> defaultFactories = null;
private static final Object NULL_COMPONENT = new Object();
// component and method containers
private final ConcurrentMap componentLookup = new ConcurrentHashMap(1);
protected volatile ComponentStatus state = ComponentStatus.INSTANTIATED;
private static final PrioritizedMethod[] EMPTY_PRIO_METHODS = {};
/**
* Retrieves the state of the registry
*
* @return state of the registry
*/
public ComponentStatus getStatus() {
return state;
}
protected abstract ClassLoader getClassLoader();
protected abstract Log getLog();
public abstract ComponentMetadataRepo getComponentMetadataRepo();
/**
* Wires an object instance with dependencies annotated with the {@link Inject} annotation, creating more components
* as needed based on the Configuration passed in if these additional components don't exist in the {@link
* ComponentRegistry}. Strictly for components that don't otherwise live in the registry and have a lifecycle, such
* as Commands.
*
* @param target object to wire
* @throws CacheConfigurationException if there is a problem wiring the instance
*/
public void wireDependencies(Object target) throws CacheConfigurationException {
try {
Class> targetClass = target.getClass();
ComponentMetadata metadata = getComponentMetadataRepo().findComponentMetadata(targetClass);
if (metadata != null && metadata.getInjectMethods() != null && metadata.getInjectMethods().length != 0) {
// search for anything we need to inject
for (ComponentMetadata.InjectMetadata injectMetadata : metadata.getInjectMethods()) {
Class>[] methodParameters = injectMetadata.getParameterClasses();
if (methodParameters == null) {
if (System.getSecurityManager() == null) {
methodParameters = ReflectionUtil.toClassArray(injectMetadata.getParameters(), getClassLoader());
} else {
methodParameters = AccessController.doPrivileged((PrivilegedExceptionAction[]>) () -> ReflectionUtil.toClassArray(injectMetadata.getParameters(), getClassLoader()));
}
injectMetadata.setParameterClasses(methodParameters);
}
Method method = injectMetadata.getMethod();
if (method == null) {
if (System.getSecurityManager() == null) {
method = ReflectionUtil.findMethod(targetClass, injectMetadata.getMethodName(), injectMetadata.getParameterClasses());
} else {
method = AccessController.doPrivileged((PrivilegedExceptionAction) () -> ReflectionUtil.findMethod(targetClass, injectMetadata.getMethodName(), injectMetadata.getParameterClasses()));
}
injectMetadata.setMethod(method);
}
invokeInjectionMethod(target, injectMetadata);
}
}
} catch (Exception e) {
throw new CacheConfigurationException("Unable to configure component (type: " + target.getClass() + ", instance " + target + ")", e);
}
}
/**
* Registers a component in the registry under the given type, and injects any dependencies needed. If a component
* of this type already exists, it is overwritten.
*
* @param component component to register
* @param type type of component
*/
public synchronized final void registerComponent(Object component, Class> type) {
registerComponent(component, type.getName(), type.equals(component.getClass()));
}
public synchronized final void registerComponent(Object component, String name) {
registerComponent(component, name, name.equals(component.getClass().getName()));
}
public synchronized final void registerComponent(Object component, String name, boolean nameIsFQCN) {
registerComponentInternal(component, name, nameIsFQCN);
}
protected synchronized final void registerNonVolatileComponent(Object component, String name) {
registerComponentInternal(component, name, false);
}
protected synchronized final void registerNonVolatileComponent(Object component, Class> type) {
registerComponentInternal(component, type.getName(), true);
}
protected synchronized void registerComponentInternal(Object component, String name, boolean nameIsFQCN) {
if (state.isStopping() || state.isTerminated()) {
throw new IllegalLifecycleStateException("Trying to register a component after stopping: " + name);
}
if (component == null)
throw new NullPointerException("Cannot register a null component under name [" + name + "]");
Component old = componentLookup.get(name);
if (old != null) {
// if they are equal don't bother
if (old.instance.equals(component)) {
getLog().tracef("Attempting to register a component equal to one that already exists under the same name (%s). Not doing anything.", name);
return;
}
}
Component c;
if (old != null) {
getLog().tracef("Replacing old component %s with new instance %s", old, component);
old.instance = component;
old.methodsScanned = false;
c = old;
} else {
c = new Component();
c.name = name;
c.instance = component;
componentLookup.put(name, c);
}
c.metadata = getComponentMetadataRepo().findComponentMetadata(component.getClass());
try {
c.buildInjectionMethodsList();
} catch (ClassNotFoundException cnfe) {
throw new CacheException("Error injecting dependencies for component " + name, cnfe);
}
// inject dependencies for this component
// we inject dependencies only after the component is already in the map to support cyclical dependencies
c.injectDependencies();
if (old == null) getLog().tracef("Registering component %s under name %s", c, name);
if (state == ComponentStatus.RUNNING) {
populateLifeCycleMethods(c);
try {
invokeStartMethods(Arrays.asList(c.startMethods));
} catch (Throwable t) {
// the component hasn't started properly, remove its registration
componentLookup.remove(name);
// the caller will log the exception
handleLifecycleTransitionFailure(t);
}
}
}
@SuppressWarnings("unchecked")
private void invokeInjectionMethod(Object o, ComponentMetadata.InjectMetadata injectMetadata) {
Class>[] dependencies = injectMetadata.getParameterClasses();
if (dependencies.length > 0) {
Object[] params = new Object[dependencies.length];
if (trace)
getLog().tracef("Injecting dependencies for method [%s] on an instance of [%s].", injectMetadata,
o.getClass().getName());
for (int i = 0; i < dependencies.length; i++) {
String name = injectMetadata.getParameterName(i);
boolean nameIsFQCN = !injectMetadata.isParameterNameSet(i);
params[i] = getOrCreateComponent(dependencies[i], name, nameIsFQCN);
}
if (System.getSecurityManager() == null) {
invokeAccessibly(o, injectMetadata.getMethod(), params);
} else {
AccessController.doPrivileged((PrivilegedAction