1 24 package org.objectweb.jalisto.se.impl.trace; 25 26 import java.text.MessageFormat ; 27 import java.util.*; 28 29 public abstract class TraceImpl extends Trace { 30 public TraceImpl(TraceContext context) { 31 this.context = context; 32 this.enabledModules = Collections.EMPTY_SET; 33 this.isShowThread = false; 34 this.maxModuleSize = 3; 35 } 36 37 public boolean isEnabled(Trace.Module component) { 38 return context.isTracing(component); 39 } 40 41 public boolean isShowThread() { 42 return isShowThread; 43 } 44 45 public void enableShowThread() { 46 isShowThread = true; 47 } 48 49 public void enableTrace(boolean enable) { 50 this.enabled = enable; 51 } 52 53 public Set enabledModules() { 54 return enabledModules; 55 } 56 57 protected void setEnabledModules(Set set) { 58 enabledModules = set; 59 } 60 61 62 public void init(Iterator traceModuleNames) { 63 this.mask = new BitSet(); 64 TRACE_ON.putInMask(mask); 65 66 Set resolvedModules = new HashSet(); 67 Set unresolvedModuleNames = new HashSet(); 68 getModules(traceModuleNames, resolvedModules, unresolvedModuleNames); 69 this.enabledModules = resolvedModules; 70 71 if (!resolvedModules.isEmpty()) { 72 for (Iterator iterator = resolvedModules.iterator(); iterator.hasNext();) { 73 Module module = (Module) iterator.next(); 74 module.putInMask(mask); 75 maxModuleSize = Math.max(maxModuleSize, module.getName().length() + 2); 76 } 77 println(TRACE_ON, "TRACE ENABLED"); 78 println(TRACE_ON, "the module(s) {0} has/have been enabled", resolvedModules); 79 } 80 81 if (!unresolvedModuleNames.isEmpty()) { 82 println(TRACE_ON, "the specified module(s) {0} is/are not valid", unresolvedModuleNames); 83 } 84 } 85 86 public synchronized void println(Module component, String message) { 87 if ((!enabled) || (!component.isInMask(mask))) { 88 return; 89 } 90 print(component, message); 91 } 92 93 public synchronized void println(Module component, String message, Object parameter1) { 94 if ((!enabled) || (!component.isInMask(mask))) { 95 return; 96 } 97 args1[0] = parameter1; 98 printFormatted(component, message, args1); 99 } 100 101 public synchronized void println(Module component, String message, Object parameter1, Object parameter2) { 102 if ((!enabled) || (!component.isInMask(mask))) { 103 return; 104 } 105 args2[0] = parameter1; 106 args2[1] = parameter2; 107 printFormatted(component, message, args2); 108 } 109 110 public synchronized void println(Module component, String message, 111 Object parameter1, Object parameter2, 112 Object parameter3) { 113 if ((!enabled) || (!component.isInMask(mask))) { 114 return; 115 } 116 args3[0] = parameter1; 117 args3[1] = parameter2; 118 args3[2] = parameter3; 119 printFormatted(component, message, args3); 120 } 121 122 public synchronized void println(Module component, String message, Object [] parameters) { 123 if ((!enabled) || (!component.isInMask(mask))) { 124 return; 125 } 126 printFormatted(component, message, parameters); 127 } 128 129 protected String pad(Trace.Module module) { 130 return rightPad(module.toString(), maxModuleSize, " "); 131 } 132 133 protected void printFormatted(Trace.Module module, String message, Object [] args) { 134 String formatedMessage = MessageFormat.format(message, args); 135 print(module, formatedMessage); 136 } 137 138 public String toString() { 139 return getTraceType() + " " + enabledModules(); 140 } 141 142 private static void getModules(Iterator moduleNames, Set resolvedModules, Set unresolvedModuleNames) { 143 while (moduleNames.hasNext()) { 144 String moduleName = ((String ) moduleNames.next()).toUpperCase(); 145 Module module = (Module) MODULE_REPOSITORY.get(moduleName); 146 if (module == null) { 147 unresolvedModuleNames.add(moduleName); 148 } else { 149 resolvedModules.add(module); 150 } 151 } 152 } 153 154 private static String rightPad(String str, int size, String delim) { 155 size = (size - str.length()) / delim.length(); 156 if (size > 0) { 157 str += repeat(delim, size); 158 } 159 return str; 160 } 161 162 private static String repeat(String str, int repeat) { 163 StringBuffer buffer = new StringBuffer (repeat * str.length()); 164 for (int i = 0; i < repeat; i++) { 165 buffer.append(str); 166 } 167 return buffer.toString(); 168 } 169 170 protected abstract String getTraceType(); 171 172 protected abstract void print(Module module, String message); 173 174 private BitSet mask; 175 private TraceContext context; 176 private Set enabledModules; 177 private Object [] args1 = new Object [1]; 178 private Object [] args2 = new Object [2]; 179 private Object [] args3 = new Object [3]; 180 private boolean isShowThread; 181 private int maxModuleSize; 182 183 184 static final long serialVersionUID = -7589377097964441459L; 185 } 186 | Popular Tags |