KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > mavenplugins > car > PlanProcessorMojo


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

19
20 package org.apache.geronimo.mavenplugins.car;
21
22 import java.io.File JavaDoc;
23 import java.io.StringWriter JavaDoc;
24
25 import java.util.Iterator JavaDoc;
26 import java.util.LinkedHashSet JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Properties JavaDoc;
29
30 import javax.xml.namespace.QName JavaDoc;
31
32 import org.apache.geronimo.deployment.service.EnvironmentBuilder;
33 import org.apache.geronimo.deployment.xbeans.ArtifactType;
34 import org.apache.geronimo.deployment.xbeans.EnvironmentType;
35 import org.apache.geronimo.kernel.repository.Environment;
36 import org.apache.geronimo.kernel.repository.Artifact;
37 import org.apache.geronimo.kernel.repository.ImportType;
38
39 import org.apache.maven.model.Dependency;
40
41 import org.apache.velocity.Template;
42 import org.apache.velocity.VelocityContext;
43 import org.apache.velocity.app.VelocityEngine;
44
45 import org.apache.xmlbeans.XmlCursor;
46 import org.apache.xmlbeans.XmlObject;
47 import org.apache.xmlbeans.XmlOptions;
48
49 //
50
// TODO: Rename to PreparePlanMojo
51
//
52

53 /**
54  * Add dependencies to a plan and process with velocity
55  *
56  * @goal prepare-plan
57  *
58  * @version $Rev: 480326 $ $Date: 2006-11-28 20:48:06 -0500 (Tue, 28 Nov 2006) $
59  */

