KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > impl > trace > TraceImpl


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.se.impl.trace;
25
26 import java.text.MessageFormat JavaDoc;
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 JavaDoc message) {
87         if ((!enabled) || (!component.isInMask(mask))) {
88             return;
89         }
90         print(component, message);
91     }
92
93     public synchronized void println(Module component, String JavaDoc message, Object JavaDoc 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 JavaDoc message, Object JavaDoc parameter1, Object JavaDoc 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 JavaDoc message,
111                                      Object JavaDoc parameter1, Object JavaDoc parameter2,
112                                      Object JavaDoc 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 JavaDoc message, Object JavaDoc[] parameters) {
123         if ((!enabled) || (!component.isInMask(mask))) {
124             return;
125         }
126         printFormatted(component, message, parameters);
127     }
128
129     protected String JavaDoc pad(Trace.Module module) {
130         return rightPad(module.toString(), maxModuleSize, " ");
131     }
132
133     protected void printFormatted(Trace.Module module, String JavaDoc message, Object JavaDoc[] args) {
134         String JavaDoc formatedMessage = MessageFormat.format(message, args);
135         print(module, formatedMessage);
136     }
137
138     public String JavaDoc toString() {
139         return getTraceType() + " " + enabledModules();
140     }
141
142     private static void getModules(Iterator moduleNames, Set resolvedModules, Set unresolvedModuleNames) {
143         while (moduleNames.hasNext()) {
144             String JavaDoc moduleName = ((String JavaDoc) 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 JavaDoc rightPad(String JavaDoc str, int size, String JavaDoc 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 JavaDoc repeat(String JavaDoc str, int repeat) {
163         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(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 JavaDoc getTraceType();
171
172     protected abstract void print(Module module, String JavaDoc message);
173
174     private BitSet mask;
175     private TraceContext context;
176     private Set enabledModules;
177     private Object JavaDoc[] args1 = new Object JavaDoc[1];
178     private Object JavaDoc[] args2 = new Object JavaDoc[2];
179     private Object JavaDoc[] args3 = new Object JavaDoc[3];
180     private boolean isShowThread;
181     private int maxModuleSize;
182
183
184     static final long serialVersionUID = -7589377097964441459L;
185 }
186
Popular Tags