KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > diagnostics > ServiceConfigHelper


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;
24
25 import com.sun.enterprise.server.ApplicationServer;
26 import com.sun.enterprise.config.ConfigContext;
27 import com.sun.enterprise.config.serverbeans.Config;
28 import com.sun.enterprise.config.serverbeans.ServerHelper;
29 import com.sun.enterprise.config.serverbeans.ConfigAPIHelper;
30 import com.sun.enterprise.config.serverbeans.DiagnosticService;
31 import com.sun.enterprise.config.serverbeans.Server;
32 import com.sun.enterprise.config.serverbeans.LogService;
33 import com.sun.enterprise.config.serverbeans.ServerTags;
34 import com.sun.enterprise.config.ConfigException;
35 import com.sun.enterprise.admin.server.core.AdminService;
36 import com.sun.logging.LogDomains;
37 import com.sun.enterprise.diagnostics.collect.DomainXMLHelper;
38
39 import java.util.logging.Level JavaDoc;
40 import java.util.logging.Logger JavaDoc;
41 import org.w3c.dom.*;
42
43
44
45 /**
46  * Helper which helps reading diagnostic service config elements from
47  * domain.xml either from runtime MBeans or by loading the document in local
48  * mode.
49  * @author Manisha Umbarje
50  */

51 public class ServiceConfigHelper {
52     private Element configElement;
53     private Element diagnosticElement;
54     private DomainXMLHelper xmlHelper;
55     private boolean local;
56     private String JavaDoc configName;
57     private String JavaDoc repositoryDir;
58     private String JavaDoc instanceName;
59     private static String JavaDoc envInstanceRootVar ="${com.sun.aas.instanceRoot}";
60    
61     
62     private static Logger JavaDoc logger =
63     LogDomains.getLogger(LogDomains.ADMIN_LOGGER);
64
65     public ServiceConfigHelper(String JavaDoc instanceName) {
66         this.instanceName = instanceName;
67     }
68     
69     public ServiceConfigHelper(String JavaDoc repositoryDir, String JavaDoc instanceName,
70             boolean local) {
71     this.repositoryDir = repositoryDir;
72         this.instanceName = instanceName;
73     this.local = local;
74         
75     }
76
77     /**
78      * Returns value of specified attribute from domain.xml
79      */

80     public String JavaDoc getAttribute(String JavaDoc attribute)
81         throws DiagnosticException {
82     if (local){
83             if (diagnosticElement == null)
84                 initializeXMLElements();
85         return getDOMAttribute(attribute);
86     }
87     return getRuntimeAttribute(attribute);
88     }
89     
90     /**
91      * Gets config name
92      * @return config name
93      */

94     public String JavaDoc getConfigName() {
95         return configName;
96     }
97     
98     /**
99      * Retrieves instance name
100      * @return instance name
101      */

102     public String JavaDoc instanceName() {
103         return instanceName;
104     }
105  
106     /**
107      * Initialize XML elements
108      */

109     private void initializeXMLElements() throws DiagnosticException {
110         try {
111             xmlHelper = new DomainXMLHelper(repositoryDir);
112             Element element = xmlHelper.getElement("server", instanceName);
113             configName = xmlHelper.getAttribute(element, "config-ref");
114             configElement = xmlHelper.getElement("config", configName);
115             diagnosticElement = xmlHelper.getElement(configElement,
116                                                     "diagnostic-service");
117         }catch(Exception JavaDoc e) {
118             e.printStackTrace();
119             throw new DiagnosticException(e.getMessage());
120         }
121     }
122     /**
123      * Retrieves value from runtime attributes
124      * @reutrn runtime value of a attribute
125      */

126     private String JavaDoc getRuntimeAttribute(String JavaDoc attribute)
127         throws DiagnosticException {
128     try {
129
130             logger.log(Level.FINE, "Instance Name :" + instanceName);
131
132             ConfigContext configContext =
133                     AdminService.getAdminService().getAdminContext().getAdminConfigContext();
134             Server server = ServerHelper.getServerByName(configContext, instanceName);
135             configName = server.getConfigRef();
136             Config config = ConfigAPIHelper.getConfigByName(configContext,
137                     configName);
138         if(attribute.equals(ServerTags.FILE)) {
139         LogService logService = config.getLogService();
140                 return determineLogFile(logService.getFile());
141         }
142         else {
143         DiagnosticService diagService = config.getDiagnosticService();
144         return diagService.getAttributeValue(attribute);
145         }
146     } catch(ConfigException ce) {
147         logger.log(Level.SEVERE,
148             "diagnostic-service.error_retrieving_logFileName",
149             ce.getMessage());
150         throw new DiagnosticException(ce.getMessage());
151     }
152
153     }//getRuntimeAttribute
154

155     /**
156      * Retrieves attribute from loaded DOM - domain.xml
157      * @return returns value of a attribute in a local mode
158      */

159     private String JavaDoc getDOMAttribute(String JavaDoc attribute)
160         throws DiagnosticException {
161
162     if(attribute.equals(ServerTags.FILE)) {
163         Element logElement = xmlHelper.getElement(configElement,"log-service");
164         return determineLogFile(logElement.getAttribute(ServerTags.FILE));
165     }
166
167     logger.log(Level.FINEST, "diagnostic-service.attribute_name",
168                 new Object JavaDoc[] {attribute});
169     logger.log(Level.FINEST, "diagnostic-service.attribute_value",
170                 new Object JavaDoc[] {diagnosticElement.getAttribute(attribute)});
171
172         return diagnosticElement.getAttribute(attribute);
173     }//getDOMAttribute
174

175     
176     /**
177      * Computes absolute path to log file from log-service/file
178      * attribute.
179      * @param logFileName log-service/file value
180      * @return absolute path of the log file.
181      */

182     private String JavaDoc determineLogFile(String JavaDoc logFileName) {
183         if(logFileName.startsWith(envInstanceRootVar)) {
184             int length = envInstanceRootVar.length() ;
185             if(length > 0) {
186                 String JavaDoc logFileSuffix = logFileName.substring(length);
187                 if(repositoryDir != null)
188                     logFileName = repositoryDir + logFileSuffix;
189                 else
190                     logFileName = logFileSuffix;
191             }
192          }
193          return logFileName;
194     }
195 }
196
Popular Tags