KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > config > schema > migrate > V1toV2


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.config.schema.migrate;
6
7 import org.apache.commons.lang.StringUtils;
8 import org.apache.xmlbeans.StringEnumAbstractBase;
9 import org.apache.xmlbeans.XmlCursor;
10 import org.apache.xmlbeans.XmlException;
11 import org.apache.xmlbeans.XmlOptions;
12
13 import com.terracottatech.configV1.Application;
14 import com.terracottatech.configV1.DsoApplication;
15 import com.terracottatech.configV1.DsoClientData;
16 import com.terracottatech.configV1.DsoServerData;
17
18 import java.io.BufferedReader JavaDoc;
19 import java.io.ByteArrayInputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.InputStreamReader JavaDoc;
23
24 import javax.xml.namespace.QName JavaDoc;
25
26 /*
27  * Converts from the V1 to the V2 configuration format. The namespace has permanently changed from
28  * http://www.terracottatech.com/config.v1 to http://www.terracotta.org/config. DSO and JMX are now required. The
29  * following are gone: - JDBC - embedded HTTP server (Jetty) and HTTP interface to JMX -
30  * DsoClientData.maxInMemoryObjectCount - DsoServerData.serverCachedObjectCount - ConfigurationModel.DEMO - DSO
31  * ChangeListener
32  */

33
34 public class V1toV2 extends BaseConfigUpdate {
35   private static final String JavaDoc V1_NAMESPACE = "http://www.terracottatech.com/config-v1";
36   private static final String JavaDoc V2_NAMESPACE = "http://www.terracotta.org/config";
37   private static final String JavaDoc SCHEMA_LOCATION = "http://www.terracotta.org/schema/terracotta-2.2.xsd";
38   private static final String JavaDoc XSI_NAMESPACE = "http://www.w3.org/2001/XMLSchema-instance";
39
40   protected XmlOptions defaultXmlOptions;
41   private boolean addSchemaLocation;
42
43   public InputStream JavaDoc convert(InputStream JavaDoc in, XmlOptions xmlOptions) throws XmlException, IOException JavaDoc {
44     XmlOptions options = new XmlOptions(xmlOptions);
45     options.setDocumentType(com.terracottatech.configV1.TcConfigDocument.type);
46     
47     com.terracottatech.configV1.TcConfigDocument v1Doc = com.terracottatech.configV1.TcConfigDocument.Factory.parse(in);
48     if (v1Doc != null) {
49       com.terracottatech.configV1.TcConfigDocument.TcConfig v1Config = v1Doc.getTcConfig();
50
51       if (v1Config.isSetSystem()) {
52         com.terracottatech.configV1.System system = v1Config.getSystem();
53         com.terracottatech.configV1.ConfigurationModel configModel = system.xgetConfigurationModel();
54
55         if (configModel != null) {
56           StringEnumAbstractBase value = configModel.enumValue();
57           if (value.intValue() == com.terracottatech.configV1.ConfigurationModel.INT_DEMO) {
58             configModel.set(com.terracottatech.configV1.ConfigurationModel.DEVELOPMENT);
59           }
60         }
61
62         if (system.isSetDsoEnabled()) {
63           system.unsetDsoEnabled();
64         }
65         if (system.isSetJdbcEnabled()) {
66           system.unsetJdbcEnabled();
67         }
68         if (system.isSetHttpEnabled()) {
69           system.unsetHttpEnabled();
70         }
71         if (system.isSetJmxEnabled()) {
72           system.unsetJmxEnabled();
73         }
74         if (system.isSetJmxHttpEnabled()) {
75           system.unsetJmxHttpEnabled();
76         }
77
78         if (!system.isSetConfigurationModel() && !system.isSetLicense()) {
79           v1Config.unsetSystem();
80         }
81       }
82
83       if (v1Config.isSetServers()) {
84         com.terracottatech.configV1.Servers servers = v1Config.getServers();
85         com.terracottatech.configV1.Server server;
86         if (servers != null) {
87           for (int i = 0; i < servers.sizeOfServerArray(); i++) {
88             server = servers.getServerArray(i);
89
90             if (server.isSetHttpPort()) {
91               server.unsetHttpPort();
92             }
93             if (server.isSetJdbcPort()) {
94               server.unsetJdbcPort();
95             }
96             if (server.isSetJmxHttpPort()) {
97               server.unsetJmxHttpPort();
98             }
99
100             if (server.isSetDso()) {
101               DsoServerData dsoServerData = server.getDso();
102
103               if (dsoServerData.isSetServerCachedObjectCount()) {
104                 dsoServerData.unsetServerCachedObjectCount();
105               }
106
107               if (!dsoServerData.isSetClientReconnectWindow() && !dsoServerData.isSetGarbageCollection()
108                   && !dsoServerData.isSetPersistence()) {
109                 server.unsetDso();
110               }
111             }
112           }
113         }
114       }
115
116       if (v1Config.isSetClients()) {
117         com.terracottatech.configV1.Client client = v1Config.getClients();
118         if (client != null) {
119           if (client.isSetDso()) {
120             DsoClientData dsoClientData = client.getDso();
121
122             if (dsoClientData.isSetMaxInMemoryObjectCount()) {
123               dsoClientData.unsetMaxInMemoryObjectCount();
124             }
125
126             if (!dsoClientData.isSetDebugging() && !dsoClientData.isSetFaultCount()) {
127               client.unsetDso();
128             }
129           }
130         }
131       }
132
133       if (v1Config.isSetApplication()) {
134         Application application = v1Config.getApplication();
135
136         if (application.isSetJdbc()) {
137           application.unsetJdbc();
138         }
139         if (application.isSetDso()) {
140           DsoApplication dsoApp = application.getDso();
141
142           if (dsoApp.isSetChangeListener()) {
143             dsoApp.unsetChangeListener();
144           }
145         }
146       }
147
148       if (addSchemaLocation) {
149         XmlCursor cursor = v1Doc.newCursor();
150         if (cursor.toFirstChild()) {
151           QName JavaDoc name = new QName JavaDoc(XSI_NAMESPACE, "schemaLocation");
152           cursor.setAttributeText(name, SCHEMA_LOCATION);
153         }
154         cursor.dispose();
155       }
156
157       InputStream JavaDoc inStream = v1Doc.newInputStream(options);
158       InputStreamReader JavaDoc inReader = new InputStreamReader JavaDoc(inStream);
159       BufferedReader JavaDoc bufferedReader = new BufferedReader JavaDoc(inReader);
160       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
161       String JavaDoc nl = System.getProperty("line.separator");
162       String JavaDoc s;
163
164       try {
165         while ((s = bufferedReader.readLine()) != null) {
166           sb.append(StringUtils.replace(s, V1_NAMESPACE, V2_NAMESPACE));
167           sb.append(nl);
168         }
169       } catch (IOException JavaDoc ioe) {
170         /* this won't happen because the source stream isn't file- or network-based */
171       }
172
173       return new ByteArrayInputStream JavaDoc(sb.toString().getBytes());
174     }
175
176     return null;
177   }
178 }
179
Popular Tags