KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > julia > logger > LoggerLifeCycleMixin


1 /***
2  * Julia: France Telecom's implementation of the Fractal API
3  * Copyright (C) 2001-2002 France Telecom R&D
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: Eric.Bruneton@rd.francetelecom.com
20  *
21  * Author: Eric Bruneton
22  */

23
24 package org.objectweb.fractal.julia.logger;
25
26 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
27 import org.objectweb.fractal.api.control.BindingController;
28 import org.objectweb.fractal.api.Component;
29
30 import org.objectweb.fractal.julia.control.lifecycle.LifeCycleCoordinator;
31 import org.objectweb.fractal.julia.control.binding.ContentBindingController;
32 import org.objectweb.fractal.julia.Util;
33 import org.objectweb.fractal.julia.loader.Initializable;
34 import org.objectweb.fractal.julia.loader.Tree;
35
36 import org.objectweb.util.monolog.api.Logger;
37 import org.objectweb.util.monolog.api.MonologFactory;
38 import org.objectweb.util.monolog.api.Loggable;
39 import org.objectweb.util.monolog.Monolog;
40
41 /**
42  * Assigns a logger to a component during its startup. The topic
43  * name is based on the component path in the architecture. To use this mixin
44  * you have to add two lines in the julia.cfg configuration file:
45  * <pre>
46  * (lifecycle-controller-impl
47  * ((org.objectweb.fractal.julia.asm.MixinClassGenerator
48  * LifeCycleControllerImpl
49  * org.objectweb.fractal.julia.BasicControllerMixin
50  * org.objectweb.fractal.julia.UseComponentMixin
51  * org.objectweb.fractal.julia.control.lifecycle.BasicLifeCycleCoordinatorMixin
52  * org.objectweb.fractal.julia.control.lifecycle.BasicLifeCycleControllerMixin
53  * # to check that mandatory client interfaces are bound in startFc:
54  * org.objectweb.fractal.julia.control.lifecycle.TypeLifeCycleMixin
55  * <font color="red"># to automatically assign the logger and logger factory:
56  * org.objectweb.fractal.julia.BasicInitializableMixin
57  * org.objectweb.fractal.julia.logger.LoggerLifeCycleMixin</font>
58  * # to notify the encapsulated component (if present) when its state changes:
59  * org.objectweb.fractal.julia.control.lifecycle.ContainerLifeCycleMixin
60  * )
61  * <font color="red"># optional initialization parameter (monolog configuration file name):
62  * (monolog-conf-file monolog.properties)</font>
63  * )
64  * )</pre>
65  * The user component must also implement either {@link BindingController} or
66  * {@link Loggable}. In the first case, the logger and logger factory can be
67  * retreived like this:
68  * <pre>
69  * public void bindFc (String s, Object o) {
70  * if ("logger".equals(s)) {
71  * myLogger = (Logger) o;
72  * } else if ("monolog-factory".equals(s)) { // optional
73  * String baseName = myLogger.getName();
74  * otherLogger1 = ((LoggerFactory) o).getLogger(baseName + ".toto");
75  * otherLogger2 = ((LoggerFactory) o).getLogger(baseName + ".titi");
76  * } else ...
77  * }</pre>
78  * The other {@link BindingController} methods, and in particular the {@link
79  * BindingController#listFc listFc} method, must not take these logger
80  * "bindings" into account.
81  *
82  * @author E.Bruneton, S.Chassande-Barrioz
83  */

84
85 public abstract class LoggerLifeCycleMixin
86   implements LifeCycleCoordinator, Initializable
87 {
88
89   // -------------------------------------------------------------------------
90
// Private constructor
91
// -------------------------------------------------------------------------
92

93   private LoggerLifeCycleMixin () {
94   }
95
96   // -------------------------------------------------------------------------
97
// Fields and methods added and overriden by the mixin class
98
// -------------------------------------------------------------------------
99

100   public void initialize (final Tree args) throws Exception JavaDoc {
101     _super_initialize(args);
102     for (int i = 0; i < args.getSize(); ++i) {
103       Tree arg = args.getSubTree(i);
104       if (arg.getSize() == 2 && arg.getSubTree(0).equals("monolog-conf-file")) {
105         String JavaDoc monologConfFile = arg.getSubTree(1).toString();
106         if (Monolog.monologFactory == Monolog.getDefaultMonologFactory()) {
107           Monolog.getMonologFactory(monologConfFile);
108         }
109       }
110     }
111   }
112
113   /**
114    * Calls the overriden method and then sets the logger and logger factory of
115    * the user component encapsulated in this component (if there is one).
116    *
117    * @return <tt>true</tt> if the execution state has changed, or <tt>false</tt>
118    * if it had already the {@link #STARTED STARTED} value.
119    * @throws IllegalLifeCycleException if a problem occurs.
120    */

121
122   public boolean setFcStarted () throws IllegalLifeCycleException {
123     boolean result = _super_setFcStarted();
124     try {
125       if (Monolog.monologFactory == Monolog.getDefaultMonologFactory()) {
126         Monolog.initialize();
127       }
128       StringBuffer JavaDoc path = new StringBuffer JavaDoc();
129       Util.toString(_this_weaveableC, path);
130       String JavaDoc s = path.toString().substring(1).replace('/', '.');
131       MonologFactory mf = Monolog.monologFactory;
132       Logger logger = Monolog.monologFactory.getLogger(s);
133       Object JavaDoc content = _this_weaveableC.getFcInterface("/content");
134       if (content instanceof Loggable) {
135         ((Loggable)content).setLogger(logger);
136         ((Loggable)content).setLoggerFactory(mf);
137       }
138       if (content instanceof ContentBindingController) {
139         ContentBindingController bc = (ContentBindingController)content;
140         // final static constants must be avoided in mixins
141
bc.bindFcContent("logger", logger);
142         bc.bindFcContent("monolog-factory", mf);
143       } else if (content instanceof BindingController) {
144         BindingController bc = (BindingController)content;
145         // final static constants must be avoided in mixins
146
bc.bindFc("logger", logger);
147         bc.bindFc("monolog-factory", mf);
148       }
149     } catch (Exception JavaDoc ignored) {
150     }
151     return result;
152   }
153
154   // -------------------------------------------------------------------------
155
// Fields and methods required by the mixin class in the base class
156
// -------------------------------------------------------------------------
157

158   /**
159    * The <tt>weaveableC</tt> field required by this mixin. This field is
160    * supposed to reference the {@link Component} interface of the component to
161    * which this controller object belongs.
162    */

163
164   public Component _this_weaveableC;
165
166   /**
167    * The {@link Initializable#initialize initialize} method overriden by this
168    * mixin.
169    *
170    * @param args the arguments to be used to initialize this object. The format
171    * of these arguments depends on the class of this object.
172    * @throws Exception if a problem occurs during the object initialization.
173    */

174
175   public abstract void _super_initialize (final Tree args) throws Exception JavaDoc;
176
177   /**
178    * The {@link LifeCycleCoordinator#setFcStarted setFcStarted} method overriden
179    * by this mixin.
180    *
181    * @return <tt>true</tt> if the execution state has changed, or <tt>false</tt>
182    * if it had already the {@link #STARTED STARTED} value.
183    * @throws IllegalLifeCycleException if a problem occurs.
184    */

185
186   public abstract boolean _super_setFcStarted() throws IllegalLifeCycleException;
187 }
188
Popular Tags