KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > maven > plugin > jbi > ServiceMixEmbeddedMojo


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.maven.plugin.jbi;
18
19 import java.io.File JavaDoc;
20 import java.net.MalformedURLException JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.net.URLClassLoader JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Set JavaDoc;
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 /**
35  * Starts a ServiceMix JBI container in embedded mode using the servicemix.xml
36  *
37  * @author <a HREF="pdodds@apache.org">Philip Dodds</a>
38  * @version $Id: GenerateComponentDescriptorMojo 314956 2005-10-12 16:27:15Z
39  * brett $
40  * @goal embeddedServicemix
41  * @execute phase="compile"
42  * @requiresDependencyResolution runtime
43  * @description Starts a local servicemix instance using the servicemix config
44  * provided
45  */

46 public class ServiceMixEmbeddedMojo extends AbstractJbiMojo {
47
48     /**
49      * @parameter default-value="${basedir}/src/main/resources/servicemix.xml"
50      */

51     private File JavaDoc 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 JavaDoc lock = new Object JavaDoc();
63             container.setShutdownLock(lock);
64
65             // lets wait until we're killed.
66
synchronized (lock) {
67                 lock.wait();
68             }
69         } catch (Exception JavaDoc 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 JavaDoc e) {
77                     // Ignore
78
}
79             }
80         }
81
82     }
83
84     private void startServiceMix() throws MojoExecutionException {
85         ClassLoader JavaDoc 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 JavaDoc e) {
92             throw new MojoExecutionException(
93                     "Unable to start the ServiceMix container", e);
94         } finally {
95             Thread.currentThread().setContextClassLoader(old);
96         }
97     }
98
99     /**
100      * Set up a classloader for the execution of the
101      * main class.
102      *
103      * @return
104      * @throws MojoExecutionException
105      */

106     private URLClassLoader JavaDoc getClassLoader() throws MojoExecutionException {
107         try {
108             Set JavaDoc urls = new HashSet JavaDoc();
109
110             URL JavaDoc mainClasses = new File JavaDoc(project.getBuild().getOutputDirectory()).toURL();
111             getLog().debug("Adding to classpath : " + mainClasses);
112             urls.add(mainClasses);
113
114             URL JavaDoc testClasses = new File JavaDoc(project.getBuild().getTestOutputDirectory()).toURL();
115             getLog().debug("Adding to classpath : " + testClasses);
116             urls.add(testClasses);
117
118             Set JavaDoc dependencies = project.getArtifacts();
119             Iterator JavaDoc 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 JavaDoc appClassloader = new URLClassLoader JavaDoc(
126                             (URL JavaDoc[]) urls.toArray(new URL JavaDoc[urls.size()]),
127                             this.getClass().getClassLoader());
128             return appClassloader;
129         } catch (MalformedURLException JavaDoc e) {
130             throw new MojoExecutionException("Error during setting up classpath", e);
131         }
132     }
133
134 }
135
Popular Tags