1 19 20 package org.apache.geronimo.mavenplugins.geronimo.module; 21 22 import java.io.IOException ; 23 import java.util.List ; 24 import java.util.ArrayList ; 25 26 import javax.enterprise.deploy.spi.DeploymentManager ; 27 import javax.enterprise.deploy.spi.Target ; 28 import javax.enterprise.deploy.spi.TargetModuleID ; 29 import javax.enterprise.deploy.spi.status.ProgressObject ; 30 import javax.enterprise.deploy.spi.status.ProgressListener ; 31 import javax.enterprise.deploy.spi.status.ProgressEvent ; 32 import javax.enterprise.deploy.spi.status.DeploymentStatus ; 33 import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException ; 34 import javax.enterprise.deploy.shared.factories.DeploymentFactoryManager ; 35 36 import org.apache.geronimo.deployment.plugin.factories.DeploymentFactoryImpl; 37 import org.apache.geronimo.mavenplugins.geronimo.ModuleConfig; 38 import org.apache.geronimo.mavenplugins.geronimo.reporting.ReportingMojoSupport; 39 40 import org.apache.geronimo.genesis.util.ArtifactItem; 41 42 import org.apache.maven.plugin.MojoExecutionException; 43 44 49 public abstract class ModuleMojoSupport 50 extends ReportingMojoSupport 51 { 52 private static final String URI_PREFIX = "deployer:geronimo:jmx"; 53 54 60 protected ModuleConfig[] modules; 61 62 65 private DeploymentManager deploymentManager; 66 67 75 protected DeploymentManager getDeploymentManager() throws IOException , DeploymentManagerCreationException { 76 if (deploymentManager == null) { 77 DeploymentFactoryManager manager = DeploymentFactoryManager.getInstance(); 79 manager.registerDeploymentFactory(new DeploymentFactoryImpl()); 80 81 String uri = URI_PREFIX + "://" + hostname + ":" + port; 82 83 DeploymentFactoryManager factoryManager = DeploymentFactoryManager.getInstance(); 84 deploymentManager = factoryManager.getDeploymentManager(uri, username, password); 85 } 86 87 return deploymentManager; 88 } 89 90 98 protected DeploymentStatus waitFor(final ProgressObject progress) throws InterruptedException { 99 assert progress != null; 100 101 105 ProgressListener listener = new ProgressListener () 106 { 107 public void handleProgressEvent(final ProgressEvent event) { 108 DeploymentStatus status = event.getDeploymentStatus(); 109 110 if (!status.isRunning()) { 111 synchronized (progress) { 112 progress.notify(); 113 } 114 } 115 } 116 }; 117 118 progress.addProgressListener(listener); 119 120 synchronized (progress) { 121 while (progress.getDeploymentStatus().isRunning()) { 122 progress.wait(); 123 } 124 } 125 126 return progress.getDeploymentStatus(); 127 } 128 129 135 protected String getModuleId(final ArtifactItem item) { 136 assert item != null; 137 138 return item.getGroupId() + "/" + item.getArtifactId() + "/" + item.getVersion() + "/" + item.getType(); 139 } 140 141 149 protected TargetModuleID [] getNonRunningModules(final TargetModuleID [] moduleIds) throws Exception { 150 assert moduleIds != null; 151 152 List modulesList = new ArrayList (); 153 154 DeploymentManager manager = getDeploymentManager(); 155 156 Target [] targets = manager.getTargets(); 157 TargetModuleID runningModuleIds[] = manager.getRunningModules(null, targets); 158 159 for (int j = 0; j < moduleIds.length; j++) { 160 String moduleId = moduleIds[j].getModuleID(); 161 log.debug("Checking if module is running: " + moduleId); 162 163 boolean found = false; 164 for (int i = 0; i < runningModuleIds.length; i++) { 165 String runningModuleId = runningModuleIds[i].getModuleID(); 166 if (moduleId.equals(runningModuleId)) { 167 log.debug("Module is running: " + moduleId); 168 found = true; 169 break; 170 } 171 } 172 173 if (!found) { 174 log.debug("Module is not running: " + moduleId); 175 modulesList.add(moduleIds[j]); 176 } 177 } 178 return (TargetModuleID []) modulesList.toArray(new TargetModuleID [modulesList.size()]); 179 } 180 181 189 protected boolean isModuleStarted(final String moduleId) throws Exception { 190 assert moduleId != null; 191 192 log.debug("Checking if module is started: " + moduleId); 193 194 DeploymentManager manager = getDeploymentManager(); 195 196 Target [] targets = manager.getTargets(); 197 TargetModuleID targetIds[] = manager.getRunningModules(null, targets); 198 199 for (int i = 0; i < targetIds.length; i++) { 200 if (moduleId.equals(targetIds[i].getModuleID())) { 201 return true; 202 } 203 } 204 205 return false; 206 } 207 208 protected TargetModuleID [] findModules(final String moduleId, final TargetModuleID targetIds[]) { 209 assert moduleId != null; 210 assert targetIds != null; 211 212 List found = new ArrayList (); 213 214 log.debug("Scanning for modules that match: " + moduleId); 215 for (int i = 0; i < targetIds.length; i++) { 216 log.debug("Checking: " + targetIds[i].getModuleID()); 217 218 if (moduleId.equals(targetIds[i].getModuleID())) { 219 found.add(targetIds[i]); 220 } 221 } 222 223 return (TargetModuleID []) found.toArray(new TargetModuleID [found.size()]); 224 } 225 226 230 protected void startModule() throws Exception { 231 assert modules != null; 232 233 for (int i=0; i<modules.length; i++) { 234 String moduleId = getModuleId(modules[i]); 235 236 if (isModuleStarted(moduleId)) { 237 log.warn("Module is already started: " + moduleId); 238 continue; 239 } 241 242 DeploymentManager manager = getDeploymentManager(); 243 Target [] targets = manager.getTargets(); 244 TargetModuleID [] targetIds = manager.getNonRunningModules(null, targets); 245 246 TargetModuleID [] found = findModules(moduleId, targetIds); 247 248 if (found.length == 0) { 249 throw new MojoExecutionException("Module is not deployed: " + moduleId); 250 } 251 252 log.info("Starting module: " + moduleId); 253 ProgressObject progress = manager.start(found); 254 255 DeploymentStatus status = waitFor(progress); 256 if (status.isFailed()) { 257 throw new MojoExecutionException("Failed to start module: " + moduleId); 258 } 259 260 log.info("Started module(s):"); 261 logModules(found, " "); 262 } 263 } 264 265 protected void stopModule() throws Exception { 266 assert modules != null; 267 268 DeploymentManager manager = getDeploymentManager(); 269 Target [] targets = manager.getTargets(); 270 TargetModuleID [] targetIds = manager.getRunningModules(null, targets); 271 272 for (int i=0; i<modules.length; i++) { 273 String moduleId = getModuleId(modules[i]); 274 if (!isModuleStarted(moduleId)) { 275 log.info("Module is already stopped: " + moduleId); 276 continue; 277 } 279 280 TargetModuleID [] found = findModules(moduleId, targetIds); 281 282 if (found.length == 0) { 283 throw new MojoExecutionException("Module not deployed: " + moduleId); 284 } 285 286 log.info("Stopping module: " + moduleId); 287 ProgressObject progress = manager.stop(found); 288 289 DeploymentStatus status = waitFor(progress); 290 if (status.isFailed()) { 291 throw new MojoExecutionException("Failed to stop module: " + moduleId); 292 } 293 294 log.info("Stopped module(s):"); 295 logModules(found, " "); 296 } 297 } 298 299 protected void undeployModule() throws Exception { 300 assert modules != null; 301 302 stopModule(); 303 304 DeploymentManager manager = getDeploymentManager(); 305 Target [] targets = manager.getTargets(); 306 TargetModuleID [] targetIds = manager.getNonRunningModules(null, targets); 307 308 for (int i=0; i<modules.length; i++) { 309 String moduleId = getModuleId(modules[i]); 310 311 TargetModuleID [] found = findModules(moduleId, targetIds); 312 313 if (found.length == 0) { 314 log.info("Module is not deployed: " + moduleId); 315 continue; 316 } 318 319 log.info("Undeploying module: " + moduleId); 320 ProgressObject progress = manager.undeploy(found); 321 322 DeploymentStatus status = waitFor(progress); 323 if (status.isFailed()) { 324 throw new MojoExecutionException("Failed to undeploy module: " + moduleId); 325 } 326 327 log.info("Undeployed module(s):"); 328 logModules(found, " "); 329 } 330 } 331 332 protected void logModules(final TargetModuleID [] targetIds) { 333 logModules(targetIds, ""); 334 } 335 336 protected void logModules(final TargetModuleID [] targetIds, final String pad) { 337 assert targetIds != null; 338 assert pad != null; 339 340 for (int i=0; i<targetIds.length; i++) { 341 String url = targetIds[i].getWebURL(); 342 log.info(pad + "[" + i + "] " + targetIds[i].getModuleID() + (url == null ? "" : ("; URL: " + url))); 343 344 TargetModuleID [] children = targetIds[i].getChildTargetModuleID(); 345 if (children != null) { 346 logModules(children, pad + " "); 347 } 348 } 349 } 350 351 } 352 | Popular Tags |