1 19 20 package org.apache.geronimo.mavenplugins.car; 21 22 import java.io.File ; 23 import java.io.StringWriter ; 24 25 import java.util.Iterator ; 26 import java.util.LinkedHashSet ; 27 import java.util.List ; 28 import java.util.Properties ; 29 30 import javax.xml.namespace.QName ; 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 53 60 public class PlanProcessorMojo 61 extends AbstractCarMojo 62 { 63 private static final String ENVIRONMENT_LOCAL_NAME = "environment"; 64 65 private static final QName ENVIRONMENT_QNAME = new QName ("http://geronimo.apache.org/xml/ns/deployment-1.2", "environment"); 66 67 71 protected File sourceDir = null; 72 73 77 protected File targetDir = null; 78 79 83 protected String planFileName = null; 84 85 89 protected File targetFile = null; 90 91 private VelocityContext createContext() { 92 VelocityContext context = new VelocityContext(); 93 94 Properties props = project.getProperties(); 96 for (Iterator iter = props.keySet().iterator(); iter.hasNext();) { 97 String key = (String ) iter.next(); 98 String 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 { 110 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 writer = new StringWriter (); 124 template.merge(context, writer); 125 126 String plan = writer.toString(); 127 128 XmlObject doc = XmlObject.Factory.parse(plan); 129 XmlCursor xmlCursor = doc.newCursor(); 130 LinkedHashSet 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 ("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 dependencies) { 157 moveToFirstStartElement(xmlCursor); 158 159 boolean atLeastOneChild = xmlCursor.toFirstChild(); 160 if (!atLeastOneChild) { 161 xmlCursor.toEndToken(); 163 } 164 QName 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 { 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 ("Cannot find first start element"); 205 } 206 } 207 208 private void convertElement(final XmlCursor cursor, final String 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 (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 toDependencies() { 234 List artifacts = project.getDependencies(); 235 LinkedHashSet dependencies = new LinkedHashSet (); 236 237 Iterator iter = artifacts.iterator(); 238 while (iter.hasNext()) { 239 Dependency dependency = (Dependency) iter.next(); 240 241 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 type = dependency.getType(); 262 String scope = dependency.getScope(); 263 String groupId = dependency.getGroupId(); 264 265 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 else if ("car".equalsIgnoreCase(type) && (scope == null || "compile".equalsIgnoreCase(scope))) { return new org.apache.geronimo.kernel.repository.Dependency(artifact, ImportType.ALL); 285 } 286 else { 287 return null; 289 } 290 } 291 292 private static Artifact toGeronimoArtifact(final Dependency dependency) { 293 String groupId = dependency.getGroupId(); 294 String artifactId = dependency.getArtifactId(); 295 String version = null; 296 String 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 |