001: // ========================================================================
002: //
003: // $Id: MonologSink.java,v 1.3 2007-10-19 10:05:39 sinisa Exp $
004: // ========================================================================
005:
006: package org.enhydra.logging;
007:
008: import java.util.Enumeration;
009: import java.util.Iterator;
010: import java.util.Locale;
011: import java.util.Properties;
012: import java.util.ResourceBundle;
013: import java.util.Set;
014:
015: import javax.management.MBeanServer;
016: import javax.management.MBeanServerFactory;
017: import javax.management.ObjectName;
018:
019: import org.mortbay.log.Frame;
020: import org.mortbay.log.LogImpl;
021: import org.mortbay.log.LogSink;
022: import org.objectweb.util.monolog.Monolog;
023: import org.objectweb.util.monolog.api.BasicLevel;
024: import org.objectweb.util.monolog.api.Level;
025: import org.objectweb.util.monolog.api.Logger;
026: import org.objectweb.util.monolog.wrapper.common.Configurable;
027:
028: import com.lutris.util.ConfigException;
029:
030: public class MonologSink implements LogSink {
031: private String _options;
032: private transient boolean _started;
033: private org.objectweb.util.monolog.api.LoggerFactory lf;
034: private Logger logger = null;
035: // private String path, jonas, fileName;
036: private static ResourceBundle rb = null;
037: public static String PROPERTY_FILE;
038: static ObjectName objectName;
039:
040: public MonologSink() throws ConfigException {
041: this ("MonologSink");
042: }
043:
044: public MonologSink(String name) throws ConfigException {
045:
046: try {
047: configure();
048: } catch (ConfigException ex) {
049: throw new ConfigException(
050: "Cannot configure logger. Logger not initialized.");
051: }
052:
053: }
054:
055: /** For use with a Monolog factory
056: */
057: public MonologSink(Logger logger) {
058: this .logger = logger;
059: }
060:
061: /* ------------------------------------------------------------ */
062: public void setOptions(String filename) {
063: _options = filename;
064: }
065:
066: /* ------------------------------------------------------------ */
067: public String getOptions() {
068: return _options;
069: }
070:
071: /* ------------------------------------------------------------ */
072: public void setLogImpl(LogImpl li) {
073: }
074:
075: /* ------------------------------------------------------------ */
076: public void start() throws Exception {
077: _started = true;
078: }
079:
080: /* ------------------------------------------------------------ */
081: public void stop() {
082: _started = false;
083: }
084:
085: /* ------------------------------------------------------------ */
086: public boolean isStarted() {
087: return _started;
088: }
089:
090: /* ------------------------------------------------------------ */
091: public void log(String tag, Object msg, Frame frame, long time) {
092: String method = frame.getMethod();
093: int lb = method.indexOf('(');
094: int ld = (lb > 0) ? method.lastIndexOf('.', lb) : method
095: .lastIndexOf('.');
096: if (ld < 0)
097: ld = lb;
098:
099: String class_name = (ld > 0) ? method.substring(0, ld) : method;
100: // Finding a Monolog properties file or system property
101:
102: Level priority = BasicLevel.LEVEL_INFO;
103:
104: if (LogImpl.DEBUG.equals(tag))
105: priority = BasicLevel.LEVEL_DEBUG;
106: else if (LogImpl.WARN.equals(tag) || LogImpl.ERROR.equals(tag))
107: priority = BasicLevel.LEVEL_ERROR;
108: else if (LogImpl.FAIL.equals(tag))
109: priority = BasicLevel.LEVEL_FATAL;
110:
111: if (!logger.isLoggable(priority))
112: return;
113:
114: logger.log(priority, "org.enhydra.logging.MonologSink", ""
115: + msg, null);
116: }
117:
118: /* ------------------------------------------------------------ */
119: public synchronized void log(String s) {
120: logger.log(BasicLevel.LEVEL_INFO,
121: "org.enhydra.logging.MonologSink", s, null);
122: }
123:
124: public synchronized void configure() throws ConfigException {
125: String b = null;
126: String propkey;
127: String propvalue;
128: Properties props = new Properties();
129:
130: try {
131: findMBeanServer();
132: findObjectName();
133: if (objectName != null)
134: PROPERTY_FILE = objectName.getKeyProperty("fname");
135: // Search the classpath for the logger configuration file
136: rb = ResourceBundle.getBundle(PROPERTY_FILE, new Locale(
137: "en", "US"), ClassLoader.getSystemClassLoader());
138:
139: Enumeration enumeration = rb.getKeys();
140:
141: while (enumeration.hasMoreElements()) {
142:
143: propkey = (String) enumeration.nextElement();
144: propvalue = rb.getString(propkey);
145: props.setProperty(propkey, propvalue);
146: }
147:
148: } catch (Exception e) {
149: throw new ConfigException(
150: "Logger configuration file could not be found: logger could not be initialized!");
151: }
152:
153: try {
154:
155: // Search the class name of the LoggerFactory which must be instanciated
156: b = props.getProperty("log.config.classname", null);
157: if (b == null) {
158: throw new ConfigException(
159: "Malformed configuration log file:"
160: + " log.config.classname not available");
161: }
162:
163: // Instanciate the LoggerFactory
164:
165: props.put(Configurable.LOG_CONFIGURATION_TYPE,
166: Configurable.PROPERTY);
167: lf = Monolog.getMonologFactory(b);
168: this .logger = lf.getLogger("MonologSink");
169:
170: } catch (Exception e) {
171: throw new ConfigException(
172: "Malformed configuration log file:"
173: + " log.config.classname not available");
174: }
175:
176: }
177:
178: private MBeanServer findMBeanServer() throws Exception {
179: MBeanServer mBeanServer;
180: try {
181: java.util.ArrayList server = MBeanServerFactory
182: .findMBeanServer(null);
183: if (server == null) {
184: throw new Exception("MBeansServer not found");
185: } else {
186: mBeanServer = (MBeanServer) server.get(0);
187: //System.err.println("domain:" + mBeanServer.getDefaultDomain());
188: }
189: } catch (Exception e) {
190: throw new com.lutris.appserver.server.session.SessionException(
191: e);
192: }
193: return mBeanServer;
194: }
195:
196: /**
197: * finds the ObjectName which correspondes to the MBean of the jonas logger
198: */
199: private void findObjectName() throws Exception {
200: try {
201: objectName = new ObjectName("jonas:type=service,name=log,*");
202: MBeanServer mBeanServer = findMBeanServer();
203: Set mBeans = mBeanServer.queryNames(objectName, null);
204: int l;
205: if ((l = mBeans.size()) > 1) {
206: throw new Exception("MBean set size not equal 1: " + l);
207: } else if (l == 0) {
208: objectName = null;
209: return;
210: }
211: Iterator i = mBeans.iterator();
212: objectName = (ObjectName) i.next();
213: } catch (Exception e) {
214: throw new Exception(e);
215: }
216: return;
217: }
218:
219: }
|