1 17 package org.apache.servicemix.maven.plugin.jbi; 18 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.Collections ; 22 import java.util.Comparator ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Stack ; 26 27 import org.apache.maven.artifact.Artifact; 28 import org.apache.maven.artifact.resolver.ArtifactNotFoundException; 29 import org.apache.maven.artifact.resolver.ArtifactResolutionException; 30 import org.apache.maven.plugin.MojoExecutionException; 31 import org.apache.maven.plugin.MojoFailureException; 32 import org.apache.maven.project.MavenProject; 33 import org.apache.maven.project.ProjectBuildingException; 34 import org.apache.servicemix.jbi.management.task.DeployServiceAssemblyTask; 35 import org.apache.servicemix.jbi.management.task.InstallComponentTask; 36 import org.apache.servicemix.jbi.management.task.InstallSharedLibraryTask; 37 import org.apache.servicemix.jbi.management.task.ShutDownComponentTask; 38 import org.apache.servicemix.jbi.management.task.ShutDownServiceAssemblyTask; 39 import org.apache.servicemix.jbi.management.task.StartComponentTask; 40 import org.apache.servicemix.jbi.management.task.StartServiceAssemblyTask; 41 import org.apache.servicemix.jbi.management.task.StopComponentTask; 42 import org.apache.servicemix.jbi.management.task.StopServiceAssemblyTask; 43 import org.apache.servicemix.jbi.management.task.UndeployServiceAssemblyTask; 44 import org.apache.servicemix.jbi.management.task.UninstallComponentTask; 45 import org.apache.servicemix.jbi.management.task.UninstallSharedLibraryTask; 46 47 59 public class JbiProjectDeployerMojo extends AbstractDeployableMojo { 60 61 private static final String JBI_SHARED_LIBRARY = "jbi-shared-library"; 62 63 private static final String JBI_COMPONENT = "jbi-component"; 64 65 private static final String JBI_SERVICE_ASSEMBLY = "jbi-service-assembly"; 66 67 private List deploymentTypes; 68 69 72 private boolean deployDependencies; 73 74 public void execute() throws MojoExecutionException, MojoFailureException { 75 deployProject(); 76 } 77 78 protected void deployProject() throws MojoExecutionException { 79 if (!getDeployablePackagingTypes().contains(project.getPackaging())) { 80 throw new MojoExecutionException( 81 "Project must be of packaging type [" 82 + getDeployablePackagingTypes() + "]"); 83 } 84 85 try { 86 Stack dependencies = new Stack (); 87 dependencies.add(resolveDeploymentPackage(project, project 88 .getArtifact())); 89 ArrayList artifactList = new ArrayList (); 90 artifactList.addAll(project.getArtifacts()); 91 Collections.sort(artifactList, new ArtifactDepthComparator()); 92 for (Iterator iter = artifactList.iterator(); iter.hasNext();) { 93 Artifact artifact = (Artifact) iter.next(); 94 resolveArtifact(artifact, dependencies); 95 } 96 97 getLog() 98 .info( 99 "------------------ Deployment Analysis --------------------"); 100 getLog().info( 101 project.getName() + " has " + (dependencies.size() - 1) 102 + " child dependencies"); 103 104 for (Iterator iterator = dependencies.iterator(); iterator 105 .hasNext();) { 106 getLog().info(" - " + iterator.next()); 107 } 108 109 getLog() 110 .info( 111 "-----------------------------------------------------------"); 112 113 if (deployDependencies) { 114 for (Iterator iterator = dependencies.iterator(); iterator 116 .hasNext();) { 117 JbiDeployableArtifact jbiDeployable = (JbiDeployableArtifact) iterator 118 .next(); 119 120 if (isDeployed(jbiDeployable)) { 121 stopDependency(jbiDeployable); 122 undeployDependency(jbiDeployable); 123 } 124 } 125 126 while (!dependencies.empty()) { 129 JbiDeployableArtifact jbiDeployable = (JbiDeployableArtifact) dependencies 130 .pop(); 131 deployDependency(jbiDeployable); 132 startDependency(jbiDeployable); 133 } 134 } else { 135 JbiDeployableArtifact jbiDeployable = (JbiDeployableArtifact) dependencies 136 .firstElement(); 137 if (isDeployed(jbiDeployable)) { 138 stopDependency(jbiDeployable); 139 undeployDependency(jbiDeployable); 140 } 141 deployDependency(jbiDeployable); 142 startDependency(jbiDeployable); 143 } 144 145 } catch (Exception e) { 146 throw new MojoExecutionException("Unable to deploy project, " 147 + e.getMessage(), e); 148 } 149 150 } 151 152 private void startDependency(JbiDeployableArtifact jbiDeployable) { 153 getLog().info("Starting " + jbiDeployable.getName()); 154 if (JBI_SERVICE_ASSEMBLY.equals(jbiDeployable.getType())) { 155 StartServiceAssemblyTask startTask = new StartServiceAssemblyTask(); 156 initializeJbiTask(startTask); 157 startTask.setName(jbiDeployable.getName()); 158 startTask.execute(); 159 } 160 if (JBI_COMPONENT.equals(jbiDeployable.getType())) { 161 StartComponentTask startTask = new StartComponentTask(); 162 initializeJbiTask(startTask); 163 startTask.setName(jbiDeployable.getName()); 164 startTask.execute(); 165 } 166 } 167 168 private void undeployDependency(JbiDeployableArtifact jbiDeployable) { 169 getLog().info("Undeploying " + jbiDeployable.getFile()); 170 if (JBI_SHARED_LIBRARY.equals(jbiDeployable.getType())) { 171 UninstallSharedLibraryTask sharedLibraryTask = new UninstallSharedLibraryTask(); 172 initializeJbiTask(sharedLibraryTask); 173 sharedLibraryTask.setName(jbiDeployable.getName()); 174 sharedLibraryTask.execute(); 175 } else if (JBI_SERVICE_ASSEMBLY.equals(jbiDeployable.getType())) { 176 UndeployServiceAssemblyTask serviceAssemblyTask = new UndeployServiceAssemblyTask(); 177 initializeJbiTask(serviceAssemblyTask); 178 serviceAssemblyTask.setName(jbiDeployable.getName()); 179 serviceAssemblyTask.execute(); 180 } 181 if (JBI_COMPONENT.equals(jbiDeployable.getType())) { 182 UninstallComponentTask componentTask = new UninstallComponentTask(); 183 initializeJbiTask(componentTask); 184 componentTask.setName(jbiDeployable.getName()); 185 componentTask.execute(); 186 } 187 } 188 189 private boolean isDeployed(JbiDeployableArtifact jbiDeployable) { 190 IsDeployedTask isDeployedTask = new IsDeployedTask(); 191 isDeployedTask.setType(jbiDeployable.getType()); 192 isDeployedTask.setName(jbiDeployable.getName()); 193 initializeJbiTask(isDeployedTask); 194 isDeployedTask.execute(); 195 boolean deployed = isDeployedTask.isDeployed(); 196 if (deployed) 197 getLog().info(jbiDeployable.getName() + " is deployed"); 198 else 199 getLog().info(jbiDeployable.getName() + " is not deployed"); 200 return deployed; 201 } 202 203 private void stopDependency(JbiDeployableArtifact jbiDeployable) { 204 getLog().info("Stopping " + jbiDeployable.getName()); 205 if (JBI_SERVICE_ASSEMBLY.equals(jbiDeployable.getType())) { 206 StopServiceAssemblyTask stopTask = new StopServiceAssemblyTask(); 207 initializeJbiTask(stopTask); 208 stopTask.setName(jbiDeployable.getName()); 209 stopTask.execute(); 210 211 ShutDownServiceAssemblyTask shutdownTask = new ShutDownServiceAssemblyTask(); 212 initializeJbiTask(shutdownTask); 213 shutdownTask.setName(jbiDeployable.getName()); 214 shutdownTask.execute(); 215 } 216 if (JBI_COMPONENT.equals(jbiDeployable.getType())) { 217 StopComponentTask stopTask = new StopComponentTask(); 218 initializeJbiTask(stopTask); 219 stopTask.setName(jbiDeployable.getName()); 220 stopTask.execute(); 221 222 ShutDownComponentTask shutdownTask = new ShutDownComponentTask(); 223 initializeJbiTask(shutdownTask); 224 shutdownTask.setName(jbiDeployable.getName()); 225 shutdownTask.execute(); 226 } 227 } 228 229 private void deployDependency(JbiDeployableArtifact jbiDeployable) { 230 231 getLog().info( 232 "Deploying " + jbiDeployable.getType() + " from " 233 + jbiDeployable.getFile()); 234 if (JBI_SHARED_LIBRARY.equals(jbiDeployable.getType())) { 235 InstallSharedLibraryTask componentTask = new InstallSharedLibraryTask(); 236 initializeJbiTask(componentTask); 237 componentTask.setFile(jbiDeployable.getFile()); 238 componentTask.execute(); 239 } else if (JBI_SERVICE_ASSEMBLY.equals(jbiDeployable.getType())) { 240 DeployServiceAssemblyTask componentTask = new DeployServiceAssemblyTask(); 241 initializeJbiTask(componentTask); 242 componentTask.setFile(jbiDeployable.getFile()); 243 componentTask.execute(); 244 } 245 if (JBI_COMPONENT.equals(jbiDeployable.getType())) { 246 InstallComponentTask componentTask = new InstallComponentTask(); 247 initializeJbiTask(componentTask); 248 componentTask.setFile(jbiDeployable.getFile()); 249 componentTask.execute(); 250 } 251 252 } 253 254 private List getDeployablePackagingTypes() { 255 if (deploymentTypes == null) { 256 deploymentTypes = new ArrayList (); 257 deploymentTypes.add(JBI_SHARED_LIBRARY); 258 deploymentTypes.add(JBI_SERVICE_ASSEMBLY); 259 deploymentTypes.add(JBI_COMPONENT); 260 } 261 return deploymentTypes; 262 } 263 264 private Collection resolveArtifact(Artifact artifact, Stack dependencies) 265 throws ArtifactResolutionException, ArtifactNotFoundException { 266 MavenProject project = null; 267 try { 268 project = projectBuilder.buildFromRepository(artifact, remoteRepos, localRepo, 269 true); 270 } catch (ProjectBuildingException e) { 271 getLog().warn( 272 "Unable to determine packaging for dependency : " 273 + artifact.getArtifactId() + " assuming jar"); 274 } 275 276 if (project != null) { 277 if (getDeployablePackagingTypes().contains(project.getPackaging())) { 278 getLog().debug( 279 "Checking for dependency from project " 280 + project.getArtifactId()); 281 JbiDeployableArtifact deployableArtifact = resolveDeploymentPackage( 282 project, artifact); 283 if (!dependencies.contains(deployableArtifact)) { 284 getLog().debug( 285 "Adding dependency from project " 286 + project.getArtifactId()); 287 dependencies.push(deployableArtifact); 288 } 289 } 290 } 291 return dependencies; 292 } 293 294 private JbiDeployableArtifact resolveDeploymentPackage( 295 MavenProject project, Artifact artifact) 296 throws ArtifactResolutionException, ArtifactNotFoundException { 297 Artifact jbiArtifact = factory.createArtifactWithClassifier(artifact 298 .getGroupId(), artifact.getArtifactId(), artifact.getVersion(), 299 "zip", getExtension(project)); 300 resolver.resolve(jbiArtifact, remoteRepos, localRepo); 301 return new JbiDeployableArtifact(project.getArtifactId(), project 302 .getPackaging(), jbiArtifact.getFile().getAbsolutePath()); 303 } 304 305 private String getExtension(MavenProject project2) { 306 if (project2.getPackaging().equals(JBI_SERVICE_ASSEMBLY)) 307 return ""; 308 else 309 return "installer"; 310 } 311 312 private class ArtifactDepthComparator implements Comparator { 313 314 public int compare(Object arg0, Object arg1) { 315 int size1 = ((Artifact) arg0).getDependencyTrail().size(); 316 int size2 = ((Artifact) arg1).getDependencyTrail().size(); 317 if (size1 == size2) 318 return 0; 319 if (size1 > size2) 320 return 1; 321 else 322 return -1; 323 } 324 325 } 326 327 private class JbiDeployableArtifact { 328 private String file; 329 330 private String type; 331 332 private String name; 333 334 public String getName() { 335 return name; 336 } 337 338 public JbiDeployableArtifact(String name, String type, String file) { 339 this.name = name; 340 this.file = file; 341 this.type = type; 342 } 343 344 public String getFile() { 345 return file; 346 } 347 348 public String getType() { 349 return type; 350 } 351 352 public String toString() { 353 return type + " : " + file; 354 } 355 356 public boolean equals(Object obj) { 357 if (obj instanceof JbiDeployableArtifact) 358 return ((JbiDeployableArtifact) obj).toString().equals( 359 this.toString()); 360 else 361 return false; 362 } 363 } 364 } 365 | Popular Tags |