KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > logging > MonologSink


1 // ========================================================================
2
//
3
// $Id: MonologSink.java,v 1.7 2005/03/24 12:22:00 slobodan Exp $
4
// ========================================================================
5

6 package org.enhydra.logging;
7
8 import java.util.Enumeration JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.Locale JavaDoc;
11 import java.util.Properties JavaDoc;
12 import java.util.ResourceBundle JavaDoc;
13 import java.util.Set JavaDoc;
14
15 import javax.management.MBeanServer JavaDoc;
16 import javax.management.MBeanServerFactory JavaDoc;
17 import javax.management.ObjectName JavaDoc;
18
19 import org.mortbay.log.Frame;
20 import org.mortbay.log.LogImpl;
21 import org.mortbay.log.LogSink;
22 import org.objectweb.util.monolog.Monolog;
23 import org.objectweb.util.monolog.api.BasicLevel;
24 import org.objectweb.util.monolog.api.Level;
25 import org.objectweb.util.monolog.api.Logger;
26 import org.objectweb.util.monolog.wrapper.common.Configurable;
27
28 import com.lutris.util.ConfigException;
29
30
31 public class MonologSink implements LogSink
32 {
33     private String JavaDoc _options;
34     private transient boolean _started;
35     private org.objectweb.util.monolog.api.LoggerFactory lf;
36     private Logger logger = null;
37     private String JavaDoc path, jonas, fileName;
38     private static ResourceBundle JavaDoc rb = null;
39     public static String JavaDoc PROPERTY_FILE;
40     static ObjectName JavaDoc objectName;
41     
42     public MonologSink() throws ConfigException {
43       this("MonologSink");
44       }
45     
46     public MonologSink (String JavaDoc name) throws ConfigException {
47       
48       try {
49               configure();
50             }catch (ConfigException ex) {
51                 throw new ConfigException("Cannot configure logger. Logger not initialized.");
52             }
53      
54     }
55
56     /** For use with a Monolog factory
57      */

58     public MonologSink(Logger logger ) {
59         this.logger=logger;
60     }
61     
62     /* ------------------------------------------------------------ */
63     public void setOptions(String JavaDoc filename)
64     {
65         _options=filename;
66     }
67     
68     /* ------------------------------------------------------------ */
69     public String JavaDoc getOptions()
70     {
71         return _options;
72     }
73     
74     /* ------------------------------------------------------------ */
75     public void setLogImpl(LogImpl li){}
76     
77     /* ------------------------------------------------------------ */
78     public void start()
79         throws Exception JavaDoc
80     {
81         _started=true;
82     }
83     
84     /* ------------------------------------------------------------ */
85     public void stop()
86     {
87         _started=false;
88     }
89
90     /* ------------------------------------------------------------ */
91     public boolean isStarted()
92     {
93         return _started;
94     }
95     
96     /* ------------------------------------------------------------ */
97     public void log(String JavaDoc tag,
98                      Object JavaDoc msg,
99                      Frame frame,
100                      long time)
101     {
102         String JavaDoc method=frame.getMethod();
103         int lb=method.indexOf('(');
104         int ld = (lb>0)
105             ?method.lastIndexOf('.',lb)
106             :method.lastIndexOf('.');
107         if (ld<0) ld=lb;
108         
109         String JavaDoc class_name = (ld>0)?method.substring(0,ld):method;
110         // Finding a Monolog properties file or system property
111

112
113         Level priority=BasicLevel.LEVEL_INFO;
114
115         if (LogImpl.DEBUG.equals(tag))
116             priority=BasicLevel.LEVEL_DEBUG;
117         else if (LogImpl.WARN.equals(tag) || LogImpl.ERROR.equals(tag))
118             priority=BasicLevel.LEVEL_ERROR;
119         else if (LogImpl.FAIL.equals(tag))
120             priority=BasicLevel.LEVEL_FATAL;
121         
122         if (!logger.isLoggable(priority))
123             return;
124
125         logger.log(priority,
126                 "org.enhydra.logging.MonologSink",
127                 ""+msg,
128                 null);
129     }
130
131     /* ------------------------------------------------------------ */
132     public synchronized void log(String JavaDoc s)
133     {
134         logger.log(BasicLevel.LEVEL_INFO,
135                         "org.enhydra.logging.MonologSink",
136                                    s,
137                                    null);
138     }
139     
140     public synchronized void configure()
141         throws ConfigException {
142           String JavaDoc b = null;
143           String JavaDoc propkey;
144           String JavaDoc propvalue;
145           Properties JavaDoc props = new Properties JavaDoc();
146              
147      try{
148          findMBeanServer();
149          findObjectName();
150          if (objectName != null)
151             PROPERTY_FILE = objectName.getKeyProperty("fname");
152          // Search the classpath for the logger configuration file
153
rb = ResourceBundle.getBundle(PROPERTY_FILE,new Locale JavaDoc("en","US"), ClassLoader.getSystemClassLoader());
154       
155      Enumeration JavaDoc enumeration = rb.getKeys();
156      
157      while (enumeration.hasMoreElements())
158      {
159
160          propkey = (String JavaDoc) enumeration.nextElement();
161          propvalue = rb.getString(propkey);
162          props.setProperty(propkey, propvalue);
163      }
164          
165       } catch (Exception JavaDoc e){
166         throw new ConfigException("Logger configuration file could not be found: logger could not be initialized!");
167     }
168
169         try {
170
171         // Search the class name of the LoggerFactory which must be instanciated
172
b = props.getProperty("log.config.classname", null);
173            if (b == null) {
174                throw new ConfigException("Malformed configuration log file:"
175                    + " log.config.classname not available");
176            }
177
178            // Instanciate the LoggerFactory
179

180          props.put(Configurable.LOG_CONFIGURATION_TYPE, Configurable.PROPERTY);
181          lf = Monolog.getMonologFactory(b);
182          this.logger = lf.getLogger("MonologSink");
183
184           }
185         catch (Exception JavaDoc e) {
186            throw new ConfigException("Malformed configuration log file:"
187                    + " log.config.classname not available");
188        }
189     
190 }
191
192   private MBeanServer JavaDoc findMBeanServer () throws Exception JavaDoc {
193     MBeanServer JavaDoc mBeanServer;
194     try {
195       java.util.ArrayList JavaDoc server = MBeanServerFactory.findMBeanServer(null);
196       if (server == null) {
197         throw new Exception JavaDoc("MBeansServer not found");
198       }
199       else {
200         mBeanServer = (MBeanServer JavaDoc)server.get(0);
201         //System.err.println("domain:" + mBeanServer.getDefaultDomain());
202
}
203     } catch (Exception JavaDoc e) {
204       throw new com.lutris.appserver.server.session.SessionException(e);
205     }
206     return mBeanServer;
207   }
208
209   /**
210    * finds the ObjectName which correspondes to the MBean of the jonas logger
211    */

212   private void findObjectName () throws Exception JavaDoc {
213     try {
214       objectName = new ObjectName JavaDoc("jonas:type=service,name=log,*");
215       MBeanServer JavaDoc mBeanServer = findMBeanServer();
216       Set JavaDoc mBeans = mBeanServer.queryNames(objectName, null);
217       int l;
218       if ((l = mBeans.size()) > 1) {
219         throw new Exception JavaDoc("MBean set size not equal 1: " + l);
220       }else if (l == 0){
221         objectName = null;
222         return;
223         }
224       Iterator JavaDoc i = mBeans.iterator();
225       objectName = (ObjectName JavaDoc)i.next();
226     } catch (Exception JavaDoc e) {
227       throw new Exception JavaDoc(e);
228     }
229     return;
230   }
231
232 }
Popular Tags