1 23 24 package org.objectweb.jorm.metainfo.lib; 25 26 import org.objectweb.jorm.metainfo.api.MetaObject; 27 import org.objectweb.jorm.metainfo.api.Manager; 28 import org.objectweb.jorm.util.api.Loggable; 29 import org.objectweb.util.monolog.api.Logger; 30 import org.objectweb.util.monolog.api.LoggerFactory; 31 import org.objectweb.util.monolog.api.BasicLevel; 32 33 import java.util.Collection ; 34 import java.util.Iterator ; 35 36 39 public class BasicMetaObject implements MetaObject, Loggable { 40 41 44 protected MetaObject parent; 45 46 transient protected Logger logger = null; 47 transient protected LoggerFactory loggerFactory = null; 48 transient protected boolean debug = false; 49 50 55 public BasicMetaObject(MetaObject parent) { 56 this.parent = parent; 57 if (parent instanceof Loggable) { 58 loggerFactory = ((Loggable) parent).getLoggerFactory(); 59 if (loggerFactory == null) { 60 logger = ((Loggable) parent).getLogger(); 61 } else { 62 logger = loggerFactory.getLogger(getClass().getName()); 63 } 64 } 65 debug = logger != null && logger.isLoggable(BasicLevel.DEBUG); 66 } 67 68 protected Collection getChildren() { 69 return null; 70 } 71 72 public void setLoggingOnChild(MetaObject mo) { 73 if (mo instanceof Loggable) { 74 if (loggerFactory != null) { 75 ((Loggable) mo).setLoggerFactory(loggerFactory); 76 } else if (logger != null) { 77 ((Loggable) mo).setLogger(logger); 78 } 79 } 80 } 81 82 public Logger getLogger() { 83 return logger; 84 } 85 86 public LoggerFactory getLoggerFactory() { 87 return loggerFactory; 88 } 89 90 public void setLogger(Logger logger) { 91 this.logger = logger; 92 Collection children = getChildren(); 93 if (children != null && !children.isEmpty()) { 94 for (Iterator it = children.iterator(); it.hasNext();) { 95 Object o = it.next(); 96 if (o != null && o instanceof Loggable) { 97 ((Loggable) o).setLogger(logger); 98 } 99 } 100 } 101 } 102 103 public void setLoggerFactory(LoggerFactory loggerfactory) { 104 this.loggerFactory = loggerfactory; 105 Collection children = getChildren(); 106 if (children != null && !children.isEmpty()) { 107 for (Iterator it = children.iterator(); it.hasNext();) { 108 Object o = it.next(); 109 if (o != null && o instanceof Loggable) { 110 ((Loggable) o).setLoggerFactory(loggerFactory); 111 } 112 } 113 } 114 if (loggerFactory != null && logger == null) { 115 setLogger(loggerFactory.getLogger(getClass().getName())); 116 } 117 } 118 119 123 129 public MetaObject getParent() { 130 return parent; 131 } 132 133 138 public void setParent(MetaObject parent) { 139 this.parent = parent; 140 } 141 142 146 public Manager getManager() { 147 MetaObject tmp = parent; 148 MetaObject manager = null; 149 while (tmp != null) { 150 manager = tmp; 151 tmp = tmp.getParent(); 152 } 153 return (Manager) manager; 154 } 155 } | Popular Tags |