KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > upgrade > transform > TransformManager


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 /*
25  * TransformManager.java
26  *
27  * Created on August 4, 2003, 12:47 PM
28  */

29
30 package com.sun.enterprise.tools.upgrade.transform;
31
32 import com.sun.enterprise.tools.upgrade.transform.elements.*;
33 import java.io.File JavaDoc;
34 import java.io.IOException JavaDoc;
35
36 import javax.xml.parsers.DocumentBuilder JavaDoc;
37 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
38 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
39 import javax.xml.parsers.ParserConfigurationException JavaDoc;
40
41 import org.xml.sax.SAXException JavaDoc;
42 import org.xml.sax.SAXParseException JavaDoc;
43
44 import org.w3c.dom.Document JavaDoc;
45 import org.w3c.dom.DOMException JavaDoc;
46 import org.w3c.dom.Element JavaDoc;
47 import org.w3c.dom.Node JavaDoc;
48
49 // For transformation. Not really needed to retain.
50
import javax.xml.transform.Transformer JavaDoc;
51 import javax.xml.transform.TransformerFactory JavaDoc;
52 import javax.xml.transform.TransformerException JavaDoc;
53 import javax.xml.transform.TransformerConfigurationException JavaDoc;
54
55 import javax.xml.transform.dom.DOMSource JavaDoc;
56 import javax.xml.transform.stream.StreamResult JavaDoc;
57 import javax.xml.transform.OutputKeys JavaDoc;
58
59 import java.io.*;
60 import com.sun.enterprise.tools.upgrade.common.*;
61 import com.sun.enterprise.util.i18n.StringManager;
62 import com.sun.enterprise.tools.upgrade.logging.*;
63 import java.util.logging.*;
64 import java.util.Vector JavaDoc;
65 import java.util.Enumeration JavaDoc;
66
67
68 /**
69  *
70  * @author prakash
71  */

72 public class TransformManager implements BaseModule{
73     
74     private static TransformManager transManager;
75     private Document JavaDoc sourceDocument;
76     private Document JavaDoc resultDocument;
77     private StringManager stringManager = StringManager.getManager("com.sun.enterprise.tools.upgrade.transform");
78     private Logger logger = CommonInfoModel.getDefaultLogger();
79     private Vector JavaDoc recoveryList = new Vector JavaDoc();
80     
81     /** Creates a new instance of TransformManager */
82     public TransformManager() {
83     }
84     public static TransformManager getTransformManager(){
85         if(transManager == null)
86             transManager = new TransformManager();
87         return transManager;
88     }
89     
90     public void transform(Document JavaDoc source, Document JavaDoc result){
91         this.sourceDocument = source;
92         this.resultDocument = result;
93         try{
94             Element JavaDoc docEle = sourceDocument.getDocumentElement();
95             BaseElement baseElement = ElementToObjectMapper.getMapper().getElementObject(docEle.getTagName());
96             baseElement.transform(docEle, source.getDocumentElement(), result.getDocumentElement());
97         }catch(Exception JavaDoc ex){
98             // ****** LOG MESSAGE *************
99
logger.log(Level.SEVERE, stringManager.getString("upgrade.transform.startFailureMessage",ex.getMessage()),ex);
100         }
101     }
102     public boolean upgrade(CommonInfoModel commonInfo) {
103         logger.log(Level.INFO, stringManager.getString("upgrade.transform.startMessage"));
104         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
105         //factory.setValidating(false);
106

107         factory.setNamespaceAware(true);
108         if(commonInfo.getSourceDomainRootFlag()) {
109             factory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd",Boolean.FALSE);
110         }
111         
112         try {
113             backup(commonInfo.getTargetConfigXMLFile(), commonInfo);
114             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
115             DocumentBuilder JavaDoc builderDomainXml = factory.newDocumentBuilder();
116             builderDomainXml.setEntityResolver((org.xml.sax.helpers.DefaultHandler JavaDoc)Class.forName
117                     ("com.sun.enterprise.config.serverbeans.ServerValidationHandler").newInstance());
118             BaseElement.setCommonInfoModel(commonInfo);
119             Document JavaDoc resultDoc = builderDomainXml.parse( new File JavaDoc(commonInfo.getTargetConfigXMLFile()) );
120             // for testing only
121
if(commonInfo.getSourceVersionAndEdition().equals(UpgradeConstants.VERSION_AS7X_SE) ||
122                     commonInfo.getSourceVersionAndEdition().equals(UpgradeConstants.VERSION_AS7X_EE)){
123                 Document JavaDoc sourceDoc = builder.parse( new File JavaDoc(commonInfo.getSourceConfigXMLFile()));
124                 this.transform(sourceDoc, resultDoc);
125                 
126             }else{
127                 if(!commonInfo.getSourceVersion().equals(UpgradeConstants.VERSION_7X)){
128                     builder.setEntityResolver((org.xml.sax.helpers.DefaultHandler JavaDoc)Class.forName
129                             ("com.sun.enterprise.config.serverbeans.ServerValidationHandler").newInstance());
130                 }
131                 Document JavaDoc sourceDoc = builder.parse( new File JavaDoc(commonInfo.getSourceConfigXMLFile()));
132                 if((sourceDoc.getDocumentElement() != null) && (resultDoc.getDocumentElement() != null))
133                     this.transform(sourceDoc, resultDoc);
134                 else
135                     return false;
136             }
137             
138             // write out the resultDoc to destination file.
139

140             // Use a Transformer for output
141
TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
142             Transformer JavaDoc transformer = tFactory.newTransformer();
143             //transformer.setOutputProperty(OutputKeys.METHOD,"xml");
144
//transformer.setOutputProperty(OutputKeys.INDENT,"no");
145

146             if (resultDoc.getDoctype() != null){
147                 String JavaDoc systemValue = resultDoc.getDoctype().getSystemId();
148                 transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, systemValue);
149                 String JavaDoc pubValue = resultDoc.getDoctype().getPublicId();
150                 transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, pubValue);
151             }
152             
153             DOMSource JavaDoc source = new DOMSource JavaDoc(resultDoc);
154             StreamResult JavaDoc result = new StreamResult JavaDoc(new FileOutputStream(commonInfo.getTargetConfigXMLFile()));
155             transformer.transform(source, result);
156             result.getOutputStream().close();
157             
158         }catch (Exception JavaDoc ex){
159             UpdateProgressManager.getProgressManager().setContinueUpgrade(false);
160             logger.log(Level.SEVERE, stringManager.getString("upgrade.transform.startFailureMessage",ex.getMessage()),ex);
161             logger.log(Level.SEVERE, stringManager.getString("upgrade.transform.startFailureCheckAccessMessage"));
162             //ex.printStackTrace();
163
return false;
164         }
165         return true;
166     }
167     
168     private void backup(String JavaDoc filePath, CommonInfoModel commonInfo) throws IOException JavaDoc {
169         String JavaDoc backupFilePath = filePath + ".bak";
170         UpgradeUtils.getUpgradeUtils(commonInfo).copyFile(filePath, backupFilePath);
171         recoveryList.add(filePath);
172     }
173     
174     public void recovery(CommonInfoModel commonInfo) {
175         Enumeration JavaDoc e = recoveryList.elements();
176         while(e.hasMoreElements()){
177             String JavaDoc recoverPath = (String JavaDoc)e.nextElement();
178             String JavaDoc backupPath = recoverPath + ".bak";
179             try {
180                 UpgradeUtils.getUpgradeUtils(commonInfo).copyFile(backupPath, recoverPath);
181                 new File JavaDoc(backupPath).delete();
182             } catch (IOException JavaDoc ioe) {
183                 logger.log(Level.SEVERE, stringManager.getString("upgrade.realm.recoveryFailureMessage",ioe.getMessage()),new Object JavaDoc[]{recoverPath,ioe});
184             }
185         }
186     }
187     
188     /**
189      * @param args the command line arguments
190      */

