com.avanza.astrix.beans.factory.AstrixBeanFactory Maven / Gradle / Ivy
/*
* Copyright 2014 Avanza Bank AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.avanza.astrix.beans.factory;
import java.util.Set;
import java.util.Stack;
import javax.annotation.PreDestroy;
import com.avanza.astrix.beans.core.AstrixBeanKey;
import com.avanza.astrix.modules.ObjectCache;
/**
*
* @author Elias Lindholm (elilin)
*
*/
final class AstrixBeanFactory implements BeanFactory {
private final AstrixFactoryBeanRegistry registry = new AstrixFactoryBeanRegistry();
private final ObjectCache beanInstanceCache = new ObjectCache();
@Override
public T getBean(final AstrixBeanKey key) {
return new CircularDependencyAwareAstrixBeanInstances().getBean(key);
}
@PreDestroy
public void destroy() {
this.beanInstanceCache.destroy();
}
/**
* This method returns all beans that was requested during creation of a given bean. Effectively returning
* all direct and transitive dependencies for a given bean.
*
* NOTE: This method will trigger CREATION of the given bean if its not created before.
*
* @param beanKey
* @return
*/
@Override
public Set> getDependencies(AstrixBeanKey extends Object> beanKey) {
return new CircularDependencyAwareAstrixBeanInstances().getBeanInstance(beanKey).getDependencies();
}
/**
* The CircularDependencyAwareAstrixBeans is responsible for:
*
* 1. Detecting circular dependencies.
* 2. Ensure that each created bean is managed by the ObjectCache.
*
*/
private class CircularDependencyAwareAstrixBeanInstances implements AstrixBeans {
private final Stack> constructionStack = new Stack<>();
@Override
public T getBean(final AstrixBeanKey beanKey) {
return getBeanInstance(beanKey).get();
}
public AstrixBeanInstance extends T> getBeanInstance(final AstrixBeanKey beanKey) {
final AstrixBeanKey extends T> resolvedBeanKey = registry.resolveBean(beanKey);
return beanInstanceCache.getInstance(resolvedBeanKey, new ObjectCache.ObjectFactory>() {
@Override
public AstrixBeanInstance extends T> create() throws Exception {
// Bean instance not created, create!
try {
return doCreateBean(resolvedBeanKey);
} catch (MissingBeanProviderException e) {
if (constructionStack.size() > 1) {
// Its a dependency thats missing
throw new MissingBeanDependencyException(e.getBeanType(), constructionStack);
}
// It's the top level bean thats missing, propagate
throw e;
}
}
});
}
private AstrixBeanInstance extends T> doCreateBean(final AstrixBeanKey beanKey) {
if (constructionStack.contains(beanKey)) {
throw new CircularDependency(constructionStack);
}
constructionStack.add(beanKey);
StandardFactoryBean extends T> factoryBean = registry.getFactoryBean(beanKey);
AstrixBeanInstance extends T> instance = createBeanInstance(factoryBean);
constructionStack.pop();
return instance;
}
private AstrixBeanInstance createBeanInstance(StandardFactoryBean factoryBean) {
return AstrixBeanInstance.create(this, factoryBean);
}
@Override
public Set> getBeansOfType(Class type) {
return registry.getBeansOfType(type);
}
}
@Override
public Set> getBeansOfType(Class type) {
return this.registry.getBeansOfType(type);
}
@Override
public void registerFactory(FactoryBean> factory) {
this.registry.registerFactory(factory);
}
}