1 17 package org.apache.geronimo.upgrade; 18 19 import java.io.InputStream ; 20 import java.io.IOException ; 21 import java.io.Writer ; 22 import java.util.Map ; 23 import java.util.HashMap ; 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 27 import javax.xml.namespace.QName ; 28 import javax.management.ObjectName ; 29 import javax.management.MalformedObjectNameException ; 30 31 import org.apache.xmlbeans.XmlObject; 32 import org.apache.xmlbeans.XmlException; 33 import org.apache.xmlbeans.XmlOptions; 34 import org.apache.xmlbeans.XmlCursor; 35 import org.apache.geronimo.kernel.repository.Artifact; 36 import org.apache.geronimo.kernel.repository.Environment; 37 import org.apache.geronimo.kernel.repository.ImportType; 38 import org.apache.geronimo.deployment.xbeans.EnvironmentType; 39 import org.apache.geronimo.deployment.service.EnvironmentBuilder; 40 41 44 public class Upgrade1_0To1_1 { 45 46 private static final Map NAMESPACE_UPDATES = new HashMap (); 47 48 static { 49 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/j2ee/application-client", "http://geronimo.apache.org/xml/ns/j2ee/application-client-1.2"); 50 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/j2ee/application-client-1.0", "http://geronimo.apache.org/xml/ns/j2ee/application-client-1.2"); 51 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/j2ee/application-client-1.1", "http://geronimo.apache.org/xml/ns/j2ee/application-client-1.2"); 52 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/j2ee/application", "http://geronimo.apache.org/xml/ns/j2ee/application-1.2"); 53 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/j2ee/application-1.0", "http://geronimo.apache.org/xml/ns/j2ee/application-1.2"); 54 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/j2ee/application-1.1", "http://geronimo.apache.org/xml/ns/j2ee/application-1.2"); 55 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/deployment", "http://geronimo.apache.org/xml/ns/deployment-1.2"); 56 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/deployment-1.0", "http://geronimo.apache.org/xml/ns/deployment-1.2"); 57 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/deployment-1.1", "http://geronimo.apache.org/xml/ns/deployment-1.2"); 58 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/j2ee/connector", "http://geronimo.apache.org/xml/ns/j2ee/connector-1.2"); 59 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/j2ee/connector-1.0", "http://geronimo.apache.org/xml/ns/j2ee/connector-1.2"); 60 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/j2ee/connector-1.1", "http://geronimo.apache.org/xml/ns/j2ee/connector-1.2"); 61 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/deployment/javabean", "http://geronimo.apache.org/xml/ns/deployment/javabean-1.0"); 62 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/loginconfig", "http://geronimo.apache.org/xml/ns/loginconfig-1.0"); 63 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/loginconfig-1.0", "http://geronimo.apache.org/xml/ns/loginconfig-1.0"); 64 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/loginconfig-1.1", "http://geronimo.apache.org/xml/ns/loginconfig-1.0"); 65 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/naming", "http://geronimo.apache.org/xml/ns/naming-1.2"); 66 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/naming-1.0", "http://geronimo.apache.org/xml/ns/naming-1.2"); 67 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/naming-1.1", "http://geronimo.apache.org/xml/ns/naming-1.2"); 68 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/security", "http://geronimo.apache.org/xml/ns/security-1.2"); 69 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/security-1.0", "http://geronimo.apache.org/xml/ns/security-1.2"); 70 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/security-1.1", "http://geronimo.apache.org/xml/ns/security-1.2"); 71 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/web", "http://geronimo.apache.org/xml/ns/j2ee/web-1.2"); 72 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/j2ee/web-1.0", "http://geronimo.apache.org/xml/ns/j2ee/web-1.2"); 73 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/j2ee/web-1.1", "http://geronimo.apache.org/xml/ns/j2ee/web-1.2"); 74 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/web-1.0", "http://geronimo.apache.org/xml/ns/j2ee/web-1.2"); 75 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/web/jetty", "http://geronimo.apache.org/xml/ns/j2ee/web/jetty-1.2"); 76 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/web/jetty-1.0", "http://geronimo.apache.org/xml/ns/j2ee/web/jetty-1.2"); 77 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/j2ee/web/jetty-1.0", "http://geronimo.apache.org/xml/ns/j2ee/web/jetty-1.2"); 78 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/j2ee/web/jetty-1.1", "http://geronimo.apache.org/xml/ns/j2ee/web/jetty-1.2"); 79 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/web/jetty/config", "http://geronimo.apache.org/xml/ns/j2ee/web/jetty/config-1.0"); 80 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/web/tomcat", "http://geronimo.apache.org/xml/ns/j2ee/web/tomcat-1.2"); 81 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/web/tomcat-1.0", "http://geronimo.apache.org/xml/ns/j2ee/web/tomcat-1.2"); 82 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/j2ee/web/tomcat-1.0", "http://geronimo.apache.org/xml/ns/j2ee/web/tomcat-1.2"); 83 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/j2ee/web/tomcat-1.1", "http://geronimo.apache.org/xml/ns/j2ee/web/tomcat-1.2"); 84 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/web/tomcat/config", "http://geronimo.apache.org/xml/ns/j2ee/web/tomcat/config-1.0"); 85 NAMESPACE_UPDATES.put("http://www.openejb.org/xml/ns/openejb-jar", "http://www.openejb.org/xml/ns/openejb-jar-2.2"); 86 NAMESPACE_UPDATES.put("http://www.openejb.org/xml/ns/openejb-jar-2.0", "http://www.openejb.org/xml/ns/openejb-jar-2.2"); 87 NAMESPACE_UPDATES.put("http://www.openejb.org/xml/ns/openejb-jar-2.1", "http://www.openejb.org/xml/ns/openejb-jar-2.2"); 88 NAMESPACE_UPDATES.put("http://www.openejb.org/xml/ns/pkgen", "http://www.openejb.org/xml/ns/pkgen-2.0"); 89 NAMESPACE_UPDATES.put("http://www.openejb.org/xml/ns/corba-css-config_1_0", "http://www.openejb.org/xml/ns/corba-css-config-2.0"); 90 NAMESPACE_UPDATES.put("http://www.openejb.org/xml/ns/corba-tss-config_1_0", "http://www.openejb.org/xml/ns/corba-tss-config-2.0"); 91 } 92 93 private static final QName ENVIRONMENT_QNAME = new QName ("http://geronimo.apache.org/xml/ns/deployment-1.2", "environment"); 94 private static final String DEFAULT_GROUPID = "default"; 95 private static final String DEFAULT_VERSION = "1-default"; 96 private static final QName CLIENT_ENVIRONMENT_QNAME = new QName ("http://geronimo.apache.org/xml/ns/deployment-1.2", "client-environment"); 97 private static final QName SERVER_ENVIRONMENT_QNAME = new QName ("http://geronimo.apache.org/xml/ns/deployment-1.2", "server-environment"); 98 private static final QName PATTERN_QNAME = new QName ("http://geronimo.apache.org/xml/ns/naming-1.2", "pattern"); 99 private static final QName GROUP_QNAME = new QName ("http://geronimo.apache.org/xml/ns/naming-1.2", "groupId"); 100 private static final QName ARTIFACT_QNAME = new QName ("http://geronimo.apache.org/xml/ns/naming-1.2", "artifactId"); 101 private static final QName MODULE_QNAME = new QName ("http://geronimo.apache.org/xml/ns/naming-1.2", "module"); 102 private static final QName NAME_QNAME = new QName ("http://geronimo.apache.org/xml/ns/naming-1.2", "name"); 103 private static final QName NAME_QNAME2 = new QName ("http://geronimo.apache.org/xml/ns/deployment-1.2", "name"); 104 private static final QName GBEAN_NAME_QNAME = new QName (null, "gbeanName"); 105 106 public void upgrade(InputStream source, Writer target) throws IOException , XmlException { 107 XmlObject xmlObject = parse(source); 108 xmlObject = upgrade(xmlObject); 109 110 XmlOptions xmlOptions = new XmlOptions(); 111 xmlOptions.setSavePrettyPrint(); 112 xmlObject.save(target, xmlOptions); 113 114 } 115 116 public XmlObject upgrade(XmlObject xmlObject) throws XmlException { 117 XmlCursor cursor = xmlObject.newCursor(); 118 XmlCursor.TokenType token; 119 while ((token = cursor.toNextToken()) != XmlCursor.TokenType.ENDDOC) { 120 if (token == XmlCursor.TokenType.START) { 121 Artifact configId = extractArtifact("configId", cursor); 122 Artifact parentId = extractArtifact("parentId", cursor); 123 Artifact clientConfigId = extractArtifact("clientConfigId", cursor); 124 Artifact clientParentId = extractArtifact("clientParentId", cursor); 125 boolean suppressDefaultEnvironment = extractSuppressDefaultEnvironment(cursor); 126 if (clientConfigId != null) { 127 128 insertEnvironment(clientConfigId, clientParentId, cursor, CLIENT_ENVIRONMENT_QNAME, suppressDefaultEnvironment); 129 130 insertEnvironment(configId, parentId, cursor, SERVER_ENVIRONMENT_QNAME, false); 131 132 } else if (configId != null) { 133 134 insertEnvironment(configId, parentId, cursor, ENVIRONMENT_QNAME, suppressDefaultEnvironment); 135 } else { 136 cleanContextPriorityClassLoader(cursor); 137 cleanRef(cursor); 138 } 139 checkInvalid(cursor); 140 } 141 } 142 return xmlObject; 143 } 144 145 private void cleanContextPriorityClassLoader(XmlCursor cursor) { 146 String localName = getLocalName(cursor); 147 if ("context-priority-classloader".equals(localName)) { 148 String value = cursor.getTextValue(); 149 if ("false".equals(value)) { 150 cursor.removeXml(); 151 } else if ("true".equals("true")) { 152 cursor.removeXml(); 153 cursor.insertComment("YOU MUST INSERT THE ELEMENT <inverse-classloading/> INTO THE ENVIRONMENT ELEMENT FOR THIS MODULE"); 154 } 155 } 156 } 157 158 private static void checkInvalid(XmlCursor cursor) throws XmlException { 159 String localName = getLocalName(cursor); 160 if ("gbean".equals(localName)) { 161 if (cursor.getAttributeText(GBEAN_NAME_QNAME) != null) { 162 throw new XmlException("You must replace the gbeanName attribute manually: " + cursor.getAttributeText(GBEAN_NAME_QNAME)); 163 } 164 } 165 } 166 167 private static void cleanRef(XmlCursor cursor) throws XmlException { 168 String localName = getLocalName(cursor); 169 if ("ejb-ref".equals(localName)) { 170 cursor.toFirstChild(); 171 String application = null; 172 String module = null; 173 String name = null; 174 while (cursor.getName() != null) { 175 localName = cursor.getName().getLocalPart(); 176 if ("ref-name".equals(localName)) { 177 } else if ("domain".equals(localName)) { 179 cursor.removeXml(); 180 } else if ("server".equals(localName)) { 181 cursor.removeXml(); 182 } else if ("application".equals(localName)) { 183 application = cursor.getTextValue(); 184 if ("null".equals(application)) { 185 application = null; 186 } 187 cursor.removeXml(); 188 } else if ("module".equals(localName)) { 189 if (application == null) { 190 application = cursor.getTextValue(); 192 } else { 193 module = cursor.getTextValue(); 194 } 195 cursor.removeXml(); 196 } else if ("type".equals(localName)) { 197 cursor.removeXml(); 198 } else if ("name".equals(localName)) { 199 name = cursor.getTextValue(); 200 cursor.removeXml(); 201 } else if ("ejb-link".equals(localName)) { 202 break; 203 } else if ("target-name".equals(localName)) { 204 ObjectName targetName = extractObjectName(cursor); 205 name = targetName.getKeyProperty("name"); 206 application = targetName.getKeyProperty("J2EEApplication"); 207 if ("null".equals(application)) { 208 application = targetName.getKeyProperty("EJBModule"); 209 } else { 210 module = targetName.getKeyProperty("EJBModule"); 211 } 212 213 } else if ("ns-corbaloc".equals(localName)) { 214 cursor.toNextSibling(); 215 } else if ("css".equals(localName)) { 217 } else if ("css-link".equals(localName)) { 220 } else if ("css-name".equals(localName)) { 222 } else { 224 throw new IllegalStateException ("unrecognized element: " + cursor.getTextValue()); 225 } 226 if (!cursor.toNextSibling()) { 227 break; 228 } 229 } 230 if (name != null) { 231 cursor.beginElement(PATTERN_QNAME); 232 if (application != null) { 233 try { 234 Artifact artifact = Artifact.create(application); 235 cursor.insertElementWithText(GROUP_QNAME, artifact.getGroupId()); 236 cursor.insertElementWithText(ARTIFACT_QNAME, artifact.getArtifactId()); 237 } catch (Exception e) { 238 cursor.insertElementWithText(ARTIFACT_QNAME, application.replace('/', '_')); 239 } 240 } 242 if (module != null) { 243 cursor.insertElementWithText(MODULE_QNAME, module); 244 } 245 cursor.insertElementWithText(NAME_QNAME, name); 246 cursor.toNextToken(); 247 } 248 } else if ("gbean-name".equals(localName)) { 249 ObjectName targetName = extractObjectName(cursor); 250 String name = targetName.getKeyProperty("name"); 251 cursor.insertComment("CHECK THAT THE TARGET GBEAN IS IN THE ANCESTOR SET OF THIS MODULE AND THAT THE NAME UNIQUELY IDENTIFIES IT"); 252 cursor.insertElementWithText(NAME_QNAME2, name); 253 } 254 } 255 256 private static ObjectName extractObjectName(XmlCursor cursor) throws XmlException { 257 String targetNameString = cursor.getTextValue(); 258 cursor.removeXml(); 259 ObjectName targetName; 260 try { 261 targetName = ObjectName.getInstance(targetNameString); 262 } catch (MalformedObjectNameException e) { 263 throw new XmlException("Invalid object name: " + targetNameString); 264 } 265 return targetName; 266 } 267 268 private static String getLocalName(XmlCursor cursor) { 269 QName name = cursor.getName(); 270 return name == null ? null : name.getLocalPart(); 271 } 272 273 private static void insertEnvironment(Artifact configId, Artifact parentId, XmlCursor cursor, QName environmentQname, boolean suppressDefaultEnvironment) { 274 positionEnvironment(cursor); 275 Environment environment = new Environment(); 276 environment.setConfigId(configId); 277 if (parentId != null) { 278 environment.addDependency(parentId, ImportType.ALL); 279 } 280 environment.setSuppressDefaultEnvironment(suppressDefaultEnvironment); 281 extractDependencies(cursor, environment); 282 EnvironmentType environmentType = EnvironmentBuilder.buildEnvironmentType(environment); 283 cursor.beginElement(environmentQname); 284 XmlCursor element = environmentType.newCursor(); 285 try { 286 element.copyXmlContents(cursor); 287 } finally { 288 element.dispose(); 289 } 290 } 291 292 private static void extractDependencies(XmlCursor cursor, Environment environment) { 293 if (cursor.getName() == null) { 294 return; 296 } 297 do { 298 String localPart = getLocalName(cursor); 299 if (localPart.equals("dependency") || localPart.equals("import")) { 300 extractDependency(cursor, environment); 301 } else { 302 break; 303 } 304 } while (cursor.toNextSibling()); 305 } 306 307 private static void extractDependency(XmlCursor cursor, Environment environment) { 308 cursor.push(); 309 cursor.toFirstChild(); 310 Artifact artifact; 311 if (cursor.getName().getLocalPart().equals("uri")) { 312 String uri = cursor.getTextValue(); 313 artifact = toArtifact(uri); 314 } else { 315 checkName(cursor, "groupId"); 316 String groupId = cursor.getTextValue(); 317 cursor.toNextSibling(); 318 String type = "jar"; 319 if (cursor.getName().getLocalPart().equals("type")) { 320 type = cursor.getTextValue(); 321 cursor.toNextSibling(); 322 } 323 checkName(cursor, "artifactId"); 324 String artifactId = cursor.getTextValue(); 325 cursor.toNextSibling(); 326 checkName(cursor, "version"); 327 String version = cursor.getTextValue(); 328 artifact = new Artifact(groupId, artifactId, version, type); 329 } 330 environment.addDependency(artifact, ImportType.ALL); 331 cursor.pop(); 332 cursor.removeXml(); 333 } 334 335 private static void checkName(XmlCursor cursor, String localName) { 336 if (!cursor.getName().getLocalPart().equals(localName)) { 337 throw new IllegalArgumentException ("Expected element: " + localName + " but actually: " + cursor.getName().getLocalPart()); 338 } 339 340 } 341 342 private static void positionEnvironment(XmlCursor cursor) { 343 XmlCursor.TokenType token; 344 while ((token = cursor.toNextToken()) != XmlCursor.TokenType.START && token != XmlCursor.TokenType.END) { 345 } 347 } 348 349 private static Artifact extractArtifact(String attrName, XmlCursor cursor) { 350 String attrValue; 351 QName attrQName = new QName (null, attrName); 352 if ((attrValue = cursor.getAttributeText(attrQName)) != null) { 353 cursor.removeAttribute(attrQName); 354 return toArtifact(attrValue); 355 } 356 return null; 357 } 358 359 private static Artifact toArtifact(String attrValue) { 360 try { 361 return Artifact.create(attrValue); 362 } catch (Exception e) { 363 return new Artifact(DEFAULT_GROUPID, attrValue.replace('/', '_'), DEFAULT_VERSION, "car"); 364 } 365 } 366 367 private static boolean extractSuppressDefaultEnvironment(XmlCursor cursor) { 368 String attrValue; 369 QName attrQName = new QName (null, "suppressDefaultParentId"); 370 if ((attrValue = cursor.getAttributeText(attrQName)) != null) { 371 cursor.removeAttribute(attrQName); 372 return Boolean.valueOf(attrValue).booleanValue(); 373 } 374 return false; 375 } 376 377 public static XmlObject parse(InputStream is) throws IOException , XmlException { 378 ArrayList errors = new ArrayList (); 379 XmlObject parsed = XmlObject.Factory.parse(is, createXmlOptions(errors)); 380 if (errors.size() != 0) { 381 throw new XmlException(errors.toArray().toString()); 382 } 383 return parsed; 384 } 385 386 public static XmlOptions createXmlOptions(Collection errors) { 387 XmlOptions options = new XmlOptions(); 388 options.setLoadLineNumbers(); 389 options.setErrorListener(errors); 390 options.setLoadSubstituteNamespaces(NAMESPACE_UPDATES); 391 return options; 392 } 393 394 } 395 | Popular Tags |