1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package de.bea.domingo.monitor;
24
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import de.bea.domingo.DNotesFactory;
29 import de.bea.domingo.DNotesMonitor;
30
31 /***
32 * Abstract base class for implementations of the Monitor interface.
33 *
34 * <p>This class handles the level of monitors.</p>
35 *
36 * @author <a href=mailto:kriede@users.sourceforge.net>Kurt Riede</a>
37 */
38 public abstract class AbstractMonitor implements DNotesMonitor {
39
40 /***
41 * Map of all string representing valid levels to their corresponding integer value.
42 *
43 * <p>The standard values are represented by the following strings (not case sensitive):
44 * <code>"{@link #DEBUG}"</code>,
45 * <code>"{@link #INFO}"</code>,
46 * <code>"{@link #WARN}"</code>,
47 * <code>"{@link #ERROR}"</code>,
48 * <code>"{@link #FATAL}"</code> and
49 * <code>"{@link #NONE}"</code>.</p>
50 *
51 * <p>For convenience, also the following values are synonyms:</p>
52 * <pre>"TRACE" -> DEBUG
53 *"INFORMATION" -> INFO
54 *"WARNING" -> WARN
55 *"FATALERROR" -> FATAL
56 *"NOTHING" -> NONE
57 *"DEFAULT" -> WARN</pre>
58 */
59 public static final Map LEVELS = new HashMap();
60
61 static {
62 LEVELS.put("TRACE", new Integer(DEBUG));
63 LEVELS.put("DEBUG", new Integer(DEBUG));
64 LEVELS.put("INFO", new Integer(INFO));
65 LEVELS.put("INFORMATION", new Integer(INFO));
66 LEVELS.put("WARN", new Integer(WARN));
67 LEVELS.put("WARNING", new Integer(WARN));
68 LEVELS.put("ERROR", new Integer(ERROR));
69 LEVELS.put("FATAL", new Integer(FATAL));
70 LEVELS.put("FATALERROR", new Integer(FATAL));
71 LEVELS.put("NONE", new Integer(NONE));
72 LEVELS.put("NOTHING", new Integer(NONE));
73 LEVELS.put("DEFAULT", new Integer(WARN));
74 }
75
76 /*** Current monitoring level. */
77 private int level = DEFAULT_LEVEL;
78
79 /***
80 * Constructor.
81 */
82 public AbstractMonitor() {
83 final String levelString = DNotesFactory.getProperty("de.bea.domingo.monitor.level", "INFO");
84 level = getLevel(levelString);
85 }
86
87 /***
88 * Constructor.
89 *
90 * @param theLevel the level of the new monitor
91 */
92 public AbstractMonitor(final int theLevel) {
93 level = theLevel;
94 }
95
96 /***
97 * Sets the monitoring level of the monitor.
98 *
99 * <p>See {@link #LEVELS} for allowed values.</p>
100 * {@link #DEBUG}
101 * @param theLevel the new level
102 */
103 public final void setLevel(final int theLevel) {
104 this.level = theLevel;
105 }
106
107 /***
108 * {@inheritDoc}
109 * @see de.bea.domingo.DNotesMonitor#isFatalErrorEnabled()
110 */
111 public final boolean isFatalErrorEnabled() {
112 return level >= FATAL;
113 }
114
115 /***
116 * {@inheritDoc}
117 * @see de.bea.domingo.DNotesMonitor#isErrorEnabled()
118 */
119 public final boolean isErrorEnabled() {
120 return level >= ERROR;
121 }
122
123 /***
124 * {@inheritDoc}
125 * @see de.bea.domingo.DNotesMonitor#isWarnEnabled()
126 */
127 public final boolean isWarnEnabled() {
128 return level >= WARN;
129 }
130
131 /***
132 * {@inheritDoc}
133 * @see de.bea.domingo.DNotesMonitor#isInfoEnabled()
134 */
135 public final boolean isInfoEnabled() {
136 return level >= INFO;
137 }
138
139 /***
140 * {@inheritDoc}
141 * @see de.bea.domingo.DNotesMonitor#isDebugEnabled()
142 */
143 public final boolean isDebugEnabled() {
144 return level >= DEBUG;
145 }
146
147 /***
148 * Checks if the monitor level is set to <code>NONE</code>.
149 *
150 * @return <code>true</code> if the monitor level is set to <code>NONE</code>, else <code>false</code>
151 */
152 public final boolean isNoMesssages() {
153 return level == NONE;
154 }
155
156 /***
157 * Given a string, returns the monitoring level as integer.
158 *
159 * <p>See {@link #LEVELS} for allowed values.</p>
160 *
161 * @param levelString the level as a string
162 * @return the level as an integer
163 */
164 private int getLevel(final String levelString) {
165 if (levelString == null) {
166 return DEFAULT_LEVEL;
167 }
168 Integer integer = (Integer) LEVELS.get(levelString.toUpperCase());
169 return integer == null ? DEFAULT_LEVEL : integer.intValue();
170 }
171 }