001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.config.schema.migrate;
006:
007: import org.apache.commons.lang.StringUtils;
008: import org.apache.xmlbeans.StringEnumAbstractBase;
009: import org.apache.xmlbeans.XmlCursor;
010: import org.apache.xmlbeans.XmlException;
011: import org.apache.xmlbeans.XmlOptions;
012:
013: import com.terracottatech.configV1.Application;
014: import com.terracottatech.configV1.DsoApplication;
015: import com.terracottatech.configV1.DsoClientData;
016: import com.terracottatech.configV1.DsoServerData;
017:
018: import java.io.BufferedReader;
019: import java.io.ByteArrayInputStream;
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.io.InputStreamReader;
023:
024: import javax.xml.namespace.QName;
025:
026: /*
027: * Converts from the V1 to the V2 configuration format. The namespace has permanently changed from
028: * http://www.terracottatech.com/config.v1 to http://www.terracotta.org/config. DSO and JMX are now required. The
029: * following are gone: - JDBC - embedded HTTP server (Jetty) and HTTP interface to JMX -
030: * DsoClientData.maxInMemoryObjectCount - DsoServerData.serverCachedObjectCount - ConfigurationModel.DEMO - DSO
031: * ChangeListener
032: */
033:
034: public class V1toV2 extends BaseConfigUpdate {
035: private static final String V1_NAMESPACE = "http://www.terracottatech.com/config-v1";
036: private static final String V2_NAMESPACE = "http://www.terracotta.org/config";
037: private static final String SCHEMA_LOCATION = "http://www.terracotta.org/schema/terracotta-2.2.xsd";
038: private static final String XSI_NAMESPACE = "http://www.w3.org/2001/XMLSchema-instance";
039:
040: protected XmlOptions defaultXmlOptions;
041: private boolean addSchemaLocation;
042:
043: public InputStream convert(InputStream in, XmlOptions xmlOptions)
044: throws XmlException, IOException {
045: XmlOptions options = new XmlOptions(xmlOptions);
046: options
047: .setDocumentType(com.terracottatech.configV1.TcConfigDocument.type);
048:
049: com.terracottatech.configV1.TcConfigDocument v1Doc = com.terracottatech.configV1.TcConfigDocument.Factory
050: .parse(in);
051: if (v1Doc != null) {
052: com.terracottatech.configV1.TcConfigDocument.TcConfig v1Config = v1Doc
053: .getTcConfig();
054:
055: if (v1Config.isSetSystem()) {
056: com.terracottatech.configV1.System system = v1Config
057: .getSystem();
058: com.terracottatech.configV1.ConfigurationModel configModel = system
059: .xgetConfigurationModel();
060:
061: if (configModel != null) {
062: StringEnumAbstractBase value = configModel
063: .enumValue();
064: if (value.intValue() == com.terracottatech.configV1.ConfigurationModel.INT_DEMO) {
065: configModel
066: .set(com.terracottatech.configV1.ConfigurationModel.DEVELOPMENT);
067: }
068: }
069:
070: if (system.isSetDsoEnabled()) {
071: system.unsetDsoEnabled();
072: }
073: if (system.isSetJdbcEnabled()) {
074: system.unsetJdbcEnabled();
075: }
076: if (system.isSetHttpEnabled()) {
077: system.unsetHttpEnabled();
078: }
079: if (system.isSetJmxEnabled()) {
080: system.unsetJmxEnabled();
081: }
082: if (system.isSetJmxHttpEnabled()) {
083: system.unsetJmxHttpEnabled();
084: }
085:
086: if (!system.isSetConfigurationModel()
087: && !system.isSetLicense()) {
088: v1Config.unsetSystem();
089: }
090: }
091:
092: if (v1Config.isSetServers()) {
093: com.terracottatech.configV1.Servers servers = v1Config
094: .getServers();
095: com.terracottatech.configV1.Server server;
096: if (servers != null) {
097: for (int i = 0; i < servers.sizeOfServerArray(); i++) {
098: server = servers.getServerArray(i);
099:
100: if (server.isSetHttpPort()) {
101: server.unsetHttpPort();
102: }
103: if (server.isSetJdbcPort()) {
104: server.unsetJdbcPort();
105: }
106: if (server.isSetJmxHttpPort()) {
107: server.unsetJmxHttpPort();
108: }
109:
110: if (server.isSetDso()) {
111: DsoServerData dsoServerData = server
112: .getDso();
113:
114: if (dsoServerData
115: .isSetServerCachedObjectCount()) {
116: dsoServerData
117: .unsetServerCachedObjectCount();
118: }
119:
120: if (!dsoServerData
121: .isSetClientReconnectWindow()
122: && !dsoServerData
123: .isSetGarbageCollection()
124: && !dsoServerData
125: .isSetPersistence()) {
126: server.unsetDso();
127: }
128: }
129: }
130: }
131: }
132:
133: if (v1Config.isSetClients()) {
134: com.terracottatech.configV1.Client client = v1Config
135: .getClients();
136: if (client != null) {
137: if (client.isSetDso()) {
138: DsoClientData dsoClientData = client.getDso();
139:
140: if (dsoClientData.isSetMaxInMemoryObjectCount()) {
141: dsoClientData.unsetMaxInMemoryObjectCount();
142: }
143:
144: if (!dsoClientData.isSetDebugging()
145: && !dsoClientData.isSetFaultCount()) {
146: client.unsetDso();
147: }
148: }
149: }
150: }
151:
152: if (v1Config.isSetApplication()) {
153: Application application = v1Config.getApplication();
154:
155: if (application.isSetJdbc()) {
156: application.unsetJdbc();
157: }
158: if (application.isSetDso()) {
159: DsoApplication dsoApp = application.getDso();
160:
161: if (dsoApp.isSetChangeListener()) {
162: dsoApp.unsetChangeListener();
163: }
164: }
165: }
166:
167: if (addSchemaLocation) {
168: XmlCursor cursor = v1Doc.newCursor();
169: if (cursor.toFirstChild()) {
170: QName name = new QName(XSI_NAMESPACE,
171: "schemaLocation");
172: cursor.setAttributeText(name, SCHEMA_LOCATION);
173: }
174: cursor.dispose();
175: }
176:
177: InputStream inStream = v1Doc.newInputStream(options);
178: InputStreamReader inReader = new InputStreamReader(inStream);
179: BufferedReader bufferedReader = new BufferedReader(inReader);
180: StringBuffer sb = new StringBuffer();
181: String nl = System.getProperty("line.separator");
182: String s;
183:
184: try {
185: while ((s = bufferedReader.readLine()) != null) {
186: sb.append(StringUtils.replace(s, V1_NAMESPACE,
187: V2_NAMESPACE));
188: sb.append(nl);
189: }
190: } catch (IOException ioe) {
191: /* this won't happen because the source stream isn't file- or network-based */
192: }
193:
194: return new ByteArrayInputStream(sb.toString().getBytes());
195: }
196:
197: return null;
198: }
199: }
|