KickJava   Java API By Example, From Geeks To Geeks.

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


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.server.logging;
24
25 import java.util.logging.ConsoleHandler JavaDoc;
26 import java.util.logging.FileHandler JavaDoc;
27 import java.util.logging.Handler JavaDoc;
28
29 import com.sun.enterprise.config.ConfigContext;
30 import com.sun.enterprise.config.ConfigException;
31 import com.sun.enterprise.config.serverbeans.LogService;
32 import com.sun.enterprise.config.serverbeans.ServerBeansFactory;
33 import com.sun.enterprise.server.ApplicationServer;
34 import com.sun.enterprise.server.ServerContext;
35 import com.sun.enterprise.server.pluggable.LoggingSupport;
36 import com.sun.enterprise.util.StringUtils;
37
38 /**
39  * Support for logging features when used with http engine from
40  * tomcat.
41  */

42 public class TomcatLoggingSupport implements LoggingSupport {
43
44     private static String JavaDoc defaultLogFileName = null;
45     private static String JavaDoc logFileName = null;
46     private static FileHandler JavaDoc fHandler = null;
47
48     /**
49      * Create a log handler object.
50      */

51     public Handler JavaDoc createLogHandler() {
52         String JavaDoc fileName = getLogFileName();
53         Handler JavaDoc h = null;
54         try {
55         if (fHandler == null) {
56                 fHandler = new FileHandler JavaDoc(fileName, true);
57         }
58         } catch (Exception JavaDoc e) {
59             // If there is an exception in creation of file handler,
60
// use a console handler instead.
61
// NOI18N
62
System.err.println("Error creating log handler for " + fileName
63                     + ". Using ConsoleHandler -- " + e.getMessage());
64             e.printStackTrace();
65             h = new ConsoleHandler JavaDoc();
66         return h;
67         }
68
69     h = fHandler;
70         return h;
71     }
72
73     /**
74      * Get log file name. If a log file name is defined in configuration
75      * then the method returns that value, otherwise it returns default
76      * log file name.
77      */

78     private static String JavaDoc getLogFileName() {
79         if (logFileName == null) {
80             ServerContext sc = ApplicationServer.getServerContext();
81             if (sc != null) {
82                 ConfigContext ctx = sc.getConfigContext();
83                 LogService ls = null;
84                 try {
85                     ls = ServerBeansFactory.getConfigBean(ctx).getLogService();
86                 } catch (ConfigException ce) {
87                     // Ignore this exception, intent is to use default file
88
// NOI18N
89
System.err.println("Error accessing log service config -- "
90                             + ce.getMessage() + "\nUsing default file");
91                     ce.printStackTrace();
92                 }
93                 if (ls != null) {
94                     logFileName = ls.getFile();
95                 }
96             }
97         }
98         if (logFileName != null) {
99             return logFileName;
100         } else {
101             return getDefaultLogFileName();
102         }
103     }
104
105     /**
106      * Get default log file name.
107      */

108     private static String JavaDoc getDefaultLogFileName() {
109         if (defaultLogFileName == null) {
110             ServerContext sc = ApplicationServer.getServerContext();
111             String JavaDoc instDir;
112             if (sc != null) {
113                 instDir = sc.getInstanceEnvironment().getInstancesRoot();
114             } else {
115                 // FIX: Use some other system property before user.dir
116
instDir = System.getProperty("user.dir");
117             }
118             String JavaDoc[] names = {instDir, LOGS_DIR, LOG_FILE};
119             defaultLogFileName = StringUtils.makeFilePath(names, false);
120         }
121         return defaultLogFileName;
122     }
123
124     private static final String JavaDoc LOGS_DIR = "logs";
125     private static final String JavaDoc LOG_FILE = "server.log";
126
127 }
128
Popular Tags