KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > log > MLevel


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v2.log;
25
26 import java.util.*;
27
28 public final class MLevel
29 {
30     public final static MLevel ALL;
31     public final static MLevel CONFIG;
32     public final static MLevel FINE;
33     public final static MLevel FINER;
34     public final static MLevel FINEST;
35     public final static MLevel INFO;
36     public final static MLevel OFF;
37     public final static MLevel SEVERE;
38     public final static MLevel WARNING;
39
40     private final static Map integersToMLevels;
41     private final static Map namesToMLevels;
42
43     public static MLevel fromIntValue(int intval)
44     { return (MLevel) integersToMLevels.get( new Integer JavaDoc( intval ) ); }
45
46     public static MLevel fromSeverity(String JavaDoc name)
47     { return (MLevel) namesToMLevels.get( name ); }
48
49     static
50     {
51     Class JavaDoc lvlClass;
52     boolean jdk14api; //not just jdk14 -- it is possible for the api to be present with older vms
53
try
54         {
55         lvlClass = Class.forName( "java.util.logging.Level" );
56         jdk14api = true;
57         }
58     catch (ClassNotFoundException JavaDoc e )
59         {
60         lvlClass = null;
61         jdk14api = false;
62         }
63
64     MLevel all;
65     MLevel config;
66     MLevel fine;
67     MLevel finer;
68     MLevel finest;
69     MLevel info;
70     MLevel off;
71     MLevel severe;
72     MLevel warning;
73
74     try
75         {
76         // numeric values match the intvalues from java.util.logging.Level
77
all = new MLevel( (jdk14api ? lvlClass.getField("ALL").get(null) : null), Integer.MIN_VALUE, "ALL" );
78         config = new MLevel( (jdk14api ? lvlClass.getField("CONFIG").get(null) : null), 700, "CONFIG" );
79         fine = new MLevel( (jdk14api ? lvlClass.getField("FINE").get(null) : null), 500, "FINE" );
80         finer = new MLevel( (jdk14api ? lvlClass.getField("FINER").get(null) : null), 400, "FINER" );
81         finest = new MLevel( (jdk14api ? lvlClass.getField("FINEST").get(null) : null), 300, "FINEST" );
82         info = new MLevel( (jdk14api ? lvlClass.getField("INFO").get(null) : null), 800, "INFO" );
83         off = new MLevel( (jdk14api ? lvlClass.getField("OFF").get(null) : null), Integer.MAX_VALUE, "OFF" );
84         severe = new MLevel( (jdk14api ? lvlClass.getField("SEVERE").get(null) : null), 900, "SEVERE" );
85         warning = new MLevel( (jdk14api ? lvlClass.getField("WARNING").get(null) : null), 1000, "WARNING" );
86         }
87     catch ( Exception JavaDoc e )
88         {
89         e.printStackTrace();
90         throw new InternalError JavaDoc("Huh? java.util.logging.Level is here, but not its expected public fields?");
91         }
92
93     ALL = all;
94     CONFIG = config;
95     FINE = fine;
96     FINER = finer;
97     FINEST = finest;
98     INFO = info;
99     OFF = off;
100     SEVERE = severe;
101     WARNING = warning;
102
103     Map tmp = new HashMap();
104     tmp.put( new Integer JavaDoc(all.intValue()), all);
105     tmp.put( new Integer JavaDoc(config.intValue()), config);
106     tmp.put( new Integer JavaDoc(fine.intValue()), fine);
107     tmp.put( new Integer JavaDoc(finer.intValue()), finer);
108     tmp.put( new Integer JavaDoc(finest.intValue()), finest);
109     tmp.put( new Integer JavaDoc(info.intValue()), info);
110     tmp.put( new Integer JavaDoc(off.intValue()), off);
111     tmp.put( new Integer JavaDoc(severe.intValue()), severe);
112     tmp.put( new Integer JavaDoc(warning.intValue()), warning);
113
114     integersToMLevels = Collections.unmodifiableMap( tmp );
115
116     tmp = new HashMap();
117     tmp.put( all.getSeverity(), all);
118     tmp.put( config.getSeverity(), config);
119     tmp.put( fine.getSeverity(), fine);
120     tmp.put( finer.getSeverity(), finer);
121     tmp.put( finest.getSeverity(), finest);
122     tmp.put( info.getSeverity(), info);
123     tmp.put( off.getSeverity(), off);
124     tmp.put( severe.getSeverity(), severe);
125     tmp.put( warning.getSeverity(), warning);
126
127     namesToMLevels = Collections.unmodifiableMap( tmp );
128     }
129
130     Object JavaDoc level;
131     int intval;
132     String JavaDoc lvlstring;
133
134     public int intValue()
135     { return intval; }
136
137     public Object JavaDoc asJdk14Level()
138     { return level; }
139
140     public String JavaDoc getSeverity()
141     { return lvlstring; }
142
143     public String JavaDoc toString()
144     { return this.getClass().getName() + this.getLineHeader(); }
145
146     public String JavaDoc getLineHeader()
147     { return "[" + lvlstring + ']';}
148
149     private MLevel(Object JavaDoc level, int intval, String JavaDoc lvlstring)
150     {
151     this.level = level;
152     this.intval = intval;
153     this.lvlstring = lvlstring;
154     }
155 }
Popular Tags