1 29 30 package com.caucho.j2ee.deployserver; 31 32 import com.caucho.config.ConfigException; 33 import com.caucho.j2ee.deployclient.ProgressObjectImpl; 34 import com.caucho.j2ee.deployclient.TargetImpl; 35 import com.caucho.j2ee.deployclient.TargetModuleIDImpl; 36 import com.caucho.jmx.Jmx; 37 import com.caucho.loader.EnvironmentLocal; 38 import com.caucho.management.server.ArchiveDeployMXBean; 39 import com.caucho.util.L10N; 40 import com.caucho.vfs.Path; 41 import com.caucho.vfs.Vfs; 42 import com.caucho.vfs.WriteStream; 43 44 import javax.enterprise.deploy.spi.Target ; 45 import javax.enterprise.deploy.spi.TargetModuleID ; 46 import javax.enterprise.deploy.spi.exceptions.TargetException ; 47 import javax.enterprise.deploy.spi.status.ProgressObject ; 48 import javax.management.MalformedObjectNameException ; 49 import javax.management.ObjectName ; 50 import java.io.IOException ; 51 import java.io.InputStream ; 52 import java.util.ArrayList ; 53 import java.util.Set ; 54 import java.util.TreeSet ; 55 import java.util.logging.Level ; 56 import java.util.logging.Logger ; 57 import java.util.zip.ZipEntry ; 58 import java.util.zip.ZipInputStream ; 59 import java.util.zip.ZipOutputStream ; 60 61 public class DeploymentService 62 { 63 private static final L10N L = new L10N(DeploymentService.class); 64 private static final Logger log = Logger.getLogger(DeploymentService.class.getName()); 65 private static final EnvironmentLocal<DeploymentService> _local 66 = new EnvironmentLocal<DeploymentService>(); 67 68 public static DeploymentService getDeploymentService() 69 { 70 synchronized (_local) { 71 DeploymentService deploymentService = _local.get(); 72 73 if (deploymentService == null) { 74 deploymentService = new DeploymentService(); 75 _local.set(deploymentService); 76 } 77 78 return deploymentService; 79 } 80 } 81 82 private DeploymentService() 83 { 84 } 85 86 public TargetImpl[] getTargets() 87 throws IllegalStateException 88 { 89 ArrayList <String > names = new ArrayList <String >(); 90 91 try { 92 Set <ObjectName > objectNames = Jmx.getMBeanServer().queryNames(new ObjectName ("resin:type=EarDeploy,*"), null); 93 94 for (ObjectName objectName : objectNames) 95 names.add(objectName.getCanonicalName()); 96 } 97 catch (MalformedObjectNameException e) { 98 if (log.isLoggable(Level.WARNING)) 99 log.log(Level.WARNING, e.toString(), e); 100 } 101 102 try { 103 Set <ObjectName > objectNames = Jmx.getMBeanServer().queryNames(new ObjectName ("resin:type=WebAppDeploy,*"), null); 104 105 for (ObjectName objectName : objectNames) 106 names.add(objectName.getCanonicalName()); 107 } 108 catch (MalformedObjectNameException e) { 109 if (log.isLoggable(Level.WARNING)) 110 log.log(Level.WARNING, e.toString(), e); 111 112 } 113 114 try { 115 Set <ObjectName > objectNames = Jmx.getMBeanServer().queryNames(new ObjectName ("resin:type=ResourceDeploy,*"), null); 116 117 for (ObjectName objectName : objectNames) 118 names.add(objectName.getCanonicalName()); 119 } 120 catch (MalformedObjectNameException e) { 121 if (log.isLoggable(Level.WARNING)) 122 log.log(Level.WARNING, e.toString(), e); 123 } 124 125 final int size = names.size(); 126 127 TargetImpl[] targets = new TargetImpl[size]; 128 for (int i = 0; i < size; i++) { 129 targets[i] = new TargetImpl(names.get(i), ""); 130 } 131 132 return targets; 133 } 134 135 public TargetModuleID []getAvailableModules(String type) 136 throws TargetException , IllegalStateException 137 { 138 return new TargetModuleID [] {}; 139 } 140 141 private ArchiveDeployMXBean getMXBean(Target target) 142 throws MalformedObjectNameException 143 { 144 ArchiveDeployMXBean mxbean; 145 mxbean = (ArchiveDeployMXBean) Jmx.find(target.getName()); 146 return mxbean; 147 } 148 149 private String getExceptionMessage(Throwable exception) 150 { 151 if (exception == null) 152 return ""; 153 else if (exception instanceof ConfigException) 154 return exception.getMessage(); 155 else 156 return exception.toString(); 157 } 158 159 public ProgressObject distribute(TargetImpl []targets, 160 InputStream archiveIs, 161 DeploymentPlan plan) 162 throws IllegalStateException 163 { 164 String moduleID = plan.getName(); 165 166 ArrayList <TargetModuleIDImpl> targetModuleIDList = new ArrayList <TargetModuleIDImpl>(); 167 168 for (TargetImpl target : targets) { 169 targetModuleIDList.add(new TargetModuleIDImpl(target, moduleID)); 170 } 171 172 TargetModuleIDImpl[] targetModuleIDs = new TargetModuleIDImpl[targetModuleIDList.size()]; 173 174 targetModuleIDs = targetModuleIDList.toArray(targetModuleIDs); 175 176 ProgressObjectImpl progress = new ProgressObjectImpl(targetModuleIDs); 177 178 boolean failed = false; 179 StringBuilder message = new StringBuilder (); 180 181 Path archivePath = null; 182 183 for (TargetModuleIDImpl targetModuleID : targetModuleIDs) { 184 Throwable exception = null; 185 186 ArchiveDeployMXBean mxbean = null; 187 188 try { 189 mxbean = getMXBean(targetModuleID.getTarget()); 190 191 if ("ear".equals(plan.getArchiveType()) 192 && ! "EarDeploy".equals(mxbean.getType())) 193 continue; 194 else if ("war".equals(plan.getArchiveType()) 195 && ! "WebAppDeploy".equals(mxbean.getType())) 196 continue; 197 else if ("rar".equals(plan.getArchiveType()) 198 && ! "RarDeploy".equals(mxbean.getType())) 199 continue; 200 201 Path deployPath = Vfs.lookup(mxbean.getArchivePath(moduleID)); 202 203 deployPath.getParent().mkdirs(); 204 205 if (archivePath == null) { 206 createArchive(deployPath, plan, archiveIs); 207 archivePath = deployPath; 208 } 209 else { 210 WriteStream deployStream = deployPath.openWrite(); 211 212 try { 213 deployStream.writeFile(archivePath); 214 } 215 finally { 216 deployStream.close(); 217 } 218 } 219 220 mxbean.update(); 221 222 exception = mxbean.getConfigException(moduleID); 223 } 224 catch (Exception e) { 225 if (log.isLoggable(Level.INFO)) 226 log.log(Level.INFO, e.toString(), e); 227 228 exception = e; 229 } 230 231 if (exception != null) { 232 failed = true; 233 describe(message, targetModuleID, false, getExceptionMessage(exception)); 234 235 245 } 246 else 247 describe(message, targetModuleID, true); 248 } 249 250 if (failed) 251 progress.failed(L.l("deploy {0}", message)); 252 else 253 progress.completed(L.l("deploy {0}", message)); 254 255 return progress; 256 } 257 258 private void createArchive(Path archivePath, DeploymentPlan plan, InputStream archiveIs) 259 throws IOException 260 { 261 if (log.isLoggable(Level.FINER)) 262 log.log(Level.FINER, L.l("jsr88 creating archive {0}", archivePath)); 263 264 WriteStream archiveStream = null; 265 ZipInputStream zipInputStream = null; 266 ZipOutputStream zipOutputStream = null; 267 268 try { 269 archiveStream = archivePath.openWrite(); 270 zipOutputStream = new ZipOutputStream (archiveStream); 271 272 zipInputStream = new ZipInputStream (archiveIs); 273 274 ZipEntry zipEntry = zipInputStream.getNextEntry(); 275 276 TreeSet <String > entryNames = new TreeSet <String >(); 277 278 int copyCount = 0; 279 280 while (zipEntry != null) { 281 if (log.isLoggable(Level.FINEST)) 282 log.log(Level.FINEST, L.l("jsr88 copying entry {0}", zipEntry)); 283 284 entryNames.add(zipEntry.getName()); 285 286 zipOutputStream.putNextEntry(zipEntry); 287 288 for (int ch = zipInputStream.read(); ch != -1; ch = zipInputStream.read()) 289 zipOutputStream.write(ch); 290 291 zipEntry = zipInputStream.getNextEntry(); 292 293 copyCount++; 294 } 295 296 if (log.isLoggable(Level.FINER)) 297 log.log(Level.FINER, L.l("copied {0} entries", copyCount)); 298 299 if (archiveIs.read() != -1) { 300 if (log.isLoggable(Level.FINE)) 301 log.log(Level.FINE, L.l("unexpected data at end of archive")); 302 303 while (archiveIs.read() != -1) {} 304 } 305 306 int fileCount = 0; 307 308 for (DeploymentPlan.PlanFile file : plan.getFileList()) { 309 String zipEntryName = file.getPath(); 310 if (zipEntryName.startsWith("/")) 311 zipEntryName = zipEntryName.substring(1); 312 313 if (log.isLoggable(Level.FINEST)) 314 log.log(Level.FINEST, L.l("jsr88 plan file {0} output to {1}", file, zipEntryName)); 315 316 if (entryNames.contains(zipEntryName)) 317 log.log(Level.WARNING, L.l("plan file {0} overwrites existing file", zipEntryName)); 318 319 entryNames.add(zipEntryName); 320 321 zipEntry = new ZipEntry (zipEntryName); 322 zipOutputStream.putNextEntry(zipEntry); 323 file.writeToStream(zipOutputStream); 324 325 fileCount++; 326 } 327 328 if (log.isLoggable(Level.FINER)) 329 log.log(Level.FINER, L.l("created {0} entries from plan", fileCount)); 330 331 zipInputStream.close(); 332 zipInputStream = null; 333 334 zipOutputStream.close(); 335 zipOutputStream = null; 336 337 archiveStream.close(); 338 archiveStream = null; 339 } 340 finally { 341 if (zipInputStream != null) { 342 try { 343 zipInputStream.close(); 344 } 345 catch (Throwable ex) { 346 log.log(Level.FINER, ex.toString(), ex); 347 } 348 } 349 350 if (zipOutputStream != null) { 351 try { 352 zipOutputStream.close(); 353 } 354 catch (Throwable ex) { 355 log.log(Level.FINER, ex.toString(), ex); 356 } 357 } 358 359 if (archiveStream != null) { 360 try { 361 archiveStream.close(); 362 } 363 catch (Throwable ex) { 364 log.log(Level.FINER, ex.toString(), ex); 365 } 366 } 367 } 368 } 369 370 public ProgressObject start(TargetModuleID [] ids) 371 { 372 ProgressObjectImpl progress = new ProgressObjectImpl(ids); 373 374 boolean failed = false; 375 StringBuilder message = new StringBuilder (); 376 377 for (TargetModuleID targetModuleID : ids) { 378 if (log.isLoggable(Level.FINE)) 379 log.log(Level.FINE, L.l("starting {0}", targetModuleID.getModuleID())); 380 381 Throwable exception = null; 382 ArchiveDeployMXBean mxbean = null; 383 384 try { 385 mxbean = getMXBean(targetModuleID.getTarget()); 386 mxbean.start(targetModuleID.getModuleID()); 387 } 388 catch (Exception t) { 389 log.log(Level.INFO, t.toString(), t); 390 } 393 394 if (exception == null && mxbean != null) { 395 exception = mxbean.getConfigException(targetModuleID.getModuleID()); 396 exception = null; 398 } 399 400 if (exception != null) { 401 failed = true; 402 describe(message, targetModuleID, false, getExceptionMessage(exception)); 403 } 404 else 405 describe(message, targetModuleID, true); 406 } 407 408 if (failed) 409 progress.failed(L.l("start {0}", message)); 410 else 411 progress.completed(L.l("start {0}", message)); 412 413 return progress; 414 } 415 416 public ProgressObject stop(TargetModuleID [] ids) 417 { 418 ProgressObjectImpl progress = new ProgressObjectImpl(ids); 419 420 boolean failed = false; 421 StringBuilder message = new StringBuilder (); 422 423 for (TargetModuleID targetModuleID : ids) { 424 if (log.isLoggable(Level.FINE)) 425 log.log(Level.FINE, L.l("stopping {0}", targetModuleID.getModuleID())); 426 427 Throwable exception = null; 428 ArchiveDeployMXBean mxbean = null; 429 430 try { 431 mxbean = getMXBean(targetModuleID.getTarget()); 432 mxbean.stop(targetModuleID.getModuleID()); 433 } 434 catch (Exception t) { 435 log.log(Level.INFO, t.toString(), t); 436 exception = t; 437 } 438 439 if (exception != null) { 440 failed = true; 441 describe(message, targetModuleID, false, getExceptionMessage(exception)); 442 } 443 else 444 describe(message, targetModuleID, true); 445 } 446 447 if (failed) 448 progress.failed(L.l("stop {0}", message)); 449 else 450 progress.completed(L.l("stop {0}", message)); 451 452 return progress; 453 } 454 455 public ProgressObject undeploy(TargetModuleID []ids) 456 throws IllegalStateException 457 { 458 ProgressObjectImpl progress = new ProgressObjectImpl(ids); 459 460 boolean failed = false; 461 StringBuilder message = new StringBuilder (); 462 463 for (TargetModuleID targetModuleID : ids) { 464 if (log.isLoggable(Level.FINE)) 465 log.log(Level.FINE, L.l("undeploying {0}", targetModuleID.getModuleID())); 466 467 ArchiveDeployMXBean mxbean = null; 468 Throwable exception = null; 469 470 try { 471 mxbean = getMXBean(targetModuleID.getTarget()); 472 mxbean.undeploy(targetModuleID.getModuleID()); 473 } 474 catch (Throwable t) { 475 log.log(Level.INFO, t.toString(), t); 476 exception = t; 477 } 478 479 if (exception != null) { 480 failed = true; 481 describe(message, targetModuleID, false, getExceptionMessage(exception)); 482 } 483 else 484 describe(message, targetModuleID, true); 485 } 486 487 if (failed) 488 progress.failed(L.l("undeploy {0}", message)); 489 else 490 progress.completed(L.l("undeploy {0}", message)); 491 492 return progress; 493 } 494 495 private void describe(StringBuilder builder, 496 TargetModuleID targetModuleID, 497 boolean success) 498 { 499 describe(builder, targetModuleID, success, null); 500 } 501 502 private void describe(StringBuilder builder, 503 TargetModuleID targetModuleID, 504 boolean success, 505 String message) 506 { 507 if (builder.length() > 0) 508 builder.append(", "); 509 510 if (success) 511 builder.append(L.l("successful for target {0} module {1}", 512 targetModuleID.getTarget().getName(), 513 targetModuleID.getModuleID())); 514 else { 515 Thread.dumpStack(); 516 builder.append(L.l("failed for target {0} module {1}", 517 targetModuleID.getTarget().getName(), 518 targetModuleID.getModuleID())); 519 } 520 521 if (message != null) { 522 builder.append(" '"); 523 builder.append(message); 524 builder.append("'"); 525 } 526 } 527 } 528
| Popular Tags
|