KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > Deployment > AssemblyBase


1 /*
2  * Created on 28 janv. 2004
3  *
4  * To change the template for this generated file go to
5  * Window>Preferences>Java>Code Generation>Code and Comments
6  */

7 package org.objectweb.openccm.Deployment;
8
9 import java.io.FileReader JavaDoc;
10 import java.io.File JavaDoc;
11 import org.objectweb.ccm.descriptor.componentassembly.beans.ComponentassemblyBean;
12 import org.objectweb.ccm.descriptor.componentassembly.beans.ComponentassemblyBeanImpl;
13 import org.objectweb.ccm.descriptor.componentassembly.ccm.deployer.base.FatalDeploymentException;
14 import org.objectweb.ccm.descriptor.componentassembly.ccm.deployer.base.HandlerContext;
15 import org.objectweb.ccm.descriptor.componentassembly.ccm.deployer.base.InitializationError;
16 import org.objectweb.ccm.descriptor.componentassembly.ccm.deployer.installer.DefaultDestinationDeployerFactory;
17 import org.objectweb.ccm.descriptor.componentassembly.ccm.deployer.managers.DefaultDeploymentLogger;
18 import org.objectweb.ccm.descriptor.componentassembly.ccm.deployer.managers.DefaultErrorManager;
19 import org.objectweb.ccm.descriptor.componentassembly.ccm.deployer.root.ComponentassemblyDeployer;
20 import org.objectweb.ccm.descriptor.componentassembly.ccm.deployer.root.ComponentassemblyDeployerHandler;
21 import org.objectweb.ccm.descriptor.deployerconfig.DeployerconfigImpl;
22 import org.objectweb.ccm.descriptor.deployerconfig.beans.DeployerconfigBeanImpl;
23 import org.omg.Components.Deployment.AssemblyPOA;
24 /**
25  * @author briclet
26  *
27  * To change the template for this generated type comment go to
28  * Window>Preferences>Java>Code Generation>Code and Comments
29  */

