1 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 ; 19 import java.io.ByteArrayInputStream ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.InputStreamReader ; 23 24 import javax.xml.namespace.QName ; 25 26 33 34 public class V1toV2 extends BaseConfigUpdate { 35 private static final String V1_NAMESPACE = "http://www.terracottatech.com/config-v1"; 36 private static final String V2_NAMESPACE = "http://www.terracotta.org/config"; 37 private static final String SCHEMA_LOCATION = "http://www.terracotta.org/schema/terracotta-2.2.xsd"; 38 private static final String XSI_NAMESPACE = "http://www.w3.org/2001/XMLSchema-instance"; 39 40 protected XmlOptions defaultXmlOptions; 41 private boolean addSchemaLocation; 42 43 public InputStream convert(InputStream in, XmlOptions xmlOptions) throws XmlException, IOException { 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 name = new QName (XSI_NAMESPACE, "schemaLocation"); 152 cursor.setAttributeText(name, SCHEMA_LOCATION); 153 } 154 cursor.dispose(); 155 } 156 157 InputStream inStream = v1Doc.newInputStream(options); 158 InputStreamReader inReader = new InputStreamReader (inStream); 159 BufferedReader bufferedReader = new BufferedReader (inReader); 160 StringBuffer sb = new StringBuffer (); 161 String nl = System.getProperty("line.separator"); 162 String 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 ioe) { 170 171 } 172 173 return new ByteArrayInputStream (sb.toString().getBytes()); 174 } 175 176 return null; 177 } 178 } 179 | Popular Tags |