KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2002 USTL - LIFL - GOAL
5 Contact: openccm-team@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 Frederic
23 Contributor(s): ---------------------------------------------
24
25 ====================================================================*/

26
27 package org.objectweb.openccm.OpenCCM_DCI.cif;
28 import java.io.FileInputStream JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.net.ServerSocket JavaDoc;
31
32 import org.objectweb.openccm.OpenCCM_DCI.*;
33
34 /**
35  * This is the implementation of the AssemblyInstanceManagement facet
36  * provided by the AssemblyManagerModel component.
37  *
38  * This interface allows the management of all the AssemblyManager produce
39  * by an AssemblyManagerModel.
40  *
41  * This class inherits from the skeleton
42  * generated by the OpenCCM's CIF to Java mapping generator.
43  *
44  * @author <a HREF="mailto:Frederic.Briclet@lifl.fr">Briclet Frederic</A>
45  */

46 public class AssemblyInstanceManagementSegImpl
47     extends org.objectweb.openccm.OpenCCM_DCI
48             .AssemblyManagerModelSessionComposition
49             .AssemblyInstanceManagementSeg
50 {
51
52     // ==================================================================
53
//
54
// Internal class.
55
//
56
// ==================================================================
57
/**
58      * This class implements the Iterator behavior on the AssemblyManager
59      * Model connections.
60      */

61     private class AssemblyManagerIterator
62     {
63         //The current array of AssemblyManagerModelConnection
64
private org.objectweb.openccm.OpenCCM_DCI.
65             AssemblyManagerModelPackage.assembly_managersConnection []
66             connect;
67         //The current index on the array;
68
private int index;
69
70         /**
71          * No parameter required only the DCI of the uppon class
72          * is directly used.
73          * At the creation a rewind is automaticaly done.
74          */

75         public AssemblyManagerIterator()
76         {
77             rewind();
78         }
79
80         /**
81          * Return the next AssemblyManagerModel
82          * @return the next AssemblyManagerModel
83          */

84         public org.objectweb.openccm.OpenCCM_DCI.AssemblyManager
85         next()
86         throws java.util.NoSuchElementException JavaDoc
87         {
88             if(index==connect.length)
89                 throw new java.util.NoSuchElementException JavaDoc();
90             index++;
91             return connect[index-1].objref;
92         }
93
94          /**
95          * Return the current AssemblyManagerModel
96          * @return the current AssemblyManagerModel
97          */

98         public org.objectweb.openccm.OpenCCM_DCI.AssemblyManager
99         current()
100         {
101             return connect[index-1].objref;
102         }
103
104         public org.omg.Components.Cookie
105         currentCookie()
106         {
107             return connect[index-1].ck;
108         }
109
110         /**
111          * Test the end of the iterator
112          * @return the test result
113          */

114         public boolean
115         hasNext()
116         {
117             return index<connect.length;
118         }
119         
120         public void
121         remove()
122         {
123             //Empty implementation
124
}
125         /**
126          * Clear the current iterator, initializing it value with the
127          * most recent connection.
128          */

129         public void
130         rewind()
131         {
132             //initialize to the first index
133
index=0;
134             //initialize the array of model
135
connect=AMM.get_connections_assembly_managers();
136         }
137
138         /**
139          *Return the iterator length
140          * @return the current iterator length
141          */

142         public int
143         length()
144         {
145             return connect.length;
146         }
147
148
149     }
150
151     // ==================================================================
152
//
153
// Internal state
154
//
155
// ==================================================================
156
// The main segment
157
private org.objectweb.openccm.OpenCCM_DCI.AssemblyManagerModelCCM AMM;
158     // The descriptor to use
159
private String JavaDoc xml_descriptor;
160     
161     //THe componentArchive used
162
private byte[] componentArchive;
163     //The assemblymanger name
164
private String JavaDoc model_name;
165     //Th serversocket use to communicate with assemblyfactory
166
private ServerSocket JavaDoc serverSocket;
167     private java.io.File JavaDoc tmpZipFile ;
168     // Iterator on the AssemblyManager
169
private AssemblyManagerIterator amIterator;
170     //Prefix for clarifying output print
171
private String JavaDoc prefix="AssemblyInstanceManagement >> ";
172     //Private boolean state -> to replace by state pattern
173
private boolean removed=false;
174     //the cookie connection used to register to the DCIManager
175
private org.omg.Components.Cookie cookie;
176     
177     //Private table to retrieve a cookie from a AssemblyManager instanceUUID;
178
// private java.util.Hashtable cookies;
179

180     // ==================================================================
181
//
182
// internal methods.
183
//
184
// ==================================================================
185
private AssemblyManager
186     getAssemblyManager(String JavaDoc instanceUUID)
187     throws org.ist.coach.DCI.UnknownAssembly
188     {
189         for(amIterator.rewind();amIterator.hasNext();)
190             if(amIterator.next().ass_id().equals(instanceUUID))
191                 return amIterator.current();
192         throw new org.ist.coach.DCI.UnknownAssembly();
193     }
194     
195     // ==================================================================
196
//
197
// public methods.
198
//
199
// ==================================================================
200

201     public String JavaDoc
202     getModelName()
203     {
204         return model_name;
205     }
206     
207     /**
208      * Public method for setting the XML code to process.
209      * An AssemblyManagerIterator is also open for all the other
210      * processing.
211      */

212     public void
213     setupXML(String JavaDoc xml_descriptor)
214     {
215         this.xml_descriptor=xml_descriptor;
216         AMM=(org.objectweb.openccm
217              .OpenCCM_DCI.AssemblyManagerModelCCM)get_context();
218         amIterator= new AssemblyManagerIterator();
219     }
220     
221     
222     public void
223     setupCookie(org.omg.Components.Cookie cookie)
224     {
225         this.cookie=cookie;
226     }
227     
228     /**
229      * Public method to setup the componentarchive
230      *
231      */

232     public void
233     setupComponentArchive(byte[]componentArchive)
234     {
235         this.componentArchive=componentArchive;
236         try{
237         
238         AMM=(org.objectweb.openccm
239                     .OpenCCM_DCI.AssemblyManagerModelCCM)get_context();
240                amIterator= new AssemblyManagerIterator();
241         tmpZipFile =java.io.File.createTempFile(System.currentTimeMillis()+"","aar");
242         java.io.FileOutputStream JavaDoc fos= new java.io.FileOutputStream JavaDoc(tmpZipFile);
243         fos.write(componentArchive);
244         fos.flush();
245         fos.close();
246         java.util.zip.ZipFile JavaDoc zipFile=
247                         new java.util.zip.ZipFile JavaDoc(tmpZipFile);
248
249        for(java.util.Enumeration JavaDoc entries=zipFile.entries();entries.hasMoreElements();)
250             {
251
252                 java.util.zip.ZipEntry JavaDoc ze=(java.util.zip.ZipEntry JavaDoc)entries.nextElement();
253                             //System.out.println("Filename:"+ze.getName());
254
if(ze.getName().endsWith(".cad"))
255                     model_name=ze.getName();
256             }
257         }
258         catch(Exception JavaDoc e){
259             e.printStackTrace();
260         }
261        model_name=model_name.substring(model_name.lastIndexOf("/")+1,model_name.lastIndexOf(".cad"));
262                     
263     }
264     
265     public void
266     disconnectRemovedAssembly(org.ist.coach.DCI.AssemblyManager am)
267     throws Exception JavaDoc
268     {
269        AssemblyManagerIterator amIt=new AssemblyManagerIterator();
270         
271         //System.err.println(prefix+"Nb connections:"+amIterator.length());
272

273         for(amIt.rewind();amIt.hasNext();)
274             if(am._is_equivalent(amIt.next()))
275                 AMM.disconnect_assembly_managers
276                     (amIt.currentCookie());
277
278                 
279     }
280
281     // ==================================================================
282
//
283
// AssemblyInstanceManagement methods.
284
//
285
// ==================================================================
286
public void
287     uninstall()
288     throws org.ist.coach.DCI.UninstallationFailure,
289            org.ist.coach.DCI.UnknownAssembly,
290            org.ist.coach.DCI.AssemblyUsed
291     {
292         if(removed)
293             return ;
294         try{
295          
296             removed=true;
297             for(amIterator.rewind();
298                 amIterator.hasNext();)
299                 {
300                     amIterator.next().remove();
301                     //AMM.disconnect_assembly_managers(amIterator.currentCookie());
302
}
303             
304             AMM.get_connection_assembly_manager_model_registration()
305             .unregister_assembly_manager_model(cookie);
306             if(!tmpZipFile.delete())
307                 tmpZipFile.deleteOnExit();
308              AMM.remove();
309         }
310         catch(org.omg.Components.RemoveFailure rf)
311             {
312                 System.err.println(prefix+"Error occured during removing of an AssemblyManager");
313                 System.err.println(prefix+rf.getMessage());
314                 throw new org.ist.coach.DCI.UninstallationFailure();
315             }
316         catch(org.omg.Components.CookieRequired cr)
317             {
318                 System.err.println(prefix+"Error occured during disconnection of model:Bad cookie");
319                 throw new org.ist.coach.DCI.UninstallationFailure();
320             }
321        /* catch(org.omg.Components.ExceededConnectionLimit ecl)
322             {
323                 System.err.println(prefix+"Error occured during disconnection of an AssemblyManager");
324                 System.err.println(prefix+ecl.getMessage());
325                 throw new org.ist.coach.DCI.UninstallationFailure();
326             }*/

327     }
328
329
330     /**
331      * This method allow to make an instance of AssemblyManagerModel
332      * @return the assembly uuid
333      */

334     public String JavaDoc
335     create_assembly_instance()
336     throws org.ist.coach.DCI.CreationFailure
337     {
338         if(xml_descriptor!=null)
339             return create_assembly_instanceXML();
340         return create_assembly_componentArchive_instance();
341     }
342     
343     /**
344      * This method allow to make an instance of AssemblyManagerModel
345      * @return the assembly uuid
346      */

347     protected String JavaDoc
348     create_assembly_instanceXML()
349     throws org.ist.coach.DCI.CreationFailure
350     {
351         try{
352             //create the AssemblyManager componentt
353
org.omg.Components.Cookie ck=
354                 AMM.get_connection_assembly_factory()
355                 .create_assembly_XML(xml_descriptor);
356             //Retrieve the assembly manager reference
357
org.objectweb.openccm.OpenCCM_DCI.AssemblyManager AM=
358                 AMM.get_connection_assembly_factory()
359                 .lookup_assembly_manager(ck);
360             //connect the assembly manager to the assembly manager model
361
AMM.connect_assembly_managers(AM);
362             // cookies.put(AM.ass_id(),AMM.connect_assembly_managers(AM));
363
//subscribe the Model to the assembly
364
AM.subscribe_assembly_manager_removed
365                 (AMM.get_consumer_assembly_manager_removed());
366             AM.configuration_complete();
367             // build the assembly
368
AM.provide_assembly().build();
369             
370             // return the assembly id
371
return AM.ass_id();
372         }
373         catch(Exception JavaDoc e){
374             System.err.println("Cannot create an assembly instance");
375             e.printStackTrace();
376             
377             throw new org.ist.coach.DCI.CreationFailure();
378         }
379
380     }
381     
382     
383     protected ServerSocket JavaDoc
384     getServerSocket()
385     throws Exception JavaDoc
386     {
387         if(serverSocket==null)
388             serverSocket=new ServerSocket JavaDoc(0);
389         return serverSocket;
390     }
391     
392     protected InputStream JavaDoc
393     getInputStream()
394     throws Exception JavaDoc
395     {
396         return new FileInputStream JavaDoc(tmpZipFile);
397     }
398     
399     protected String JavaDoc
400     create_assembly_componentArchive_instance()
401     throws org.ist.coach.DCI.CreationFailure
402     {
403         try{
404             
405             
406             (new Thread JavaDoc
407                 (new org.objectweb.openccm.deploytool.MicroServerHttp
408                     (getServerSocket(),
409                      getInputStream(),
410                      "ComponentArchive"))).start();
411              String JavaDoc path="http://" +
412             java.net.InetAddress.getLocalHost().getHostAddress() + ':' +
413             getServerSocket().getLocalPort() + '/' +"ComponentArchive";
414             
415             //create the AssemblyManager componentt
416
org.omg.Components.Cookie ck=
417                   AMM.get_connection_assembly_factory()
418                   .create_assembly(path);
419               //Retrieve the assembly manager reference
420
org.objectweb.openccm.OpenCCM_DCI.AssemblyManager AM=
421                   AMM.get_connection_assembly_factory()
422                   .lookup_assembly_manager(ck);
423               //connect the assembly manager to the assembly manager model
424
AMM.connect_assembly_managers(AM);
425               // cookies.put(AM.ass_id(),AMM.connect_assembly_managers(AM));
426
//subscribe the Model to the assembly
427
AM.subscribe_assembly_manager_removed
428                   (AMM.get_consumer_assembly_manager_removed());
429               //AM.configuration_complete();
430

431               // build the assembly
432
AM.provide_assembly().build();
433               // return the assembly id
434
return AM.ass_id();
435         }
436         catch(Exception JavaDoc e){
437             System.err.println("Cannot create an assembly instance");
438             e.printStackTrace();
439     
440             throw new org.ist.coach.DCI.CreationFailure();
441         }
442     }
443     /**
444      * This method return all the id of the running
445      * assemblies.
446      * @return the assemblies uuid.
447      */

448     public String JavaDoc[]
449     get_assembly_instances()
450     throws org.ist.coach.DCI.UnknownAssembly
451     {
452         //Create the buffer list
453
java.util.LinkedList JavaDoc ll = new java.util.LinkedList JavaDoc();
454         //Fill the list
455
for(amIterator.rewind();amIterator.hasNext();)
456             ll.addLast(amIterator.next().ass_id());
457             
458         //return the result
459
return (String JavaDoc[])ll.toArray(new String JavaDoc[ll.size()]);
460     }
461
462     /**
463      * Return the assembly manager whitch have the specified
464      * instance_id give in parameter.
465      * @return the binded assembly manager
466      */

467     public AssemblyManager
468     get_assembly_manager(String JavaDoc instance_id)
469     throws org.ist.coach.DCI.UnknownAssembly
470     {
471         return getAssemblyManager(instance_id);
472     }
473
474     /**
475      * Destroy the specified instance_id
476      * @param instance_id the instance_id of the AssemblyManager to destroy
477      */

478     public void
479     destroy_assembly_instance(String JavaDoc instance_id)
480     throws org.ist.coach.DCI.DestroyError
481     {
482         try{
483             getAssemblyManager(instance_id).remove() ;
484             //AMM.disconnect_assembly_managers(amIterator.currentCookie());
485
return ;
486         }
487         catch(Exception JavaDoc e){
488             throw new org.ist.coach.DCI.DestroyError();
489         }
490     }
491 }
492
Popular Tags