com.presidentio.but.unit.common.DependencyResolver Maven / Gradle / Ivy
The newest version!
/**
* 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.presidentio.but.unit.common;
import com.presidentio.but.unit.common.annotation.BigDataUnit;
import com.presidentio.but.unit.common.exception.BigDataTestFrameworkException;
import com.presidentio.but.unit.common.exception.DestroyUnitException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by presidentio on 04.09.15.
*/
public class DependencyResolver {
private Map units = new HashMap();
private Map> dependencies = new HashMap>();
public void resolve(Object object) throws BigDataTestFrameworkException {
for (Field field : object.getClass().getDeclaredFields()) {
BigDataUnit bigDataUnit = field.getAnnotation(BigDataUnit.class);
if (bigDataUnit != null && !bigDataUnit.global()) {
resolveDependency(field, object);
}
}
}
public void resolveStatic(Object object) throws BigDataTestFrameworkException {
for (Field field : object.getClass().getDeclaredFields()) {
BigDataUnit bigDataUnit = field.getAnnotation(BigDataUnit.class);
if (bigDataUnit != null && bigDataUnit.global()) {
resolveDependency(field, object);
}
}
}
private void resolveDependency(Field field, Object ref) throws BigDataTestFrameworkException {
Class bigDataComponentClass = field.getType();
if (Unit.class.isAssignableFrom(bigDataComponentClass)) {
Unit unit = getUnit(bigDataComponentClass);
setDependency(ref, field, unit);
} else {
throw new BigDataTestFrameworkException(
String.format("%s doesn't implements EmbeddedComponent interface",
bigDataComponentClass.getSimpleName()));
}
}
public void destroy(Object object) throws DestroyUnitException {
for (Field field : object.getClass().getDeclaredFields()) {
BigDataUnit bigDataUnit = field.getAnnotation(BigDataUnit.class);
if (bigDataUnit != null && !bigDataUnit.global()) {
dependencies.get(field.getType()).remove(object);
}
}
garbadgeCollector();
}
public void destroyStatic(Object object) throws DestroyUnitException {
for (Field field : object.getClass().getDeclaredFields()) {
BigDataUnit bigDataUnit = field.getAnnotation(BigDataUnit.class);
if (bigDataUnit != null && bigDataUnit.global()) {
dependencies.get(field.getType()).remove(object);
}
}
garbadgeCollector();
}
private void garbadgeCollector() throws DestroyUnitException {
boolean destroyAtLeastOne = true;
while (destroyAtLeastOne) {
destroyAtLeastOne = false;
List destroyedComponents = new ArrayList();
for (Map.Entry classEmbeddedComponentEntry : units.entrySet()) {
if (dependencies.get(classEmbeddedComponentEntry.getKey()).isEmpty()) {
for (List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy