1 package org.neo.swarm;
2
3 import java.lang.reflect.InvocationTargetException;
4 import java.util.Collection;
5
6 import org.neo.swarm.core.aop.Aspect;
7 import org.neo.swarm.core.aop.AspectContainer;
8 import org.neo.swarm.core.aop.silc.comp.Invocation;
9 import org.neo.swarm.core.aop.silc.comp.Perspective;
10 import org.neo.swarm.core.container.ComponentContainer;
11 import org.neo.swarm.core.container.CreationType;
12
13 /***
14 * @author navery
15 * @author damiang
16 */
17 public class SwarmApplicationContext implements ApplicationContext{
18 private final transient ComponentContainer componentContainer;
19 private final transient AspectContainer aspectContainer;
20 private String uri;
21
22 public SwarmApplicationContext(String uri, ComponentContainer componentContainer, AspectContainer aspectContainer) {
23 this.uri = uri;
24 this.componentContainer = componentContainer;
25 this.aspectContainer = aspectContainer;
26 }
27
28 public void addAspect(Aspect aspect) {
29 aspectContainer.addApplicationAspect(aspect);
30 }
31
32
33 public void addOrthogonalAspect(Aspect aspect) {
34 aspectContainer.addOrthogonalAspect(aspect);
35 }
36
37 public void start() {
38 aspectContainer.start();
39 componentContainer.start();
40 }
41
42 public void addCachedComponent(Object key, Class implementation) {
43 componentContainer.registerComponent(key, implementation, CreationType.SINGLE);
44 }
45
46 public void addComponent(Object key, Class implementation) {
47 componentContainer.registerComponent(key, implementation, CreationType.MULTI);
48 }
49
50 public void addCachedComponent(Object key, Class impl, Object[] args) {
51 componentContainer.registerComponent(key, impl, args, CreationType.SINGLE);
52 }
53
54 public void addComponent(Object key, Class impl, Object[] args) {
55 componentContainer.registerComponent(key, impl, args, CreationType.MULTI);
56 }
57
58 public Object retrieveComponent(Object key) {
59 return componentContainer.getComponent(key);
60 }
61
62 public Object retrieveComponent(Perspective perspective, Object key) {
63 return componentContainer.getComponent(perspective, key);
64 }
65
66 public Collection retrieveComponentsOfType(Class type) {
67 return componentContainer.getComponentsOfType(type);
68 }
69
70 public Collection retrieveComponentsOfType(Perspective perspective, Class type) {
71 return componentContainer.getComponentsOfType(perspective, type);
72 }
73
74 public void removeComponent(Object key) {
75 componentContainer.removeComponent(key);
76 }
77
78
79 /***
80 * Propogate Exception back to caller which should handle serialization response.
81 */
82 public Object inject(Object key, Invocation invocation) throws Exception {
83 Object instance = componentContainer.getComponent(key);
84 try {
85 return invocation.getMethod().invoke(instance,invocation.getArguments());
86 } catch (IllegalAccessException ex) {
87 ex.printStackTrace();
88 throw ex;
89 } catch (InvocationTargetException ex) {
90 ex.printStackTrace();
91 throw ex;
92 } catch (NullPointerException ex) {
93 ex.printStackTrace();
94 throw ex;
95 }
96 }
97
98 public void addPreConstructedComponent(String name, Object object) {
99
100 if (object instanceof AppContextBinding) {
101 AppContextBinding binded = (AppContextBinding) object;
102 binded.bind(this);
103 }
104 componentContainer.registerComponentInstance(name, object);
105 }
106
107 public void stop() {
108 componentContainer.stop();
109 }
110
111 public String getURI() {
112 return this.uri;
113 }
114 }