001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: /*
038: * SystemLoggerUtilities.java
039: */
040:
041: package com.sun.jbi.jsf.util;
042:
043: import com.sun.enterprise.tools.admingui.util.AMXUtil;
044: import com.sun.enterprise.tools.admingui.util.JMXUtil;
045: import com.sun.enterprise.tools.admingui.util.GuiUtil;
046: import com.sun.appserv.management.config.ConfigConfig;
047: import com.sun.appserv.management.config.ModuleLogLevelsConfig;
048: import com.sun.jbi.jsf.util.BeanUtilities;
049: import com.sun.jbi.ui.common.JBIAdminCommands;
050: import java.util.Hashtable;
051: import java.util.TreeMap;
052: import java.util.Map;
053: import java.io.InputStream;
054: import java.util.logging.Logger;
055: import java.util.logging.Level;
056: import java.util.List;
057:
058: import javax.xml.parsers.DocumentBuilderFactory;
059: import org.w3c.dom.Document;
060: import org.w3c.dom.NodeList;
061: import org.w3c.dom.Element;
062: import org.w3c.dom.Attr;
063: import java.io.IOException;
064: import org.xml.sax.SAXException;
065: import org.xml.sax.SAXParseException;
066: import javax.xml.parsers.ParserConfigurationException;
067:
068: /**
069: *
070: * JBI Contstants related to the JSR-208 Specification
071: *
072: **/
073: public final class SystemLoggerUtilities {
074:
075: public static final Hashtable loggerLabels = new Hashtable();
076: static {
077: loggerLabels.put("admin", "Admin");
078: loggerLabels.put("ejb", "EJB Container");
079: loggerLabels.put("classloader", "Classloader");
080: loggerLabels.put("configuration", "Configuration");
081: loggerLabels.put("connector", "Connector");
082: loggerLabels.put("corba", "CORBA");
083: loggerLabels.put("deployment", "Deployment");
084: loggerLabels.put("javamail", "JavaMail");
085: loggerLabels.put("jaxr", "JAXR");
086: loggerLabels.put("jaxrpc", "JAXRPC");
087: loggerLabels.put("jms", "JMS");
088: loggerLabels.put("jta", "JTA");
089: loggerLabels.put("jts", "JTS");
090: loggerLabels.put("mdb", "MDB Container");
091: loggerLabels.put("naming", "Naming");
092: loggerLabels.put("root", "Root");
093: loggerLabels.put("saaj", "SAAJ");
094: loggerLabels.put("security", "Security");
095: loggerLabels.put("selfmanagement", "Self Management");
096: loggerLabels.put("server", "System");
097: loggerLabels.put("util", "Util");
098: loggerLabels.put("verifier", "Verifier");
099: loggerLabels.put("web", "Web Container");
100: loggerLabels.put("jbi", "JBI");
101: loggerLabels.put("nodeggent", "Node Agent");
102: loggerLabels.put("synchronization", "Synchronization");
103: loggerLabels.put("gms", "Group Management Service");
104: }
105:
106: public static final Hashtable loggerNames = new Hashtable();
107: static {
108: loggerNames.put("javax.enterprise.system.tools.admin", "Admin");
109: loggerNames.put("javax.enterprise.system.container.ejb", "EJB");
110: loggerNames.put("javax.enterprise.system.core.classloading",
111: "Classloader");
112: loggerNames.put("javax.enterprise.system.core.config",
113: "Configuration");
114: loggerNames.put("javax.enterprise.resource.resourceadapter",
115: "Connector");
116: loggerNames.put("javax.enterprise.resource.corba", "Corba");
117: loggerNames.put("javax.enterprise.system.tools.deployment",
118: "Deployment");
119: loggerNames.put("javax.enterprise.resource.javamail",
120: "Javamail");
121: loggerNames.put("javax.enterprise.system.webservices.registry",
122: "Jaxr");
123: loggerNames.put("javax.enterprise.system.webservices.rpc",
124: "Jaxrpc");
125: loggerNames.put(
126: "javax.enterprise.resource.webservices.jaxws.rpc",
127: "Jaxws");
128: loggerNames
129: .put(
130: "javax.enterprise.resource.jms; javax.resourceadapter.mqjmsra",
131: "Jms");
132: loggerNames.put("javax.enterprise.resource.jta", "Jta");
133: loggerNames.put("javax.enterprise.system.core.transaction",
134: "Jts");
135: loggerNames.put("javax.enterprise.system.container.ejb.mdb",
136: "MDB");
137: loggerNames
138: .put("javax.enterprise.system.core.naming", "Naming");
139: loggerNames.put("javax.enterprise", "Root");
140: loggerNames.put("javax.enterprise.system.webservices.saaj",
141: "Saaj");
142: loggerNames.put("javax.enterprise.system.core.security",
143: "Security");
144: loggerNames.put("javax.enterprise.system.core.selfmanagement",
145: "SelfManagement");
146: loggerNames.put("javax.enterprise.system", "Server");
147: loggerNames.put("javax.enterprise.system.util", "Util");
148: loggerNames.put("javax.enterprise.system.tools.verifier",
149: "Verifier");
150: loggerNames
151: .put(
152: "javax.enterprise.system.container.web; org.apache.catalina; org.apache.coyote; org.apache.jasper;",
153: "WEB");
154: loggerNames.put("com.sun.jbi", "JBI");
155: loggerNames.put("javax.ee.enterprise.system.nodeagent",
156: "NodeAgent");
157: loggerNames.put(
158: "javax.ee.enterprise.system.tools.synchronization",
159: "Synchronization");
160: loggerNames.put("javax.ee.enterprise.system.gms", "Gms");
161: }
162:
163: private static final String JAXWS_MODULE_PROPERTY = "javax.enterprise.resource.webservices.jaxws";
164: private static final String JBI_MODULE_PROPERTY = "com.sun.jbi";
165:
166: private static Logger sLog = JBILogger.getInstance();
167:
168: /**
169: * This constructor method is passed an XML file. It uses the JAXP API to
170: * obtain a DOM parser, and to parse the file into a DOM Document object,
171: * which is used by the remaining methods of the class.
172: **/
173: private static Document parseDomDocument(InputStream documentStream)
174: throws IOException, SAXException,
175: ParserConfigurationException {
176: // Get a JAXP parser factory object
177: DocumentBuilderFactory dbf = DocumentBuilderFactory
178: .newInstance();
179:
180: // Tell the factory what kind of parser we want
181: dbf.setValidating(false);
182:
183: // Use the factory to get a JAXP parser object
184: javax.xml.parsers.DocumentBuilder parser = dbf
185: .newDocumentBuilder();
186:
187: Document document = parser.parse(documentStream);
188: return document;
189: }
190:
191: private static String getLoggerList(String module) {
192: String[] params = { module };
193: String[] types = { "java.lang.String" };
194:
195: String logList = "";
196: String seperator = "";
197: List loggers = (List) JMXUtil
198: .invoke(
199: "com.sun.appserv:name=logmanager,category=runtime,server=server",
200: "getLognames4LogModule", params, types);
201: if (loggers != null) {
202: for (int cnt = 0; cnt < loggers.size(); cnt++) {
203: logList += loggers.get(cnt);
204: logList += seperator;
205: seperator = "; ";
206: }
207: }
208:
209: return logList;
210: }
211:
212: public static TreeMap addAdditionalSystemLoggers(
213: TreeMap aLogLevels, String aComponentName, String aTarget,
214: String additionalFile)
215:
216: {
217: InputStream fileInputStream = null;
218: Class c = null;
219: Document document = null;
220: try {
221: c = Class
222: .forName("com.sun.jbi.jsf.util.JBILogLevelsPropertySheetAdaptor");
223: fileInputStream = c.getResourceAsStream(additionalFile);
224: } catch (Exception ex) {
225: sLog
226: .fine("JBILogLevelsPropertySheetAdaptor(): error retrieving input stream for the additional logger xml file.");
227: return (aLogLevels);
228: }
229:
230: // If the inputStream is null, then we can assume that the file xml did not
231: // exist. This is ok, we'll just continue with our processing.
232: if (fileInputStream != null) {
233: try {
234:
235: aTarget = ClusterUtilities
236: .getInstanceDomainCluster(aTarget);
237: String targetConfig = aTarget.toLowerCase() + "-config";
238:
239: document = parseDomDocument(fileInputStream);
240: NodeList loggersList = document
241: .getElementsByTagName("additional-loggers");
242: int loggersListLength = loggersList.getLength();
243:
244: for (int i = 0; i < loggersListLength; i++) {
245:
246: Element componentNameElement = (Element) loggersList
247: .item(i);
248: NodeList componentList = componentNameElement
249: .getElementsByTagName("component-name");
250: int componentListLength = componentList.getLength();
251:
252: for (int j = 0; j < componentListLength; j++) {
253:
254: Element componentElement = (Element) componentList
255: .item(j);
256: Attr componentNameAttr = componentElement
257: .getAttributeNode("value");
258: String componentName = componentNameAttr
259: .getValue();
260:
261: if (aComponentName.equals(componentName)) {
262: NodeList moduleList = componentElement
263: .getElementsByTagName("module-name");
264: int moduleListLength = moduleList
265: .getLength();
266:
267: for (int k = 0; k < moduleListLength; k++) {
268: Element moduleElement = (Element) moduleList
269: .item(k);
270: Attr moduleNameAttr = moduleElement
271: .getAttributeNode("value");
272: String moduleName = moduleNameAttr
273: .getValue();
274: String listName = moduleName
275: .toLowerCase();
276:
277: sLog
278: .fine("JBILogLevelsPropertySheetAdaptor - addAdditionalSystemLoggers: "
279: + "componentName="
280: + aComponentName
281: + ", targetName="
282: + aTarget
283: + ", moduleName="
284: + moduleName);
285:
286: String loggerList = getLoggerList(listName);
287: ConfigConfig config = AMXUtil
288: .getConfig(targetConfig);
289: ModuleLogLevelsConfig mConfig = config
290: .getLogServiceConfig()
291: .getModuleLogLevelsConfig();
292:
293: String logLevelValue = SystemLoggerUtilities
294: .getLogLevelValue(moduleName,
295: targetConfig);
296: Level systemLogLevel = Level
297: .parse(logLevelValue);
298: aLogLevels.put(loggerList,
299: systemLogLevel);
300: }
301: }
302: }
303: }
304: } catch (IOException ex) {
305: sLog
306: .fine("JBILogLevelsPropertySheetAdaptor(): Unable to find theAdditional Logger File.");
307: } catch (ParserConfigurationException ex) {
308: sLog
309: .fine("JBILogLevelsPropertySheetAdaptor(): Error parsing the Additional Logger File.");
310: } catch (SAXException ex) {
311: sLog
312: .fine("JBILogLevelsPropertySheetAdaptor(): Error parsing the Additional Logger File.");
313: } catch (RuntimeException ex) {
314: sLog
315: .fine("JBILogLevelsPropertySheetAdaptor(): Error parsing the Additional Logger File.");
316: }
317: }
318: return (aLogLevels);
319: }
320:
321: /**
322: * <p> Sets the Component Id using the given id tag string.</p>
323: *
324: * @param aLoggerName The logger name.
325: * @param aTargetConfig The target configuration name
326: */
327: public static String getLogLevelValue(String aLoggerName,
328: String aTargetConfig) {
329: ConfigConfig config = AMXUtil.getConfig(aTargetConfig);
330: ModuleLogLevelsConfig mConfig = config.getLogServiceConfig()
331: .getModuleLogLevelsConfig();
332: String levelValue = "INFO";
333:
334: if (aLoggerName.equalsIgnoreCase("Admin")) {
335: levelValue = mConfig.getAdmin();
336: } else if (aLoggerName.equalsIgnoreCase("Classloader")) {
337: levelValue = mConfig.getClassloader();
338: } else if (aLoggerName.equalsIgnoreCase("Configuration")) {
339: levelValue = mConfig.getConfiguration();
340: } else if (aLoggerName.equalsIgnoreCase("Connector")) {
341: levelValue = mConfig.getConnector();
342: } else if (aLoggerName.equalsIgnoreCase("Corba")) {
343: levelValue = mConfig.getCORBA();
344: } else if (aLoggerName.equalsIgnoreCase("Deployment")) {
345: levelValue = mConfig.getDeployment();
346: } else if (aLoggerName.equalsIgnoreCase("Javamail")) {
347: levelValue = mConfig.getJavamail();
348: } else if (aLoggerName.equalsIgnoreCase("Jaxr")) {
349: levelValue = mConfig.getJAXR();
350: } else if (aLoggerName.equalsIgnoreCase("Jaxrpc")) {
351: levelValue = mConfig.getJAXRPC();
352: } else if (aLoggerName.equalsIgnoreCase("Jms")) {
353: levelValue = mConfig.getJMS();
354: } else if (aLoggerName.equalsIgnoreCase("Jta")) {
355: levelValue = mConfig.getJTA();
356: } else if (aLoggerName.equalsIgnoreCase("Jts")) {
357: levelValue = mConfig.getJTS();
358: } else if (aLoggerName.equalsIgnoreCase("MDB")) {
359: levelValue = mConfig.getMDBContainer();
360: } else if (aLoggerName.equalsIgnoreCase("Naming")) {
361: levelValue = mConfig.getNaming();
362: } else if (aLoggerName.equalsIgnoreCase("EJB")) {
363: levelValue = mConfig.getEJBContainer();
364: } else if (aLoggerName.equalsIgnoreCase("Root")) {
365: levelValue = mConfig.getRoot();
366: } else if (aLoggerName.equalsIgnoreCase("Saaj")) {
367: levelValue = mConfig.getSAAJ();
368: } else if (aLoggerName.equalsIgnoreCase("Security")) {
369: levelValue = mConfig.getSecurity();
370: } else if (aLoggerName.equalsIgnoreCase("Server")) {
371: levelValue = mConfig.getServer();
372: } else if (aLoggerName.equalsIgnoreCase("Util")) {
373: levelValue = mConfig.getUtil();
374: } else if (aLoggerName.equalsIgnoreCase("Verifier")) {
375: levelValue = mConfig.getVerifier();
376: } else if (aLoggerName.equalsIgnoreCase("WEB")) {
377: levelValue = mConfig.getWebContainer();
378: } else if (aLoggerName.equalsIgnoreCase("Jbi")) {
379: if (mConfig.existsProperty(JBI_MODULE_PROPERTY))
380: levelValue = mConfig
381: .getPropertyValue(JBI_MODULE_PROPERTY);
382: } else if (aLoggerName.equalsIgnoreCase("Jaxws")) {
383: if (mConfig.existsProperty(JAXWS_MODULE_PROPERTY))
384: levelValue = mConfig
385: .getPropertyValue(JAXWS_MODULE_PROPERTY);
386: } else if (aLoggerName.equalsIgnoreCase("NodeAgent")) {
387: levelValue = mConfig.getNodeAgent();
388: } else if (aLoggerName.equalsIgnoreCase("Synchronization")) {
389: levelValue = mConfig.getSynchronization();
390: } else if (aLoggerName.equalsIgnoreCase("Gms")) {
391: levelValue = mConfig.getGroupManagementService();
392: }
393: return levelValue;
394: }
395:
396: /**
397: * <p> Sets the Component Id using the given id tag string.</p>
398: *
399: * @param aLoggerName The logger name.
400: * @param aTargetConfig The target configuration name
401: */
402: public static void setLogLevelValue(String aLoggerName,
403: String aTarget, String aLevel) {
404: String loggerTagName = (String) loggerNames.get(aLoggerName);
405:
406: aTarget = ClusterUtilities.getInstanceDomainCluster(aTarget);
407: String targetConfig = aTarget.toLowerCase() + "-config";
408:
409: ConfigConfig config = AMXUtil.getConfig(targetConfig);
410: ModuleLogLevelsConfig mConfig = config.getLogServiceConfig()
411: .getModuleLogLevelsConfig();
412:
413: if (loggerTagName.equalsIgnoreCase("Admin")) {
414: mConfig.setAdmin(aLevel);
415: } else if (loggerTagName.equalsIgnoreCase("Classloader")) {
416: mConfig.setClassloader(aLevel);
417: } else if (loggerTagName.equalsIgnoreCase("Configuration")) {
418: mConfig.setConfiguration(aLevel);
419: } else if (loggerTagName.equalsIgnoreCase("Connector")) {
420: mConfig.setConnector(aLevel);
421: } else if (loggerTagName.equalsIgnoreCase("Corba")) {
422: mConfig.setCORBA(aLevel);
423: } else if (loggerTagName.equalsIgnoreCase("Deployment")) {
424: mConfig.setDeployment(aLevel);
425: } else if (loggerTagName.equalsIgnoreCase("Javamail")) {
426: mConfig.setJavamail(aLevel);
427: } else if (loggerTagName.equalsIgnoreCase("Jaxr")) {
428: mConfig.setJAXR(aLevel);
429: } else if (loggerTagName.equalsIgnoreCase("Jaxrpc")) {
430: mConfig.setJAXRPC(aLevel);
431: } else if (loggerTagName.equalsIgnoreCase("Jms")) {
432: mConfig.setJMS(aLevel);
433: } else if (loggerTagName.equalsIgnoreCase("Jta")) {
434: mConfig.setJTA(aLevel);
435: } else if (loggerTagName.equalsIgnoreCase("Jts")) {
436: mConfig.setJTS(aLevel);
437: } else if (loggerTagName.equalsIgnoreCase("MDB")) {
438: mConfig.setMDBContainer(aLevel);
439: } else if (loggerTagName.equalsIgnoreCase("Naming")) {
440: mConfig.setNaming(aLevel);
441: } else if (loggerTagName.equalsIgnoreCase("EJB")) {
442: mConfig.setEJBContainer(aLevel);
443: } else if (loggerTagName.equalsIgnoreCase("Root")) {
444: mConfig.setRoot(aLevel);
445: } else if (loggerTagName.equalsIgnoreCase("Saaj")) {
446: mConfig.setSAAJ(aLevel);
447: } else if (loggerTagName.equalsIgnoreCase("Security")) {
448: mConfig.setSecurity(aLevel);
449: } else if (loggerTagName.equalsIgnoreCase("Server")) {
450: mConfig.setServer(aLevel);
451: } else if (loggerTagName.equalsIgnoreCase("Util")) {
452: mConfig.setUtil(aLevel);
453: } else if (loggerTagName.equalsIgnoreCase("Verifier")) {
454: mConfig.setVerifier(aLevel);
455: } else if (loggerTagName.equalsIgnoreCase("WEB")) {
456: mConfig.setWebContainer(aLevel);
457: } else if (loggerTagName.equalsIgnoreCase("Jbi")) {
458: if (mConfig.existsProperty(JBI_MODULE_PROPERTY))
459: mConfig.setPropertyValue(JBI_MODULE_PROPERTY, aLevel);
460: else
461: mConfig.createProperty(JBI_MODULE_PROPERTY, aLevel);
462: } else if (loggerTagName.equalsIgnoreCase("Jaxws")) {
463: if (mConfig.existsProperty(JAXWS_MODULE_PROPERTY))
464: mConfig.setPropertyValue(JAXWS_MODULE_PROPERTY, aLevel);
465: else
466: mConfig.createProperty(JAXWS_MODULE_PROPERTY, aLevel);
467: } else if (loggerTagName.equalsIgnoreCase("NodeAgent")) {
468: mConfig.setNodeAgent(aLevel);
469: } else if (loggerTagName.equalsIgnoreCase("Synchronization")) {
470: mConfig.setSynchronization(aLevel);
471: } else if (loggerTagName.equalsIgnoreCase("Gms")) {
472: mConfig.setGroupManagementService(aLevel);
473: }
474: }
475:
476: /**
477: * <p> Retrieve the JBI Runtime System log levels.</p>
478: *
479: * @param target The target name.
480: * @param instanceName The instance name.
481: */
482: public static TreeMap getJBIRuntimeLoggerLevels(String targetName,
483: String instanceName) {
484: Map result = null;
485: TreeMap treeMap = null;
486: try {
487: JBIAdminCommands mJac = BeanUtilities.getClient();
488: if (null != mJac) {
489: sLog
490: .fine("SystemLoggerUtilities - getJBIRuntimeLoggerLevels: "
491: + ", targetName="
492: + targetName
493: + ", instanceName=" + instanceName);
494: result = mJac.getRuntimeLoggerLevels(targetName,
495: instanceName);
496: treeMap = new TreeMap(result);
497: }
498: } catch (com.sun.jbi.ui.common.JBIRemoteException jbiRemoteEx) {
499: sLog.fine("SystemLoggerUtilities(): caught jbiRemoteEx="
500: + jbiRemoteEx);
501: }
502: return treeMap;
503: }
504:
505: /**
506: * <p> Retrieve the JBI Runtime System log display name.</p>
507: *
508: * @param loggerName The logger name.
509: * @param target The target name.
510: * @param instanceName The instance name.
511: */
512: public static String getRuntimeDisplayName(String loggerName,
513: String targetName, String instanceName) {
514: String displayName = "";
515: try {
516: JBIAdminCommands mJac = BeanUtilities.getClient();
517: if (null != mJac) {
518: sLog
519: .fine("SystemLoggerUtilities - getRuntimeDisplayName: "
520: + ", loggerName="
521: + loggerName
522: + ", targetName="
523: + targetName
524: + ", instanceName=" + instanceName);
525: displayName = mJac.getRuntimeLoggerDisplayName(
526: loggerName, targetName, instanceName);
527: }
528: } catch (com.sun.jbi.ui.common.JBIRemoteException jbiRemoteEx) {
529: sLog.fine("SystemLoggerUtilities(): caught jbiRemoteEx="
530: + jbiRemoteEx);
531: }
532: return displayName;
533: }
534:
535: /**
536: * <p> Set the JBI Runtime System log level.</p>
537: *
538: * @param loggerName The logger name.
539: * @param logLevel The new log level value.
540: * @param instanceName The instance name.
541: * @param target The target name.
542: */
543: public static void setJBIRuntimeLoggerLevel(String loggerName,
544: String logLevelValueName, String instanceName,
545: String targetName) {
546: try {
547: JBIAdminCommands mJac = BeanUtilities.getClient();
548: if (null != mJac) {
549: Level logLevel = null;
550: if (!(logLevelValueName
551: .equalsIgnoreCase(GuiUtil
552: .getMessage(I18nUtilities
553: .getResourceString("loglevel.DEFAULT"))))) {
554: logLevel = Level.parse(logLevelValueName);
555: }
556: sLog
557: .fine("SystemLoggerUtilities - setJBIRuntimeLoggerLevel: "
558: + ", loggerName="
559: + loggerName
560: + ", logLevelValueName="
561: + logLevelValueName
562: + ", instanceName="
563: + instanceName
564: + ", targetName="
565: + targetName);
566: mJac.setRuntimeLoggerLevel(loggerName, logLevel,
567: targetName, instanceName);
568: }
569: } catch (com.sun.jbi.ui.common.JBIRemoteException jbiRemoteEx) {
570: sLog
571: .fine("SystemLoggerUtilities() - setJBIRuntimeLoggerLevel: caught jbiRemoteEx="
572: + jbiRemoteEx);
573: }
574: }
575:
576: }
|