1 24 25 package org.objectweb.dream.control.logger; 26 27 import java.util.HashMap ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 import java.util.Set ; 32 33 import org.objectweb.fractal.julia.InitializationContext; 34 import org.objectweb.fractal.julia.loader.Initializable; 35 import org.objectweb.fractal.julia.loader.Tree; 36 import org.objectweb.util.monolog.Monolog; 37 import org.objectweb.util.monolog.api.Level; 38 import org.objectweb.util.monolog.api.Logger; 39 40 45 public abstract class BasicLoggerControllerMixin 46 implements 47 LoggerController, 48 LoggerControllerRegister, 49 Initializable 50 { 51 52 String baseName; 53 54 55 Map registrations; 56 57 Map loggers; 58 59 63 66 public void initialize(final Tree args) throws Exception 67 { 68 _super_initialize(args); 69 for (int i = 0; i < args.getSize(); ++i) 70 { 71 Tree arg = args.getSubTree(i); 72 if (arg.getSize() == 2 && arg.getSubTree(0).equals("monolog-conf-file")) 73 { 74 String monologConfFile = arg.getSubTree(1).toString(); 75 if (Monolog.monologFactory == Monolog.getDefaultMonologFactory()) 76 { 77 Monolog.getMonologFactory(monologConfFile); 78 } 79 } 80 } 81 } 82 83 86 public void initFcController(InitializationContext ic) 87 throws InstantiationException 88 { 89 _super_initFcController(ic); 90 baseName = Util.getNextUnnamedBaseName(); 91 registrations = new HashMap (); 92 loggers = new HashMap (); 93 } 94 95 98 public String getBaseName() 99 { 100 return baseName; 101 } 102 103 106 public void setBaseName(String name) 107 { 108 this.baseName = name; 109 baseNameChanged(); 110 } 111 112 115 public Level getLoggerLevel(String loggerName) 116 { 117 Logger l = (Logger) loggers.get(loggerName); 118 if (l == null) 119 { 120 return null; 121 } 122 return l.getCurrentLevel(); 123 } 124 125 128 public void setLoggerLevel(String loggerName, Level level) 129 { 130 Logger l = (Logger) loggers.get(loggerName); 131 if (l == null) 132 { 133 return; 134 } 135 l.setLevel(level); 136 } 137 138 141 public void register(String loggerName, Loggable loggable) 142 { 143 Set s = (Set ) registrations.get(loggerName); 144 if (s == null) 145 { 146 s = new HashSet (); 147 registrations.put(loggerName, s); 148 } 149 s.add(loggable); 150 giveLogger(loggerName, loggable); 151 } 152 153 156 public void unregiser(String loggerName, Loggable loggable) 157 { 158 Set s = (Set ) registrations.get(loggerName); 159 if (s == null) 160 { 161 return; 162 } 163 s.remove(loggable); 164 if (s.isEmpty()) 165 { 166 registrations.remove(loggerName); 169 loggers.remove(loggerName); 170 } 171 } 172 173 177 void baseNameChanged() 178 { 179 loggers.clear(); 180 Iterator iter = registrations.entrySet().iterator(); 181 while (iter.hasNext()) 182 { 183 Map.Entry entry = (Map.Entry ) iter.next(); 184 String loggerName = (String ) entry.getKey(); 185 Set loggables = (Set ) entry.getValue(); 186 Iterator iter2 = loggables.iterator(); 187 while (iter2.hasNext()) 188 { 189 Loggable loggable = (Loggable) iter2.next(); 190 giveLogger(loggerName, loggable); 191 } 192 } 193 } 194 195 void giveLogger(String loggerName, Loggable loggable) 196 { 197 Logger logger = (Logger) loggers.get(loggerName); 198 if (logger == null) 199 { 200 String name; 201 if (loggerName == null) 202 { 203 name = baseName; 204 } 205 else 206 { 207 name = baseName + "." + loggerName; 208 } 209 logger = Monolog.monologFactory.getLogger(name); 210 loggers.put(loggerName, logger); 211 } 212 loggable.setLogger(loggerName, logger); 213 } 214 215 219 226 public abstract void _super_initFcController(InitializationContext ic) 227 throws InstantiationException ; 228 229 235 public abstract void _super_initialize(final Tree args) throws Exception ; 236 237 } | Popular Tags |