KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > PookaLogManager


1 package net.suberic.pooka;
2
3 import net.suberic.util.*;
4
5 import java.util.logging.*;
6 import java.util.*;
7
8 /**
9  * This class manages logging for Pooka. It basically provides a bridge
10  * between Pooka configuration files and the JDK 1.4 Logging system.
11  */

12
13 public class PookaLogManager implements ValueChangeListener {
14   
15   // the logging strings that we're currently monitoring
16
Set mMonitoredLogStrings = null;
17   
18   // the default list of log settings we monitor
19
String JavaDoc[] mDefaultLogSettings = new String JavaDoc[] {
20     "Pooka.debug",
21     "Pooka.debug.session",
22     "editors.debug",
23     "Pooka.debug.gui",
24     "Pooka.debug.gui.focus",
25     "Pooka.debug.gui.filechooser",
26     "Pooka.debug.folderTracker",
27     "Pooka.debug.logManager",
28     "Pooka.debug.startupManager",
29     "Pooka.debug.messaging",
30     "Pooka.debug.sslFactory"
31   };
32   
33   
34   /**
35    * Constructor. Sets itself as a valueChangeListener for all of the
36    * log settings configured.
37    */

38   public PookaLogManager() {
39     VariableBundle globalBundle = Pooka.getResources();
40     mMonitoredLogStrings = new HashSet(Arrays.asList(mDefaultLogSettings));
41     
42     Iterator it = mMonitoredLogStrings.iterator();
43     while (it.hasNext()) {
44       globalBundle.addValueChangeListener(this, (String JavaDoc) it.next() + ".logLevel");
45     }
46     
47     refresh();
48     
49     configureListeners();
50     
51     // set up logging to log all messages. stupid.
52
Logger global = Logger.getLogger("");
53     Handler[] globalHandlers = global.getHandlers();
54     for (int i = 0; i < globalHandlers.length; i++) {
55       globalHandlers[i].setLevel(Level.ALL);
56     }
57   }
58   
59   
60   /**
61    * Refreshes all logging states from the current configuration.
62    */

63   public void refresh() {
64     Iterator it = mMonitoredLogStrings.iterator();
65     while (it.hasNext()) {
66       String JavaDoc current = (String JavaDoc) it.next();
67       refresh(current);
68     }
69   }
70
71   /**
72    * Refreshes the logging state for the given property.
73    */

74   public void refresh(String JavaDoc pKey) {
75     String JavaDoc levelKey = Pooka.getProperty(pKey + ".logLevel", "DEFAULT");
76     Level newLevel = null;
77     if (levelKey == null || levelKey.equals("") || levelKey.equalsIgnoreCase("DEFAULT")) {
78       newLevel = null;
79     } else {
80       newLevel = Level.parse(levelKey);
81     }
82     getLogger().log(Level.FINE, "set log level for " + pKey + " to " + newLevel);
83     setLogLevel(pKey, newLevel);
84     
85   }
86   
87   /**
88    * Sets up additional listeners.
89    */

90   public void configureListeners() {
91     // focus listener
92
java.awt.KeyboardFocusManager JavaDoc mgr = java.awt.KeyboardFocusManager.getCurrentKeyboardFocusManager();
93     mgr.addPropertyChangeListener(new java.beans.PropertyChangeListener JavaDoc() {
94     public void propertyChange(java.beans.PropertyChangeEvent JavaDoc evt) {
95       Logger logger = Logger.getLogger("Pooka.debug.gui.focus");
96       Level logLevel = Level.FINEST;
97       if (evt.getPropertyName().equalsIgnoreCase("permanentFocusOwner")) {
98         logLevel = Level.FINE;
99       } else if (evt.getPropertyName().equalsIgnoreCase("focusOwner") || evt.getPropertyName().equalsIgnoreCase("focusOwner")) {
100         logLevel = Level.FINER;
101       }
102       String JavaDoc oldValue = "null";
103       String JavaDoc newValue = "null";
104       if (evt.getOldValue() != null) {
105         oldValue = evt.getOldValue().getClass().getName();
106       }
107       if (evt.getNewValue() != null) {
108         newValue = evt.getNewValue().getClass().getName();
109       }
110       logger.log(logLevel, evt.getPropertyName() + ": oldValue=" + oldValue + "; newValue=" + newValue);
111
112     }
113       });
114     
115   }
116   
117   /**
118    * Sets the appropriate log setting.
119    */

120   public void setLogLevel(String JavaDoc pName, Level pLogLevel) {
121     Logger current = Logger.getLogger(pName);
122     if (current.getLevel() != pLogLevel) {
123       current.setLevel(pLogLevel);
124
125     }
126
127     if (pName == "Pooka.debug") {
128       setLogLevel("", pLogLevel);
129     }
130
131   }
132   
133   // ValueChangeListener
134
/**
135    * Responds to a change in a configured value.
136    */

137     public void valueChanged(String JavaDoc pChangedValue) {
138       String JavaDoc key = pChangedValue;
139       // this should always end with .logLevel.
140
if (pChangedValue.endsWith(".logLevel")) {
141     key = pChangedValue.substring(0, pChangedValue.length() - 9);
142       }
143       
144       refresh(key);
145     }
146   
147   /**
148    * Adds a logger for us to watch over.
149    */

150   public void addLogger(String JavaDoc pKey) {
151     if (! mMonitoredLogStrings.contains(pKey)) {
152       mMonitoredLogStrings.add(pKey);
153       Pooka.getResources().addValueChangeListener(this, pKey + ".logLevel");
154       String JavaDoc levelKey = Pooka.getProperty(pKey + ".logLevel", "DEFAULT");
155       Level newLevel = null;
156       if (levelKey == null || levelKey.equals("") || levelKey.equalsIgnoreCase("DEFAULT")) {
157     newLevel = null;
158       } else {
159     newLevel = Level.parse(levelKey);
160       }
161       setLogLevel(pKey, newLevel);
162       getLogger().log(Level.FINE, "added key " + pKey + "; set value to " + newLevel);
163     }
164   }
165   
166   /**
167    * Removes a logger from the monitored list.
168    */

169   public void removeLogger(String JavaDoc pKey) {
170     if (mMonitoredLogStrings.contains(pKey)) {
171       mMonitoredLogStrings.remove(pKey);
172       Pooka.getResources().removeValueChangeListener(this, pKey + ".logLevel");
173       getLogger().log(Level.FINE, "removed key " + pKey);
174     }
175   }
176   
177   /**
178    * Gets the logger for this class.
179    */

180   public Logger getLogger() {
181     return Logger.getLogger("Pooka.debug.logManager");
182   }
183 }
184
Popular Tags