1 23 24 package com.sun.enterprise.deployment.client; 25 26 import com.sun.appserv.management.base.AMX; 27 import com.sun.appserv.management.client.AppserverConnectionSource; 28 import com.sun.appserv.management.client.ConnectionSource; 29 import com.sun.appserv.management.client.ProxyFactory; 30 import com.sun.appserv.management.deploy.DeploymentMgr; 31 import com.sun.appserv.management.deploy.DeploymentProgress; 32 import com.sun.appserv.management.deploy.DeploymentProgressImpl; 33 import com.sun.appserv.management.deploy.DeploymentSourceImpl; 34 import com.sun.appserv.management.deploy.DeploymentSupport; 35 import com.sun.enterprise.deployapi.ProgressObjectImpl; 36 import com.sun.enterprise.deployapi.SunTarget; 37 import com.sun.enterprise.deployment.backend.DeploymentStatus; 38 import com.sun.enterprise.deployment.deploy.shared.Archive; 39 import com.sun.enterprise.deployment.deploy.shared.MemoryMappedArchive; 40 import com.sun.enterprise.deployment.util.DeploymentProperties; 41 import com.sun.enterprise.deployment.util.FileUploadUtil; 42 import com.sun.logging.LogDomains; 43 import com.sun.enterprise.util.i18n.StringManager; 44 45 import java.io.BufferedInputStream ; 46 import java.io.ByteArrayInputStream ; 47 import java.io.EOFException ; 48 import java.io.File ; 49 import java.io.FileInputStream ; 50 import java.io.IOException ; 51 import java.net.URI ; 52 import java.net.InetAddress ; 53 import java.net.UnknownHostException ; 54 import java.util.HashMap ; 55 import java.util.Map ; 56 import java.util.Set ; 57 import java.util.logging.*; 58 59 import javax.enterprise.deploy.shared.ModuleType ; 60 import javax.enterprise.deploy.shared.StateType ; 61 import javax.enterprise.deploy.spi.Target ; 62 import javax.management.Notification ; 63 64 public class DeployAction extends ProgressObjectImpl { 65 66 private DeploymentMgr deplMgr = null; 67 private static int SLEEP_TIME = 100; 68 private static long TIMEOUT_LOOPS = 1000000000 / SLEEP_TIME; 69 70 private static String JMX_UPLOAD_CHUNK_SIZE = "jmx.upload.chunk.size"; 73 private static String HTTP_PROXYHOST = "http.proxyHost"; 75 76 private String jmxUploadChunkSizeProp = 77 System.getProperty(JMX_UPLOAD_CHUNK_SIZE); 78 79 private String httpProxyHostProp = System.getProperty(HTTP_PROXYHOST); 80 81 private static StringManager localStrings = StringManager.getManager(DeployAction.class); 82 private static Logger _logger = 83 LogDomains.getLogger(LogDomains.DPL_LOGGER); 84 85 public DeployAction(SunTarget[] targets) { 86 super(targets); 87 } 88 89 92 private Object uploadArchive(Archive module) throws IOException { 93 94 long totalSize = module.getArchiveSize(); 95 int chunkSize = 32 * 1024; 96 if (jmxUploadChunkSizeProp != null && 97 jmxUploadChunkSizeProp.length() > 0) { 98 chunkSize = Integer.parseInt(jmxUploadChunkSizeProp); 99 } 100 Object uploadID = null; 101 long remaining = totalSize; 102 BufferedInputStream bis = null; 103 try { 104 String name = getArchiveName(module); 105 if (module instanceof MemoryMappedArchive) { 106 byte[] bytes = ((MemoryMappedArchive) module).getByteArray(); 108 bis = new BufferedInputStream (new ByteArrayInputStream (bytes)); 109 } else { 110 bis = new BufferedInputStream (new FileInputStream (new File (module.getURI().getPath()))); 111 } 112 uploadID = deplMgr.initiateFileUpload(name, totalSize); 113 while(remaining != 0) { 114 int actual = (remaining < chunkSize) ? (int) remaining : chunkSize; 115 byte[] bytes = new byte[actual]; 116 try { 117 bis.read(bytes); 118 } catch (EOFException eof) { 119 break; 120 } 121 _logger.log(Level.FINE, "Uploading one chunk..."); 122 deplMgr.uploadBytes(uploadID, bytes); 123 remaining -= actual; 124 } 125 } finally { 126 if(bis!=null) { 127 bis.close(); 128 } 129 } 130 return uploadID; 131 } 132 133 private String uploadArchiveOverHTTP(ServerConnectionIdentifier serverId, 135 Archive module) throws Exception { 136 return FileUploadUtil.uploadToServlet(serverId.getHostName(), 137 Integer.toString(serverId.getHostPort()), serverId.getUserName(), 138 serverId.getPassword(), module); 139 } 140 141 public void run() { 142 ConnectionSource dasConnection= (ConnectionSource) args[0]; 143 Archive deployArchive = (Archive) args[1]; 144 Archive deployPlan = (Archive) args[2]; 145 Map deployOptions = (Map ) args[3]; 146 SunTarget[] targetList = (SunTarget[]) args[4]; 147 SunTarget domain = (SunTarget) args[5]; 148 boolean isLocalConnectionSource = ((Boolean ) args[6]).booleanValue(); 149 ServerConnectionIdentifier serverId = 150 (ServerConnectionIdentifier) args[7]; 151 Object archiveUploadID = null; 152 Object planUploadID = null; 153 Map deployedTargets = null; 154 Object deployActionID = null; 155 boolean isDirectoryDeploy = false; 156 boolean isRedeploy = false; 157 158 if (deployArchive == null) { 163 setupForAbnormalExit(localStrings.getString("enterprise.deployment.client.archive_not_specified"), domain); 164 return; 165 } 166 if (deployArchive.getURI() != null) { 167 168 File tmpFile = new File (deployArchive.getURI().getPath()); 169 if(!tmpFile.exists()) { 170 setupForAbnormalExit(localStrings.getString("enterprise.deployment.client.archive_not_in_location"), domain); 171 return; 172 } 173 if(!tmpFile.canRead()) { 174 setupForAbnormalExit(localStrings.getString("enterprise.deployment.client.archive_no_read_permission"), domain); 175 return; 176 } 177 if (tmpFile.isDirectory()) { 178 isDirectoryDeploy = true; 179 } 180 } 181 182 try { 183 this.moduleID = (String )deployOptions.get(DeploymentProperties.DEPLOY_OPTION_NAME_KEY); 185 186 if(("false".equals(deployOptions.get(DeploymentProperties.DEPLOY_OPTION_FORCE_KEY))) && 188 (isModuleDeployed(dasConnection, moduleID)) ) { 189 setupForAbnormalExit(localStrings.getString("enterprise.deployment.client.deploy_error_module_exists"), domain); 190 return; 191 } 192 193 deplMgr = ProxyFactory.getInstance(dasConnection).getDomainRoot().getDeploymentMgr(); 194 195 199 if( (targetList.length == 1) && (TargetType.STAND_ALONE_SERVER.equals(targetList[0].getTargetType())) && !("server".equals(targetList[0].getName())) ) { 200 deployOptions.put(DeploymentProperties.WSDL_TARGET_HINT, targetList[0].getName()); 201 } 202 203 if( ("true".equals(deployOptions.get(DeploymentProperties.DEPLOY_OPTION_FORCE_KEY))) && 205 (isModuleDeployed(dasConnection, moduleID)) ) { 206 isRedeploy = true; 207 208 deployedTargets = DeploymentClientUtils.getDeployedTargetList(dasConnection, moduleID); 210 211 if(DeploymentClientUtils.isNewTarget(deployedTargets, targetList)) { 214 setupForAbnormalExit(localStrings.getString("enterprise.deployment.client.useCreateAppRef", 215 moduleID), domain); 216 } 217 218 if (deployedTargets.size() > 0) { 220 if ((TargetType.DOMAIN.equals(targetList[0].getName()))) { 223 DeploymentFacility deploymentFacility; 224 if(isLocalConnectionSource) { 225 deploymentFacility = DeploymentFacilityFactory.getLocalDeploymentFacility(); 226 } else { 227 deploymentFacility = DeploymentFacilityFactory.getDeploymentFacility(); 228 } 229 deploymentFacility.connect( 230 targetList[0].getConnectionInfo()); 231 Set nameSet = deployedTargets.keySet(); 232 String [] targetNames = (String [])nameSet.toArray( 233 new String [nameSet.size()]); 234 Target[] targetList2 = 235 deploymentFacility.createTargets(targetNames); 236 if (targetList2 == null) { 237 setupForAbnormalExit(localStrings.getString("enterprise.deployment.client.createTargetsFailed"), domain); 238 return; 239 } 240 targetList = new SunTarget[targetList2.length]; 241 for (int ii = 0; ii < targetList2.length; ii++) { 242 targetList[ii] = (SunTarget)targetList2[ii]; 243 } 244 } 245 else if (!DeploymentClientUtils.isTargetListComplete( 248 deployedTargets, targetList)) { 249 setupForAbnormalExit( 250 localStrings.getString("enterprise.deployment.client.specifyAllTargets", moduleID, "redeploy"), 251 domain); 252 return; 253 } 254 255 Map options = new HashMap (); 257 options.putAll(deployOptions); 258 RollBackAction undeplRollback = new RollBackAction(RollBackAction.DELETE_APP_REF_OPERATION, 259 moduleID, deployOptions); 260 for(int i=0; i<targetList.length; i++) { 261 options.put(DeploymentProperties.DEPLOY_OPTION_CASCADE_KEY, "true"); 262 263 266 DeploymentClientUtils.setResourceOptions( 267 options, 268 DeploymentProperties.RES_UNDEPLOYMENT, 269 targetList[i].getName()); 270 DeploymentStatus stat = 271 DeploymentClientUtils.stopApplication( 272 dasConnection.getExistingMBeanServerConnection(), 273 moduleID, targetList[i], options); 274 checkStatusAndAddStage(targetList[i], null, localStrings.getString("enterprise.deployment.client.redeploy_stop", targetList[i].getName()) , dasConnection, stat); 275 276 options.put(DeploymentProperties.DEPLOY_OPTION_CASCADE_KEY, "false"); 278 stat = DeploymentClientUtils.deleteApplicationReference( 279 dasConnection.getExistingMBeanServerConnection(), 280 moduleID, targetList[i], options); 281 if(!checkStatusAndAddStage(targetList[i], undeplRollback, localStrings.getString("enterprise.deployment.client.redeploy_remove_ref", targetList[i].getName()), dasConnection, stat)) { 282 return; 283 } 284 undeplRollback.addTarget(targetList[i], RollBackAction.APP_REF_DELETED); 285 } 286 } 287 } 288 289 deployActionID = deplMgr.initDeploy(); 291 292 Map dupOptions = new HashMap (); 297 dupOptions.putAll(deployOptions); 298 dupOptions.put(DeploymentProperties.DEPLOY_OPTION_ENABLE_KEY, Boolean.TRUE.toString()); 299 300 if ((TargetType.DOMAIN.equals(targetList[0].getName()))) { 303 if (isRedeploy) { 304 DeploymentClientUtils.setResourceOptions( 305 dupOptions, 306 DeploymentProperties.RES_REDEPLOYMENT, 307 targetList); 308 } else { 309 DeploymentClientUtils.setResourceOptions( 310 dupOptions, 311 DeploymentProperties.RES_DEPLOYMENT, 312 targetList); 313 } 314 } else { 315 DeploymentClientUtils.setResourceOptions( 316 dupOptions, 317 DeploymentProperties.RES_NO_OP, 318 targetList); 319 } 320 321 if(!isDirectoryDeploy && !isLocalConnectionSource) { 324 326 long startTime = System.currentTimeMillis(); 327 long endTime = startTime; 328 329 if ( serverId.isSecure() || 334 (jmxUploadChunkSizeProp != null && 335 jmxUploadChunkSizeProp.length() > 0) || 336 (httpProxyHostProp != null && 337 httpProxyHostProp.length() > 0) ){ 338 archiveUploadID = uploadArchive(deployArchive); 340 341 if (deployPlan != null){ 343 if (deployPlan.getURI()!=null) { 344 File f = new File (deployPlan.getURI().getPath()); 345 if (f.length()!= 0) { 346 planUploadID = uploadArchive(deployPlan); 347 } 348 } 349 } 350 351 endTime = System.currentTimeMillis(); 352 353 deplMgr.startDeploy(deployActionID, archiveUploadID, planUploadID, dupOptions); 355 } else { 356 String archivePath = uploadArchiveOverHTTP(serverId, 358 deployArchive); 359 DeploymentSourceImpl archiveSource = 360 new DeploymentSourceImpl(archivePath, true, 361 new String [1], new String [1], new String [1], 362 new HashMap ()); 363 364 String planPath = null; 365 DeploymentSourceImpl planSource = null; 366 367 if (deployPlan != null){ 369 if (deployPlan.getURI()!=null){ 370 File f = new File (deployPlan.getURI().getPath()); 371 if (f.length()!= 0) { 372 planPath = uploadArchiveOverHTTP(serverId, deployPlan); 373 planSource = 374 new DeploymentSourceImpl(planPath, true, 375 new String [1], new String [1], new String [1], 376 new HashMap ()); 377 } 378 } 379 } 380 381 endTime = System.currentTimeMillis(); 382 383 deplMgr.startDeploy(deployActionID, archiveSource.asMap(), 384 planSource == null ? null : planSource.asMap(), 385 dupOptions); 386 } 387 _logger.log(Level.FINE, 388 "time in upload: " + (endTime-startTime)); 389 } else { 390 if((isDirectoryDeploy) && (!isDomainLocal(domain))) { 392 setupForAbnormalExit(localStrings.getString("enterprise.deployment.client.domainNotLocal"), 393 domain); 394 return; 395 } 396 DeploymentSourceImpl archive = new DeploymentSourceImpl(deployArchive.getURI().getPath(), true, 397 new String [1], new String [1], new String [1], new HashMap ()); 398 deplMgr.startDeploy(deployActionID, archive.asMap(), null, dupOptions); 401 } 402 403 if(deployActionID == null) { 405 setupForAbnormalExit(localStrings.getString("enterprise.deployment.client.no_deployment_id"), domain); 406 return; 407 } 408 409 boolean done = false; 411 int waitLoopCount = 0; 412 com.sun.appserv.management.deploy.DeploymentStatus finalStatusFromMBean = null; 413 do { 414 Notification [] notifs = deplMgr.takeNotifications(deployActionID); 415 for(int i=0; i<notifs.length; i++) { 416 Map notifType = (Map ) notifs[i].getUserData(); 417 if(notifType.get(deplMgr.NOTIF_DEPLOYMENT_COMPLETED_STATUS_KEY) != null) { 418 finalStatusFromMBean = 419 DeploymentSupport.mapToDeploymentStatus((Map )deplMgr.getFinalDeploymentStatus(deployActionID)); 420 done = true; 421 } else if(notifType.get(deplMgr.NOTIF_DEPLOYMENT_PROGRESS_KEY) != null) { 422 DeploymentProgress prog = DeploymentSupport.mapToDeploymentProgress((Map )notifType.get(deplMgr.NOTIF_DEPLOYMENT_PROGRESS_KEY)); 423 String progStr = prog.getDescription() + " : " + prog.getProgressPercent() + "%"; 424 fireProgressEvent(StateType.RUNNING, progStr, domain); 425 } 426 } 427 if(!done) { 428 if(waitLoopCount > TIMEOUT_LOOPS) { 429 setupForAbnormalExit(localStrings.getString("enterprise.deployment.client.deployment_time_out"), domain); 430 return; 431 } 432 try { 433 Thread.sleep(SLEEP_TIME); 434 } catch(InterruptedException e) { 435 } 437 } 438 waitLoopCount++; 439 } while(!done); 440 441 DeploymentStatus tmp = DeploymentClientUtils.getDeploymentStatusFromAdminStatus(finalStatusFromMBean); 442 443 if(!checkStatusAndAddStage( 444 domain, null, 445 localStrings.getString("enterprise.deployment.client.deploy_in_domain"), 446 dasConnection, tmp)) { 447 return; 448 } 449 450 if (moduleID == null) { 452 moduleID = tmp.getProperty(DeploymentStatus.MODULE_ID); 453 } 454 455 String key = moduleID + DeploymentStatus.KEY_SEPARATOR + DeploymentStatus.MODULE_TYPE; 456 this.moduleType = ModuleType.getModuleType( 457 (new Integer (tmp.getProperty(key))).intValue()); 458 459 RollBackAction rollback = new RollBackAction(RollBackAction.DEPLOY_OPERATION, moduleID, deployOptions); 461 462 if(!(TargetType.DOMAIN.equals(targetList[0].getName()))) { 464 for(int i=0; i<targetList.length; i++) { 465 466 if(deployedTargets != null) { 468 dupOptions.put(DeploymentProperties.DEPLOY_OPTION_ENABLE_KEY, deployedTargets.get(targetList[i].getName()).toString()); 469 } else { 470 dupOptions.put(DeploymentProperties.DEPLOY_OPTION_ENABLE_KEY, deployOptions.get(DeploymentProperties.DEPLOY_OPTION_ENABLE_KEY)); 471 } 472 DeploymentStatus stat = 473 DeploymentClientUtils.createApplicationReference( 474 dasConnection.getExistingMBeanServerConnection(), 475 moduleID, targetList[i], dupOptions); 476 if(!checkStatusAndAddStage(targetList[i], rollback, 477 localStrings.getString("enterprise.deployment.client.deploy_create_ref", targetList[i].getName()), dasConnection, stat)) { 478 return; 479 } 480 rollback.addTarget(targetList[i], rollback.APP_REF_CREATED); 481 482 497 if ((deployedTargets != null) && 498 (Boolean.FALSE.equals(deployedTargets.get(targetList[i].getName())))) { 499 continue; 500 } 501 502 505 if (isRedeploy) { 506 DeploymentClientUtils.setResourceOptions( 507 deployOptions, 508 DeploymentProperties.RES_REDEPLOYMENT, 509 targetList[i].getName()); 510 } else { 511 DeploymentClientUtils.setResourceOptions( 512 deployOptions, 513 DeploymentProperties.RES_DEPLOYMENT, 514 targetList[i].getName()); 515 } 516 517 stat = DeploymentClientUtils.startApplication( 518 dasConnection.getExistingMBeanServerConnection(), 519 moduleID, targetList[i], deployOptions); 520 checkStatusAndAddStage(targetList[i], null, 521 localStrings.getString("enterprise.deployment.client.deploy_start", targetList[i].getName()), dasConnection, stat, true); 522 } 523 } 524 525 if ( !isLocalConnectionSource ) { 527 try { 528 DeploymentClientUtils.doWsdlFilePublishing(tmp, dasConnection); 529 } catch (Exception wsdlEx) { 530 DeploymentStatus newStatus = new DeploymentStatus(); 531 newStatus.setStageStatus(DeploymentStatus.FAILURE); 532 newStatus.setStageStatusMessage(wsdlEx.getMessage()); 533 newStatus.setStageException(wsdlEx); 534 checkStatusAndAddStage(domain, rollback, 535 localStrings.getString("enterprise.deployment.client.deploy_publish_wsdl"), dasConnection, newStatus); 536 String msg = localStrings.getString("enterprise.deployment.client.deploy_publish_wsdl_exception", wsdlEx.getMessage()); 537 setupForAbnormalExit(msg, domain); 538 return; 539 } 540 } 541 542 initializeTargetModuleIDForAllServers( 543 tmp, dasConnection.getMBeanServerConnection(false)); 544 545 setupForNormalExit(localStrings.getString("enterprise.deployment.client.deploy_application", moduleID), domain); 546 } catch (Throwable ioex) { 547 finalDeploymentStatus.setStageException(ioex); 548 setupForAbnormalExit(localStrings.getString("enterprise.deployment.client.deploy_application_failed", ioex.getMessage()), domain); 549 return; 550 } 551 } 552 553 private boolean isModuleDeployed(ConnectionSource dasConnection, String moduleID) throws Exception { 554 return DeploymentClientUtils.isModuleDeployed( 555 dasConnection.getExistingMBeanServerConnection(), moduleID); 556 } 557 558 private boolean isDomainLocal(SunTarget domain) { 559 if("localhost".equalsIgnoreCase(domain.getHostName())) { 561 return true; 562 } 563 564 try { 566 InetAddress lh = InetAddress.getLocalHost(); 567 if(domain.getHostName().equalsIgnoreCase(lh.getCanonicalHostName())) { 568 return true; 569 } 570 if(domain.getHostName().equalsIgnoreCase(lh.getHostName())) { 571 return true; 572 } 573 if(domain.getHostName().equalsIgnoreCase(lh.getHostAddress())) { 574 return true; 575 } 576 } catch (UnknownHostException ex) { 577 return false; 578 } 579 return false; 580 } 581 582 private String getArchiveName(Archive archive) { 583 584 if (archive.getURI()==null){ 585 return null; 586 } 587 String name = archive.getURI().getPath(); 588 if (name != null) { 589 return name.substring(name.lastIndexOf("/")+1); 590 } 591 return name; 592 } 593 } 594 | Popular Tags |