1 18 package org.objectweb.util.monolog.wrapper.remote.lib; 19 20 import org.objectweb.util.monolog.Monolog; 21 import org.objectweb.util.monolog.api.Handler; 22 import org.objectweb.util.monolog.api.Level; 23 import org.objectweb.util.monolog.api.Logger; 24 import org.objectweb.util.monolog.api.MonologFactory; 25 import org.objectweb.util.monolog.api.TopicalLogger; 26 import org.objectweb.util.monolog.file.monolog.PropertiesConfAccess; 27 import org.objectweb.util.monolog.wrapper.remote.api.LoggerInfo; 28 import org.objectweb.util.monolog.wrapper.remote.api.MonologFactoryMBean; 29 30 import java.util.Arrays ; 31 import java.util.Collections ; 32 import java.util.Comparator ; 33 import java.util.HashMap ; 34 import java.util.Map ; 35 import java.util.Properties ; 36 37 43 public class MonologFactoryMBeanImpl implements MonologFactoryMBean { 44 45 private MonologFactory mf; 46 47 51 public MonologFactoryMBeanImpl() { 52 this(Monolog.getMonologFactory()); 53 } 54 55 59 public MonologFactoryMBeanImpl(MonologFactory mf) { 60 this.mf = mf; 61 } 62 63 public boolean defineLevel(String name, int value) { 64 return mf.defineLevel(name, value) != null; 65 } 66 67 public boolean defineLevel(String name, String value) 68 { 69 return mf.defineLevel(name, value) != null; 70 } 71 72 public void removeLevel(String name) { 73 mf.removeLevel(name); 74 } 75 76 public Level getLevel(String name) { 77 return mf.getLevel(name); 78 } 79 80 public Level getLevel(int value) { 81 return mf.getLevel(value); 82 } 83 84 public Level[] getLevels() { 85 return mf.getLevels(); 86 } 87 88 public int compareTo(String levelname1, String levelname2) 89 { 90 Level l1 = mf.getLevel(levelname1); 91 if (l1 == null) { 92 return Integer.MAX_VALUE; 93 } 94 Level l2 = mf.getLevel(levelname2); 95 if (l2 == null) { 96 return Integer.MIN_VALUE; 97 } 98 return l1.compareTo(l2); 99 } 100 101 public boolean createHandler(String hn, String handlertype) 102 { 103 return mf.createHandler(hn, handlertype) != null; 104 } 105 106 public boolean removeHandler(String handlername) { 107 return mf.removeHandler(handlername) == null; 108 } 109 110 public String [] getHandlerNames() { 111 Handler[] hs = mf.getHandlers(); 112 String [] hns = new String [hs.length]; 113 for(int i=0; i<hs.length; i++) { 114 hns[i] = hs[i].getName(); 115 } 116 return hns; 117 } 118 119 public Map getHandlerAttributes(String handlername) { 120 Handler h = mf.getHandler(handlername); 121 String [] ans = h.getAttributeNames(); 122 Map m = new HashMap (ans.length); 123 for(int i=0; i<ans.length; i++) { 124 m.put(ans[i], h.getAttribute(ans[i])); 125 } 126 m.put("name", handlername); 127 return m; 128 } 129 130 public Map getAllHandlerAttributes() 131 { 132 Handler[] hs = mf.getHandlers(); 133 Map m = new HashMap (hs.length); 134 for(int i=0; i<hs.length; i++) { 135 m.put(hs[i], getHandlerAttributes(hs[i].getName())); 136 } 137 return m; 138 } 139 140 public void setHandlerAttribute(String handlername, String attributeName, 141 String value) { 142 Handler h = mf.getHandler(handlername); 143 if (h == null) { 144 return; 145 } 146 h.setAttribute(attributeName, value); 147 } 148 149 public LoggerInfo getLogger(String loggername) { 150 return new LoggerInfo((TopicalLogger) mf.getLogger(loggername)); 151 } 152 153 public LoggerInfo getLogger(String loggername, String resourceBundleName) 154 { 155 return new LoggerInfo((TopicalLogger) 156 mf.getLogger(loggername, resourceBundleName)); 157 } 158 159 public String getResourceBundleName() { 160 return mf.getResourceBundleName(); 161 } 162 163 public void setResourceBundleName(String resourceBundleName) 164 { 165 mf.setResourceBundleName(resourceBundleName); 166 } 167 168 public LoggerInfo[] getLoggers() { 169 Logger[] ls = mf.getLoggers(); 170 LoggerInfo[] lis = new LoggerInfo[ls.length]; 171 for(int i=0; i<ls.length; i++) { 172 lis[i] = new LoggerInfo((TopicalLogger) ls[i]); 173 } 174 Arrays.sort(lis, new Comparator () { 175 public int compare(Object o1, Object o2) { 176 return ((LoggerInfo) o1).topics[0].compareTo( 177 ((LoggerInfo) o2).topics[0]); 178 } 179 }); 180 return lis; 181 } 182 183 public void addHandlerToLogger(String handlername, String loggerName) 184 { 185 Handler h = mf.getHandler(handlername); 186 if (h == null) { 187 return; 188 } 189 TopicalLogger l = (TopicalLogger) mf.getLogger(loggerName); 190 try { 191 l.addHandler(h); 192 } catch (Exception e) { 193 return; 194 } 195 } 196 197 public void removeHandlerFromLogger(String handlerName, String loggerName) 198 { 199 Handler h = mf.getHandler(handlerName); 200 if (h == null) { 201 return; 202 } 203 TopicalLogger l = (TopicalLogger) mf.getLogger(loggerName); 204 try { 205 l.removeHandler(h); 206 } catch (Exception e) { 207 return; 208 } 209 } 210 211 public void removeAllHandlersFromLogger(String loggerName) { 212 TopicalLogger l = (TopicalLogger) mf.getLogger(loggerName); 213 try { 214 l.removeAllHandlers(); 215 } catch (Exception e) { 216 return; 217 } 218 } 219 220 public void setAdditivity(boolean a, String loggerName) { 221 TopicalLogger l = (TopicalLogger) mf.getLogger(loggerName); 222 l.setAdditivity(a); 223 } 224 225 230 public void setLoggerLevel(int level, String loggerName) { 231 TopicalLogger l = (TopicalLogger) mf.getLogger(loggerName); 232 l.setIntLevel(level); 233 } 234 235 240 public void setLoggerLevel(String levelName, String loggerName) { 241 TopicalLogger l = (TopicalLogger) mf.getLogger(loggerName); 242 Level level = mf.getLevel(levelName); 243 if (level != null) { 244 l.setLevel(level); 245 } 246 } 247 248 249 public void addTopicToLogger(String topic, String loggerName) { 250 TopicalLogger l = (TopicalLogger) mf.getLogger(loggerName); 251 try { 252 l.addTopic(topic); 253 } catch (Exception e) { 254 return; 255 } 256 } 257 258 public void removeTopicFromLogger(String topic, String loggerName) { 259 TopicalLogger l = (TopicalLogger) mf.getLogger(loggerName); 260 try { 261 l.removeTopic(topic); 262 } catch (Exception e) { 263 return; 264 } 265 } 266 267 public Properties getMonologProperties() { 268 Properties p = new Properties (); 269 try { 270 PropertiesConfAccess.store(p, mf); 271 } catch (Exception e) { 272 return null; 273 } 274 return p; 275 } 276 277 public void setMonologProperties(Properties p) { 278 try { 279 PropertiesConfAccess.load(p, mf); 280 } catch (Exception e) { 281 } 282 } 283 284 } 285 | Popular Tags |