KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > OpenCCM_DCI > cif > AssemblySegImpl


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): Briclet Fr?d?ric.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26
27
28
29 package org.objectweb.openccm.OpenCCM_DCI.cif;
30
31 //Package dependencies
32
import java.net.URL JavaDoc;
33
34 import org.objectweb.openccm.OpenCCM_DCI.*;
35 import org.objectweb.openccm.Deployment.AssemblyBase;
36 import org.objectweb.openccm.Deployment.AssemblyConfigurator;
37 import org.objectweb.openccm.descriptor.componentassembly.ccm.deployer.root.*;
38 import org.objectweb.openccm.descriptor.componentassembly.beans.*;
39 import org.objectweb.openccm.descriptor.*;
40 import org.omg.Components.Deployment.Assembly;
41
42 /**
43  * AssemblyImpl is the top level implementation of the
44  * assembly behaviour. In the DCI implementation the assembly interface
45  * is provided by a AssemblyManager component. So many connection of
46  * this component are used for the initialization of the deployment
47  * process.
48  *
49  * @author <a HREF="mailto:frederic.briclet@lifl.fr">Briclet Frederic</a>
50  *
51  * @version 0.1
52  */

53 public class AssemblySegImpl
54     extends org.objectweb.openccm.OpenCCM_DCI.
55              AssemblyManagerSessionComposition.AssemblySeg
56     implements AssemblyConfigurator
57 {
58
59     // ==================================================================
60
//
61
// Internal state.
62
//
63
// ==================================================================
64
// xml handler path if
65
private String JavaDoc xmlFilePath;
66     
67     // Console for emittion of message
68
private org.objectweb.util.cmdline.api.Console console;
69     // State of the assembly
70
private AssemblyBase etat;
71     
72     // The assembly manager component
73
private AssemblyManager am;
74
75     private java.util.zip.ZipFile JavaDoc zipFile;
76     private java.io.File JavaDoc tmpZipFile;
77     private java.io.InputStreamReader JavaDoc reader;
78     //Root of xml tree cad representation
79
private ComponentassemblyBean assemblyObject;
80     // ==================================================================
81
//
82
// Internal methods.
83
//
84
// ==================================================================
85
// ==================================================================
86
//
87
// Internal class.
88
//
89
// ==================================================================
90
public ComponentassemblyDeployerHandler
91     configure(ComponentassemblyDeployerHandler context){
92         context.connectHomefinder( get_context().get_connection_home_finder()) ;
93         context.connectHomeregistration(get_context().get_connection_home_registration());
94         context.connectNamingcontext(get_context().get_connection_naming_service());
95         context.connectDciinfo(get_context().get_connection_dci_information());
96         context.connectAssemblyManager(am);
97         context.connectZipEntryRetriever(new ZipEntryRetriever(zipFile));
98         return context;
99     }
100  
101    public Assembly getAssembly(){
102        return
103        ((org.objectweb.openccm
104         .OpenCCM_DCI.AssemblyManagerCCM)get_context())
105         .provide_assembly();
106    }
107     
108    public void removeAssembly(){
109           
110    }
111    
112    public void
113    deleteTemporaryFile()
114    {
115     
116     try{
117         reader.close();
118         zipFile.close();
119     }catch(Exception JavaDoc e)
120     {
121         //TODO correct the lose of temporary file
122
//System.err.println("ZipFile cannot be closed");
123
}
124
125       if(!tmpZipFile.delete())
126              tmpZipFile.deleteOnExit();
127            
128    }
129    
130    public ComponentassemblyBean getComponentassemblyTree()
131       {
132           try{
133           // get the ORB
134
org.omg.CORBA.ORB JavaDoc orb
135              = org.objectweb.openccm.corba.TheORB.getORB();
136          //console.message("Got the ORB");
137

138          // inits OpenCCM runtime
139
org.objectweb.openccm.Components.Runtime.init (orb);
140          //console.message("OpenCCM runtime is ready");
141

142          java.io.InputStreamReader JavaDoc reader ;
143          
144           java.net.URL JavaDoc url= new URL JavaDoc(xmlFilePath);
145           tmpZipFile =java.io.File.createTempFile(System.currentTimeMillis()+"","aar");
146           java.io.FileOutputStream JavaDoc fos= new java.io.FileOutputStream JavaDoc(tmpZipFile);
147
148     
149            java.io.InputStream JavaDoc bis=url.openStream();
150    
151            byte [] byteArray= new byte[32000];
152            int readed=readed=bis.read(byteArray,0,32000);
153     
154            System.err.print("Downloading "+xmlFilePath+" [");
155            while(readed>0)
156                {
157                    fos.write(byteArray,0,readed);
158                    readed=bis.read(byteArray,0,32000);
159                    System.err.print("*");
160                }
161            System.err.println("]");
162            fos.flush();
163            fos.close();
164     
165            zipFile=
166                new java.util.zip.ZipFile JavaDoc(tmpZipFile);
167                                    
168        
169         
170          for(java.util.Enumeration JavaDoc entries=zipFile.entries();entries.hasMoreElements();)
171              {
172         
173                  java.util.zip.ZipEntry JavaDoc ze=(java.util.zip.ZipEntry JavaDoc)entries.nextElement();
174                  //System.out.println("Filename:"+ze.getName());
175
if(ze.getName().endsWith(".cad"))
176                      {
177                          System.err.println("AssemblyManager>>File ["+ze.getName()+"] is used for deployment");
178                          reader=new java.io.InputStreamReader JavaDoc
179                              (zipFile.getInputStream(ze));
180                         
181                         assemblyObject= org.objectweb.openccm.descriptor
182                              .componentassembly.beans.ComponentassemblyBeanImpl
183                              .unmarshalBean(reader,true);
184                         return assemblyObject;
185                      }
186              }
187         }
188         catch(Exception JavaDoc e){
189                 e.printStackTrace();
190         }
191         return assemblyObject;
192       }
193     
194     // ==================================================================
195
//
196
// Public methods.
197
//
198
// ==================================================================
199

200
201     /**
202      * Method to setup the path to the XML file to process
203      */

204     public void
205     setupXMLFilePath(String JavaDoc xmlFilePath)
206     {
207         this.xmlFilePath=xmlFilePath;
208         etat=new AssemblyBase(this);
209     }
210     
211     public void
212     setupAssemblyManager(org.objectweb.openccm.OpenCCM_DCI.AssemblyManager am)
213     {
214         this.am=am;
215     }
216         
217     public void registerAssembly(){
218         etat.registerAssembly();
219     }
220     
221     public void unregisterAssembly(){
222         try{
223             zipFile.close();
224             tmpZipFile.delete();
225         }
226         catch(Exception JavaDoc e)
227         {
228             e.printStackTrace();
229             //Not a problem tmp file will be removed by the OS
230
}
231
232         etat.unregisterAssembly();
233     }
234
235     // ==================================================================
236
//
237
// Assembly interface methods
238
//
239
// ==================================================================
240

241     /*Delegation operation to the state assembly*/
242     public void
243     build()
244     throws org.omg.Components.CreateFailure
245     {
246         etat.build();
247     }
248     
249     public void
250     tear_down()
251     throws org.omg.Components.RemoveFailure
252     {
253         etat.tear_down();
254     }
255     
256     
257     public org.omg.Components.Deployment.AssemblyState
258     get_state()
259     {
260         return etat.get_state();
261     }
262     
263     public String JavaDoc
264     getName()
265     {
266         try{
267             return assemblyObject.getId();
268         }
269         catch(Exception JavaDoc e){
270             e.printStackTrace();
271             return "AssemblyManager";
272         }
273     }
274     
275 }
276
Popular Tags