KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ve > luz > ica > jackass > daemon > ComponentDeployer


1 /*
2  * Copyright (c) 2003 by The Jackass Team
3  * Licensed under the Open Software License version 2.0
4  */

5 package ve.luz.ica.jackass.daemon;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9 import org.apache.commons.pool.impl.GenericObjectPool;
10 import org.omg.CosNaming.NamingContextExt JavaDoc;
11 import org.omg.PortableServer.POA JavaDoc;
12
13 import ve.luz.ica.jackass.component.ApplicationContext;
14 import ve.luz.ica.jackass.component.ComponentInfo;
15 import ve.luz.ica.jackass.component.ComponentInfoManager;
16 import ve.luz.ica.jackass.component.StatelessContext;
17 import ve.luz.ica.jackass.component.StatelessContextHelper;
18 import ve.luz.ica.jackass.component.StatelessContextImpl;
19 import ve.luz.ica.jackass.component.StatelessContextPOATie;
20 import ve.luz.ica.jackass.deploy.util.Application;
21 import ve.luz.ica.jackass.deploy.util.Component;
22 import ve.luz.ica.jackass.daemon.proxy.ProxyServantFactory;
23 import ve.luz.ica.jackass.deploy.daemon.ProxyInfo;
24 import ve.luz.ica.jackass.deploy.descriptor.ica.PoolSettings;
25 import ve.luz.ica.jackass.deploy.descriptor.ica.StatelessComponentIcaType;
26 import ve.luz.ica.jackass.deploy.descriptor.standard.Properties;
27 import ve.luz.ica.jackass.deploy.descriptor.standard.Property;
28 import ve.luz.ica.jackass.deploy.descriptor.standard.StatelessComponent;
29 import ve.luz.ica.jackass.instantiator.JInstantiator;
30 import ve.luz.ica.jackass.instantiator.StatelessPoolData;
31 import ve.luz.ica.jackass.solver.ComponentProxyManager;
32
33 /**
34  * A ComponentDeployer deploys components
35  * @author Carlos Arévalo
36  */

