KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.InputStream JavaDoc;
8 import java.io.InputStreamReader JavaDoc;
9 import java.io.Reader JavaDoc;
10 import java.util.ArrayList JavaDoc;
11 import java.util.HashSet JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.Properties JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19 import org.omg.CORBA.ORB JavaDoc;
20 import org.omg.CORBA.Object JavaDoc;
21 import org.omg.CORBA.Request JavaDoc;
22 import org.omg.CORBA.UserException JavaDoc;
23 import org.omg.CosNaming.NamingContextExt JavaDoc;
24 import org.omg.CosNaming.NamingContextExtHelper JavaDoc;
25 import org.omg.CosNaming.NamingContextPackage.AlreadyBound JavaDoc;
26 import org.omg.PortableServer.POA JavaDoc;
27
28 import ve.luz.ica.jackass.deploy.DeployerPOA;
29 import ve.luz.ica.jackass.deploy.UnableToDeployException;
30 import ve.luz.ica.jackass.deploy.UnableToUndeployException;
31 import ve.luz.ica.jackass.deploy.daemon.NodeDeployer;
32 import ve.luz.ica.jackass.deploy.daemon.NodeDeployerHelper;
33 import ve.luz.ica.jackass.deploy.daemon.ProxyInfo;
34 import ve.luz.ica.jackass.deploy.daemon.ProxyInfoListHelper;
35 import ve.luz.ica.jackass.deploy.descriptor.ica.IcaJackassApplication;
36 import ve.luz.ica.jackass.deploy.descriptor.standard.JackassApplication;
37 import ve.luz.ica.jackass.deploy.util.Application;
38 import ve.luz.ica.jackass.deploy.util.Component;
39 import ve.luz.ica.jackass.ref.RefUtil;
40 import ve.luz.ica.jackass.solver.ComponentProxyManager;
41 import ve.luz.ica.jackass.util.ConfigurationManager;
42 import ve.luz.ica.remoteio.FileUtil;
43 import ve.luz.ica.remoteio.RemoteFile;
44 import ve.luz.ica.remoteio.RemoteFileHelper;
45 import ve.luz.ica.util.ZipUtil;
46
47 /**
48  * A Deployer deploys applications given the archive file containing
49  * the deployment descriptors and the binary executables
50  * @author Carlos Arévalo
51  */

