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.HashSet ; 24 import java.util.Iterator ; 25 import java.util.Set ; 26 27 import org.apache.maven.artifact.Artifact; 28 import org.apache.maven.plugin.MojoExecutionException; 29 import org.apache.maven.plugin.MojoFailureException; 30 import org.apache.servicemix.jbi.container.SpringJBIContainer; 31 import org.apache.xbean.spring.context.FileSystemXmlApplicationContext; 32 import org.springframework.beans.factory.DisposableBean; 33 34 46 public class ServiceMixEmbeddedMojo extends AbstractJbiMojo { 47 48 51 private File servicemixConfig; 52 53 private FileSystemXmlApplicationContext context; 54 55 private SpringJBIContainer container; 56 57 public void execute() throws MojoExecutionException, MojoFailureException { 58 59 try { 60 startServiceMix(); 61 62 Object lock = new Object (); 63 container.setShutdownLock(lock); 64 65 synchronized (lock) { 67 lock.wait(); 68 } 69 } catch (Exception e) { 70 throw new MojoExecutionException( 71 "Apache ServiceMix was able to deploy project", e); 72 } finally { 73 if (context instanceof DisposableBean) { 74 try { 75 ((DisposableBean) context).destroy(); 76 } catch (Exception e) { 77 } 79 } 80 } 81 82 } 83 84 private void startServiceMix() throws MojoExecutionException { 85 ClassLoader old = Thread.currentThread().getContextClassLoader(); 86 try { 87 Thread.currentThread().setContextClassLoader(getClassLoader()); 88 context = new FileSystemXmlApplicationContext("file:///" + servicemixConfig 89 .getAbsolutePath()); 90 container = (SpringJBIContainer) context.getBean("jbi"); 91 } catch (Exception e) { 92 throw new MojoExecutionException( 93 "Unable to start the ServiceMix container", e); 94 } finally { 95 Thread.currentThread().setContextClassLoader(old); 96 } 97 } 98 99 106 private URLClassLoader getClassLoader() throws MojoExecutionException { 107 try { 108 Set urls = new HashSet (); 109 110 URL mainClasses = new File (project.getBuild().getOutputDirectory()).toURL(); 111 getLog().debug("Adding to classpath : " + mainClasses); 112 urls.add(mainClasses); 113 114 URL testClasses = new File (project.getBuild().getTestOutputDirectory()).toURL(); 115 getLog().debug("Adding to classpath : " + testClasses); 116 urls.add(testClasses); 117 118 Set dependencies = project.getArtifacts(); 119 Iterator iter = dependencies.iterator(); 120 while (iter.hasNext()) { 121 Artifact classPathElement = (Artifact) iter.next(); 122 getLog().debug("Adding artifact: " + classPathElement.getArtifactId() + " to classpath"); 123 urls.add(classPathElement.getFile().toURL()); 124 } 125 URLClassLoader appClassloader = new URLClassLoader ( 126 (URL []) urls.toArray(new URL [urls.size()]), 127 this.getClass().getClassLoader()); 128 return appClassloader; 129 } catch (MalformedURLException e) { 130 throw new MojoExecutionException("Error during setting up classpath", e); 131 } 132 } 133 134 } 135 | Popular Tags |