1 5 6 7 package org.enhydra.logging; 8 9 import java.io.FileInputStream ; 10 import java.util.Enumeration ; 11 import java.util.Iterator ; 12 import java.util.Locale ; 13 import java.util.Properties ; 14 import java.util.ResourceBundle ; 15 import java.util.Set ; 16 17 import javax.management.MBeanServerConnection ; 18 import javax.management.ObjectName ; 19 import javax.management.remote.JMXConnector ; 20 import javax.management.remote.JMXConnectorFactory ; 21 import javax.management.remote.JMXServiceURL ; 22 23 import org.apache.commons.logging.Log; 24 import org.objectweb.util.monolog.Monolog; 25 import org.objectweb.util.monolog.api.BasicLevel; 26 import org.objectweb.util.monolog.api.Logger; 27 import org.objectweb.util.monolog.api.LoggerFactory; 28 import org.objectweb.util.monolog.wrapper.common.Configurable; 29 30 import com.lutris.util.ConfigException; 31 32 33 34 35 44 public final class CommonLoggingMonologLogger implements Log { 45 46 47 49 50 private static final String FQCN = CommonLoggingMonologLogger.class.getName(); 51 52 53 private org.objectweb.util.monolog.api.LoggerFactory lf; 54 private Logger logger = null; 55 private static ResourceBundle rb = null; 56 public static String PROPERTY_FILE; 57 static ObjectName objectName; 58 59 60 62 public CommonLoggingMonologLogger() throws ConfigException { 63 try { 64 65 configure(); 66 this.logger = lf.getLogger("MonologFactory"); 67 68 }catch (ConfigException ex) { 69 throw new ConfigException("Cannot configure logger. Logger not initialized."); 70 } 71 this.logger = lf.getLogger("CommonLoggingMonologAdapter"); 72 } 73 74 75 78 public CommonLoggingMonologLogger(String name) throws ConfigException { 79 try { 80 configure(); 82 this.logger = lf.getLogger("MonologFactory"); 83 }catch (ConfigException ex) { 84 throw new ConfigException("Cannot configure logger. Logger not initialized."); 85 } 86 this.logger = lf.getLogger("CommonLoggingMonologAdapter"); 87 } 88 89 91 public CommonLoggingMonologLogger(Logger logger ) { 92 this.logger=logger; 93 } 94 95 96 97 98 100 101 105 public void trace(Object message) { 106 logger.log(BasicLevel.LEVEL_DEBUG, message); 107 } 108 109 110 114 public void trace(Object message, Throwable t) { 115 logger.log(BasicLevel.LEVEL_DEBUG, message, t ); 116 } 117 118 119 122 public void debug(Object message) { 123 logger.log(BasicLevel.LEVEL_DEBUG, message); 124 } 125 126 129 public void debug(Object message, Throwable t) { 130 logger.log(BasicLevel.LEVEL_DEBUG,message, t ); 131 } 132 133 134 137 public void info(Object message) { 138 logger.log(BasicLevel.LEVEL_INFO, message); 139 } 140 141 142 145 public void info(Object message, Throwable t) { 146 logger.log(BasicLevel.LEVEL_INFO, message, t ); 147 } 148 149 150 153 public void warn(Object message) { 154 logger.log(BasicLevel.LEVEL_WARN, message ); 155 } 156 157 158 161 public void warn(Object message, Throwable t) { 162 logger.log(BasicLevel.LEVEL_WARN, message, t ); 163 } 164 165 166 169 public void error(Object message) { 170 logger.log(BasicLevel.LEVEL_ERROR, message ); 171 } 172 173 174 177 public void error(Object message, Throwable t) { 178 logger.log(BasicLevel.LEVEL_ERROR, message, t ); 179 } 180 181 182 185 public void fatal(Object message) { 186 logger.log(BasicLevel.LEVEL_FATAL, message ); 187 } 188 189 190 193 public void fatal(Object message, Throwable t) { 194 logger.log(BasicLevel.LEVEL_FATAL, message, t ); 195 } 196 197 198 201 public Logger getLogger() { 202 203 return (this.logger); 204 } 205 206 207 210 public boolean isDebugEnabled() { 211 return logger.isLoggable(BasicLevel.LEVEL_DEBUG); 212 } 213 214 215 218 public boolean isErrorEnabled() { 219 return logger.isLoggable(BasicLevel.LEVEL_ERROR); 220 } 221 222 223 226 public boolean isFatalEnabled() { 227 return logger.isLoggable(BasicLevel.LEVEL_FATAL); 228 } 229 230 231 234 public boolean isInfoEnabled() { 235 return logger.isLoggable(BasicLevel.LEVEL_INFO); 236 } 237 238 239 243 public boolean isTraceEnabled() { 244 return logger.isLoggable(BasicLevel.LEVEL_DEBUG); 245 } 246 247 250 public boolean isWarnEnabled() { 251 return logger.isLoggable(BasicLevel.LEVEL_WARN); 252 } 253 254 255 267 public synchronized void configure(String monologConfFile) { 268 269 if (monologConfFile==null || monologConfFile.length() == 0) { 270 } 273 String b = null; 275 Properties p = new Properties (); 276 try { 277 278 p.load(new FileInputStream (monologConfFile)); 279 b = p.getProperty("log.config.classname", null); 281 if (b == null) { 282 } 285 286 lf = (LoggerFactory) Class.forName(b).newInstance(); 288 p.put(Configurable.LOG_CONFIGURATION_TYPE, Configurable.PROPERTY); 290 p.put(Configurable.LOG_CONFIGURATION_FILE, monologConfFile); 291 ((Configurable) lf).configure(p); 293 294 } 296 catch (Exception e) { 297 } 300 301 } 302 303 307 public synchronized void configure() 308 throws ConfigException { 309 String b = null; 310 String propkey; 311 String propvalue; 312 Properties props = new Properties (); 313 314 try{ 315 findObjectName(); 317 System.out.println ("***PROSAO findObjectName"); 318 if (objectName != null) 319 PROPERTY_FILE = objectName.getKeyProperty("fname"); 320 rb = ResourceBundle.getBundle(PROPERTY_FILE,new Locale ("en","US"), ClassLoader.getSystemClassLoader()); 322 323 Enumeration enumeration = rb.getKeys(); 324 325 while (enumeration.hasMoreElements()) 326 { 327 328 propkey = (String ) enumeration.nextElement(); 329 propvalue = rb.getString(propkey); 330 props.setProperty(propkey, propvalue); 331 } 332 333 } catch (Exception e){ 334 throw new ConfigException("Logger configuration file could not be found: logger could not be initialized!"); 335 } 336 337 try { 338 339 b = props.getProperty("log.config.classname", null); 341 if (b == null) { 342 throw new ConfigException("Malformed configuration log file:" 343 + " log.config.classname not available"); 344 } 345 346 348 props.put(Configurable.LOG_CONFIGURATION_TYPE, Configurable.PROPERTY); 349 lf = Monolog.getMonologFactory(props); 350 this.logger = lf.getLogger("MonologFileLogger"); 351 352 } 353 catch (Exception e) { 354 throw new ConfigException("Malformed configuration log file:" 355 + " log.config.classname not available"); 356 } 357 358 } 359 360 361 364 private void findObjectName () throws Exception { 365 JMXConnector connector = null; 366 try { 367 368 String serviceName = "jonas"; 369 371 serviceName = System.getProperty("jonas.name"); 373 if ( serviceName == null) { 374 serviceName = "jonas"; 375 } 376 377 System.out.println ("findObjectName servicename: " + serviceName); 378 379 JMXServiceURL url = new JMXServiceURL ("service:jmx:rmi://localhost/jndi/jrmpconnector_" + serviceName); 381 382 connector = JMXConnectorFactory.connect(url); 384 385 MBeanServerConnection connection = connector.getMBeanServerConnection(); 388 389 objectName = new ObjectName (serviceName + ":type=service,name=log,*"); 390 391 Set mBeans = connection.queryNames(objectName, null); 392 int l = mBeans.size(); 393 if ((l) > 1) { 394 throw new Exception ("MBean set size not equal 1: " + l); 395 }else if (l == 0){ 396 objectName = null; 397 if (connector!=null){ 398 try { 399 connector.close(); 400 } catch (Exception exept){} 401 } 402 return; 403 } 404 Iterator i = mBeans.iterator(); 405 objectName = (ObjectName )i.next(); 406 } catch (Exception e) { 407 throw new Exception (e); 408 }finally { 409 if (connector!=null){ 410 try { 411 connector.close(); 412 } catch (Exception exept){} 413 } 414 } 415 return; 416 } 417 418 419 } 420 | Popular Tags |