KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > upgrade > Upgrade1_0To1_1


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.geronimo.upgrade;
18
19 import java.io.InputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.Writer JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26
27 import javax.xml.namespace.QName JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29 import javax.management.MalformedObjectNameException JavaDoc;
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 /**
42  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
43  */

44 public class Upgrade1_0To1_1 {
45
46     private static final Map JavaDoc NAMESPACE_UPDATES = new HashMap JavaDoc();
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 JavaDoc ENVIRONMENT_QNAME = new QName JavaDoc("http://geronimo.apache.org/xml/ns/deployment-1.2", "environment");
94     private static final String JavaDoc DEFAULT_GROUPID = "default";
95     private static final String JavaDoc DEFAULT_VERSION = "1-default";
96     private static final QName JavaDoc CLIENT_ENVIRONMENT_QNAME = new QName JavaDoc("http://geronimo.apache.org/xml/ns/deployment-1.2", "client-environment");
97     private static final QName JavaDoc SERVER_ENVIRONMENT_QNAME = new QName JavaDoc("http://geronimo.apache.org/xml/ns/deployment-1.2", "server-environment");
98     private static final QName JavaDoc PATTERN_QNAME = new QName JavaDoc("http://geronimo.apache.org/xml/ns/naming-1.2", "pattern");
99     private static final QName JavaDoc GROUP_QNAME = new QName JavaDoc("http://geronimo.apache.org/xml/ns/naming-1.2", "groupId");
100     private static final QName JavaDoc ARTIFACT_QNAME = new QName JavaDoc("http://geronimo.apache.org/xml/ns/naming-1.2", "artifactId");
101     private static final QName JavaDoc MODULE_QNAME = new QName JavaDoc("http://geronimo.apache.org/xml/ns/naming-1.2", "module");
102     private static final QName JavaDoc NAME_QNAME = new QName JavaDoc("http://geronimo.apache.org/xml/ns/naming-1.2", "name");
103     private static final QName JavaDoc NAME_QNAME2 = new QName JavaDoc("http://geronimo.apache.org/xml/ns/deployment-1.2", "name");
104     private static final QName JavaDoc GBEAN_NAME_QNAME = new QName JavaDoc(null, "gbeanName");
105
106     public void upgrade(InputStream JavaDoc source, Writer JavaDoc target) throws IOException JavaDoc, 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 JavaDoc localName = getLocalName(cursor);
147         if ("context-priority-classloader".equals(localName)) {
148             String JavaDoc 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 JavaDoc 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 JavaDoc localName = getLocalName(cursor);
169         if ("ejb-ref".equals(localName)) {
170             cursor.toFirstChild();
171             String JavaDoc application = null;
172             String JavaDoc module = null;
173             String JavaDoc name = null;
174             while (cursor.getName() != null) {
175                 localName = cursor.getName().getLocalPart();
176                 if ("ref-name".equals(localName)) {
177 // cursor.toNextSibling();
178
} 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                         //this is a configuration name
191
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 JavaDoc 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 // cursor.toNextSibling();
216
} else if ("css".equals(localName)) {
217                     //TODO fix this
218
// cursor.toNextSibling();
219
} else if ("css-link".equals(localName)) {
220 // cursor.toNextSibling();
221
} else if ("css-name".equals(localName)) {
222 // cursor.toNextSibling();
223
} else {
224                     throw new IllegalStateException JavaDoc("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 JavaDoc e) {
238                         cursor.insertElementWithText(ARTIFACT_QNAME, application.replace('/', '_'));
239                     }
240 // cursor.insertElementWithText(VERSION_QNAME, artifact.getVersion().toString());
241
}
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 JavaDoc targetName = extractObjectName(cursor);
250             String JavaDoc 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 JavaDoc extractObjectName(XmlCursor cursor) throws XmlException {
257         String JavaDoc targetNameString = cursor.getTextValue();
258         cursor.removeXml();
259         ObjectName JavaDoc targetName;
260         try {
261             targetName = ObjectName.getInstance(targetNameString);
262         } catch (MalformedObjectNameException JavaDoc e) {
263             throw new XmlException("Invalid object name: " + targetNameString);
264         }
265         return targetName;
266     }
267
268     private static String JavaDoc getLocalName(XmlCursor cursor) {
269         QName JavaDoc name = cursor.getName();
270         return name == null ? null : name.getLocalPart();
271     }
272
273     private static void insertEnvironment(Artifact configId, Artifact parentId, XmlCursor cursor, QName JavaDoc 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             //no dependencies, do nothing
295
return;
296         }
297         do {
298             String JavaDoc 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 JavaDoc uri = cursor.getTextValue();
313             artifact = toArtifact(uri);
314         } else {
315             checkName(cursor, "groupId");
316             String JavaDoc groupId = cursor.getTextValue();
317             cursor.toNextSibling();
318             String JavaDoc type = "jar";
319             if (cursor.getName().getLocalPart().equals("type")) {
320                 type = cursor.getTextValue();
321                 cursor.toNextSibling();
322             }
323             checkName(cursor, "artifactId");
324             String JavaDoc artifactId = cursor.getTextValue();
325             cursor.toNextSibling();
326             checkName(cursor, "version");
327             String JavaDoc 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 JavaDoc localName) {
336         if (!cursor.getName().getLocalPart().equals(localName)) {
337             throw new IllegalArgumentException JavaDoc("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             //keep going
346
}
347     }
348
349     private static Artifact extractArtifact(String JavaDoc attrName, XmlCursor cursor) {
350         String JavaDoc attrValue;
351         QName JavaDoc attrQName = new QName JavaDoc(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 JavaDoc attrValue) {
360         try {
361             return Artifact.create(attrValue);
362         } catch (Exception JavaDoc e) {
363             return new Artifact(DEFAULT_GROUPID, attrValue.replace('/', '_'), DEFAULT_VERSION, "car");
364         }
365     }
366
367     private static boolean extractSuppressDefaultEnvironment(XmlCursor cursor) {
368         String JavaDoc attrValue;
369         QName JavaDoc attrQName = new QName JavaDoc(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 JavaDoc is) throws IOException JavaDoc, XmlException {
378         ArrayList JavaDoc errors = new ArrayList JavaDoc();
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 JavaDoc 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