KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > server > logging > ModuleToLoggerNameMapper


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 package com.sun.enterprise.server.logging;
25
26 import java.util.logging.ErrorManager JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import com.sun.logging.LogDomains;
30 import com.sun.enterprise.server.ServerContext;
31 import com.sun.enterprise.server.ApplicationServer;
32 import com.sun.enterprise.config.serverbeans.ServerBeansFactory;
33 import com.sun.enterprise.config.serverbeans.Server;
34 import com.sun.enterprise.config.serverbeans.Config;
35 import com.sun.enterprise.config.serverbeans.ElementProperty;
36 import com.sun.enterprise.config.serverbeans.ModuleLogLevels;
37 import com.sun.enterprise.config.serverbeans.LogService;
38
39 /**
40  * A Simple No Brainer Utility to map the Module Name to Logger Name..
41  *
42  * @author Hemanth Puttaswamy
43  */

44 public class ModuleToLoggerNameMapper {
45     
46     /*the sequence of each module entry in this table is important. always place
47       module with longer loggername in the first.
48       This table makes it possible to support more than 1 loggernames for each
49       module. Each module log level's change through admin will result in level
50       change in all its logger objects.
51      
52       The log module part is consistent with what module is defined in
53       LogService.java (package com.sun.enterprise.config.serverbeans)
54       refer also: ModuleLogLevels.java & ServerTags.java
55
56       In sun-domain.dtd:
57       <!ATTLIST module-log-levels
58          root %log-level; "INFO"
59          server %log-level; "INFO"
60          ejb-container %log-level; "INFO"
61          cmp-container %log-level; "INFO"
62          mdb-container %log-level; "INFO"
63          web-container %log-level; "INFO"
64          classloader %log-level; "INFO"
65          configuration %log-level; "INFO"
66          naming %log-level; "INFO"
67          security %log-level; "INFO"
68          jts %log-level; "INFO"
69          jta %log-level; "INFO"
70          admin %log-level; "INFO"
71          deployment %log-level; "INFO"
72          verifier %log-level; "INFO"
73          jaxr %log-level; "INFO"
74          jaxrpc %log-level; "INFO"
75          saaj %log-level; "INFO"
76          corba %log-level; "INFO"
77          javamail %log-level; "INFO"
78          jms %log-level; "INFO"
79          connector %log-level; "INFO"
80          jdo %log-level; "INFO"
81          cmp %log-level; "INFO"
82          util %log-level; "INFO"
83          resource-adapter %log-level; "INFO"
84          synchronization %log-level; "INFO"
85          node-agent %log-level; "INFO"
86          self-management %log-level; "INFO"
87          group-management-service %log-level; "INFO"
88          management-event %log-level; "INFO">
89     */

90     private static Object JavaDoc[][] ModuleAndLoggerTable = {
91         {"admin", new String JavaDoc[] { LogDomains.ADMIN_LOGGER } }, //admin
92
{"classloader", new String JavaDoc[] { LogDomains.LOADER_LOGGER} }, //classloader
93
{"cmp", new String JavaDoc[] { LogDomains.CMP_LOGGER} },
94         {"cmp-container", new String JavaDoc[] { LogDomains.CMP_LOGGER} }, //todo: verify with "cmp"
95
{"configuration", new String JavaDoc[] { LogDomains.CONFIG_LOGGER} },
96         {"connector", new String JavaDoc[] { LogDomains.RSR_LOGGER} },
97         {"resource-adapter", new String JavaDoc[] { LogDomains.RSR_LOGGER} },//todo: verify with "connector"
98
{"corba", new String JavaDoc[] { LogDomains.CORBA_LOGGER} },
99         {"deployment", new String JavaDoc[] { LogDomains.DPL_LOGGER} },
100         {"ejb-container", new String JavaDoc[] { LogDomains.EJB_LOGGER} },
101         {"javamail", new String JavaDoc[] { LogDomains.JAVAMAIL_LOGGER} },
102         {"jaxr", new String JavaDoc[] { LogDomains.JAXR_LOGGER} },
103         {"jaxrpc", new String JavaDoc[] { LogDomains.JAXRPC_LOGGER} },
104         {"jdo", new String JavaDoc[] { LogDomains.JDO_LOGGER} },
105         {"jms", new String JavaDoc[] { LogDomains.JMS_LOGGER, "javax.resourceadapter.mqjmsra"} },
106         {"jta", new String JavaDoc[] { LogDomains.JTA_LOGGER} },
107         {"jts", new String JavaDoc[] { LogDomains.TRANSACTION_LOGGER} },
108         {"mdb-container", new String JavaDoc[] { LogDomains.MDB_LOGGER} },
109         //{"management-event" //todo: management-event module owner needs to impl this.
110
{"naming", new String JavaDoc[] { LogDomains.JNDI_LOGGER} },
111         {"saaj", new String JavaDoc[] { LogDomains.SAAJ_LOGGER} },
112         {"security", new String JavaDoc[] { LogDomains.SECURITY_LOGGER} },
113         {"self-management",new String JavaDoc[] { LogDomains.SELF_MANAGEMENT_LOGGER} },
114         {"synchronization",new String JavaDoc[] { "javax.ee.enterprise.system.tools.synchronization"} },
115         {"verifier", new String JavaDoc[] { LogDomains.APPVERIFY_LOGGER} },
116         {"web-container", new String JavaDoc[] { LogDomains.WEB_LOGGER,
117                                           "org.apache.catalina",
118                                           "org.apache.coyote","org.apache.jasper"
119                                         } },
120         {"group-management-service", new String JavaDoc[] { LogDomains.GMS_LOGGER} },
121         {"node-agent", new String JavaDoc[] { "javax.ee.enterprise.system.nodeagent" } },
122         {"util", new String JavaDoc[] { LogDomains.UTIL_LOGGER } },
123         {"core", new String JavaDoc[] { LogDomains.CORE_LOGGER} },
124         {"server", new String JavaDoc[] { LogDomains.SERVER_LOGGER} },
125         {"root", new String JavaDoc[] { LogDomains.ROOT_LOGGER} }
126     };
127     
128
129     /**
130      * @loggername the logname
131      * @return the module name the logger is for.
132      */

133     public static String JavaDoc getModuleName(String JavaDoc loggerName) {
134         for (int i=0; i<ModuleAndLoggerTable.length; i++) {
135             Object JavaDoc[] dim = ModuleAndLoggerTable[i];
136             String JavaDoc modName = (String JavaDoc)dim[0];
137             String JavaDoc[] loggerNames = (String JavaDoc[]) dim[1];
138             for (int j=0; loggerNames!=null && j<loggerNames.length;j++) {
139                 String JavaDoc name=loggerNames[j];
140                 if (loggerName.equals(name))
141                     return modName;
142             }
143         }
144         return null;
145     }
146
147
148     /**
149      * @moduleName the log module name (eg. "admin");
150                      if null, it means all modules.
151      * @return the logger names for this module; size of returned String[] >=1.
152      */

153     public static String JavaDoc[] getLoggerNames( String JavaDoc moduleName ) {
154         ArrayList JavaDoc result = new ArrayList JavaDoc();
155         for (int i=0; i<ModuleAndLoggerTable.length; i++) {
156             Object JavaDoc[] dim = ModuleAndLoggerTable[i];
157             String JavaDoc modName = (String JavaDoc)dim[0];
158             String JavaDoc[] loggerNames = (String JavaDoc[]) dim[1];
159             if (loggerNames!=null) {
160                 if (moduleName == null) { //we return all AS module loggers in this case
161
result.addAll(Arrays.asList(loggerNames) );
162                 } else if (moduleName.equals(modName)) {
163                     result.addAll( Arrays.asList(loggerNames) );
164                     break;
165                 }
166         }
167         }
168         String JavaDoc[] lNames = new String JavaDoc[ result.size()];
169         return (String JavaDoc[])result.toArray(lNames);
170     }
171
172
173    /**
174      * @loggername the logname (could be the name of a child logger of a module logger)
175      * @return the module logger name.
176      */

177     public static String JavaDoc getMatchedModuleLoggerName(String JavaDoc loggerName) {
178         for (int i=0; i<ModuleAndLoggerTable.length; i++) {
179             Object JavaDoc[] dim = ModuleAndLoggerTable[i];
180             String JavaDoc[] loggerNames = (String JavaDoc[]) dim[1];
181             for (int j=0; loggerNames!=null && j<loggerNames.length;j++) {
182                 String JavaDoc name=loggerNames[j];
183                 if (loggerName.startsWith(name) )
184                     return name;
185             }
186         }
187         
188         try {
189             ServerContext sc = ApplicationServer.getServerContext();
190             if (sc!=null) {
191                 Config cfg = ServerBeansFactory.getConfigBean(sc.getConfigContext());
192                 ModuleLogLevels allModulesLogLevels = cfg.getLogService().getModuleLogLevels( );
193                 ElementProperty[] elementProperties = allModulesLogLevels.getElementProperty( );
194                 if( elementProperties != null ) {
195                     for( int i = 0; i < elementProperties.length; i++ ) {
196                         String JavaDoc name = elementProperties[i].getName();
197                         if( loggerName.startsWith(name)) {
198                             return name;
199                         }
200                     }
201                 }
202             }
203         } catch ( Exception JavaDoc e ) {
204             new ErrorManager JavaDoc().error( "Error In Setting Initial Loglevel", e,
205                 ErrorManager.GENERIC_FAILURE );
206         }
207         return null;
208     }
209 }
210
Popular Tags