View Javadoc

1   /*
2    * Created on Feb 3, 2004
3    *
4    */
5   package org.neo.swarm.config;
6   
7   import java.net.InetAddress;
8   import java.net.UnknownHostException;
9   
10  import org.neo.swarm.ApplicationContext;
11  import org.neo.swarm.DefaultSwarmContainer;
12  import org.neo.swarm.SwarmApplicationContext;
13  import org.neo.swarm.SwarmContainer;
14  import org.neo.swarm.core.aop.AspectContainer;
15  import org.neo.swarm.core.aop.silc.SilcAspectContainer;
16  import org.neo.swarm.core.container.ComponentContainer;
17  import org.neo.swarm.core.container.pico.PicoComponentContainer;
18  import org.neo.swarm.core.container.pico.SilcComponentAdapterFactory;
19  import org.neo.swarm.core.container.pico.SilcComponentAdapterFactoryImpl;
20  import org.neo.swarm.interceptor.appContext.GetMethodConvertor;
21  import org.neo.swarm.interceptor.logging.Tracer;
22  import org.neo.swarm.interceptor.tcp.TcpClient;
23  import org.neo.swarm.services.binding.BindingService;
24  import org.neo.swarm.services.binding.StaticRunnable;
25  import org.neo.swarm.services.tcp.ServerCompRouter;
26  import org.neo.swarm.services.tcp.TcpServer;
27  import org.neo.swarm.util.network.tcp.client.TcpSenderFactory;
28  import org.neo.swarm.util.serialize.Serializer;
29  import org.neo.swarm.util.serialize.StandardSerializer;
30  import org.neo.swarm.util.threads.OswegoThreadPool;
31  import org.neo.swarm.util.threads.ThreadPool;
32  
33  import EDU.oswego.cs.dl.util.concurrent.PooledExecutor;
34  
35  /***
36   * Provides simple setup of the SwarmContainer.
37   * @TODO: nga move threadpool to provide generic workerthreads instead of TcpWorkerThreads.... these thread should be shared between all container components (tcpserver, tcpclient etc)
38   *  @author navery
39   */
40  public class SimpleConfig implements Config {
41  
42  	SwarmContainer container;
43  	InetAddress myAddress;
44  	int myPort;
45  	InetAddress otherAddress;
46  	int otherPort;
47  	Serializer serializer;
48  	int threadPoolSize;
49  	boolean enableLogging;
50      String uri;
51  	
52  	
53  	public SimpleConfig(String uri, InetAddress myAddress, int myPort, InetAddress otherAddress, int otherPort, boolean enableLogging) {
54          this.uri = uri;
55  		this.myAddress = myAddress;
56  		this.myPort = myPort;
57  		this.otherAddress = otherAddress;
58  		this.otherPort = otherPort;
59  		this.serializer = new StandardSerializer();
60  		this.threadPoolSize = 3;
61  		this.enableLogging = enableLogging;
62  	}
63  	
64  	/* (non-Javadoc)
65  	 * @see org.neo.swarm.config.Config#setup()
66  	 */
67  	public SwarmContainer setup() throws Exception {
68  		// assemble the SwarmContainer.
69  		// tricky part cause of cyclic relationship...how can something create itself if it hasnt been created. 
70  		SwarmApplicationContext appContexts = createAppContext();
71          container = new DefaultSwarmContainer(appContexts, createAppContext());
72          container.registerServiceInstance(SwarmContainer.class.toString(),container);
73  
74  		setupAppContext(container);
75  		setupTcpService((ApplicationContext) appContexts.retrieveComponent(uri));
76  		setupBindingService();
77  		
78  		return container;
79  	}
80  
81      private SwarmApplicationContext createAppContext() {
82          SilcAspectContainer aspectContainer = new SilcAspectContainer();
83          SilcComponentAdapterFactory factory = new SilcComponentAdapterFactoryImpl(aspectContainer);
84          PicoComponentContainer componentContainer = new PicoComponentContainer(factory);
85          SwarmApplicationContext appContexts = new SwarmApplicationContext(uri, componentContainer, aspectContainer);
86          return appContexts;
87      }
88  
89  	private void setupAppContext(SwarmContainer container) throws UnknownHostException {
90  		
91  		// Setup the AppContext in the first container
92          AspectContainer aspects = new SilcAspectContainer();
93          ComponentContainer compContainer = createComponentContainer(aspects);
94  		container.registerServiceComponent(uri , SwarmApplicationContext.class, new Object [] { uri, compContainer, aspects});
95  		ApplicationContext ac = (ApplicationContext) container.lookupService(uri);
96  		container.registerAppContext(uri, ac);
97          ac.addPreConstructedComponent(uri, ac);
98  
99  		// Setup a RemotePerspective and Aspects for the AppContext
100 		ac.addAspect(new Tracer(this.enableLogging));
101 		ac.addAspect(new GetMethodConvertor());
102 		ac.addAspect(new TcpClient(myAddress, myPort, new TcpSenderFactory(), this.serializer));
103 		ac.addPreConstructedComponent("container", container);
104 	}
105 
106     private ComponentContainer createComponentContainer(AspectContainer aspects) {
107         return new PicoComponentContainer(new SilcComponentAdapterFactoryImpl(aspects));
108     }
109 
110 	private void setupTcpService(ApplicationContext context) {
111 		container.registerServiceComponent(StandardSerializer.class);
112 		container.registerServiceComponent("scr", ServerCompRouter.class, new Object [] {context,container.lookupService(StandardSerializer.class)});        
113     	container.registerServiceComponent("threadpool", OswegoThreadPool.class, new Object[] { new PooledExecutor(), new Integer(1), new Integer(this.threadPoolSize)});
114 		container.registerServiceComponent("tcpServer", TcpServer.class, new Object [] { myAddress, new Integer(myPort), container.lookupService("scr"), container.lookupService("threadpool")} );
115 	}
116 	
117 	private void setupBindingService() throws Exception {
118         StaticRunnable sr = new StaticRunnable((ApplicationContext) container.getComponent(uri), null, this.serializer, 1500);
119         sr.addLocation(otherAddress, otherPort);        
120         BindingService staticBinding = new BindingService("StaticBindingService", sr, (ThreadPool) container.lookupService("threadpool")); 
121         container.registerServiceInstance("StaticBinding", staticBinding);
122 	}
123 }