KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > diagnostics > collect > ConfigCollector


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 package com.sun.enterprise.diagnostics.collect;
24 import com.sun.logging.LogDomains;
25 import com.sun.enterprise.diagnostics.Data;
26 import com.sun.enterprise.diagnostics.Constants;
27 import com.sun.enterprise.diagnostics.ReportTarget;
28 import com.sun.enterprise.diagnostics.DiagnosticException;
29 import com.sun.enterprise.diagnostics.util.DiagnosticServiceHelper;
30 import com.sun.enterprise.diagnostics.util.XmlUtils;
31 import com.sun.enterprise.diagnostics.util.FileUtils;
32
33 import java.io.File JavaDoc;
34 import java.io.IOException JavaDoc;
35 import javax.xml.transform.TransformerException JavaDoc;
36 import javax.xml.transform.TransformerConfigurationException JavaDoc;
37 import javax.xml.parsers.ParserConfigurationException JavaDoc;
38 import java.util.logging.Level JavaDoc;
39 import java.util.logging.Logger JavaDoc;
40 import org.xml.sax.SAXException JavaDoc;
41 import org.w3c.dom.*;
42
43
44
45 /**
46  * Responsible for capturing domain.xml, sun-acc.xml, server.policy, login.conf
47  *
48  * @author Manisha Umbarje
49  */

50 public class ConfigCollector implements Collector {
51     
52     private static final String JavaDoc PASSWORD_REPLACEMENT ="****";
53     private static final String JavaDoc PASSWORD = "password";
54     
55     private String JavaDoc repositoryDir ;
56     private String JavaDoc reportDir ;
57    
58     private static Logger JavaDoc logger =
59     LogDomains.getLogger(LogDomains.ADMIN_LOGGER);
60     
61     /**
62      * @param repositoryDir central/local cache repository
63      * @param reportDir directory in which config information is collected.
64      * @targetType type of the target for which report generation is invoked
65      * instance
66      */

67     public ConfigCollector(String JavaDoc repositoryDir, String JavaDoc reportDir) {
68         this.repositoryDir = repositoryDir;
69         this.reportDir = reportDir;
70       }
71     
72       
73     /**
74      * Capture config information
75      * @throw DiagnosticException
76      */

77     public Data capture() throws DiagnosticException {
78         WritableDataImpl dataImpl = new WritableDataImpl(DataType.CONFIG_DETAILS);
79         
80         dataImpl.addChild(captureXMLFile(Constants.DOMAIN_XML));
81         dataImpl.addChild(captureFile(Constants.SERVER_POLICY));
82         dataImpl.addChild(captureFile(Constants.LOGIN_CONF));
83         dataImpl.addChild(captureFile(Constants.SUN_ACC));
84         return dataImpl;
85      }//captureConfigFiles
86

87     /**
88      * Masks confidential information with **** and copies it to destination
89      * @param fileName xml file to be captured.
90      * @throw DiagnosticException
91      */

92     public Data captureXMLFile(String JavaDoc fileName) throws DiagnosticException {
93         try {
94             String JavaDoc xmlFileToModify = repositoryDir + fileName;
95             String JavaDoc destFile = reportDir + fileName;
96             String JavaDoc domainXMLDTD =
97                     DiagnosticServiceHelper.getInstallationRoot() +
98                     Constants.DOMAIN_XML_DTD;
99
100             Document serverXml = XmlUtils.loadXML(xmlFileToModify,
101                      domainXMLDTD);
102             XmlUtils.attrSearchReplace(serverXml.getDocumentElement(),
103                     PASSWORD, PASSWORD_REPLACEMENT);
104             XmlUtils.copyXMLFile(serverXml, destFile);
105             return new FileData(destFile, DataType.CONFIG_DETAILS);
106             
107         } catch (SAXException JavaDoc se) {
108             logger.log(Level.WARNING, se.getMessage());
109         } catch (IOException JavaDoc ie) {
110             logger.log(Level.WARNING, ie.getMessage());
111         } catch (TransformerConfigurationException JavaDoc tce) {
112             logger.log(Level.WARNING, tce.getMessage());
113         } catch (TransformerException JavaDoc te) {
114             logger.log(Level.WARNING,te.getMessage());
115         } catch (ParserConfigurationException JavaDoc pce) {
116             logger.log(Level.WARNING,pce.getMessage());
117         }
118         return null;
119     }
120     
121     /**
122      * Copies file
123      * @param fileName relative path of file to be copied
124      * @throw DiagnosticException
125      */

126     public Data captureFile(String JavaDoc fileName) throws DiagnosticException {
127         try {
128              FileUtils.copyFile(repositoryDir + fileName,
129                     reportDir +fileName);
130              return new FileData(reportDir + fileName, DataType.CONFIG_DETAILS);
131         } catch(IOException JavaDoc ioe) {
132             logger.log(Level.WARNING,ioe.getMessage());
133         }
134         return null;
135    }
136     
137 }
138
Popular Tags