1 package org.neo.swarm;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Iterator;
6
7 import org.neo.swarm.core.aop.Aspect;
8
9 public class DefaultSwarmContainer implements SwarmContainer {
10 private ApplicationContext appContexts;
11 private ApplicationContext services;
12
13 public DefaultSwarmContainer(ApplicationContext appContexts, ApplicationContext services) {
14 this.appContexts = appContexts;
15 this.services = services;
16 }
17
18 public void registerAppContext(String name, ApplicationContext ac) {
19 if (appContexts.retrieveComponent(name) != null) {
20
21 return;
22 }
23
24 System.out.println("Binding AppContext:" + name);
25 appContexts.addPreConstructedComponent(name, ac);
26 }
27
28 public ApplicationContext getAppContext(String name) {
29 return (ApplicationContext) appContexts.retrieveComponent(name);
30 }
31
32 public Object getComponent(String componentName) throws Exception {
33 Collection collection = appContexts.retrieveComponentsOfType(ApplicationContext.class);
34 for (Iterator iter = collection.iterator(); iter.hasNext(); ) {
35 ApplicationContext newAppContext = (ApplicationContext) iter.next();
36 Object comp = newAppContext.retrieveComponent(componentName);
37 if (comp!=null) {
38 return comp;
39 }
40 }
41 throw new RuntimeException("Unable to find component with key: " + componentName);
42 }
43
44
45
46
47 public Collection getComponentsOfType(Class componentClass) throws Exception {
48 ArrayList results = new ArrayList();
49 Collection collection = appContexts.retrieveComponentsOfType(ApplicationContext.class);
50 for (Iterator iter = collection.iterator(); iter.hasNext(); ) {
51 ApplicationContext newAppContext = (ApplicationContext) iter.next();
52 Collection comps = newAppContext.retrieveComponentsOfType(componentClass);
53 results.addAll(comps);
54 }
55 return results;
56 }
57
58 public void addServiceAspect(Aspect aspect) {
59 services.addAspect(aspect);
60 }
61
62 public void addOrthogonalServiceAspect(Aspect aspect) {
63 services.addOrthogonalAspect(aspect);
64 }
65
66
67 public void start() {
68 appContexts.start();
69 services.start();
70 }
71
72 public void stop() {
73 appContexts.stop();
74 services.stop();
75 }
76
77 public void registerServiceComponent(Class service, Object[] args) {
78 services.addComponent(service, service, args);
79 }
80
81 public void registerServiceComponent(String name, Class service, Object[] args) {
82 services.addCachedComponent(name, service, args);
83 }
84
85 public void registerServiceComponent(Class service) {
86 services.addComponent(service, service);
87 }
88
89 public void registerServiceInstance(String name, Object instance) {
90 services.addPreConstructedComponent(name, instance);
91 }
92
93 public Object lookupService(Object name) {
94 return services.retrieveComponent(name);
95 }
96
97 }