52 public class DeployerImpl extends DeployerPOA
53 {
54     private static final Log LOG = LogFactory.getLog(DeployerImpl.class);
55
56     private static final String JavaDoc UNDEPLOY_OPERATION = "undeploy";
57     private static final String JavaDoc NAME_SEPARATOR = "/";
58     private static final String JavaDoc DEPLOY_OPERATION = "deploy";
59     private static final String JavaDoc DEFAULT_TEMP_DIR = "temp";
60
61     //private POA poa;
62
private ORB JavaDoc orb;
63     private NamingContextExt JavaDoc rootContext;
64     private ComponentProxyManager componentProxyManager;
65     private String JavaDoc tempDirectory = null;
66     private NodeDeployerManager nodeDeployerManager;
67     private ApplicationManager applicationManager;
68
69     /**
70      * Class constructor
71      * @param theOrb a reference to the orb
72      * @param thePoa the poa where the deployer was created
73      * @param rootCtx the name service root context
74      * @param proxyManager a ProxyManager to store the proxy references associated
75      * to the component ID
76      * @param nodeDepManager a NodeDeployerManager used to obtain the references of the
77      * node deployers corresponding to a given group
78      */

79     public DeployerImpl(ORB JavaDoc theOrb, POA JavaDoc thePoa, NamingContextExt JavaDoc rootCtx, ComponentProxyManager proxyManager,
80             NodeDeployerManager nodeDepManager)
81     {
82         Properties JavaDoc cf = ConfigurationManager.getConfigFile();
83         this.tempDirectory = cf.getProperty(ConfigurationManager.TEMP_PATH_PROPERTY, DEFAULT_TEMP_DIR);
84
85         this.orb = theOrb;
86         //this.poa = thePoa;
87
this.rootContext = rootCtx;
88         this.componentProxyManager = proxyManager;
89         this.nodeDeployerManager = nodeDepManager;
90
91         this.applicationManager = new SingleHostApplicationManager();
92     }
93
94     /**
95      * Deploy an application
96      * @param remoteFile the file containing the application to be deployed
97      * @throws UnableToDeployException thrown when the deployer is unable to
98      * deploy the component
99      */

100     public void deploy(RemoteFile remoteFile) throws UnableToDeployException
101     {
102         try
103         {
104             String JavaDoc remoteFileName = remoteFile.getName();
105             if (LOG.isInfoEnabled()) LOG.info("Deploying " + remoteFileName);
106
107             if (LOG.isDebugEnabled()) LOG.debug("tempDirectory " + tempDirectory);
108
109             String JavaDoc fileName = FileUtil.getRemoteFile(remoteFile, tempDirectory, remoteFileName);
110             InputStream JavaDoc standardIs = ZipUtil.getInputStream(fileName, Application.STANDARD_DESCRIPTOR_NAME);
111             InputStream JavaDoc icaIs = ZipUtil.getInputStream(fileName, Application.ICA_DESCRIPTOR_NAME);
112
113             Reader JavaDoc standardReader = new InputStreamReader JavaDoc(standardIs);
114             Reader JavaDoc icaReader = new InputStreamReader JavaDoc(icaIs);
115             JackassApplication jackApp = JackassApplication.unmarshal(standardReader);
116             IcaJackassApplication icaApp = IcaJackassApplication.unmarshal(icaReader);
117             Application app = new Application(jackApp, icaApp);
118
119             if (LOG.isDebugEnabled()) LOG.debug("Application object created "+app.getName());
120
121             ApplicationInfo appInfo = applicationManager.get(app.getName());
122             if (appInfo != null)
123             {
124                 throw new Exception JavaDoc("Application already deployed " + app.getName());
125             }
126
127             // create the name service application context
128
try
129             {
130                 this.rootContext.bind_new_context(this.rootContext.to_name(app.getName()));
131             }
132             catch (AlreadyBound JavaDoc e)
133             {
134                 LOG.warn("Context for application " + app.getName() + " already bound");
135             }
136
137             Set JavaDoc deployerSet = this.getDeployerSet(app);
138             List JavaDoc requestList = this.invokeDeployers(deployerSet, app, remoteFile);
139             this.processDeploymentResults(requestList, app);
140
141             if (LOG.isInfoEnabled()) LOG.info("Application " + app.getName() + " deployed");
142         }
143         catch (Exception JavaDoc e)
144         {
145             LOG.error("Unexpected error deploying application");
146             if (LOG.isDebugEnabled()) LOG.debug("Stack trace follows", e);
147             throw new UnableToDeployException(e.getMessage());
148         }
149     }
150
151     /**
152      * Go through the list of components to obtain a NodeDeployer for each component
153      * and put them in a set. Later we'll ask each NodeDeployer to deploy the complete
154      * application. The NodeDeployer will deploy the application components which
155      * it is supposed to deploy and it will ignore the rest.
156      * @param app application object containing the deployment description data
157      * @return the set of deployers to be invoked to deploy this application
158      */

159     private Set JavaDoc getDeployerSet(Application app)
160     {
161         Set JavaDoc deployerSet = new HashSet JavaDoc();
162
163         for (Iterator JavaDoc it = app.getComponents(); it.hasNext();)
164         {
165             Component comp = (Component) it.next();
166
167             String JavaDoc depGroup = comp.getDeploymentGroup();
168             NodeDeployer[] nd = this.nodeDeployerManager.getNodeDeployers(depGroup);
169             if (nd != null)
170             {
171                 for (int i = 0; i < nd.length; i++)
172                 {
173                     deployerSet.add(nd[i]);
174                 }
175             }
176         }
177
178         return deployerSet;
179     }
180
181     /**
182      * Invoke the deploy operation on each NodeDeployer in the deployer set.
183      * Use dynamic invocation so that it is possible to do a deferred invocation
184      * @param deployerSet the set of deployers to be invoked o deploy the application
185      * @param app an object containing the application deployment data
186      * @param remoteFile A RemoteFile object that will be used bye the node deployers
187      * to retrieve the application archive file.
188      * @return the list of deployers on which the deploy operation was invoked
189      */

190     private List JavaDoc invokeDeployers(Set JavaDoc deployerSet, Application app, RemoteFile remoteFile)
191     {
192         ArrayList JavaDoc requestList = new ArrayList JavaDoc();
193
194         ApplicationInfo appInfo = new ApplicationInfo();
195         applicationManager.put(app.getName(), appInfo);
196
197         for (Iterator JavaDoc deployers = deployerSet.iterator(); deployers.hasNext();)
198         {
199             NodeDeployer nodeDeployer = (NodeDeployer) deployers.next();
200             appInfo.addNodeDeployer(nodeDeployer);
201             Request JavaDoc request = nodeDeployer._request(DEPLOY_OPERATION);
202             request.add_in_arg().insert_string(app.getName());
203             RemoteFileHelper.insert(request.add_in_arg(), remoteFile);
204
205             request.set_return_type(ProxyInfoListHelper.type());
206
207             requestList.add(request);
208             request.send_deferred();
209         }
210         return requestList;
211     }
212
213     /**
214      * Get the pending responses from the requests made to the NodeDeployers.
215      * Each node deployer returns a list of Component names - proxy reference
216      * pairs. The proxy reference is added to the list of proxy references
217      * associated with the component name in the ComponentProxyManager.
218      * @param requestList the list of pending requests.
219      * @param app the application deployment information.
220      * @throws Exception exception thrown if the remote NoteDeployer has thrown an
221      * execption
222      */

223     private void processDeploymentResults(List JavaDoc requestList, Application app) throws Exception JavaDoc
224     {
225         try
226         {
227             RefUtil refUtil = new RefUtil(orb);
228             List JavaDoc componentList = new ArrayList JavaDoc();
229             Iterator JavaDoc requests = requestList.iterator();
230             String JavaDoc appName = app.getName();
231
232             ApplicationInfo appInfo = applicationManager.get(appName);
233
234             while (requests.hasNext())
235             {
236                 Request JavaDoc request = (Request JavaDoc) requests.next();
237
238                 Object JavaDoc obj = request.target();
239                 NodeDeployer nodeDeployer = NodeDeployerHelper.narrow(obj);
240                 String JavaDoc nodeDeployerRef = orb.object_to_string(nodeDeployer);
241
242                 request.get_response();
243                 if (request.env().exception() != null)
244                 {
245                     throw request.env().exception();
246                 }
247                 ProxyInfo[] proxyList = ProxyInfoListHelper.extract(request.return_value());
248
249                 for (int i = 0; i < proxyList.length; ++i)
250                 {
251                     String JavaDoc compName = proxyList[i].componentName;
252                     String JavaDoc componentID = appName + ComponentProxyManager.COMPONENT_ID_SEPARATOR + compName;
253                     if (!componentList.contains(compName))
254                     {
255                         if (LOG.isDebugEnabled()) LOG.debug("Creating reference for " + componentID);
256                         Object JavaDoc componentReference =
257                                 refUtil.createJackassComponentReference(proxyList[i].proxyReference, componentID);
258                         componentList.add(compName);
259                         String JavaDoc nsName = this.exportReference(app, app.getComponent(compName), componentReference);
260                         appInfo.addComponentNSName(nsName);
261                     }
262                     componentProxyManager.addProxyToComponent(componentID, nodeDeployerRef,
263                             proxyList[i].proxyReference);
264                 }
265             }
266         }
267         catch (Exception JavaDoc e)
268         {
269             this.undeploy(app.getName());
270             throw e;
271         }
272     }
273
274     /**
275      * Export a component reference to the naming service
276      * @param app the application deployment information.
277      * @param comp the component deployment data.
278      * @param reference the component reference
279      * @return the complete name used to register the component in the name service
280      * @throws UserException thrown if there is an error when adding the component to the name service
281      */

282     private String JavaDoc exportReference(Application app, Component comp, Object JavaDoc reference)
283             throws UserException JavaDoc
284     {
285         String JavaDoc appName = app.getName();
286         String JavaDoc compName = comp.getName();
287         String JavaDoc nameContext = comp.getNameContext();
288
289         //NamingContextExt compContext = null;
290
String JavaDoc nameStr = null;
291         if (nameContext == null || nameContext.equals(""))
292         {
293             nameStr = appName + NAME_SEPARATOR + compName;
294             //compContext = NamingContextExtHelper.narrow(rootContext.resolve_str(appName));
295
}
296         else
297         {
298             nameStr = nameContext;
299             //compContext =
300
// NamingContextExtHelper.narrow(rootContext.bind_new_context(rootContext.to_name(nameContext)));
301
}
302
303         if (LOG.isDebugEnabled())
304         {
305             LOG.debug("Exporting reference for " + compName);
306             LOG.debug("name service name: " + nameStr);
307         }
308
309         rootContext.bind(rootContext.to_name(nameStr), reference);
310         return nameStr;
311     }
312
313     /**
314      * Undeploys an application.
315      * @param appName the name of the application to undeploy.
316      * @throws UnableToUndeployException if there is an error undeploying
317      * @see ve.luz.ica.jackass.deploy.DeployerOperations#undeploy(java.lang.String)
318      */

319     public void undeploy(String JavaDoc appName) throws UnableToUndeployException
320     {
321         LOG.info("Undeploying application " + appName);
322
323         boolean error = false;
324
325         // submit the undeploy operation to all node deployers asynchronously (don't wait)
326
ApplicationInfo appInfo = applicationManager.get(appName);
327         if (appInfo == null)
328         {
329             if (LOG.isInfoEnabled()) LOG.info("Application not deployed " + appName);
330             throw new UnableToUndeployException("Application not deployed " + appName);
331         }
332
333         List JavaDoc requests = new ArrayList JavaDoc();
334         for (Iterator JavaDoc i = appInfo.nodeDeployerIterator(); i.hasNext();)
335         {
336             NodeDeployer nodeDeployer = (NodeDeployer) i.next();
337             Request JavaDoc request = nodeDeployer._request(UNDEPLOY_OPERATION);
338             request.add_in_arg().insert_string(appName);
339             request.send_deferred();
340             requests.add(request);
341         }
342
343         // wait for requests to finnish and find out if there were errors
344
// so that they can be reported back
345
for (Iterator JavaDoc i = requests.iterator(); i.hasNext();)
346         {
347             try
348             {
349                 Request JavaDoc request = (Request JavaDoc) i.next();
350                 request.get_response();
351                 if (request.env().exception() != null)
352                 {
353                     throw request.env().exception();
354                 }
355             }
356             catch (Exception JavaDoc e)
357             {
358                 error = true;
359                 LOG.error("Error undeploying appName ", e);
360             }
361         }
362
363         // unbind the components from the name service
364
for (Iterator JavaDoc i = appInfo.componentNSNamesIterator(); i.hasNext();)
365         {
366             String JavaDoc nsName = (String JavaDoc) i.next();
367             try
368             {
369                 rootContext.unbind(rootContext.to_name(nsName));
370             }
371             catch (Exception JavaDoc e)
372             {
373                 error = true;
374                 LOG.error("Error unbinding component reference ", e);
375             }
376         }
377
378         // remove the application from the AppplicationManager
379
applicationManager.remove(appName);
380         componentProxyManager.removeApplication(appName);
381
382         // unbind the application from the name service
383
NamingContextExt JavaDoc appContext;
384         try
385         {
386             appContext = NamingContextExtHelper.narrow(this.rootContext.resolve_str(appName));
387             appContext.destroy();
388             rootContext.unbind(rootContext.to_name(appName));
389         }
390         catch (Exception JavaDoc e)
391         {
392             error = true;
393             LOG.error("Error resolving application reference in the name service ", e);
394         }
395
396         if (error)
397         {
398             throw new UnableToUndeployException();
399         }
400     }
401
402 }
403
Popular Tags