1 19 20 package org.apache.geronimo.mavenplugins.car; 21 22 import java.io.File ; 23 import java.net.URI ; 24 import java.util.Arrays ; 25 import java.util.HashMap ; 26 import java.util.HashSet ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.Set ; 31 32 import org.apache.geronimo.deployment.PluginBootstrap2; 33 import org.apache.geronimo.gbean.AbstractName; 34 import org.apache.geronimo.gbean.AbstractNameQuery; 35 import org.apache.geronimo.gbean.GBeanData; 36 import org.apache.geronimo.gbean.GBeanInfo; 37 import org.apache.geronimo.gbean.ReferencePatterns; 38 import org.apache.geronimo.genesis.util.ArtifactItem; 39 import org.apache.geronimo.genesis.dependency.DependencyTree; 40 import org.apache.geronimo.kernel.Kernel; 41 import org.apache.geronimo.kernel.KernelFactory; 42 import org.apache.geronimo.kernel.KernelRegistry; 43 import org.apache.geronimo.kernel.Naming; 44 import org.apache.geronimo.kernel.config.ConfigurationData; 45 import org.apache.geronimo.kernel.config.ConfigurationManager; 46 import org.apache.geronimo.kernel.config.ConfigurationUtil; 47 import org.apache.geronimo.kernel.config.KernelConfigurationManager; 48 import org.apache.geronimo.kernel.log.GeronimoLogging; 49 import org.apache.geronimo.kernel.management.State; 50 import org.apache.geronimo.kernel.repository.DefaultArtifactManager; 51 import org.apache.geronimo.system.configuration.RepositoryConfigurationStore; 52 import org.apache.geronimo.system.repository.Maven2Repository; 53 import org.apache.geronimo.system.resolver.ExplicitDefaultArtifactResolver; 54 import org.apache.maven.archiver.MavenArchiveConfiguration; 55 import org.apache.maven.archiver.MavenArchiver; 56 import org.apache.maven.artifact.Artifact; 57 import org.apache.maven.plugin.MojoExecutionException; 58 import org.apache.maven.project.MavenProject; 59 import org.codehaus.plexus.archiver.jar.JarArchiver; 60 import org.codehaus.plexus.util.FileUtils; 61 62 70 public class PackageMojo 71 extends AbstractCarMojo 72 { 73 80 private MavenArchiveConfiguration archive = new MavenArchiveConfiguration(); 81 82 89 private String geronimoVersion = null; 90 91 98 private JarArchiver jarArchiver = null; 99 100 107 private File baseDirectory = null; 108 109 115 private File outputDirectory = null; 116 117 123 private File classesDirectory = null; 124 125 131 private String finalName = null; 132 133 140 private File repository = null; 141 142 148 private File targetRepository = null; 149 150 157 private String defaultDeploymentConfig = null; 158 159 164 private String [] deploymentConfigs; 165 166 172 private String deployerName = null; 173 174 180 private File planFile = null; 181 182 187 private File moduleFile = null; 188 189 194 private ArtifactItem module = null; 195 196 205 private File explicitResolutionProperties = null; 206 207 216 private ClasspathElement[] classpath = null; 217 218 224 private String classpathPrefix = null; 225 226 231 private boolean bootstrap = false; 232 233 237 private static ThreadLocal lookupHolder = new ThreadLocal (); 238 239 243 protected void doExecute() throws Exception { 244 FileUtils.forceDelete(targetRepository); 246 FileUtils.forceMkdir(targetRepository); 247 248 if (deploymentConfigs == null) { 250 deploymentConfigs = new String [] { 251 defaultDeploymentConfig 252 }; 253 } 254 log.debug("Deployment configs: " + Arrays.asList(deploymentConfigs)); 255 256 Set additionalArtifacts = new HashSet (); 262 for (int i=0; i<deploymentConfigs.length; i++) { 263 Artifact artifact = geronimoToMavenArtifact(org.apache.geronimo.kernel.repository.Artifact.create(deploymentConfigs[i])); 264 log.debug("Resolving deployer module: " + artifact); 265 Artifact resolved = resolveArtifact(artifact, true); 266 additionalArtifacts.add(resolved); 267 } 268 if (project.getDependencyArtifacts() == null) { 270 Set oldArtifacts = project.createArtifacts(dependencyHelper.getArtifactFactory(), null, null); 271 additionalArtifacts.addAll(oldArtifacts); 272 } else { 273 Set oldArtifacts = project.getDependencyArtifacts(); 274 additionalArtifacts.addAll(oldArtifacts); 275 } 276 project.setDependencyArtifacts(additionalArtifacts); 277 278 279 if (module != null) { 281 Artifact artifact = getArtifact(module); 282 moduleFile = artifact.getFile(); 283 log.debug("Using module file: " + moduleFile); 284 } 285 dependencies.setRootNode(dependencyHelper.getDependencies(project).getRootNode()); 286 287 generateExplicitVersionProperties(explicitResolutionProperties, dependencies); 288 289 lookupHolder.set(new ArtifactLookupImpl(new HashMap ())); 294 295 if (bootstrap) { 296 executeBootShell(); 297 } 298 else { 299 buildPackage(); 300 } 301 302 File archive = createArchive(); 304 305 project.getArtifact().setFile(archive); 307 } 308 309 private File getArtifactInRepositoryDir() { 310 314 File dir = new File (targetRepository, project.getGroupId().replace('.', '/')); 315 dir = new File (dir, project.getArtifactId()); 316 dir = new File (dir, project.getVersion()); 317 dir = new File (dir, project.getArtifactId() + "-" + project.getVersion() + ".car"); 318 319 return dir; 320 } 321 322 public void executeBootShell() throws Exception { 323 log.debug("Starting bootstrap shell..."); 324 325 PluginBootstrap2 boot = new PluginBootstrap2(); 326 327 boot.setBuildDir(outputDirectory); 328 boot.setCarFile(getArtifactInRepositoryDir()); 329 boot.setLocalRepo(repository); 330 boot.setPlan(planFile); 331 332 boot.setExpanded(true); 334 335 boot.bootstrap(); 336 } 337 338 341 private File createArchive() throws MojoExecutionException { 342 File archiveFile = getArchiveFile(outputDirectory, finalName, null); 343 344 MavenArchiver archiver = new MavenArchiver(); 345 archiver.setArchiver(jarArchiver); 346 archiver.setOutputFile(archiveFile); 347 348 try { 349 archiver.getArchiver().addDirectory(getArtifactInRepositoryDir()); 351 352 if (classesDirectory.isDirectory()) { 354 archiver.getArchiver().addDirectory(classesDirectory); 355 } 356 357 361 String [] includes = { 365 "LICENSE.txt", 366 "LICENSE", 367 "NOTICE.txt", 368 "NOTICE", 369 "DISCLAIMER.txt", 370 "DISCLAIMER" 371 }; 372 373 archiver.getArchiver().addDirectory(baseDirectory, "META-INF/", includes, new String [0]); 374 375 if (classpath != null) { 376 archive.addManifestEntry("Class-Path", getClassPath()); 377 } 378 379 archiver.createArchive(project, archive); 380 381 return archiveFile; 382 } 383 catch (Exception e) { 384 throw new MojoExecutionException("Failed to create archive", e); 385 } 386 } 387 388 private String getClassPath() throws MojoExecutionException { 389 StringBuffer buff = new StringBuffer (); 390 391 for (int i=0; i < classpath.length; i++) { 392 393 String entry = classpath[i].getEntry(); 394 if(entry != null) { 395 buff.append(entry); 396 } else { 397 Artifact artifact = getArtifact(classpath[i]); 398 399 403 String prefix = classpath[i].getClasspathPrefix(); 404 if (prefix == null) { 405 prefix = classpathPrefix; 406 } 407 408 if (prefix != null) { 409 buff.append(prefix); 410 411 if (!prefix.endsWith("/")) { 412 buff.append("/"); 413 } 414 } 415 416 File file = artifact.getFile(); 417 buff.append(file.getName()); 418 } 419 420 if (i + 1< classpath.length) { 421 buff.append(" "); 422 } 423 } 424 425 log.debug("Using classpath: " + buff); 426 427 return buff.toString(); 428 } 429 430 434 private static final String KERNEL_NAME = "geronimo.maven"; 435 436 440 private static Kernel kernel; 441 442 private static AbstractName targetConfigStoreAName; 443 444 private static AbstractName targetRepositoryAName; 445 446 private boolean targetSet; 447 448 private static DependencyTree dependencies = new DependencyTree(); 449 450 public void buildPackage() throws Exception { 451 log.info("Packaging module configuration: " + planFile); 452 453 Kernel kernel = createKernel(); 454 if (!targetSet) { 455 kernel.stopGBean(targetRepositoryAName); 456 kernel.setAttribute(targetRepositoryAName, "root", targetRepository.toURI()); 457 kernel.startGBean(targetRepositoryAName); 458 459 if (kernel.getGBeanState(targetConfigStoreAName) != State.RUNNING_INDEX) { 460 throw new IllegalStateException ("After restarted repository then config store is not running"); 461 } 462 463 targetSet = true; 464 } 465 466 log.debug("Starting configuration..."); 467 468 ConfigurationManager configurationManager = ConfigurationUtil.getConfigurationManager(kernel); 470 try { 471 for (int i=0; i<deploymentConfigs.length; i++) { 472 String artifactName = deploymentConfigs[i]; 473 org.apache.geronimo.kernel.repository.Artifact configName = 474 org.apache.geronimo.kernel.repository.Artifact.create(artifactName); 475 if (!configurationManager.isLoaded(configName)) { 476 configurationManager.loadConfiguration(configName); 477 configurationManager.startConfiguration(configName); 478 } 479 } 480 } finally { 481 ConfigurationUtil.releaseConfigurationManager(kernel, configurationManager); 482 } 483 484 log.debug("Deploying..."); 485 486 AbstractName deployer = locateDeployer(kernel); 487 invokeDeployer(kernel, deployer, targetConfigStoreAName.toString()); 488 } 490 491 494 private synchronized Kernel createKernel() throws Exception { 495 if (kernel != null) { 497 return kernel; 498 } 499 500 log.debug("Creating kernel..."); 501 502 kernel = KernelRegistry.getKernel(KERNEL_NAME); 504 if (kernel != null) { 505 return kernel; 506 } 507 508 GeronimoLogging geronimoLogging = GeronimoLogging.getGeronimoLogging("WARN"); 509 if (geronimoLogging == null) { 510 geronimoLogging = GeronimoLogging.DEBUG; 511 } 512 GeronimoLogging.initialize(geronimoLogging); 513 514 kernel = KernelFactory.newInstance().createKernel(KERNEL_NAME); 516 kernel.boot(); 517 518 bootDeployerSystem(); 519 520 return kernel; 521 } 522 523 531 private void bootDeployerSystem() throws Exception { 532 log.debug("Booting deployer system..."); 533 534 org.apache.geronimo.kernel.repository.Artifact baseId = 535 new org.apache.geronimo.kernel.repository.Artifact("geronimo", "packaging", "fixed", "car"); 536 Naming naming = kernel.getNaming(); 537 ConfigurationData bootstrap = new ConfigurationData(baseId, naming); 538 ClassLoader cl = getClass().getClassLoader(); 539 Set repoNames = new HashSet (); 540 541 GBeanData repoGBean = bootstrap.addGBean("SourceRepository", GBeanInfo.getGBeanInfo(Maven2RepositoryAdapter.class.getName(), cl)); 546 Maven2RepositoryAdapter.ArtifactLookup lookup = new Maven2RepositoryAdapter.ArtifactLookup() { 547 private Maven2RepositoryAdapter.ArtifactLookup getDelegate() { 548 return (Maven2RepositoryAdapter.ArtifactLookup) lookupHolder.get(); 549 } 550 551 public File getBasedir() { 552 return getDelegate().getBasedir(); 553 } 554 555 public File getLocation(final org.apache.geronimo.kernel.repository.Artifact artifact) { 556 return getDelegate().getLocation(artifact); 557 } 558 }; 559 repoGBean.setAttribute("lookup", lookup); 560 repoGBean.setAttribute("dependencies", dependencies); 561 repoNames.add(repoGBean.getAbstractName()); 562 563 GBeanData targetRepoGBean = bootstrap.addGBean("TargetRepository", GBeanInfo.getGBeanInfo(Maven2Repository.class.getName(), cl)); 565 URI targetRepositoryURI = targetRepository.toURI(); 566 targetRepoGBean.setAttribute("root", targetRepositoryURI); 567 repoNames.add(targetRepoGBean.getAbstractName()); 568 targetRepositoryAName = targetRepoGBean.getAbstractName(); 569 570 GBeanData artifactManagerGBean = bootstrap.addGBean("ArtifactManager", DefaultArtifactManager.GBEAN_INFO); 571 GBeanData artifactResolverGBean = bootstrap.addGBean("ArtifactResolver", ExplicitDefaultArtifactResolver.GBEAN_INFO); 572 artifactResolverGBean.setAttribute("versionMapLocation", explicitResolutionProperties.getAbsolutePath()); 573 ReferencePatterns repoPatterns = new ReferencePatterns(repoNames); 574 artifactResolverGBean.setReferencePatterns("Repositories", repoPatterns); 575 artifactResolverGBean.setReferencePattern("ArtifactManager", artifactManagerGBean.getAbstractName()); 576 577 Set storeNames = new HashSet (); 578 579 GBeanInfo configStoreInfo = GBeanInfo.getGBeanInfo(MavenConfigStore.class.getName(), cl); 581 GBeanData storeGBean = bootstrap.addGBean("ConfigStore", configStoreInfo); 582 if (configStoreInfo.getReference("Repository") != null) { 583 storeGBean.setReferencePattern("Repository", repoGBean.getAbstractName()); 584 } 585 storeNames.add(storeGBean.getAbstractName()); 586 587 GBeanInfo targetConfigStoreInfo = GBeanInfo.getGBeanInfo(RepositoryConfigurationStore.class.getName(), cl); 589 GBeanData targetStoreGBean = bootstrap.addGBean("TargetConfigStore", targetConfigStoreInfo); 590 if (targetConfigStoreInfo.getReference("Repository") != null) { 591 targetStoreGBean.setReferencePattern("Repository", targetRepoGBean.getAbstractName()); 592 } 593 storeNames.add(targetStoreGBean.getAbstractName()); 594 595 targetConfigStoreAName = targetStoreGBean.getAbstractName(); 596 targetSet = true; 597 598 GBeanData attrManagerGBean = bootstrap.addGBean("AttributeStore", MavenAttributeStore.GBEAN_INFO); 599 GBeanData configManagerGBean = bootstrap.addGBean("ConfigManager", KernelConfigurationManager.GBEAN_INFO); 600 configManagerGBean.setReferencePatterns("Stores", new ReferencePatterns(storeNames)); 601 configManagerGBean.setReferencePattern("AttributeStore", attrManagerGBean.getAbstractName()); 602 configManagerGBean.setReferencePattern("ArtifactManager", artifactManagerGBean.getAbstractName()); 603 configManagerGBean.setReferencePattern("ArtifactResolver", artifactResolverGBean.getAbstractName()); 604 configManagerGBean.setReferencePatterns("Repositories", repoPatterns); 605 606 ConfigurationUtil.loadBootstrapConfiguration(kernel, bootstrap, cl); 607 } 608 609 617 private AbstractName locateDeployer(final Kernel kernel) { 618 AbstractName name = new AbstractName(URI.create(deployerName)); 619 620 Iterator i = kernel.listGBeans(new AbstractNameQuery(name)).iterator(); 621 if (!i.hasNext()) { 622 throw new IllegalStateException ("No deployer found matching deployerName: " + name); 623 } 624 625 AbstractName deployer = (AbstractName)i.next(); 626 if (i.hasNext()) { 627 throw new IllegalStateException ("Multiple deployers found matching deployerName: " + name); 628 } 629 630 return deployer; 631 } 632 633 private static final String [] DEPLOY_SIGNATURE = { 634 boolean.class.getName(), 635 File .class.getName(), 636 File .class.getName(), 637 File .class.getName(), 638 Boolean.TYPE.getName(), 639 String .class.getName(), 640 String .class.getName(), 641 String .class.getName(), 642 String .class.getName(), 643 String .class.getName(), 644 String .class.getName(), 645 String .class.getName(), 646 String .class.getName(), 647 }; 648 649 private List invokeDeployer(final Kernel kernel, final AbstractName deployer, final String targetConfigStore) throws Exception { 650 Object [] args = { 651 Boolean.FALSE, planFile, 653 moduleFile, 654 null, Boolean.TRUE, null, null, null, null, null, null, null, targetConfigStore 664 }; 665 666 return (List ) kernel.invoke(deployer, "deploy", args, DEPLOY_SIGNATURE); 667 } 668 669 673 676 678 } 679 | Popular Tags |