1 23 24 package com.sun.enterprise.admin.server.core.mbean.config; 25 26 27 import java.io.File ; 28 import java.io.InputStream ; 29 import java.io.FileInputStream ; 30 import java.io.FileNotFoundException ; 31 import java.io.BufferedInputStream ; 32 33 34 import com.sun.enterprise.util.io.FileUtils; 35 import com.sun.enterprise.util.OS; 36 import com.sun.enterprise.admin.util.TokenValue; 37 import com.sun.enterprise.admin.util.TokenValueSet; 38 import com.sun.enterprise.admin.util.LineTokenReplacer; 39 import com.sun.enterprise.server.Constants; 40 41 42 import javax.xml.transform.*; 43 import javax.xml.transform.stream.*; 44 45 50 public final class Domain2ServerTransformer { 51 52 private static final String ORIG_EXT = ".orig"; 53 private static final String XSL_NAME = "domain2server.xsl"; 54 private static final String DOCTYPE_TOKEN = "DTDREF"; 55 private static final String SERVER_DTD_NAME = "sun-server_1_0.dtd"; 56 57 58 private static final String SERVER_DTD_PATH = 59 System.getProperty(Constants.INSTALL_ROOT) + 60 "/lib/dtds/" + 61 SERVER_DTD_NAME; 62 private static String XSL_PATH = 63 System.getProperty(Constants.INSTALL_ROOT) + 64 "/lib/install/templates/" + 65 XSL_NAME; 66 67 private TransformerFactory tFactory; 68 69 private final String domainXmlPath; 70 private final String serverXmlPath; 71 private final String origServerXmlPath; 72 73 public Domain2ServerTransformer(String domainXmlPath, 74 String serverXmlPath) { 75 this.domainXmlPath = domainXmlPath; 76 this.serverXmlPath = serverXmlPath; 77 origServerXmlPath = serverXmlPath + ORIG_EXT; 78 createFactory(); 79 } 80 81 private void createFactory() { 82 try { 83 tFactory = TransformerFactory.newInstance(); 84 System.out.println("Created xform factory = " + tFactory); 85 } 86 catch(Exception e) { 87 System.out.println("Exception while creating transformer factory"); 88 e.printStackTrace(); 89 throw new RuntimeException (e); 90 } 91 } 92 93 public final void transform() { 94 this.transform(true); 95 } 96 97 public final void transform(boolean backup) { 98 if (backup) { 99 try { 100 if (new File (serverXmlPath).exists()) { 101 FileUtils.copy(serverXmlPath, origServerXmlPath); 102 } 103 System.out.println("ServerXmlPath = " + serverXmlPath); 104 System.out.println("DomainXmlPath = " + domainXmlPath); 105 System.out.println("ServerDtdPath = " + SERVER_DTD_PATH); 106 System.out.println("XslPath = " + XSL_PATH); 107 } 108 catch (Exception e) { 109 System.out.println("Could not backup server.xml before xform"); 110 throw new RuntimeException (e); 111 } 112 } 113 final String dtdPath = getDtdPathForServerXml(); 114 convert(); 115 replaceDocTypePath(dtdPath); 116 } 117 118 private void convert() { 119 try { 120 121 final InputStream xslStream = getXslStream(); 122 final StreamSource xsl = new StreamSource(xslStream); 123 124 125 final File domainXmlFile = new File (domainXmlPath); 126 System.out.println("The source xml = " + domainXmlFile.getAbsolutePath()); 127 final StreamSource xml = new StreamSource(domainXmlFile); 128 129 130 final File serverXmlFile = new File (serverXmlPath); 131 final StreamResult out = new StreamResult(serverXmlFile); 132 133 final Transformer transformer = tFactory.newTransformer(xsl); 134 135 transformer.transform(xml, out); 136 } 137 catch (Exception e) { 138 e.printStackTrace(); 139 throw new RuntimeException (e); 140 } 141 } 142 143 private InputStream getXslStream() throws FileNotFoundException { 144 return ( new FileInputStream (XSL_PATH) ); 145 154 } 155 156 private void replaceDocTypePath(String dtdPath) { 157 try { 158 159 final File tmpFile = File.createTempFile("temp", ".xml"); 160 final String tmpFilePath = tmpFile.getAbsolutePath(); 161 FileUtils.copy(serverXmlPath, tmpFilePath); 162 163 final TokenValue tv = new TokenValue(DOCTYPE_TOKEN, dtdPath); 164 System.out.println("TV = " + tv); 165 final TokenValueSet ts = new TokenValueSet(); 166 ts.add(tv); 167 final LineTokenReplacer replacer = new LineTokenReplacer(ts); 168 replacer.replace(tmpFilePath, serverXmlPath); 169 tmpFile.delete(); 170 } 171 catch (Exception e) { 172 throw new RuntimeException (e); 173 } 174 } 175 176 177 private String getDtdPathForServerXml() { 178 181 String PREFIX = "file://"; 182 if (OS.isWindows()) { 183 PREFIX = PREFIX+"/"; 184 } 185 return ( PREFIX + SERVER_DTD_PATH ); 186 220 } 221 } 222 | Popular Tags |