All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.avanza.astrix.modules.ModuleInjector Maven / Gradle / Ivy

There is a newer version: 2.0.6
Show newest version
/*
 * 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.modules;

import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
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 com.avanza.astrix.modules.ObjectCache.ObjectFactory;
/**
 * 
 * @author Elias Lindholm (elilin)
 *
 */
class ModuleInjector {
	
	private final ObjectCache objectCache = new ObjectCache();
	private final ConcurrentMap, Class> beanBindings = new ConcurrentHashMap<>();
	private final Set> imports = new HashSet<>();
	private final Set> exports = new HashSet<>();
	private final String moduleName;
	private final ModuleInstancePostProcessors postProcessors = new ModuleInstancePostProcessors();
	
	public ModuleInjector(String moduleName) {
		this.moduleName = moduleName;
	}

	public void registerBeanPostProcessor(ModuleInstancePostProcessor beanPostProcessor) {
		this.postProcessors.add(beanPostProcessor);
	}
	
	public  void bind(Class type, Class providerType) {
		this.beanBindings.put(type, providerType);
	}

	public  void bind(Class type, final T provider) {
		beanBindings.put(type, provider.getClass());
		objectCache.create(provider.getClass(), provider);
	}
	
	public  T getBean(Class type, ImportedDependencies importedDependencies) {
		if (!exports.contains(type)) {
			throw new IllegalArgumentException("Non exported bean: " + type);
		}
		return new CircularDependenciesAwareCreation(importedDependencies).create(type);
	}
	
	private class CircularDependenciesAwareCreation {
		
		private final Stack> constructionStack = new Stack<>();
		private final ImportedDependencies importedDependencies;
		
		public CircularDependenciesAwareCreation(ImportedDependencies importedDependencies) {
			this.importedDependencies = importedDependencies;
		}

		public  T create(Class type) {
			final Class resolvedType = resolveBean(type);
			return objectCache.getInstance(resolvedType, new ObjectFactory() {
				@Override
				public T create() throws Exception {
					return doCreate(resolvedType);
				}
			});
		}
		
		public  Class resolveBean(Class type) {
			Class boundType = beanBindings.get(type);
			if (boundType != null) {
				return (Class) boundType;
			}
			if (Modifier.isAbstract(type.getModifiers()) || type.isInterface()) {
				throw new MissingBeanBinding(moduleName, type, constructionStack);
			}
			return type;
		}
		
		private  T doCreate(final Class type) {
			try {
				if (constructionStack.contains(type)) {
					throw new CircularDependency();
				}
				constructionStack.add(type);
				final ClassConstructorFactory factory = new ClassConstructorFactory(type);
				T result = factory.create(new Dependencies() {
					@Override
					public  Collection getAll(Class type) {
						List result = new ArrayList<>();
						if (ModuleInjector.this.imports.contains(type)) {
							result.addAll(importedDependencies.getAll(type));
						}
						if (ModuleInjector.this.beanBindings.containsKey(type)) {
							result.add(create(type));
						}
						return result;
					}
					@Override
					public  E get(Class type) {
						if (ModuleInjector.this.imports.contains(type)) {
							return importedDependencies.get(type);
						}
						return create(type); 
					}
				});
				postProcessors.postProcess(result);
				constructionStack.pop();
				return result;
			} catch (ModulesConfigurationException configurationException) {
				configurationException.addToDependencyTrace(type, moduleName);
				throw configurationException;
			}
		}
	}

	public void destroy() {
		this.objectCache.destroy();
	}

	public void addExport(Class exportedBean) {
		this.exports.add(exportedBean);
	}
	
	public void addImport(Class importedBean) {
		this.imports.add(importedBean);
	}

	public Set> getExports() {
		return this.exports;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy