1 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 ; 40 import java.util.logging.Logger ; 41 import org.w3c.dom.*; 42 43 44 45 51 public class ServiceConfigHelper { 52 private Element configElement; 53 private Element diagnosticElement; 54 private DomainXMLHelper xmlHelper; 55 private boolean local; 56 private String configName; 57 private String repositoryDir; 58 private String instanceName; 59 private static String envInstanceRootVar ="${com.sun.aas.instanceRoot}"; 60 61 62 private static Logger logger = 63 LogDomains.getLogger(LogDomains.ADMIN_LOGGER); 64 65 public ServiceConfigHelper(String instanceName) { 66 this.instanceName = instanceName; 67 } 68 69 public ServiceConfigHelper(String repositoryDir, String instanceName, 70 boolean local) { 71 this.repositoryDir = repositoryDir; 72 this.instanceName = instanceName; 73 this.local = local; 74 75 } 76 77 80 public String getAttribute(String 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 94 public String getConfigName() { 95 return configName; 96 } 97 98 102 public String instanceName() { 103 return instanceName; 104 } 105 106 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 e) { 118 e.printStackTrace(); 119 throw new DiagnosticException(e.getMessage()); 120 } 121 } 122 126 private String getRuntimeAttribute(String 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 } 155 159 private String getDOMAttribute(String 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 [] {attribute}); 169 logger.log(Level.FINEST, "diagnostic-service.attribute_value", 170 new Object [] {diagnosticElement.getAttribute(attribute)}); 171 172 return diagnosticElement.getAttribute(attribute); 173 } 175 176 182 private String determineLogFile(String logFileName) { 183 if(logFileName.startsWith(envInstanceRootVar)) { 184 int length = envInstanceRootVar.length() ; 185 if(length > 0) { 186 String 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 |