60 public class PlanProcessorMojo
61     extends AbstractCarMojo
62 {
63     private static final String JavaDoc ENVIRONMENT_LOCAL_NAME = "environment";
64
65     private static final QName JavaDoc ENVIRONMENT_QNAME = new QName JavaDoc("http://geronimo.apache.org/xml/ns/deployment-1.2", "environment");
66
67     /**
68      * @parameter expression="${basedir}/src/plan"
69      * @required
70      */

71     protected File JavaDoc sourceDir = null;
72
73     /**
74      * @parameter expression="${project.build.directory}/plan"
75      * @required
76      */

77     protected File JavaDoc targetDir = null;
78
79     /**
80      * @parameter default-value="plan.xml"
81      * @required
82      */

83     protected String JavaDoc planFileName = null;
84
85     /**
86      * @parameter expression="${project.build.directory}/plan/plan.xml"
87      * @required
88      */

89     protected File JavaDoc targetFile = null;
90
91     private VelocityContext createContext() {
92         VelocityContext context = new VelocityContext();
93
94         // Load properties, It inherits them all!
95
Properties JavaDoc props = project.getProperties();
96         for (Iterator JavaDoc iter = props.keySet().iterator(); iter.hasNext();) {
97             String JavaDoc key = (String JavaDoc) iter.next();
98             String JavaDoc value = props.getProperty(key);
99
100             log.debug("Setting " + key + "=" + value);
101             context.put(key, value);
102         }
103
104         context.put("pom", project);
105
106         return context;
107     }
108
109     protected void doExecute() throws Exception JavaDoc {
110         //
111
// FIXME: Do not need velocity here, we only need to filter,
112
// could use resources plugin to do this for us, or
113
// implement what resources plugin does here
114
//
115

116         VelocityContext context = createContext();
117
118         VelocityEngine velocity = new VelocityEngine();
119         velocity.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, sourceDir.getAbsolutePath());
120         velocity.init();
121
122         Template template = velocity.getTemplate(planFileName);
123         StringWriter JavaDoc writer = new StringWriter JavaDoc();
124         template.merge(context, writer);
125
126         String JavaDoc plan = writer.toString();
127
128         XmlObject doc = XmlObject.Factory.parse(plan);
129         XmlCursor xmlCursor = doc.newCursor();
130         LinkedHashSet JavaDoc dependencies = toDependencies();
131         Artifact configId = new Artifact(project.getGroupId(), project.getArtifactId(), project.getVersion(), "car");
132
133         try {
134             mergeEnvironment(xmlCursor, configId, dependencies);
135             
136             if (targetDir.exists()) {
137                 if (!targetDir.isDirectory()) {
138                     throw new RuntimeException JavaDoc("TargetDir: " + this.targetDir + " exists and is not a directory");
139                 }
140             }
141             else {
142                 targetDir.mkdirs();
143             }
144
145             XmlOptions xmlOptions = new XmlOptions();
146             xmlOptions.setSavePrettyPrint();
147             doc.save(targetFile, xmlOptions);
148             
149             log.info("Generated: " + targetFile);
150         }
151         finally {
152             xmlCursor.dispose();
153         }
154     }
155
156     void mergeEnvironment(final XmlCursor xmlCursor, final Artifact configId, final LinkedHashSet JavaDoc dependencies) {
157         moveToFirstStartElement(xmlCursor);
158
159         boolean atLeastOneChild = xmlCursor.toFirstChild();
160         if (!atLeastOneChild) {
161             // this is an empty element. Move to EndToken such XmlCursor.beginElement inserts an element inside it.
162
xmlCursor.toEndToken();
163         }
164         QName JavaDoc childName = xmlCursor.getName();
165         Environment oldEnvironment;
166         
167         if (childName != null && childName.getLocalPart().equals(ENVIRONMENT_LOCAL_NAME)) {
168             convertElement(xmlCursor, ENVIRONMENT_QNAME.getNamespaceURI());
169             XmlObject xmlObject = xmlCursor.getObject();
170             EnvironmentType environmentType = (EnvironmentType) xmlObject.copy().changeType(EnvironmentType.type);
171             oldEnvironment = EnvironmentBuilder.buildEnvironment(environmentType);
172             xmlCursor.removeXml();
173         } else {
174             oldEnvironment = new Environment();
175         }
176
177         Environment newEnvironment = new Environment();
178         newEnvironment.setConfigId(configId);
179         newEnvironment.setDependencies(dependencies);
180
181         EnvironmentBuilder.mergeEnvironments(oldEnvironment, newEnvironment);
182         EnvironmentType environmentType = EnvironmentBuilder.buildEnvironmentType(oldEnvironment);
183
184         xmlCursor.beginElement(ENVIRONMENT_QNAME);
185         XmlCursor element = environmentType.newCursor();
186
187         try {
188             element.copyXmlContents(xmlCursor);
189         }
190         finally {
191             element.dispose();
192         }
193     }
194
195     private void moveToFirstStartElement(XmlCursor xmlCursor) throws AssertionError JavaDoc {
196         xmlCursor.toStartDoc();
197         xmlCursor.toFirstChild();
198         while (!xmlCursor.currentTokenType().isStart()) {
199             if (!xmlCursor.toNextSibling()) {
200                 break;
201             }
202         }
203         if (!xmlCursor.currentTokenType().isStart()) {
204             throw new AssertionError JavaDoc("Cannot find first start element");
205         }
206     }
207
208     private void convertElement(final XmlCursor cursor, final String JavaDoc namespace) {
209         cursor.push();
210         XmlCursor end = cursor.newCursor();
211
212         try {
213             end.toCursor(cursor);
214             end.toEndToken();
215
216             while (cursor.hasNextToken() && cursor.isLeftOf(end)) {
217                 if (cursor.isStart()) {
218                     if (!namespace.equals(cursor.getName().getNamespaceURI())) {
219                         cursor.setName(new QName JavaDoc(namespace, cursor.getName().getLocalPart()));
220                     }
221                 }
222
223                 cursor.toNextToken();
224             }
225
226             cursor.pop();
227         }
228         finally {
229             end.dispose();
230         }
231     }
232
233     private LinkedHashSet JavaDoc toDependencies() {
234         List JavaDoc artifacts = project.getDependencies();
235         LinkedHashSet JavaDoc dependencies = new LinkedHashSet JavaDoc();
236
237         Iterator JavaDoc iter = artifacts.iterator();
238         while (iter.hasNext()) {
239             Dependency dependency = (Dependency) iter.next();
240
241             //
242
// HACK: Does not appear that we can get the "extention" status of a dependency,
243
// so specifically exclude the ones that we know about, like genesis
244
//
245

246             if (dependency.getGroupId().startsWith("org.apache.geronimo.genesis")) {
247                 continue;
248             }
249
250             org.apache.geronimo.kernel.repository.Dependency gdep = toGeronimoDependency(dependency);
251             if (gdep != null) {
252                 dependencies.add(gdep);
253             }
254         }
255
256         return dependencies;
257     }
258
259     private static org.apache.geronimo.kernel.repository.Dependency toGeronimoDependency(final Dependency dependency) {
260         Artifact artifact = toGeronimoArtifact(dependency);
261         String JavaDoc type = dependency.getType();
262         String JavaDoc scope = dependency.getScope();
263         String JavaDoc groupId = dependency.getGroupId();
264
265         //!"org.apache.geronimo.specs".equals(groupId) jacc spec needed in plan.xml
266
if ("jar".equalsIgnoreCase(type) && !"junit".equals(groupId) && (scope == null || !scope.equals("provided"))) {
267             if (dependency.getVersion() != null) {
268                 artifact = new Artifact(
269                     artifact.getGroupId(),
270                     artifact.getArtifactId(),
271                     dependency.getVersion(),
272                     artifact.getType());
273             }
274             return new org.apache.geronimo.kernel.repository.Dependency(artifact, ImportType.CLASSES);
275         }
276         else if ("car".equalsIgnoreCase(type) && "runtime".equalsIgnoreCase(scope)) {
277             return new org.apache.geronimo.kernel.repository.Dependency(artifact, ImportType.CLASSES);
278         }
279         //doesn't work
280
// else if ("car".equalsIgnoreCase(type) && "provided".equalsIgnoreCase(scope)) {
281
// return new org.apache.geronimo.kernel.repository.Dependency(artifact, ImportType.CLASSES);
282
// }
283
else if ("car".equalsIgnoreCase(type) && (scope == null || "compile".equalsIgnoreCase(scope))) { //parent
284
return new org.apache.geronimo.kernel.repository.Dependency(artifact, ImportType.ALL);
285         }
286         else {
287             // not one of ours
288
return null;
289         }
290     }
291
292     private static Artifact toGeronimoArtifact(final Dependency dependency) {
293         String JavaDoc groupId = dependency.getGroupId();
294         String JavaDoc artifactId = dependency.getArtifactId();
295         String JavaDoc version = null;
296         String JavaDoc type = dependency.getType();
297
298         return new Artifact(groupId, artifactId, version, type);
299     }
300
301     interface Inserter {
302         ArtifactType insert(EnvironmentType environmentType);
303     }
304 }
305
Popular Tags