KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > deploytool > DeploymentApplication


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Christophe Contreras
23 Contributor(s): Fr?d?ric Briclet, Philippe Merle.
24
25 ====================================================================*/

26
27 package org.objectweb.openccm.deploytool;
28
29 // Package dependencies
30
import java.io.File JavaDoc;
31 import java.net.MalformedURLException JavaDoc;
32 import org.objectweb.util.cmdline.api.CommandLine;
33 import org.objectweb.util.cmdline.lib.ApplicationBase;
34 import org.objectweb.util.cmdline.lib.DefaultCommandLine;
35 import org.objectweb.util.cmdline.lib.DefaultOptionArgument;
36 import org.objectweb.util.cmdline.lib.DefaultOptionFlag;
37 import org.omg.Components.CreateFailure;
38
39 /**
40  * This is the CCM XML deployment application.
41  *
42  * @author <a HREF="mailto:Christophe.Contreras@lifl.fr">Christophe Contreras</a>
43  *
44  * @version 0.1
45  */

46
47 public class DeploymentApplication
48      extends ApplicationBase
49 {
50     // ==================================================================
51
//
52
// Internal state.
53
//
54
// ==================================================================
55

56     /** Reference to the deployer. */
57     //private DeploymentProcess _deployer;
58
/*The name of the zip file to deploy*/
59     private String JavaDoc zipFile ;
60     /*Boolean denoting if the deployment must be distributed or not*/
61     private boolean distributedDeployment =false;
62     /*In distributed deployment case the assembly factory name to use*/
63     private String JavaDoc assemblyFactoryName =null;
64     /*The assembly factory information*/
65     private DefaultOptionArgument factory_info;
66     /*This flag indicate that the deployment must be done locally*/
67     private DefaultOptionFlag local_flag;
68     // ==================================================================
69
//
70
// Constructors.
71
//
72
// ==================================================================
73

74     /**
75      * The default constructor.
76      */

77     public
78     DeploymentApplication()
79     {
80         super(
81             new DefaultCommandLine(
82                  new String JavaDoc[]{"ccm_deploy","ccm_deploy"}
83                 ,/*new String[]{"-Fassembly_factory_name",*/"zip_file.aar"/*}*/
84                 ,new String JavaDoc[]{"The component assembly archive to deploy"}
85                 ,true
86                 )
87             );
88         factory_info=new DefaultOptionArgument(
89                                 "-F",
90                                 "factory_name",
91                                 "The name of the factory to use, default is DefaultFactory ",
92                                 "DefaultFactory");
93         local_flag=new DefaultOptionFlag("-l",
94                                         "This flag indicates that the deployment must be "
95                                         +"done locally without using an assembly factory",
96                                         false);
97          
98                
99         getCommandLine().addOption(local_flag);
100         getCommandLine().addOption(factory_info);
101     }
102
103
104     // ==================================================================
105
//
106
// Internal methods.
107
//
108
// ==================================================================
109
private class UnsupportedArchiveFormat extends Exception JavaDoc{
110         public UnsupportedArchiveFormat(String JavaDoc format){
111             super("The archive format ["+format+
112                   "] is not supported by the deployment.");
113         }
114     }
115     
116     private void
117     computeArgs(String JavaDoc args[])
118     {
119         for(int i=0;i<args.length;i++){
120             if(args[i].length()>3&&(
121                args[i].endsWith("aar")||args[i].endsWith("zip")))
122                 this.zipFile=args[i];
123             /*else if(args[i].equals("-F"))
124                 this.distributedDeployment=true;
125             else if(args[i].startsWith("-F")){
126                 this.assemblyFactoryName=
127                     System.getProperty("Assembly_factory_ns_dir")+args[i].substring(2);
128                 this.distributedDeployment=true;
129             }*/

130         }
131         
132         if(local_flag.isSet()){
133             distributedDeployment=false;
134             return;
135         }else distributedDeployment=true;
136            
137     
138        if(factory_info.isSet()){
139            assemblyFactoryName=
140                           System.getProperty("Assembly_factory_ns_dir")+factory_info.getArgument();
141             getConsole().message("Use the specified assembly factory ["+factory_info.getArgument()+"]");
142        }
143     }
144       
145     private void
146     proceedLocalDeployment()
147     throws CreateFailure
148     {
149         getConsole().message("Local deployment processing");
150         try{
151             File JavaDoc f=new File JavaDoc(zipFile);
152            (new org.objectweb.openccm.Deployment.AssemblyImpl(f.toURL())).build();
153         }
154         catch(MalformedURLException JavaDoc e){
155             //System.err.println("MalFormedURL:");
156
throw new CreateFailure();
157         }
158     }
159     // ==================================================================
160
//
161
// Public methods for org.objectweb.util.api.Identifiable
162
//
163
// ==================================================================
164

165     /**
166      * Obtains its identity.
167      *
168      * @return Its identity.
169      */

170     public String JavaDoc
171     getIdentity()
172     {
173         return "ccm_deploy"
174              + ' ' + org.objectweb.ccm.util.ReleaseInfo.VERSION_MAJOR
175              + '.' + org.objectweb.ccm.util.ReleaseInfo.VERSION_MINOR;
176     }
177
178     // ==================================================================
179
//
180
// Public methods for org.objectweb.util.cmdline.api.Application
181
//
182
// ==================================================================
183

184     /**
185      * Executes the main function, i.e.
186      * 1 - Initializes the CORBA::ORB,
187      * 2 - Calls super.runMain().
188      *
189      * @param args The command line arguments.
190      */

191     public void
192     runMain(String JavaDoc[] args)
193     {
194         try
195         {
196             // Initializes the CORBA::ORB singleton.
197
// Initializes the CORBA::ORB singleton.
198
args=org.objectweb.openccm.corba.TheORB.initialize(args);
199             org.objectweb.openccm.Components.Runtime.init(org.objectweb.openccm.corba.TheORB.getORB());
200             // Set EntityResolver for CCM DTDs
201
org.objectweb.openccm.descriptor.EntityResolver.setCCMResolver();
202         }
203         // If any exception thrown then reports it.
204
//
205
catch(org.objectweb.util.misc.api.ExceptionWrapper exc)
206         {
207             report_exception(exc.getException());
208         }
209         catch(Exception JavaDoc exc)
210         {
211             report_exception(exc);
212         }
213
214         // Calls ApplicationBase.runMain method.
215
super.runMain(args);
216     }
217
218     /**
219      * Starts the application.
220      *
221      * @param args The command line arguments.
222      *
223      * @return The status.
224      */

225     public int
226     start(String JavaDoc[] args)
227     {
228         
229         computeArgs(args);
230         boolean result = deploy(zipFile);
231         return result?0:-1;
232     }
233
234     
235     // ==================================================================
236
//
237
// Public methods.
238
//
239
// ==================================================================
240

241
242     /**
243      * Deploys a XML Component Assembly Descriptor.
244      *
245      * @param assembly_filename The XML Component Assembly Descriptor file name.
246      *
247      * @return True if the deployment successes, otherwise false.
248      */

249     public boolean
250     deploy(String JavaDoc zip_file)
251     {
252         boolean result=true;
253             
254         getConsole()
255             .message(
256                 "Initiating deployment sequence of "
257                 + zip_file
258                 );
259         
260         org.omg.Components.Deployment.Assembly ass;
261         
262         try{
263
264             try{
265                 
266                 if(distributedDeployment){
267                     org.omg.Components.Deployment.AssemblyFactory af ;
268                         
269                     if(assemblyFactoryName!=null){
270                         
271                         org.omg.CosNaming.NamingContextExt JavaDoc context=
272                             org.omg.CosNaming.NamingContextExtHelper
273                             .narrow(org.objectweb.openccm.corba.TheNameService
274                                     .getNamingContext()
275                                     .getNamingContext());
276                         
277                         
278                         af= org.objectweb.openccm
279                             .OpenCCM_DCI.AssemblyFactoryManagerHelper
280                             .narrow(context.resolve(context.to_name(assemblyFactoryName)))
281                             .provide_assembly_factory();
282                     }
283                     else {
284                         
285                         org.objectweb.openccm.OpenCCM_DCI.DCIManager
286                             DCI=
287                             org.objectweb.openccm.
288                             OpenCCM_DCI.DCIManagerHelper.
289                             narrow(org.objectweb.openccm.corba
290                                    .TheORB.resolve_initial_reference("DCIManager"));
291                         af=DCI.get_connection_assembly_factory();
292                         
293                     }
294                     
295                     if(af==null)
296                         throw new org.omg.CosNaming.NamingContextPackage.NotFound JavaDoc();
297                         
298                     //Add the use of an Http server to provide the assemblyfile
299
java.net.ServerSocket JavaDoc s = new java.net.ServerSocket JavaDoc(0);
300                     
301                     String JavaDoc archive_url = "http://"
302                                 + java.net.InetAddress.getLocalHost().getHostAddress()
303                                 + ':' + s.getLocalPort() + '/' +zip_file;
304                     getConsole().message("Archive URL " + archive_url);
305
306                     //Thread serverThread=
307
(new Thread JavaDoc(new MicroServerHttp(s))).start();
308
309                     org.omg.Components.Cookie cok=
310                         af.create_assembly(archive_url);
311                     
312                     ass=af.lookup(cok);
313                     ass.build();
314                     //ass.setConsole(getConsole());
315
}
316                 else{
317                     getConsole().message("Proceed a local deployment, ");
318                     proceedLocalDeployment();
319                 }
320             }
321             catch(java.io.IOException JavaDoc ioe)
322               {
323                     getConsole().error
324                         ("Cannot use assembly factory to download file, proceed a local deployment, ");
325                     getConsole().error
326                         ("the tear_down function will be disable from console");
327                   
328                    proceedLocalDeployment();
329                    
330                     
331                 }
332             /*catch(java.net.UnknownHostException uoe)
333               {
334                     getConsole().error
335                         ("Cannot use assembly factory to download file, process a local deployment, ");
336                     getConsole().error
337                         ("the tear_down function will be disable from console");
338                   
339                     (new org.objectweb.openccm.Deployment.AssemblyImpl(assembly_filename))
340                          .build();
341                    
342                     
343                          } */

344             catch( org.omg.CosNaming.NamingContextPackage.NotFound JavaDoc nf)
345                 {
346             getConsole().error
347                         ("Cannot found a valid assembly factory, proceed a local deployment,");
348             getConsole().error
349                         ("the tear_down function will be disable from console");
350                   
351                    proceedLocalDeployment();
352                     
353                 }
354             catch(org.omg.CosNaming.NamingContextPackage.CannotProceed JavaDoc cp)
355                 {
356             getConsole().error
357                         ("Cannot use name service to found a valid assembly factory, proceed a local deployment,");
358             getConsole().error
359                         ("the tear_down function will be disable from console");
360                    proceedLocalDeployment();
361                 }
362             catch(org.omg.CosNaming.NamingContextPackage.InvalidName JavaDoc in)
363                 {
364             getConsole().error
365                         ("Cannot found a valid assembly factory, proceed a local deployment,");
366             getConsole().error
367                         ("the tear_down function will be disable from console");
368                    proceedLocalDeployment();
369                     
370                 }
371             catch(org.omg.Components.Deployment.InvalidAssembly IA)
372                 {
373             getConsole().error
374                         ("Internal error occured during retrieving of assembly, cannot process deployment");
375                     return false;
376                 }
377          
378             //ass.build();
379

380         }
381         catch(org.omg.Components.Deployment.InvalidLocation il)
382             {
383                 getConsole().error
384                     ("Cannot found the CAD file, check your path and restart the deployment");
385                 result=false;
386             }
387         catch(org.omg.Components.CreateFailure cf)
388             {
389                 getConsole().error
390                     ("Cannot achieved assembly deployment, be carefull only a partial assembly");
391                 getConsole().error
392                     ("is deployed, assembly behavior may be corrupt");
393                 result=false;
394             }
395                     
396         if (!result)
397         {
398             getConsole()
399                 .error(
400                     "Sequence aborted during deployment of "
401                     + zip_file
402                     );
403             return false;
404         }
405        
406         getConsole()
407             .message(
408                 "Successfully deployed "
409                 + zip_file
410                 );
411
412         return result;
413     }
414
415     // ==================================================================
416
//
417
// Static public methods.
418
//
419
// ==================================================================
420

421     /**
422      * The main bootstrap method.
423      *
424      * @param args The command line arguments.
425      */

426     public static void
427     main(String JavaDoc[] args)
428     {
429         DeploymentApplication application = new DeploymentApplication();
430         application.runMain(args);
431     }
432 }
433
Popular Tags