1 26 27 package org.objectweb.openccm.OpenCCM_DCI.cif; 28 import java.io.FileInputStream ; 29 import java.io.InputStream ; 30 import java.net.ServerSocket ; 31 32 import org.objectweb.openccm.OpenCCM_DCI.*; 33 34 46 public class AssemblyInstanceManagementSegImpl 47 extends org.objectweb.openccm.OpenCCM_DCI 48 .AssemblyManagerModelSessionComposition 49 .AssemblyInstanceManagementSeg 50 { 51 52 61 private class AssemblyManagerIterator 62 { 63 private org.objectweb.openccm.OpenCCM_DCI. 65 AssemblyManagerModelPackage.assembly_managersConnection [] 66 connect; 67 private int index; 69 70 75 public AssemblyManagerIterator() 76 { 77 rewind(); 78 } 79 80 84 public org.objectweb.openccm.OpenCCM_DCI.AssemblyManager 85 next() 86 throws java.util.NoSuchElementException 87 { 88 if(index==connect.length) 89 throw new java.util.NoSuchElementException (); 90 index++; 91 return connect[index-1].objref; 92 } 93 94 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 114 public boolean 115 hasNext() 116 { 117 return index<connect.length; 118 } 119 120 public void 121 remove() 122 { 123 } 125 129 public void 130 rewind() 131 { 132 index=0; 134 connect=AMM.get_connections_assembly_managers(); 136 } 137 138 142 public int 143 length() 144 { 145 return connect.length; 146 } 147 148 149 } 150 151 private org.objectweb.openccm.OpenCCM_DCI.AssemblyManagerModelCCM AMM; 158 private String xml_descriptor; 160 161 private byte[] componentArchive; 163 private String model_name; 165 private ServerSocket serverSocket; 167 private java.io.File tmpZipFile ; 168 private AssemblyManagerIterator amIterator; 170 private String prefix="AssemblyInstanceManagement >> "; 172 private boolean removed=false; 174 private org.omg.Components.Cookie cookie; 176 177 180 private AssemblyManager 186 getAssemblyManager(String 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 201 public String 202 getModelName() 203 { 204 return model_name; 205 } 206 207 212 public void 213 setupXML(String 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 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 fos= new java.io.FileOutputStream (tmpZipFile); 243 fos.write(componentArchive); 244 fos.flush(); 245 fos.close(); 246 java.util.zip.ZipFile zipFile= 247 new java.util.zip.ZipFile (tmpZipFile); 248 249 for(java.util.Enumeration entries=zipFile.entries();entries.hasMoreElements();) 250 { 251 252 java.util.zip.ZipEntry ze=(java.util.zip.ZipEntry )entries.nextElement(); 253 if(ze.getName().endsWith(".cad")) 255 model_name=ze.getName(); 256 } 257 } 258 catch(Exception 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 268 { 269 AssemblyManagerIterator amIt=new AssemblyManagerIterator(); 270 271 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 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 } 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 327 } 328 329 330 334 public String 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 347 protected String 348 create_assembly_instanceXML() 349 throws org.ist.coach.DCI.CreationFailure 350 { 351 try{ 352 org.omg.Components.Cookie ck= 354 AMM.get_connection_assembly_factory() 355 .create_assembly_XML(xml_descriptor); 356 org.objectweb.openccm.OpenCCM_DCI.AssemblyManager AM= 358 AMM.get_connection_assembly_factory() 359 .lookup_assembly_manager(ck); 360 AMM.connect_assembly_managers(AM); 362 AM.subscribe_assembly_manager_removed 365 (AMM.get_consumer_assembly_manager_removed()); 366 AM.configuration_complete(); 367 AM.provide_assembly().build(); 369 370 return AM.ass_id(); 372 } 373 catch(Exception 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 384 getServerSocket() 385 throws Exception 386 { 387 if(serverSocket==null) 388 serverSocket=new ServerSocket (0); 389 return serverSocket; 390 } 391 392 protected InputStream 393 getInputStream() 394 throws Exception 395 { 396 return new FileInputStream (tmpZipFile); 397 } 398 399 protected String 400 create_assembly_componentArchive_instance() 401 throws org.ist.coach.DCI.CreationFailure 402 { 403 try{ 404 405 406 (new Thread 407 (new org.objectweb.openccm.deploytool.MicroServerHttp 408 (getServerSocket(), 409 getInputStream(), 410 "ComponentArchive"))).start(); 411 String path="http://" + 412 java.net.InetAddress.getLocalHost().getHostAddress() + ':' + 413 getServerSocket().getLocalPort() + '/' +"ComponentArchive"; 414 415 org.omg.Components.Cookie ck= 417 AMM.get_connection_assembly_factory() 418 .create_assembly(path); 419 org.objectweb.openccm.OpenCCM_DCI.AssemblyManager AM= 421 AMM.get_connection_assembly_factory() 422 .lookup_assembly_manager(ck); 423 AMM.connect_assembly_managers(AM); 425 AM.subscribe_assembly_manager_removed 428 (AMM.get_consumer_assembly_manager_removed()); 429 431 AM.provide_assembly().build(); 433 return AM.ass_id(); 435 } 436 catch(Exception 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 448 public String [] 449 get_assembly_instances() 450 throws org.ist.coach.DCI.UnknownAssembly 451 { 452 java.util.LinkedList ll = new java.util.LinkedList (); 454 for(amIterator.rewind();amIterator.hasNext();) 456 ll.addLast(amIterator.next().ass_id()); 457 458 return (String [])ll.toArray(new String [ll.size()]); 460 } 461 462 467 public AssemblyManager 468 get_assembly_manager(String instance_id) 469 throws org.ist.coach.DCI.UnknownAssembly 470 { 471 return getAssemblyManager(instance_id); 472 } 473 474 478 public void 479 destroy_assembly_instance(String instance_id) 480 throws org.ist.coach.DCI.DestroyError 481 { 482 try{ 483 getAssemblyManager(instance_id).remove() ; 484 return ; 486 } 487 catch(Exception e){ 488 throw new org.ist.coach.DCI.DestroyError(); 489 } 490 } 491 } 492 | Popular Tags |