1 6 7 package org.enhydra.logging; 8 9 import java.io.FileInputStream ; 10 import java.util.Enumeration ; 11 import java.util.Hashtable ; 12 import java.util.Iterator ; 13 import java.util.Locale ; 14 import java.util.Properties ; 15 import java.util.ResourceBundle ; 16 import java.util.Set ; 17 import java.util.Vector ; 18 19 import javax.management.MBeanServer ; 20 import javax.management.MBeanServerFactory ; 21 import javax.management.ObjectName ; 22 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogConfigurationException; 25 import org.apache.commons.logging.LogFactory; 26 import org.objectweb.util.monolog.Monolog; 27 import org.objectweb.util.monolog.api.Logger; 28 import org.objectweb.util.monolog.api.LoggerFactory; 29 import org.objectweb.util.monolog.wrapper.common.Configurable; 30 31 import com.lutris.util.ConfigException; 32 33 43 public final class MonologFactory extends LogFactory { 44 public org.objectweb.util.monolog.api.LoggerFactory lf; 45 public String traceproperties; 46 private Logger logger = null; 47 private String path, jonas, fileName; 48 private static ResourceBundle rb = null; 49 public static String PROPERTY_FILE; 50 static ObjectName objectName; 51 52 public MonologFactory() throws ConfigException{ 53 54 super(); 55 try { 56 configure(); 57 this.logger = lf.getLogger("MonologFactory"); 58 }catch (ConfigException ex) { 59 throw new ConfigException("Cannot configure logger. Logger not initialized."); 60 } 61 } 62 63 66 private Hashtable attributes = new Hashtable (); 67 68 private Hashtable instances = new Hashtable (); 70 71 73 79 public Object getAttribute(String name) { 80 return (attributes.get(name)); 81 } 82 83 88 public String [] getAttributeNames() { 89 Vector names = new Vector (); 90 Enumeration keys = attributes.keys(); 91 while (keys.hasMoreElements()) { 92 names.addElement((String ) keys.nextElement()); 93 } 94 String results[] = new String [names.size()]; 95 for (int i = 0; i < results.length; i++) { 96 results[i] = (String ) names.elementAt(i); 97 } 98 return (results); 99 } 100 101 102 111 public Log getInstance(Class clazz) 112 throws LogConfigurationException 113 114 { 115 Log instance = (Log) instances.get(clazz.getName()); 116 if( instance != null ) 117 return instance; 118 instance=new CommonLoggingMonologLogger(lf.getLogger(clazz.getName())); 119 instances.put( clazz.getName(), instance ); 120 return instance; 121 } 122 123 public Log getInstance(String name) 124 throws LogConfigurationException 125 { 126 Log instance = (Log) instances.get(name); 127 if( instance != null ) 128 return instance; 129 130 instance=new CommonLoggingMonologLogger( lf.getLogger( name )); 131 instances.put( name, instance ); 132 return instance; 133 } 134 135 136 143 public void release() { 144 145 instances.clear(); 146 147 } 149 150 151 157 public void removeAttribute(String name) { 158 attributes.remove(name); 159 } 160 161 162 171 public void setAttribute(String name, Object value) { 172 if (value == null) { 173 attributes.remove(name); 174 } else { 175 attributes.put(name, value); 176 } 177 } 178 179 191 public synchronized void configure(String monologConfFile) { 192 193 String b = null; 195 Properties p = new Properties (); 196 try { 197 p.load(new FileInputStream (monologConfFile)); 198 b = p.getProperty("log.config.classname", null); 200 lf = (LoggerFactory) Class.forName(b).newInstance(); 202 p.put(Configurable.LOG_CONFIGURATION_TYPE, Configurable.PROPERTY); 204 p.put(Configurable.LOG_CONFIGURATION_FILE, monologConfFile); 205 206 ((Configurable) lf).configure(p); 207 208 209 } 210 catch (Exception e) { 211 212 } 213 } 214 215 219 public synchronized void configure() 220 throws ConfigException { 221 String b = null; 222 String propkey; 223 String propvalue; 224 Properties props = new Properties (); 225 226 try{ 227 findMBeanServer(); 228 findObjectName(); 229 if (objectName != null) 230 PROPERTY_FILE = objectName.getKeyProperty("fname"); 231 rb = ResourceBundle.getBundle(PROPERTY_FILE,new Locale ("en","US"), ClassLoader.getSystemClassLoader()); 233 234 Enumeration enumeration = rb.getKeys(); 235 236 while (enumeration.hasMoreElements()) 237 { 238 239 propkey = (String ) enumeration.nextElement(); 240 propvalue = rb.getString(propkey); 241 props.setProperty(propkey, propvalue); 242 } 243 244 } catch (Exception e){ 245 throw new ConfigException("Logger configuration file could not be found: logger could not be initialized!"); 246 } 247 248 try { 249 250 b = props.getProperty("log.config.classname", null); 252 if (b == null) { 253 throw new ConfigException("Malformed configuration log file:" 254 + " log.config.classname not available"); 255 } 256 257 259 props.put(Configurable.LOG_CONFIGURATION_TYPE, Configurable.PROPERTY); 260 262 lf = Monolog.getMonologFactory(b); 263 264 265 } 266 catch (Exception e) { 267 throw new ConfigException("Malformed configuration log file:" 268 + " log.config.classname not available"); 269 } 270 271 } 272 273 274 private MBeanServer findMBeanServer () throws Exception { 275 MBeanServer mBeanServer; 276 try { 277 java.util.ArrayList server = MBeanServerFactory.findMBeanServer(null); 278 if (server == null) { 279 throw new Exception ("MBeansServer not found"); 280 } 281 else { 282 mBeanServer = (MBeanServer )server.get(0); 283 } 285 } catch (Exception e) { 286 throw new com.lutris.appserver.server.session.SessionException(e); 287 } 288 return mBeanServer; 289 } 290 291 294 private void findObjectName () throws Exception { 295 try { 296 objectName = new ObjectName ("jonas:type=service,name=log,*"); 297 MBeanServer mBeanServer = findMBeanServer(); 298 Set mBeans = mBeanServer.queryNames(objectName, null); 299 int l; 300 if ((l = mBeans.size()) > 1) { 301 throw new Exception ("MBean set size not equal 1: " + l); 302 }else if (l == 0){ 303 objectName = null; 304 return; 305 } 306 Iterator i = mBeans.iterator(); 307 objectName = (ObjectName )i.next(); 308 } catch (Exception e) { 309 throw new Exception (e); 310 } 311 return; 312 } 313 314 } | Popular Tags |