37 public class ComponentDeployer
38 {
39     private static final Log LOG = LogFactory.getLog(ComponentDeployer.class);
40
41     private POA JavaDoc contextPoa = null;
42     private POA JavaDoc proxyPoa = null;
43
44     private StatelessPoolData statelessPoolData = null;
45
46     /**
47      * Class constructor
48      * @param contextPOA the poa used to create context object references
49      * @param proxyPOA the poa used to create proxy references
50      * @param poolData the pool configuration
51      */

52     public ComponentDeployer(POA JavaDoc contextPOA, POA JavaDoc proxyPOA, StatelessPoolData poolData)
53     {
54         this.contextPoa = contextPOA;
55         this.proxyPoa = proxyPOA;
56         this.statelessPoolData = poolData;
57     }
58
59     /**
60      * Generates the objectID from the application name and the component name
61      * @param appName the application name
62      * @param compName the comonent name
63      * @return the generated object ID
64      */

65     private byte[] getObjectId(String JavaDoc appName, String JavaDoc compName)
66     {
67         String JavaDoc objectId = appName+ComponentProxyManager.COMPONENT_ID_SEPARATOR + compName;
68         return objectId.getBytes();
69     }
70
71     /**
72      * Deploys a component
73      * @param instantiator the instantiator object reference
74      * @param app contains tha application deployment information
75      * @param comp a structure contaning the component deployement information
76      * @param appContext the application context
77      * @param proxyClassLoader the class loader that will be used to load the proxy class
78      * @return the CORBA reference to the created object
79      * @throws Exception thrown if the object cannot be created
80      */

81     ProxyInfo deployComponent(JInstantiator instantiator, Application app, Component comp,
82             ApplicationContext appContext, ClassLoader JavaDoc proxyClassLoader) throws Exception JavaDoc
83     {
84         if (comp.getStandardComponent() instanceof StatelessComponent)
85         {
86             return createStatelessComponent(instantiator, app, comp, appContext, proxyClassLoader);
87         }
88         throw new Exception JavaDoc("Error. Unknown component type");
89     }
90
91     /**
92      * Creates a stateless component
93      * @param instantiator the Instantiator where the CORBA Object will be created
94      * @param app the Application object containing the application deployment data
95      * @param comp the Component object containing the component deployment data
96      * @param appContext the ApplicationContext to be passed to the object
97      * @param proxyClassLoader the ClassLoader that will be used to load the proxy object class
98      * @return a ProxyInfo object containing the component name and the proxy reference
99      * @throws Exception if there is an error during the creation process
100      */

101     private ProxyInfo createStatelessComponent(JInstantiator instantiator, Application app,
102             Component comp, ApplicationContext appContext, ClassLoader JavaDoc proxyClassLoader)
103             throws Exception JavaDoc
104     {
105         if (LOG.isInfoEnabled()) LOG.info("Deploying stateless component " + comp.getName());
106
107         StatelessContext compContext = null;
108         org.omg.CORBA.Object JavaDoc realObject = null;
109         byte[] oid = getObjectId(app.getName(), comp.getName());
110         ComponentInfoManager compInfoManager = ComponentInfoManager.getManager();
111
112         if (LOG.isDebugEnabled())
113         {
114             LOG.debug("Creating object "+comp.getName());
115             LOG.debug("Interface "+comp.getInterface());
116             LOG.debug("Implementation "+comp.getImplementation());
117             LOG.debug("Object ID "+new String JavaDoc(oid));
118         }
119
120         try
121         {
122             // create the component context
123
if (LOG.isDebugEnabled()) LOG.debug("Creating the component context");
124             compContext = this.createStatelessContext(comp, oid, appContext.getRootNamingContext());
125             if (LOG.isDebugEnabled()) LOG.debug("Component context created");
126
127             // load the proxy class
128
String JavaDoc compInterface = comp.getInterface();
129             String JavaDoc corbaInterface = "IDL:" + compInterface.replace('.', '/') + ":1.0";
130             String JavaDoc proxyClassName = compInterface + "Proxy";
131             Class JavaDoc proxyClass = Class.forName(proxyClassName, true, proxyClassLoader);
132
133             // obtain pool data from ica deployment descriptor
134
int maxIdle = this.statelessPoolData.maxIdle;
135             int maxActive = this.statelessPoolData.maxActive;
136             long checkTime = this.statelessPoolData.checkInterval;
137
138             if (LOG.isDebugEnabled()) LOG.debug("Initializing pool params with default values. maxIdle = " +
139                     maxIdle + " maxActive = " + maxActive + " checkTime = " + checkTime);
140             StatelessComponentIcaType icaComp = (StatelessComponentIcaType) comp.getIcaComponent();
141             if (icaComp != null)
142             {
143                 if (LOG.isDebugEnabled()) LOG.debug("Found non standard descriptor for this component");
144
145                 PoolSettings poolSettings = icaComp.getPoolSettings();
146                 if (poolSettings != null)
147                 {
148                     maxIdle = poolSettings.getMaxIdle();
149                     maxActive = poolSettings.getMaxActive();
150                     checkTime = poolSettings.getCheckInterval();
151                     if (LOG.isDebugEnabled())
152                             LOG.debug("Initializing pool params with values from the deployment descriptor. " +
153                             " maxIdle = " + maxIdle + " maxActive = " + maxActive + " checkTime = " + checkTime);
154                 }
155             }
156             StatelessPoolData poolData = new StatelessPoolData(maxActive, maxIdle, checkTime);
157
158             // create the real object
159
// must be undone if component deployment fails
160
realObject = instantiator.createStatelessObject(app.getName(), oid,
161                     corbaInterface, comp.getImplementation(), appContext, compContext, poolData);
162
163             // create the proxy object reference
164
if (LOG.isDebugEnabled()) LOG.debug("Component interface " + corbaInterface);
165             org.omg.CORBA.Object JavaDoc proxyObject = proxyPoa.create_reference_with_id(oid, corbaInterface);
166
167             // create the pool of proxy servants
168
GenericObjectPool proxyPool = new GenericObjectPool(new ProxyServantFactory(realObject, proxyClass),
169                     maxActive, GenericObjectPool.WHEN_EXHAUSTED_BLOCK, 0, maxIdle);
170             proxyPool.setMinEvictableIdleTimeMillis(checkTime);
171
172             // create the ComponentInfo and add it to the table
173
// must be undone if component deployment fails
174
ComponentInfo compInfo = new ComponentInfo(realObject, proxyClass, proxyPool, instantiator);
175             compInfoManager.addComponentInfo(oid, compInfo);
176
177             // create and return the ProxyInfo
178
ProxyInfo proxyInfo = new ProxyInfo(comp.getName(), proxyObject);
179
180             return proxyInfo;
181         }
182         catch (Exception JavaDoc e)
183         {
184             // undo things that must be undone
185
if (compContext != null)
186             {
187                 contextPoa.deactivate_object(oid);
188             }
189             if (realObject != null)
190             {
191                 instantiator.destroyStatelessObject(oid);
192             }
193             compInfoManager.removeComponentInfo(oid);
194
195             throw e;
196         }
197     }
198
199     /**
200      * Creates the StatelesContext CORBA object that will be passed to the component
201      * @param comp the Component object containing the deployment descriptor data
202      * @param oid the object ID (application-name : component-name)
203      * @param rootNameContext the name service root context
204      * @return the Corba reference to the created object
205      * @throws Exception if there is an error during the creation process
206      */

207     private StatelessContext createStatelessContext(Component comp, byte[] oid, NamingContextExt JavaDoc rootNameContext)
208             throws Exception JavaDoc
209     {
210         // create the component context
211
if (LOG.isDebugEnabled()) LOG.debug("Creating the component context");
212
213         Property[] properties = null;
214         Properties propertyList = comp.getProperties();
215         if (propertyList != null)
216         {
217             properties = propertyList.getProperty();
218         }
219
220         StatelessContextImpl contextImpl = new StatelessContextImpl(rootNameContext, properties);
221         StatelessContextPOATie contextServant = new StatelessContextPOATie(contextImpl);
222         org.omg.CORBA.Object JavaDoc contextObj = contextPoa.create_reference_with_id(oid, StatelessContextHelper.id());
223
224         // activate the component context
225
// must be undone if component deployment fails
226
contextPoa.activate_object_with_id(oid, contextServant);
227         StatelessContext compContext = StatelessContextHelper.narrow(contextObj);
228         return compContext;
229     }
230
231     /**
232      * Undeploys a component.
233      * @param app an application object containing the application deployment information
234      * @param comp a Component object contaning the component deployement information
235      * @throws Exception if there is an error during the undeployment process
236      */

237     public void undeployComponent(Application app, Component comp) throws Exception JavaDoc
238     {
239         if (LOG.isInfoEnabled()) LOG.info("Undeploying component " + comp.getName());
240
241         ComponentInfoManager compInfoManager = ComponentInfoManager.getManager();
242
243         String JavaDoc appName = app.getName();
244         String JavaDoc compName = comp.getName();
245         byte[] oid = getObjectId(appName, compName);
246
247         // destroy the component context
248
try
249         {
250             this.contextPoa.deactivate_object(oid);
251         }
252         catch (Exception JavaDoc e)
253         {
254             LOG.warn("The poa hast thrown the following exception ", e);
255         }
256
257         ComponentInfo compInfo = compInfoManager.getComponentInfo(oid);
258         if (compInfo != null)
259         {
260             // remove the ComponentInfo object form the ComponentInfo table
261
// this destroys the pool
262
compInfoManager.removeComponentInfo(oid);
263
264             // tell the instantiator to destroy the real object
265
JInstantiator instantiator = compInfo.getInstantiator();
266             if (comp.getStandardComponent() instanceof StatelessComponent)
267             {
268                 instantiator.destroyStatelessObject(oid);
269             }
270         }
271         else
272         {
273             throw new Exception JavaDoc("No ComponentInfo found for " + compName);
274         }
275     }
276
277 }
278
Popular Tags