Clover icon

sunshower-core

  1. Project Clover database Fri Apr 6 2018 03:27:42 UTC
  2. Package io.sunshower.service.task.exec

File InjectionContext.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

16
47
7
1
129
109
17
0.36
6.71
7
2.43

Classes

Class Line # Actions
InjectionContext 19 47 17
0.914285791.4%
 

Contributing tests

This file is covered by 12 tests. .

Source view

1    package io.sunshower.service.task.exec;
2   
3    import io.sunshower.common.Identifier;
4    import io.sunshower.service.task.Context;
5    import io.sunshower.service.task.ElementDescriptor;
6    import io.sunshower.service.task.TaskContext;
7    import java.lang.reflect.Field;
8    import java.util.*;
9    import org.apache.commons.lang.StringUtils;
10    import org.apache.tinkerpop.gremlin.structure.Vertex;
11    import org.apache.tinkerpop.gremlin.structure.VertexProperty;
12    import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
13    import org.springframework.aop.framework.Advised;
14    import org.springframework.aop.support.AopUtils;
15    import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
16    import org.springframework.context.ApplicationContext;
17   
18    /** Created by haswell on 3/27/17. */
 
19    class InjectionContext {
20   
21    private final TinkerGraph graph;
22    private final TaskContext context;
23    private final ElementDescriptor descriptor;
24    private final ApplicationContext applicationContext;
25    private final Map<String, InjectionSite> namedSites;
26    private final Map<Class<?>, InjectionSite> typedSites;
27   
 
28  147 toggle InjectionContext(
29    TaskContext context,
30    ElementDescriptor descriptor,
31    TinkerGraph graph,
32    ApplicationContext applicationContext) {
33  149 this.graph = graph;
34  149 this.context = context;
35  152 this.descriptor = descriptor;
36  152 this.namedSites = new HashMap<>();
37  152 this.typedSites = new HashMap<>();
38  151 this.applicationContext = applicationContext;
39    }
40   
 
41  147 toggle public Object invoke() {
42  147 try {
43  152 final Class<?> type = descriptor.getType();
44  149 AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory();
45   
46  151 Object instance =
47    resolveTarget(beanFactory.createBean(type, AutowireCapableBeanFactory.AUTOWIRE_NO, true));
48   
49  144 final Set<InjectionSite> dependencies = new HashSet<>();
50   
51  443 for (Class<?> ct = type; ct != null; ct = ct.getSuperclass()) {
52  290 for (Field field : ct.getDeclaredFields()) {
53  582 if (field.isAnnotationPresent(Context.class)) {
54  593 String fieldName = field.getName();
55  593 String name = resolveName(field, field.getAnnotation(Context.class));
56   
57  590 Field actualField = ct.getDeclaredField(fieldName);
58   
59  578 final InjectionSite site =
60    new InjectionSite(
61    context, fieldName, name, actualField, instance, graph, descriptor, this);
62  586 dependencies.add(site);
63    }
64    }
65    }
66   
67  148 resolve(dependencies);
68   
69  148 return instance;
70    } catch (Exception ex) {
71  0 throw new RuntimeException(ex);
72    }
73    }
74   
 
75  148 toggle private Object resolveTarget(Object bean) throws Exception {
76  144 if (AopUtils.isAopProxy(bean) && bean instanceof Advised) {
77  5 return ((Advised) bean).getTargetSource().getTarget();
78    }
79  145 return bean;
80    }
81   
 
82  139 toggle private void resolve(Set<InjectionSite> dependencies) {
83  143 final Iterator<InjectionSite> deps = dependencies.iterator();
84  740 while (deps.hasNext()) {
85  596 final InjectionSite next = deps.next();
86  594 if (next.resolveAndInject()) {
87  309 deps.remove();
88    }
89    }
90   
91  153 searchAndResolve(dependencies);
92    }
93   
 
94  149 toggle private void searchAndResolve(Set<InjectionSite> dependencies) {
95  151 final Map<Class<?>, Object> resolved = new HashMap<>();
96  151 resolveDependencies(descriptor.getId(), resolved);
97   
98  148 for (InjectionSite site : dependencies) {
99  291 site.inject(resolved);
100    }
101    }
102   
 
103  492 toggle private void resolveDependencies(Identifier id, Map<Class<?>, Object> resolved) {
104  493 graph
105    .traversal()
106    .V(id)
107    .inE()
108    .forEachRemaining(
109    e -> {
110  348 Vertex vertex = e.outVertex();
111  352 VertexProperty<Object> result = vertex.property("result");
112  348 if (result.isPresent()) {
113  353 Object value = result.value();
114  353 if (value != null) {
115  352 Class<?> vtype = value.getClass();
116  354 resolved.put(vtype, result.value());
117    }
118    }
119  349 resolveDependencies((Identifier) vertex.id(), resolved);
120    });
121    }
122   
 
123  581 toggle private String resolveName(Field field, Context annotation) {
124  585 if (StringUtils.isEmpty(annotation.name())) {
125  590 return field.getName();
126    }
127  0 return annotation.name();
128    }
129    }