KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > server > core > mbean > config > Domain2ServerTransformer


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.admin.server.core.mbean.config;
25
26 /* JDK imports */
27 import java.io.File JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.FileInputStream JavaDoc;
30 import java.io.FileNotFoundException JavaDoc;
31 import java.io.BufferedInputStream JavaDoc;
32
33 /* S1AS classes */
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 /* Transform classes */
42 import javax.xml.transform.*;
43 import javax.xml.transform.stream.*;
44
45 /**
46  * Class to convert domain.xml into server.xml.
47  * Currently has the final implementation and has a single method
48  * called "transform".
49 */

50 public final class Domain2ServerTransformer {
51
52     private static final String JavaDoc ORIG_EXT = ".orig";
53     private static final String JavaDoc XSL_NAME = "domain2server.xsl";
54     private static final String JavaDoc DOCTYPE_TOKEN = "DTDREF";
55     private static final String JavaDoc SERVER_DTD_NAME = "sun-server_1_0.dtd";
56
57
58     private static final String JavaDoc SERVER_DTD_PATH =
59             System.getProperty(Constants.INSTALL_ROOT) +
60             "/lib/dtds/" +
61             SERVER_DTD_NAME;
62     private static String JavaDoc 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 JavaDoc domainXmlPath;
70     private final String JavaDoc serverXmlPath;
71     private final String JavaDoc origServerXmlPath;
72
73     public Domain2ServerTransformer(String JavaDoc domainXmlPath,
74                                     String JavaDoc 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 JavaDoc e) {
87             System.out.println("Exception while creating transformer factory");
88             e.printStackTrace();
89             throw new RuntimeException JavaDoc(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 JavaDoc(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 JavaDoc e) {
109                 System.out.println("Could not backup server.xml before xform");
110                 throw new RuntimeException JavaDoc(e);
111             }
112         }
113         final String JavaDoc dtdPath = getDtdPathForServerXml();
114         convert();
115         replaceDocTypePath(dtdPath);
116     }
117     
118     private void convert() {
119         try {
120             /* xsl */
121             final InputStream JavaDoc xslStream = getXslStream();
122             final StreamSource xsl = new StreamSource(xslStream);
123
124             /* domain xml */
125             final File JavaDoc domainXmlFile = new File JavaDoc(domainXmlPath);
126             System.out.println("The source xml = " + domainXmlFile.getAbsolutePath());
127             final StreamSource xml = new StreamSource(domainXmlFile);
128             
129             /* server xml */
130             final File JavaDoc serverXmlFile = new File JavaDoc(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 JavaDoc e) {
138             e.printStackTrace();
139             throw new RuntimeException JavaDoc(e);
140         }
141     }
142     
143     private InputStream JavaDoc getXslStream() throws FileNotFoundException JavaDoc {
144         return ( new FileInputStream JavaDoc(XSL_PATH) );
145         /*
146         final Class thisClass = Domain2ServerTransformer.class;
147         final String packageName =
148                 thisClass.getPackage().getName();
149         final String modifiedName = packageName.replace('.', '/');
150         final String xslStreamName = modifiedName + "/" + XSL_NAME;
151         System.out.println("The xslStreamName = " + xslStreamName);
152         return (thisClass.getClassLoader().getResourceAsStream(xslStreamName));
153         */

154     }
155     
156     private void replaceDocTypePath(String JavaDoc dtdPath) {
157         try {
158
159             final File JavaDoc tmpFile = File.createTempFile("temp", ".xml");
160             final String JavaDoc 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 JavaDoc e) {
172             throw new RuntimeException JavaDoc(e);
173         }
174     }
175     
176     
177     private String JavaDoc getDtdPathForServerXml() {
178         /* unfortunately, this has to be formed
179          * by the standard technique of the string concat!
180         */

181         String JavaDoc PREFIX = "file://";
182         if (OS.isWindows()) {
183             PREFIX = PREFIX+"/";
184         }
185         return ( PREFIX + SERVER_DTD_PATH );
186         /*
187         BufferedInputStream bis = null;
188         String dtd = null;
189         try {
190             final String PREFIX = "file:///";
191             final String DTD_EXT = ".dtd";
192             final File f = new File(serverXmlPath);
193             bis = new BufferedInputStream
194                     (new FileInputStream(f));
195             byte[] bytes = new byte[1024];
196             int bytesRead = 0;
197             while((bytesRead = bis.read(bytes)) != -1) {
198                 final String line = new String(bytes);
199                 final int start = line.indexOf(PREFIX);
200                 if (start != -1) {
201                     final int end = line.lastIndexOf(DTD_EXT);
202                     dtd = line.substring(start, end);
203                     dtd = dtd + DTD_EXT;
204                     break;
205                 }
206             }
207         }
208         catch(Exception e) {
209             throw new RuntimeException(e);
210         }
211         finally {
212             try {
213                 if (bis != null)
214                     bis.close();
215             }
216             catch(Exception e) {}
217         }
218         return ( dtd );
219         */

220     }
221 }
222
Popular Tags