30 public class AssemblyBase extends AssemblyPOA {
31
32
33     // ==================================================================
34
//
35
// Internal state.
36
//
37
// ==================================================================
38
// Console for emittion of message
39
private org.objectweb.util.cmdline.api.Console console;
40     
41     //Root of xml tree cad representation
42
private ComponentassemblyBean assemblyObject;
43         
44     // State of the assembly
45
private org.omg.Components.Deployment.AssemblyOperations etat;
46        
47     //Reference on the NameService
48
private org.omg.CosNaming.NamingContextExt JavaDoc ns ;
49     //Name component involve in the NS registration
50
private org.omg.CosNaming.NameComponent JavaDoc [] ncomp;
51
52     private java.util.zip.ZipFile JavaDoc zipFile;
53     
54     private AssemblyConfigurator configurator;
55     
56     private ComponentassemblyDeployerHandler handler;
57     
58     private String JavaDoc registrationName;
59     
60     private String JavaDoc assemblyWorkDirPath;
61     
62     private File JavaDoc assemblyWorkDir;
63     
64     private DefaultDeploymentLogger ddl=null;
65     // ==================================================================
66
//
67
// Internal methods.
68
//
69
// ==================================================================
70

71     public AssemblyBase(AssemblyConfigurator configurator){
72         this.configurator=configurator;
73         etat=new AssemblyImplInactive();
74         assemblyObject=configurator.getComponentassemblyTree();
75         init();
76     }
77     
78    private void
79    init()
80    {
81       
82        try{
83            handler=((ComponentassemblyDeployerHandler)
84                                assemblyObject
85                                .getExtensionManager()
86                                .getExtensionByName("ccm.deployer.root.Deployer"));
87            handler.connectHandlerContext(new HandlerContext());
88                  
89            java.net.ServerSocket JavaDoc s = new java.net.ServerSocket JavaDoc(0);
90                       
91         
92
93            handler.connectServerSocket(s);
94            ddl=new DefaultDeploymentLogger(/*System.getProperty("WorkDir")
95                                               +"DeploymentLog.txt"*/
);
96            handler.connectDeploymentLogger(ddl);
97
98        }
99        catch(Exception JavaDoc e)
100           {
101               e.printStackTrace();
102              //hrow new org.omg.Components.CreateFailure();
103
}
104         
105            try{
106                                 
107                   FileReader JavaDoc fr=new FileReader JavaDoc
108                       (System.getProperty("DeployerConfigFile"));
109                   DeployerconfigImpl depl_conf=(DeployerconfigImpl)
110                                                 DeployerconfigBeanImpl.unmarshal(fr,true);
111                   DefaultErrorManager erm=new DefaultErrorManager(depl_conf);
112                   erm.connectDeploymentLogger(ddl);
113                   handler.connectErrorManager(erm);
114                
115                   DefaultDestinationDeployerFactory dddf=
116                            new DefaultDestinationDeployerFactory(depl_conf);
117
118                   handler.connectDestinationDeployerFactory(dddf);
119
120               }
121               catch(Exception JavaDoc e){
122                 /* getDeployer().getRootDeployerContext()
123                               .getDeploymentLogger()
124                               .log(">> Error manager : No configuration file precised");*/

125                   e.printStackTrace();
126                   System.err.println(">> Error manager : No configuration file precised");
127               }
128         
129            configurator.configure(handler);
130            
131    }
132            
133     /**
134      * Internal function to register the assembly with the specified name
135      * in cad descriptor in order to retrieve and tear_down the assembly later.
136      */

137     public void
138     registerAssembly()
139     {
140       
141         try{
142             // retrieve the corba reference from the assembly object
143
org.omg.Components.Deployment.Assembly assemblyRef=
144                 configurator.getAssembly();
145             
146             // Obtain the Name Service.
147
ns=org.omg.CosNaming.NamingContextExtHelper
148                 .narrow(org.objectweb.openccm.corba
149                         .TheNameService
150                         .getNamingContext()
151                         .getNamingContext());
152             
153             // Bind the server object in the name service.
154
ncomp= org.objectweb.ccm.descriptor
155                     .componentassembly
156                     .ccm.deployer.registrar
157                     .RegisterwithnamingDeployer
158                         .rebind(ns,
159                                 System.getProperty("Assemblies_ns_dir")+
160                                 assemblyObject.getId(),
161                                 assemblyRef);
162             registrationName=ns.to_string(ncomp).replace('/','.');
163             //Now we have the assembly name we can create
164
//the assembly work directory
165
assemblyWorkDirPath=System.getProperty("WorkDir")+"/"+registrationName;
166             assemblyWorkDir=new File JavaDoc(assemblyWorkDirPath);
167             assemblyWorkDir.mkdirs();
168             ddl.setLogFile(assemblyWorkDirPath+"/DeploymentLog.log");
169             
170             }
171         catch(Exception JavaDoc e){
172              System.err.println("Following exception occur during assembly registration");
173              System.err.println(e.getMessage());
174              System.err.println("Cannot register the assembly"+
175                                 System.getProperty("Assemblies_ns_dir") +
176                                 assemblyObject.getId());
177              return ;
178         }
179         System.out.println("The Assembly"+
180                                System.getProperty("Assemblies_ns_dir")+
181                                assemblyObject.getId()+" is referenced");
182     }
183     
184   
185     /**
186      * Unregister the assembly from the NameService
187      */

188     public void
189     unregisterAssembly()
190     {
191         try{
192             ComponentassemblyDeployer cad=(ComponentassemblyDeployer)
193                 
194              handler.getInstance((ComponentassemblyBeanImpl)assemblyObject);
195              cad.clearAllStream();
196         }catch(Exception JavaDoc e)
197         {
198             e.printStackTrace();
199            System.err.println("Problem occured during stream closing");
200         }
201         
202         try{
203             ns.unbind(ncomp);
204             ddl.removeLogFile();
205             configurator.deleteTemporaryFile();
206             assemblyWorkDir.delete();
207         }
208         catch(Exception JavaDoc e){
209              System.err.println("Following exception occur during assembly unregistration");
210              System.err.println(e.getMessage());
211              System.err.println("Cannot unregister the assembly"+
212                                 System.getProperty("Assemblies_ns_dir")+
213                                 assemblyObject.getId());
214         }
215     }
216
217     // ==================================================================
218
//
219
// Internal class.
220
//
221
// ==================================================================
222
/**
223      * Inactive state for the assembly
224      */

225     private class AssemblyImplInactive
226         implements org.omg.Components.Deployment.AssemblyOperations
227     {
228         
229         private java.io.File JavaDoc tmpZipFile;
230         
231
232        
233         
234         public void
235         build()
236         throws org.omg.Components.CreateFailure
237         {
238             try{
239                     
240                 ComponentassemblyDeployer cad=(ComponentassemblyDeployer)
241                 
242                 handler.getInstance((ComponentassemblyBeanImpl)assemblyObject);
243                                 
244                 cad.setWorkDir(assemblyWorkDirPath);
245                 
246                 cad.deploy();
247                 cad.configueCompleteComponents();
248               
249             }
250             catch(InitializationError er){
251                 er.printStackTrace();
252                 /*if(er.getInitialException()!=null)
253                     er.getInitialException().printStackTrace();*/

254                 throw new org.omg.Components.CreateFailure();
255             }
256             catch(FatalDeploymentException fde){
257                 System.err.println(fde.getMessage());
258                 //fde.printStackTrace();
259
}
260            
261             etat=(org.omg.Components.Deployment.AssemblyOperations) new AssemblyImplActive();
262          }
263     
264             
265         /**
266          * Private function allowing inactive assembly to load
267          * tree representation of the xml cad file
268          */

269         
270         
271         /**
272          * Break connection between composant and destroy them
273          */

274         public void tear_down() throws org.omg.Components.RemoveFailure
275         {
276             throw new org.omg.Components.RemoveFailure();
277         }
278         
279         /**
280          * Return the current assembly state
281          */

282         public org.omg.Components.Deployment.AssemblyState get_state()
283         {
284             return org.omg.Components.Deployment.AssemblyState.INACTIVE;
285             
286         }
287     }
288     
289     /**
290      * Internal class which represent an deploied assembly, only
291      * tear_down() and get_state() operation are available
292      */

293     private class AssemblyImplActive
294         implements org.omg.Components.Deployment.AssemblyOperations
295     {
296         /**
297          * Alway thrown CreateFailure exception because the assembly
298          * is already deploied. This function is non available
299          */

300         public void build() throws org.omg.Components.CreateFailure
301         {
302             throw new org.omg.Components.CreateFailure();
303         }
304         
305         /**
306          * Tear_down the assembly in 2 part:
307          * First tear_down connections
308          * Secondly erease instantiation
309          * Switch the internal state of the assembly to inactive state.
310          */

311         public void tear_down() throws org.omg.Components.RemoveFailure
312         {
313             try{
314                 ComponentassemblyDeployer cad=
315                    ((ComponentassemblyDeployer)
316                      ((ComponentassemblyDeployerHandler)
317                     assemblyObject
318                     .getExtensionManager().getExtensionByName("ccm.deployer.root.Deployer"))
319                     .getInstance((ComponentassemblyBeanImpl)assemblyObject));
320                 
321                 cad.tearDown();
322                 etat=(org.omg.Components.Deployment.AssemblyOperations)new AssemblyImplInactive();
323                 configurator.removeAssembly();
324             }
325             catch(Exception JavaDoc e){e.printStackTrace();}
326             
327         }
328         
329         /**
330          * Break connection between composant and destroy them
331          */

332         public org.omg.Components.Deployment.AssemblyState get_state()
333         {
334             return org.omg.Components.Deployment.AssemblyState.INSERVICE;
335         }
336     }
337     
338     // ==================================================================
339
//
340
// Public methods.
341
//
342
// ==================================================================
343

344   
345         
346
347     // ==================================================================
348
//
349
// Assembly interface methods
350
//
351
// ==================================================================
352

353     /*Delegation operation to the state assembly*/
354     public void
355     build()
356     throws org.omg.Components.CreateFailure
357     {
358         etat.build();
359     }
360     
361     public void
362     tear_down()
363     throws org.omg.Components.RemoveFailure
364     {
365         etat.tear_down();
366     }
367     
368     
369     public org.omg.Components.Deployment.AssemblyState
370     get_state()
371     {
372         return etat.get_state();
373     }
374    
375     
376
377
378 }
379
Popular Tags