KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > dream > control > logger > BasicLoggerControllerMixin


1 /**
2  * Dream
3  * Copyright (C) 2003-2004 INRIA Rhone-Alpes
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 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  * Contact: dream@objectweb.org
20  *
21  * Initial developer(s): Matthieu Leclercq
22  * Contributor(s):
23  */

24
25 package org.objectweb.dream.control.logger;
26
27 import java.util.HashMap JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Set JavaDoc;
32
33 import org.objectweb.fractal.julia.InitializationContext;
34 import org.objectweb.fractal.julia.loader.Initializable;
35 import org.objectweb.fractal.julia.loader.Tree;
36 import org.objectweb.util.monolog.Monolog;
37 import org.objectweb.util.monolog.api.Level;
38 import org.objectweb.util.monolog.api.Logger;
39
40 /**
41  * Basic implementation of logger controller. A <code>"monolog-conf-file"</code>
42  * initialisation parameter can be set to specify the location of the monolog
43  * configuration file.
44  */

45 public abstract class BasicLoggerControllerMixin
46     implements
47       LoggerController,
48       LoggerControllerRegister,
49       Initializable
50 {
51
52   String JavaDoc baseName;
53
54   /** Map associating logger name and list of registred loggables */
55   Map JavaDoc registrations;
56   /** Map associating logger name and logger instance */
57   Map JavaDoc loggers;
58
59   // -------------------------------------------------------------------------
60
// Fields and methods added and overriden by the mixin class
61
// -------------------------------------------------------------------------
62

63   /**
64    * @see Initializable#initialize(org.objectweb.fractal.julia.loader.Tree)
65    */

66   public void initialize(final Tree args) throws Exception JavaDoc
67   {
68     _super_initialize(args);
69     for (int i = 0; i < args.getSize(); ++i)
70     {
71       Tree arg = args.getSubTree(i);
72       if (arg.getSize() == 2 && arg.getSubTree(0).equals("monolog-conf-file"))
73       {
74         String JavaDoc monologConfFile = arg.getSubTree(1).toString();
75         if (Monolog.monologFactory == Monolog.getDefaultMonologFactory())
76         {
77           Monolog.getMonologFactory(monologConfFile);
78         }
79       }
80     }
81   }
82
83   /**
84    * @see org.objectweb.fractal.julia.Controller#initFcController(InitializationContext)
85    */

86   public void initFcController(InitializationContext ic)
87       throws InstantiationException JavaDoc
88   {
89     _super_initFcController(ic);
90     baseName = Util.getNextUnnamedBaseName();
91     registrations = new HashMap JavaDoc();
92     loggers = new HashMap JavaDoc();
93   }
94
95   /**
96    * @see LoggerController#getBaseName()
97    */

98   public String JavaDoc getBaseName()
99   {
100     return baseName;
101   }
102
103   /**
104    * @see LoggerController#setBaseName(String)
105    */

106   public void setBaseName(String JavaDoc name)
107   {
108     this.baseName = name;
109     baseNameChanged();
110   }
111
112   /**
113    * @see LoggerController#getLoggerLevel(String)
114    */

115   public Level getLoggerLevel(String JavaDoc loggerName)
116   {
117     Logger l = (Logger) loggers.get(loggerName);
118     if (l == null)
119     {
120       return null;
121     }
122     return l.getCurrentLevel();
123   }
124
125   /**
126    * @see LoggerController#setLoggerLevel(String, Level)
127    */

128   public void setLoggerLevel(String JavaDoc loggerName, Level level)
129   {
130     Logger l = (Logger) loggers.get(loggerName);
131     if (l == null)
132     {
133       return;
134     }
135     l.setLevel(level);
136   }
137
138   /**
139    * @see LoggerControllerRegister#register(String, Loggable)
140    */

141   public void register(String JavaDoc loggerName, Loggable loggable)
142   {
143     Set JavaDoc s = (Set JavaDoc) registrations.get(loggerName);
144     if (s == null)
145     {
146       s = new HashSet JavaDoc();
147       registrations.put(loggerName, s);
148     }
149     s.add(loggable);
150     giveLogger(loggerName, loggable);
151   }
152
153   /**
154    * @see LoggerControllerRegister#unregiser(String, Loggable)
155    */

156   public void unregiser(String JavaDoc loggerName, Loggable loggable)
157   {
158     Set JavaDoc s = (Set JavaDoc) registrations.get(loggerName);
159     if (s == null)
160     {
161       return;
162     }
163     s.remove(loggable);
164     if (s.isEmpty())
165     {
166       // no more registration for this logger, remove reference to it, for
167
// garbage collector
168
registrations.remove(loggerName);
169       loggers.remove(loggerName);
170     }
171   }
172
173   // ---------------------------------------------------------------------------
174
// Utility methods
175
// ---------------------------------------------------------------------------
176

177   void baseNameChanged()
178   {
179     loggers.clear();
180     Iterator JavaDoc iter = registrations.entrySet().iterator();
181     while (iter.hasNext())
182     {
183       Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
184       String JavaDoc loggerName = (String JavaDoc) entry.getKey();
185       Set JavaDoc loggables = (Set JavaDoc) entry.getValue();
186       Iterator JavaDoc iter2 = loggables.iterator();
187       while (iter2.hasNext())
188       {
189         Loggable loggable = (Loggable) iter2.next();
190         giveLogger(loggerName, loggable);
191       }
192     }
193   }
194
195   void giveLogger(String JavaDoc loggerName, Loggable loggable)
196   {
197     Logger logger = (Logger) loggers.get(loggerName);
198     if (logger == null)
199     {
200       String JavaDoc name;
201       if (loggerName == null)
202       {
203         name = baseName;
204       }
205       else
206       {
207         name = baseName + "." + loggerName;
208       }
209       logger = Monolog.monologFactory.getLogger(name);
210       loggers.put(loggerName, logger);
211     }
212     loggable.setLogger(loggerName, logger);
213   }
214
215   // ---------------------------------------------------------------------------
216
// Fields and methods required by the mixin class in the base class
217
// ---------------------------------------------------------------------------
218

219   /**
220    * The
221    * {@link org.objectweb.fractal.julia.Controller#initFcController(InitializationContext) initFcController}
222    * method overriden by this mixin.
223    *
224    * @see org.objectweb.fractal.julia.Controller#initFcController(InitializationContext)
225    */

226   public abstract void _super_initFcController(InitializationContext ic)
227       throws InstantiationException JavaDoc;
228
229   /**
230    * The {@link Initializable#initialize(Tree) initialize}method overriden by
231    * this mixin.
232    *
233    * @see Initializable#initialize(Tree)
234    */

235   public abstract void _super_initialize(final Tree args) throws Exception JavaDoc;
236
237 }
Popular Tags