org.jboss.as.ee.component.BasicComponent Maven / Gradle / Ivy
/*
* JBoss, Home of Professional Open Source.
* Copyright 2011, Red Hat, Inc., 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.as.ee.component;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import org.jboss.as.ee.logging.EeLogger;
import org.jboss.as.ee.component.interceptors.InvocationType;
import org.jboss.as.naming.ImmediateManagedReference;
import org.jboss.as.naming.ManagedReference;
import org.jboss.as.naming.context.NamespaceContextSelector;
import org.jboss.invocation.Interceptor;
import org.jboss.invocation.InterceptorContext;
import org.jboss.invocation.InterceptorFactory;
import org.jboss.invocation.InterceptorFactoryContext;
import org.jboss.invocation.SimpleInterceptorFactoryContext;
import org.jboss.msc.service.ServiceName;
/**
* A basic component implementation.
*
* @author John Bailey
* @author David M. Lloyd
*/
public class BasicComponent implements Component {
private final String componentName;
private final Class> componentClass;
private final InterceptorFactory postConstruct;
private final InterceptorFactory preDestroy;
private final Map interceptorFactoryMap;
private final NamespaceContextSelector namespaceContextSelector;
private final ServiceName createServiceName;
private volatile boolean gate;
private final AtomicBoolean stopping = new AtomicBoolean();
private Interceptor postConstructInterceptor;
private Interceptor preDestroyInterceptor;
private Map interceptorInstanceMap;
/**
* Construct a new instance.
*
* @param createService the create service which created this component
*/
public BasicComponent(final BasicComponentCreateService createService) {
componentName = createService.getComponentName();
componentClass = createService.getComponentClass();
postConstruct = createService.getPostConstruct();
preDestroy = createService.getPreDestroy();
interceptorFactoryMap = createService.getComponentInterceptors();
namespaceContextSelector = createService.getNamespaceContextSelector();
createServiceName = createService.getServiceName();
}
/**
* {@inheritDoc}
*/
public ComponentInstance createInstance() {
BasicComponentInstance instance = constructComponentInstance(null, true);
return instance;
}
/**
* Wraps an existing object instance in a ComponentInstance, and run the post construct interceptor chain on it.
*
* @param instance The instance to wrap
* @return The new ComponentInstance
*/
public ComponentInstance createInstance(Object instance) {
BasicComponentInstance obj = constructComponentInstance(new ImmediateManagedReference(instance), true);
obj.constructionFinished();
return obj;
}
public void waitForComponentStart() {
if (!gate) {
EeLogger.ROOT_LOGGER.tracef("Waiting for component %s (%s)", componentName, componentClass);
// Block until successful start
synchronized (this) {
if (stopping.get()) {
throw EeLogger.ROOT_LOGGER.componentIsStopped();
}
while (!gate) {
// TODO: check for failure condition
try {
wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw EeLogger.ROOT_LOGGER.componentNotAvailable();
}
}
}
EeLogger.ROOT_LOGGER.tracef("Finished waiting for component %s (%s)", componentName, componentClass);
}
}
/**
* Construct the component instance. Upon return, the object instance should have injections and lifecycle
* invocations completed already.
*
*
* @param instance An instance to be wrapped, or null if a new instance should be created
* @return the component instance
*/
protected BasicComponentInstance constructComponentInstance(ManagedReference instance, boolean invokePostConstruct) {
return constructComponentInstance(instance, invokePostConstruct, Collections.emptyMap());
}
/**
* Construct the component instance. Upon return, the object instance should have injections and lifecycle
* invocations completed already.
*
*
* @param instance An instance to be wrapped, or null if a new instance should be created
* @return the component instance
*/
protected BasicComponentInstance constructComponentInstance(ManagedReference instance, boolean invokePostConstruct, final Map
© 2015 - 2025 Weber Informatics LLC | Privacy Policy