001: /*
002: * Distributed as part of c3p0 v.0.9.1.2
003: *
004: * Copyright (C) 2005 Machinery For Change, Inc.
005: *
006: * Author: Steve Waldman <swaldman@mchange.com>
007: *
008: * This library is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU Lesser General Public License version 2.1, as
010: * published by the Free Software Foundation.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this software; see the file LICENSE. If not, write to the
019: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package com.mchange.v2.log;
024:
025: import java.util.*;
026:
027: public final class MLevel {
028: public final static MLevel ALL;
029: public final static MLevel CONFIG;
030: public final static MLevel FINE;
031: public final static MLevel FINER;
032: public final static MLevel FINEST;
033: public final static MLevel INFO;
034: public final static MLevel OFF;
035: public final static MLevel SEVERE;
036: public final static MLevel WARNING;
037:
038: private final static Map integersToMLevels;
039: private final static Map namesToMLevels;
040:
041: public static MLevel fromIntValue(int intval) {
042: return (MLevel) integersToMLevels.get(new Integer(intval));
043: }
044:
045: public static MLevel fromSeverity(String name) {
046: return (MLevel) namesToMLevels.get(name);
047: }
048:
049: static {
050: Class lvlClass;
051: boolean jdk14api; //not just jdk14 -- it is possible for the api to be present with older vms
052: try {
053: lvlClass = Class.forName("java.util.logging.Level");
054: jdk14api = true;
055: } catch (ClassNotFoundException e) {
056: lvlClass = null;
057: jdk14api = false;
058: }
059:
060: MLevel all;
061: MLevel config;
062: MLevel fine;
063: MLevel finer;
064: MLevel finest;
065: MLevel info;
066: MLevel off;
067: MLevel severe;
068: MLevel warning;
069:
070: try {
071: // numeric values match the intvalues from java.util.logging.Level
072: all = new MLevel((jdk14api ? lvlClass.getField("ALL").get(
073: null) : null), Integer.MIN_VALUE, "ALL");
074: config = new MLevel((jdk14api ? lvlClass.getField("CONFIG")
075: .get(null) : null), 700, "CONFIG");
076: fine = new MLevel((jdk14api ? lvlClass.getField("FINE")
077: .get(null) : null), 500, "FINE");
078: finer = new MLevel((jdk14api ? lvlClass.getField("FINER")
079: .get(null) : null), 400, "FINER");
080: finest = new MLevel((jdk14api ? lvlClass.getField("FINEST")
081: .get(null) : null), 300, "FINEST");
082: info = new MLevel((jdk14api ? lvlClass.getField("INFO")
083: .get(null) : null), 800, "INFO");
084: off = new MLevel((jdk14api ? lvlClass.getField("OFF").get(
085: null) : null), Integer.MAX_VALUE, "OFF");
086: severe = new MLevel((jdk14api ? lvlClass.getField("SEVERE")
087: .get(null) : null), 900, "SEVERE");
088: warning = new MLevel((jdk14api ? lvlClass.getField(
089: "WARNING").get(null) : null), 1000, "WARNING");
090: } catch (Exception e) {
091: e.printStackTrace();
092: throw new InternalError(
093: "Huh? java.util.logging.Level is here, but not its expected public fields?");
094: }
095:
096: ALL = all;
097: CONFIG = config;
098: FINE = fine;
099: FINER = finer;
100: FINEST = finest;
101: INFO = info;
102: OFF = off;
103: SEVERE = severe;
104: WARNING = warning;
105:
106: Map tmp = new HashMap();
107: tmp.put(new Integer(all.intValue()), all);
108: tmp.put(new Integer(config.intValue()), config);
109: tmp.put(new Integer(fine.intValue()), fine);
110: tmp.put(new Integer(finer.intValue()), finer);
111: tmp.put(new Integer(finest.intValue()), finest);
112: tmp.put(new Integer(info.intValue()), info);
113: tmp.put(new Integer(off.intValue()), off);
114: tmp.put(new Integer(severe.intValue()), severe);
115: tmp.put(new Integer(warning.intValue()), warning);
116:
117: integersToMLevels = Collections.unmodifiableMap(tmp);
118:
119: tmp = new HashMap();
120: tmp.put(all.getSeverity(), all);
121: tmp.put(config.getSeverity(), config);
122: tmp.put(fine.getSeverity(), fine);
123: tmp.put(finer.getSeverity(), finer);
124: tmp.put(finest.getSeverity(), finest);
125: tmp.put(info.getSeverity(), info);
126: tmp.put(off.getSeverity(), off);
127: tmp.put(severe.getSeverity(), severe);
128: tmp.put(warning.getSeverity(), warning);
129:
130: namesToMLevels = Collections.unmodifiableMap(tmp);
131: }
132:
133: Object level;
134: int intval;
135: String lvlstring;
136:
137: public int intValue() {
138: return intval;
139: }
140:
141: public Object asJdk14Level() {
142: return level;
143: }
144:
145: public String getSeverity() {
146: return lvlstring;
147: }
148:
149: public String toString() {
150: return this .getClass().getName() + this .getLineHeader();
151: }
152:
153: public String getLineHeader() {
154: return "[" + lvlstring + ']';
155: }
156:
157: private MLevel(Object level, int intval, String lvlstring) {
158: this.level = level;
159: this.intval = intval;
160: this.lvlstring = lvlstring;
161: }
162: }
|