1 17 package org.apache.servicemix.maven.plugin.jbi; 18 19 import java.io.File ; 20 import java.net.MalformedURLException ; 21 import java.net.URL ; 22 import java.net.URLClassLoader ; 23 import java.util.ArrayList ; 24 import java.util.HashSet ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Set ; 28 29 import org.apache.maven.artifact.Artifact; 30 import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter; 31 import org.apache.maven.model.Plugin; 32 import org.apache.maven.plugin.MojoExecutionException; 33 import org.apache.maven.plugin.MojoFailureException; 34 import org.apache.maven.project.MavenProject; 35 import org.apache.maven.project.ProjectBuildingException; 36 import org.apache.servicemix.common.packaging.ServiceUnitAnalyzer; 37 import org.codehaus.plexus.util.xml.Xpp3Dom; 38 39 50 public class GenerateServiceUnitDescriptorMojo extends AbstractJbiMojo { 51 52 public static final String UTF_8 = "UTF-8"; 53 54 59 private Boolean generateJbiDescriptor = Boolean.TRUE; 60 61 66 private Boolean useServiceUnitAnalyzer = Boolean.TRUE; 67 68 73 private String name; 74 75 80 private String description; 81 82 87 private String encoding = UTF_8; 88 89 94 private String generatedDescriptorLocation; 95 96 101 private File serviceUnitArtifactsDir; 102 103 public void execute() throws MojoExecutionException, MojoFailureException { 104 105 getLog().debug( 106 " ======= GenerateServiceUnitDescriptorMojo settings ======="); 107 getLog().debug("workDirectory[" + workDirectory + "]"); 108 getLog().debug("generateDescriptor[" + generateJbiDescriptor + "]"); 109 getLog().debug("name[" + name + "]"); 110 getLog().debug("description[" + description + "]"); 111 getLog().debug("encoding[" + encoding + "]"); 112 getLog().debug( 113 "generatedDescriptorLocation[" + generatedDescriptorLocation 114 + "]"); 115 116 if (!generateJbiDescriptor.booleanValue()) { 117 getLog().debug("Generation of jbi.xml is disabled"); 118 return; 119 } 120 121 getLog().info("Generating jbi.xml"); 123 try { 124 generateJbiDescriptor(); 125 } catch (JbiPluginException e) { 126 throw new MojoExecutionException("Failed to generate jbi.xml", e); 127 } 128 129 } 130 131 137 private URLClassLoader getClassLoader() throws MojoExecutionException { 138 try { 139 Set urls = new HashSet (); 140 141 URL mainClasses = new File (project.getBuild().getOutputDirectory()) 142 .toURL(); 143 getLog().debug("Adding to classpath : " + mainClasses); 144 urls.add(mainClasses); 145 146 URL testClasses = new File (project.getBuild() 147 .getTestOutputDirectory()).toURL(); 148 getLog().debug("Adding to classpath : " + testClasses); 149 urls.add(testClasses); 150 151 Set dependencies = project.getArtifacts(); 152 Iterator iter = dependencies.iterator(); 153 while (iter.hasNext()) { 154 Artifact classPathElement = (Artifact) iter.next(); 155 getLog().debug( 156 "Adding artifact: " + classPathElement.getFile() 157 + " to classpath"); 158 urls.add(classPathElement.getFile().toURL()); 159 } 160 URLClassLoader appClassloader = new URLClassLoader ((URL []) urls 161 .toArray(new URL [urls.size()]), this.getClass() 162 .getClassLoader()); 163 return appClassloader; 164 } catch (MalformedURLException e) { 165 throw new MojoExecutionException( 166 "Error during setting up classpath", e); 167 } 168 } 169 170 173 protected void generateJbiDescriptor() throws JbiPluginException { 174 File outputDir = new File (generatedDescriptorLocation); 175 if (!outputDir.exists()) { 176 outputDir.mkdirs(); 177 } 178 179 ClassLoader old = Thread.currentThread().getContextClassLoader(); 180 181 try { 182 URLClassLoader newClassLoader = getClassLoader(); 183 Thread.currentThread().setContextClassLoader(newClassLoader); 184 File descriptor = new File (outputDir, JBI_DESCRIPTOR); 185 List uris = new ArrayList (); 186 JbiServiceUnitDescriptorWriter writer = new JbiServiceUnitDescriptorWriter( 187 encoding); 188 189 List consumes = new ArrayList (); 190 List provides = new ArrayList (); 191 192 if (useServiceUnitAnalyzer.booleanValue()) { 193 String serviceUnitAnalyzerClazzName = getServiceUnitAnalyzer(); 194 195 if (serviceUnitAnalyzerClazzName != null) { 198 ServiceUnitAnalyzer serviceUnitAnalyzer = (ServiceUnitAnalyzer) newClassLoader 199 .loadClass(serviceUnitAnalyzerClazzName) 200 .newInstance(); 201 getLog().info( 202 "Created Service Unit Analyzer " 203 + serviceUnitAnalyzer); 204 serviceUnitAnalyzer.init(serviceUnitArtifactsDir); 205 consumes.addAll(serviceUnitAnalyzer.getConsumes()); 206 provides.addAll(serviceUnitAnalyzer.getProvides()); 207 } 208 209 getLog().info( 210 "generated : consumes " + consumes + " provides " 211 + provides); 212 213 } 214 writer.write(descriptor, name, description, uris, consumes, 215 provides); 216 } catch (Exception e) { 217 throw new JbiPluginException( 218 "Unable to generate service unit descriptor!", e); 219 } finally { 220 Thread.currentThread().setContextClassLoader(old); 221 } 222 } 223 224 private String getServiceUnitAnalyzer() { 225 MavenProject project = getComponentProject(); 226 if (project != null) { 227 List plugins = project.getBuild().getPlugins(); 228 for (Iterator iterator = plugins.iterator(); iterator.hasNext();) { 229 Plugin plugin = (Plugin) iterator.next(); 230 if ("org.apache.servicemix.tooling".equals(plugin.getGroupId()) 231 && "jbi-maven-plugin".equals(plugin.getArtifactId())) { 232 Xpp3Dom o = (Xpp3Dom) plugin.getConfiguration(); 233 if (o != null && o.getChild("serviceUnitAnalyzer") != null) { 234 String clazzName = o.getChild("serviceUnitAnalyzer") 235 .getValue(); 236 return clazzName; 237 } 238 } 239 } 240 } 241 return null; 242 } 243 244 private MavenProject getComponentProject() { 245 Set artifacts = project.getArtifacts(); 246 for (Iterator iter = artifacts.iterator(); iter.hasNext();) { 247 Artifact artifact = (Artifact) iter.next(); 248 249 ScopeArtifactFilter filter = new ScopeArtifactFilter( 251 Artifact.SCOPE_RUNTIME); 252 if (!artifact.isOptional() && filter.include(artifact) 253 && (artifact.getDependencyTrail().size() == 2)) { 254 MavenProject project = null; 255 try { 256 project = projectBuilder.buildFromRepository(artifact, remoteRepos, 257 localRepo); 258 } catch (ProjectBuildingException e) { 259 getLog().warn( 260 "Unable to determine packaging for dependency : " 261 + artifact.getArtifactId() 262 + " assuming jar"); 263 } 264 if ((project != null) 265 && (project.getPackaging().equals("jbi-component"))) { 266 return project; 267 } 268 269 } 270 } 271 return null; 272 } 273 274 } 275 | Popular Tags |