KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import org.apache.maven.artifact.Artifact;
28 import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
29 import org.apache.maven.plugin.MojoExecutionException;
30 import org.apache.maven.plugin.MojoFailureException;
31 import org.apache.maven.project.MavenProject;
32 import org.apache.maven.project.ProjectBuildingException;
33 import org.codehaus.plexus.util.FileUtils;
34
35 /**
36  * A Mojo used to build the jbi.xml file.
37  *
38  * @author <a HREF="gnodet@apache.org">Guillaume Nodet</a>
39  * @version $Id: GenerateComponentDescriptorMojo 314956 2005-10-12 16:27:15Z
40  * brett $
41  * @goal generate-jbi-component-descriptor
42  * @phase generate-resources
43  * @requiresDependencyResolution runtime
44  * @description generates the jbi.xml deployment descriptor
45  */

46 public class GenerateComponentDescriptorMojo extends AbstractJbiMojo {
47
48     public static final String JavaDoc UTF_8 = "UTF-8";
49
50     /**
51      * Whether the application.xml should be generated or not.
52      *
53      * @parameter
54      */

55     private Boolean JavaDoc generateJbiDescriptor = Boolean.TRUE;
56
57     /**
58      * The component class name.
59      *
60      * @parameter
61      * @required
62      */

63     private String JavaDoc component;
64
65     /**
66      * The bootstrap class name.
67      *
68      * @parameter
69      * @required
70      */

71     private String JavaDoc bootstrap;
72
73     /**
74      * The component type.
75      *
76      * @parameter
77      * @required
78      */

79     private String JavaDoc type;
80
81     /**
82      * The component name.
83      *
84      * @parameter expression="${project.artifactId}"
85      */

86     private String JavaDoc name;
87
88     /**
89      * The component description.
90      *
91      * @parameter expression="${project.name}"
92      */

93     private String JavaDoc description;
94
95     /**
96      * Character encoding for the auto-generated application.xml file.
97      *
98      * @parameter
99      */

100     private String JavaDoc encoding = UTF_8;
101
102     /**
103      * Directory where the application.xml file will be auto-generated.
104      *
105      * @parameter expression="${project.build.directory}"
106      */

107     private String JavaDoc generatedDescriptorLocation;
108
109     /**
110      * The component class loader delegation
111      *
112      * @parameter expression="parent-first"
113      */

114     private String JavaDoc componentClassLoaderDelegation;
115
116     /**
117      * The bootstrap class loader delegation
118      *
119      * @parameter expression="parent-first"
120      */

121     private String JavaDoc bootstrapClassLoaderDelegation;
122
123     public void execute() throws MojoExecutionException, MojoFailureException {
124
125         getLog().debug(
126                 " ======= GenerateComponentDescriptorMojo settings =======");
127         getLog().debug("workDirectory[" + workDirectory + "]");
128         getLog().debug("generateJbiDescriptor[" + generateJbiDescriptor + "]");
129         getLog().debug("type[" + type + "]");
130         getLog().debug("component[" + component + "]");
131         getLog().debug("bootstrap[" + bootstrap + "]");
132         getLog().debug("name[" + name + "]");
133         getLog().debug("description[" + description + "]");
134         getLog().debug("encoding[" + encoding + "]");
135         getLog().debug(
136                 "generatedDescriptorLocation[" + generatedDescriptorLocation
137                         + "]");
138
139         if (!generateJbiDescriptor.booleanValue()) {
140             getLog().debug("Generation of jbi.xml is disabled");
141             return;
142         }
143
144         // Generate jbi descriptor and copy it to the build directory
145
getLog().info("Generating jbi.xml");
146         try {
147             generateJbiDescriptor();
148         } catch (JbiPluginException e) {
149             throw new MojoExecutionException("Failed to generate jbi.xml", e);
150         }
151
152         try {
153             FileUtils.copyFileToDirectory(new File JavaDoc(generatedDescriptorLocation,
154                     JBI_DESCRIPTOR), new File JavaDoc(getWorkDirectory(), META_INF));
155         } catch (IOException JavaDoc e) {
156             throw new MojoExecutionException(
157                     "Unable to copy jbi.xml to final destination", e);
158         }
159     }
160
161     /**
162      * Generates the deployment descriptor if necessary.
163      */

164     protected void generateJbiDescriptor() throws JbiPluginException {
165         File JavaDoc outputDir = new File JavaDoc(generatedDescriptorLocation);
166         if (!outputDir.exists()) {
167             outputDir.mkdirs();
168         }
169
170         File JavaDoc descriptor = new File JavaDoc(outputDir, JBI_DESCRIPTOR);
171
172         List JavaDoc uris = new ArrayList JavaDoc();
173         DependencyInformation info = new DependencyInformation();
174         info.setFilename(LIB_DIRECTORY + "/" + project.getArtifactId() + "-"
175                 + project.getVersion() + ".jar");
176         info.setVersion(project.getVersion());
177         info.setName(project.getArtifactId());
178         info.setType("jar");
179         uris.add(info);
180
181         ScopeArtifactFilter filter = new ScopeArtifactFilter(
182                 Artifact.SCOPE_RUNTIME);
183
184         JbiResolutionListener listener = resolveProject();
185         // print(listener.getRootNode(), "");
186

187         Set JavaDoc includes = new HashSet JavaDoc();
188         for (Iterator JavaDoc iter = project.getArtifacts().iterator(); iter.hasNext();) {
189             Artifact artifact = (Artifact) iter.next();
190             if (!artifact.isOptional() && filter.include(artifact)) {
191                 MavenProject project = null;
192                 try {
193                     project = projectBuilder.buildFromRepository(artifact,
194                             remoteRepos, localRepo);
195                 } catch (ProjectBuildingException e) {
196                     getLog().warn(
197                             "Unable to determine packaging for dependency : "
198                                     + artifact.getArtifactId()
199                                     + " assuming jar");
200                 }
201                 String JavaDoc type = project != null ? project.getPackaging()
202                         : artifact.getType();
203                 if ("jbi-shared-library".equals(type)) {
204                     removeChildren(listener, artifact);
205                     includes.add(artifact);
206                 } else if ("jar".equals(type)) {
207                     includes.add(artifact);
208                 }
209             }
210         }
211         // print(listener.getRootNode(), "");
212

213         for (Iterator JavaDoc iter = retainArtifacts(includes, listener).iterator(); iter
214                 .hasNext();) {
215             Artifact artifact = (Artifact) iter.next();
216             MavenProject project = null;
217             try {
218                 project = projectBuilder.buildFromRepository(artifact,
219                         remoteRepos, localRepo);
220             } catch (ProjectBuildingException e) {
221                 getLog().warn(
222                         "Unable to determine packaging for dependency : "
223                                 + artifact.getArtifactId() + " assuming jar");
224             }
225             String JavaDoc type = project != null ? project.getPackaging() : artifact
226                     .getType();
227             info = new DependencyInformation();
228             info
229                     .setFilename(LIB_DIRECTORY + "/"
230                             + artifact.getFile().getName());
231             info.setVersion(artifact.getVersion());
232             info.setName(artifact.getArtifactId());
233             info.setType(type);
234             uris.add(info);
235         }
236
237         JbiComponentDescriptorWriter writer = new JbiComponentDescriptorWriter(
238                 encoding);
239         writer.write(descriptor, component, bootstrap, type, name, description,
240                 componentClassLoaderDelegation, bootstrapClassLoaderDelegation,
241                 uris);
242     }
243 }
244
Popular Tags