191     public static void main(String JavaDoc[] args) {
192         System.setProperty("com.sun.aas.installRoot", "C:\\Softwares\\Sun\\j2eesdk1.4_beta3");
193         DocumentBuilderFactory JavaDoc factory =
194                 DocumentBuilderFactory.newInstance();
195         //factory.setValidating(true);
196
factory.setNamespaceAware(true);
197         try {
198             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
199             Document JavaDoc sourceDoc = builder.parse( new File JavaDoc("C:\\temp\\server.xml") );
200             Document JavaDoc resultDoc = builder.parse( new File JavaDoc("C:\\temp\\domain.xml") );
201             TransformManager transMan = TransformManager.getTransformManager();
202             transMan.transform(sourceDoc, resultDoc);
203             
204             // write out the resultDoc to destination file.
205

206             // Use a Transformer for output
207
TransformerFactory JavaDoc tFactory =
208                     TransformerFactory.newInstance();
209             Transformer JavaDoc transformer = tFactory.newTransformer();
210             
211             DOMSource JavaDoc source = new DOMSource JavaDoc(resultDoc);
212             StreamResult JavaDoc result = new StreamResult JavaDoc(new FileOutputStream("c:\\temp\\domainModified.xml"));
213             transformer.transform(source, result);
214             result.getOutputStream().close();
215             
216         } catch (SAXParseException JavaDoc spe) {
217             spe.printStackTrace();
218         } catch (SAXException JavaDoc sxe) {
219             sxe.printStackTrace();
220             
221         } catch (ParserConfigurationException JavaDoc pce) {
222             pce.printStackTrace();
223         } catch (IOException JavaDoc ioe) {
224             ioe.printStackTrace();
225         } catch (Exception JavaDoc ex){
226             ex.printStackTrace();
227         }
228     }
229     
230     public String JavaDoc getName() {
231         return stringManager.getString("upgrade.transform.moduleName");
232     }
233     
234 }
235
Popular Tags