KickJava   Java API By Example, From Geeks To Geeks.

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


1
2 /*
3  * The contents of this file are subject to the terms
4  * of the Common Development and Distribution License
5  * (the License). You may not use this file except in
6  * compliance with the License.
7  *
8  * You can obtain a copy of the license at
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
10  * glassfish/bootstrap/legal/CDDLv1.0.txt.
11  * See the License for the specific language governing
12  * permissions and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL
15  * Header Notice in each file and include the License file
16  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
17  * If applicable, add the following below the CDDL Header,
18  * with the fields enclosed by brackets [] replaced by
19  * you own identifying information:
20  * "Portions Copyrighted [year] [name of copyright owner]"
21  *
22  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23  */

24 package com.sun.enterprise.server.logging;
25
26
27
28 import java.io.IOException JavaDoc;
29 import java.io.File JavaDoc;
30
31 import java.util.Enumeration JavaDoc;
32 import java.util.logging.Logger JavaDoc;
33 import java.util.logging.LogManager JavaDoc;
34 import java.util.logging.Level JavaDoc;
35 import java.util.logging.Handler JavaDoc;
36 import java.util.logging.FileHandler JavaDoc;
37 import java.util.logging.SimpleFormatter JavaDoc;
38
39 import com.sun.enterprise.config.ConfigContext;
40 import com.sun.enterprise.config.ConfigException;
41 import com.sun.enterprise.config.ConfigFactory;
42 import com.sun.enterprise.config.clientbeans.ClientContainer;
43 import com.sun.enterprise.config.clientbeans.ClientBeansFactory;
44
45 import com.sun.logging.LogDomains;
46
47 /**
48  * Class ACCLogManager is intended for use in the Application Client Container.
49  * The sun-acc.xml file holds ACC configuration as well as logging
50  * configuration including log file name and log level. The FileHandler is
51  * installed as the only logging handler.
52  */

53 public class ACCLogManager extends BaseLogManager {
54
55     // property name for configuring name and location of ACC
56
// configuration file.
57
private final String JavaDoc CLIENT_XML_FULL_NAME =
58         "com.sun.enterprise.appclient.ClientContainer";
59     private final String JavaDoc DEFAULT_CLIENT_CONTAINER_XML = "sun-acc.xml";
60
61     private Handler JavaDoc _clientHandler = null;
62     private Level JavaDoc _logLevel = Level.INFO;
63
64     private boolean initialized = false;
65
66     public ACCLogManager() {
67         super();
68     }
69
70     // This method is called when appclient container
71
// figures out the location of sun-acc.xml
72
synchronized public void init(String JavaDoc configFile) {
73
74         if (initialized) return;
75         initialized = true;
76
77         try {
78             ConfigContext ctx =
79                 ConfigFactory.createConfigContext
80                 (configFile, true, false, false,
81                  com.sun.enterprise.config.clientbeans.ClientContainer.class,
82          new com.sun.enterprise.config.clientbeans.ClientBeansResolver());
83             final ClientContainer cc =
84                 ClientBeansFactory.getClientBean(ctx);
85
86             String JavaDoc logLevel = cc.getLogService().getLevel();
87             if (logLevel != null && !logLevel.equals("")) {
88                 _logLevel = Level.parse(logLevel);
89             }
90             
91             String JavaDoc logFileName = cc.getLogService().getFile();
92             if (logFileName != null && !logFileName.equals("")) {
93                 _clientHandler = new FileHandler JavaDoc(logFileName, true);
94                 _clientHandler.setFormatter(new SimpleFormatter JavaDoc());
95                 
96                 // workaround to delete lockfile upon exit
97
File JavaDoc lockFile = new File JavaDoc(logFileName + ".lck");
98                 lockFile.deleteOnExit();
99             }
100             
101         } catch (Exception JavaDoc ex) {
102             if (_logger != null) {
103                 _logger.logrb(Level.SEVERE, null, null,
104                               "com.sun.logging.enterprise.system.container.appclient.LogStrings",
105                               "acc.cannot_create_log_handler", ex);
106             }
107         }
108
109         // Previously registered loggers need to be
110
// modified to use the right handler and set the
111
// the right level.
112
Enumeration JavaDoc e = getLoggerNames();
113         while (e.hasMoreElements()) {
114             String JavaDoc loggerName = (String JavaDoc) e.nextElement();
115             Logger JavaDoc l = getLogger(loggerName);
116             initializeLogger(l);
117         }
118     }
119
120
121     synchronized protected void initializeLogger(final Logger JavaDoc l) {
122         java.security.AccessController.doPrivileged(
123             new java.security.PrivilegedAction JavaDoc() {
124                 public Object JavaDoc run() {
125
126                     l.setLevel(_logLevel);
127
128                     // only if using file handler
129
if (_clientHandler != null) {
130                         // Explicitely remove all handlers.
131
Handler JavaDoc[] h = l.getHandlers();
132                         for (int i = 0; i < h.length; i++) {
133                             l.removeHandler(h[i]);
134                         }
135                         
136                         l.setUseParentHandlers(false);
137                         
138                         // Install handler and formatter. All other handlers are removed
139
// intentionally. In theory setUseParentHandlers(false) should do
140
// the same thing, but there are subtle differences (again related
141
// to static init) that was causing duplicate output in the log.
142
l.addHandler(_clientHandler);
143                     }
144                     return null;
145                 }
146             }
147         );
148     }
149 }
150
Popular Tags