1 19 20 package org.apache.geronimo.mavenplugins.geronimo.module; 21 22 import java.io.File ; 23 import java.util.List ; 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 27 import javax.enterprise.deploy.spi.DeploymentManager ; 28 import javax.enterprise.deploy.spi.Target ; 29 import javax.enterprise.deploy.spi.TargetModuleID ; 30 import javax.enterprise.deploy.spi.status.ProgressObject ; 31 import javax.enterprise.deploy.spi.status.DeploymentStatus ; 32 33 import org.apache.maven.artifact.Artifact; 34 import org.apache.maven.plugin.MojoExecutionException; 35 36 import org.apache.geronimo.mavenplugins.geronimo.ModuleConfig; 37 38 45 public class DeployModuleMojo 46 extends ModuleMojoSupport 47 { 48 56 protected File moduleArchive = null; 57 58 66 private File modulePlan = null; 67 68 74 private boolean startModules = false; 75 76 protected void doExecute() throws Exception { 77 List completed = new ArrayList (); 78 79 if (moduleArchive != null || modulePlan != null) { 80 log.info("Using non-artifact based module archive: " + moduleArchive); 81 log.info("Using non-artifact based plan: " + modulePlan); 82 83 TargetModuleID [] ids = distribute(moduleArchive, modulePlan); 84 completed.add(ids); 85 } 86 else if (modules == null || modules.length == 0) { 87 throw new MojoExecutionException("At least one module configuration (or moduleArchive) must be specified"); 88 } 89 90 if (modules != null && modules.length != 0) { 91 log.info("Using artifact based module archive(s)..."); 92 93 for (int i=0; i<modules.length; i++) { 94 TargetModuleID [] ids = distribute(getModuleArchive(modules[i]), modules[i].getPlan()); 95 completed.add(ids); 96 } 97 } 98 99 if (startModules) { 100 log.info("Starting modules..."); 101 102 Iterator iter = completed.iterator(); 103 while (iter.hasNext()) { 104 TargetModuleID [] moduleIds = (TargetModuleID [])iter.next(); 105 for (int i=0; i < moduleIds.length; i++) { 106 String url = moduleIds[i].getWebURL(); 107 log.info("Starting module: " + moduleIds[i].getModuleID() + (url == null ? "" : ("; URL: " + url))); 108 } 109 110 ProgressObject progress = getDeploymentManager().start(moduleIds); 111 DeploymentStatus status = waitFor(progress); 112 113 if (status.isFailed()) { 114 throw new MojoExecutionException("Failed to start modules: " + status.getMessage()); 115 } 116 117 log.info("Started module(s):"); 118 logModules(moduleIds, " "); 119 } 120 } 121 } 122 123 private File getModuleArchive(final ModuleConfig module) throws MojoExecutionException { 124 Artifact artifact = getArtifact(module); 125 126 File file = artifact.getFile(); 127 if (file == null) { 128 throw new MojoExecutionException("Module artifact does not have an attached file: " + module); 129 } 130 131 String type = artifact.getType(); 132 log.debug("Artifact file is: " + file + " (" + type + ")"); 133 134 if ((!"war".equals(type)) && 135 (!"ear".equals(type)) && 136 (!"rar".equals(type)) && 137 (!"jar".equals(type))) 138 { 139 throw new MojoExecutionException("Module does not look like a J2EE archive: " + module); 140 } 141 142 return file; 143 } 144 145 private TargetModuleID [] distribute(final File file, final File plan) throws Exception { 146 log.info("Distributing module artifact: " + file + " with plan " + plan); 147 148 DeploymentManager manager = getDeploymentManager(); 149 Target [] targets = manager.getTargets(); 150 ProgressObject progress = manager.distribute(targets, file, plan); 151 DeploymentStatus status = waitFor(progress); 152 153 if (status.isFailed()) { 154 if (status.getMessage().indexOf("already exists") < 0 ) { 158 throw new MojoExecutionException("Distribution failed: " + status.getMessage()); 159 } 160 log.info("Module already exists"); 161 } 162 163 164 return (progress != null) ? progress.getResultTargetModuleIDs() : null; 165 } 166 167 protected String getFullClassName() { 168 return this.getClass().getName(); 169 } 170 } 171 | Popular Tags |