KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > kilim > model > mapping > JavaLoggerMapper


1 /**
2  * Copyright (C) 2002 Kelua SA
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package org.objectweb.kilim.model.mapping;
20
21 import java.util.HashMap JavaDoc;
22
23 import org.objectweb.kilim.KilimException;
24 import org.objectweb.kilim.KilimLoggerFactory;
25 import org.objectweb.util.monolog.api.BasicLevel;
26 import org.objectweb.util.monolog.api.Logger;
27
28 /**
29  * @author horn
30  */

31 public class JavaLoggerMapper implements Mapper {
32     private Mapper wrappedMapper;
33
34     private static boolean lOG_ON = true;
35
36     private static Logger logger = KilimLoggerFactory.getSingleton().getLogger("org.objectweb.kilim.model.mapping");
37     
38     /**
39      * Method JavaLogMapper.
40      * @param aMapper :
41      */

42     public JavaLoggerMapper(Mapper aMapper) {
43         wrappedMapper = aMapper;
44     }
45     
46     /**
47      * @see org.objectweb.kilim.model.mapping.Mapper#enterContext(Object)
48      */

49     public void enterContext(MappingContext aContext) throws KilimException { }
50     
51     /**
52      * @see org.objectweb.kilim.model.mapping.Mapper#leaveContext()
53      */

54     public void leaveContext(MappingContext aContext) throws KilimException { }
55
56     /**
57      * @see org.objectweb.kilim.model.mapping.KilimMapper#getGetterValue(Object, boolean, String, MappingContext)
58      */

59     public Object JavaDoc getGetterValue (Object JavaDoc aSupport, boolean isStatic, String JavaDoc fieldName, MappingContext aContext) throws KilimException {
60         Object JavaDoc resultValue = null;
61         if (wrappedMapper != null) {
62             resultValue = wrappedMapper.getGetterValue (aSupport, isStatic, fieldName, aContext);
63         }
64         
65         if (lOG_ON && logger.isLoggable(BasicLevel.DEBUG)) {
66             logger.log(BasicLevel.INFO , getResultString(resultValue) + ((isStatic) ? "[static] " : "") + normalizeToString(aSupport) + "." + fieldName + ";");
67         }
68             
69         return resultValue;
70     }
71     
72     /**
73      * @see org.objectweb.kilim.model.mapping.KilimMapper#executeSetter(Object, boolean, String, Object, MappingContext)
74      */

75     public void executeSetter(Object JavaDoc aSupport, boolean isStatic, String JavaDoc fieldName, Object JavaDoc toBeSet, MappingContext aContext) throws KilimException {
76
77         if (lOG_ON && logger.isLoggable(BasicLevel.DEBUG)) {
78             logger.log(BasicLevel.INFO , ((isStatic) ? "[static] " : "") + normalizeToString(aSupport) + "." + fieldName + " = " + toBeSet + ";");
79         }
80         
81         if (wrappedMapper != null) {
82             wrappedMapper.executeSetter(aSupport, isStatic, fieldName, toBeSet, aContext);
83         }
84     }
85
86     /**
87      * @see org.objectweb.kilim.model.mapping.KilimMapper#getMethodValue(Object, boolean, String, Object[], String[], MappingContext)
88      */

89     public Object JavaDoc getMethodValue(Object JavaDoc aSupport, boolean isStatic, String JavaDoc aMethodName, Object JavaDoc[] paramObjects, String JavaDoc[] typeNames, MappingContext aContext) throws KilimException {
90         Object JavaDoc resultValue = null;
91         if (wrappedMapper != null) {
92             resultValue = wrappedMapper.getMethodValue (aSupport, isStatic, aMethodName, paramObjects, typeNames, aContext);
93         }
94         
95         if (lOG_ON && logger.isLoggable(BasicLevel.DEBUG)) {
96             StringBuffer JavaDoc bffr = new StringBuffer JavaDoc(getResultString(resultValue) + ((isStatic) ? "[static] " : "") + normalizeToString(aSupport) + "." + aMethodName);
97             addParameters(bffr , paramObjects , typeNames);
98             bffr.append(";");
99             logger.log(BasicLevel.INFO , bffr.toString());
100         }
101         
102         return resultValue;
103     }
104     
105     /**
106      * @see org.objectweb.kilim.model.mapping.KilimMapper#executeMethod(Object, boolean, String, Object[], String[], MappingContext)
107      */

108     public void executeMethod(Object JavaDoc aSupport, boolean isStatic, String JavaDoc aMethodName, Object JavaDoc[] paramObjects, String JavaDoc[] typeNames, MappingContext aContext) throws KilimException {
109         if (wrappedMapper != null) {
110             wrappedMapper.executeMethod(aSupport, isStatic, aMethodName, paramObjects, typeNames, aContext);
111         }
112         
113         if (lOG_ON && logger.isLoggable(BasicLevel.DEBUG)) {
114             StringBuffer JavaDoc bffr = new StringBuffer JavaDoc(((isStatic) ? "[static] " : "") + normalizeToString(aSupport) + "." + aMethodName);
115             addParameters(bffr , paramObjects , typeNames);
116             bffr.append(";");
117             logger.log(BasicLevel.INFO , bffr.toString());
118         }
119     }
120             
121     /**
122      * @see org.objectweb.kilim.model.mapping.KilimMapper#getConstructorValue(Class, Object[], String[], MappingContext)
123      */

124     public Object JavaDoc getConstructorValue(Class JavaDoc aClass, Object JavaDoc[] paramObjects, String JavaDoc[] typeNames, MappingContext aContext) throws KilimException {
125         Object JavaDoc resultValue = null;
126         if (wrappedMapper != null) {
127             resultValue = wrappedMapper.getConstructorValue(aClass, paramObjects, typeNames, aContext);
128         }
129         
130         if (lOG_ON && logger.isLoggable(BasicLevel.DEBUG)) {
131             StringBuffer JavaDoc bffr = new StringBuffer JavaDoc(getVariableName(resultValue) + " = new " + aClass.getName());
132             addParameters(bffr , paramObjects , typeNames);
133             bffr.append(";");
134             logger.log(BasicLevel.INFO , bffr.toString());
135         }
136         
137         return resultValue;
138     }
139     
140     /**
141      * @see org.objectweb.kilim.model.mapping.KilimMapper#executeConstructor(Class, Object[], String[], MappingContext)
142      */

143     public void executeConstructor(Class JavaDoc aClass, Object JavaDoc[] paramObjects, String JavaDoc[] typeNames, MappingContext aContext) throws KilimException {
144         if (wrappedMapper != null) {
145             wrappedMapper.getConstructorValue(aClass, paramObjects, typeNames, aContext);
146         }
147         
148         if (lOG_ON && logger.isLoggable(BasicLevel.DEBUG)) {
149             StringBuffer JavaDoc bffr = new StringBuffer JavaDoc("new " + aClass.getName());
150             addParameters(bffr , paramObjects , typeNames);
151             bffr.append(";");
152             logger.log(BasicLevel.INFO , bffr.toString());
153         }
154     }
155
156     /**
157      * @see org.objectweb.kilim.model.mapping.KilimMapper#getExternalValue(Object, MappingContext)
158      */

159     public Object JavaDoc getExternalValue(Object JavaDoc aValue, MappingContext aContext) throws KilimException {
160         Object JavaDoc resultValue = null;
161         if (wrappedMapper != null) {
162             resultValue = wrappedMapper.getExternalValue(aValue, aContext);
163         }
164         return resultValue;
165     }
166     
167     /**
168      * @see org.objectweb.kilim.model.mapping.KilimMapper#getPropertyValue(Object, MappingContext)
169      */

170     public Object JavaDoc getPropertyValue(Object JavaDoc aValue, MappingContext aContext) throws KilimException {
171         Object JavaDoc resultValue = null;
172         if (wrappedMapper != null) {
173             resultValue = wrappedMapper.getPropertyValue(aValue, aContext);
174         }
175         return resultValue;
176     }
177         
178     /**
179      * @see org.objectweb.kilim.model.mapping.KilimMapper#getClassValue(String, MappingContext)
180      */

181     public Object JavaDoc getClassValue(String JavaDoc aClassName, MappingContext aContext) throws KilimException {
182         Object JavaDoc resultValue = null;
183         if (wrappedMapper != null) {
184             resultValue = wrappedMapper.getClassValue(aClassName, aContext);
185         }
186         return resultValue;
187     }
188     
189     /**
190      * @see org.objectweb.kilim.model.mapping.KilimMapper#getEventSourceValue(MappingContext)
191      */

192     public Object JavaDoc getEventSourceValue(MappingContext aContext) throws KilimException {
193         Object JavaDoc resultValue = null;
194         if (wrappedMapper != null) {
195             resultValue = wrappedMapper.getEventSourceValue(aContext);
196         }
197         return resultValue;
198     }
199     
200     /**
201      * @see org.objectweb.kilim.model.mapping.KilimMapper#getNullElementValue(MappingContext)
202      */

203     public Object JavaDoc getNullElementValue(MappingContext aContext) throws KilimException {
204         Object JavaDoc resultValue = null;
205         if (wrappedMapper != null) {
206             resultValue = wrappedMapper.getNullElementValue(aContext);
207         }
208         return resultValue;
209
210     }
211
212     /**
213      * Method executeNullElement.
214      * @param aContext :
215      */

216     public void executeNullElement(MappingContext aContext) {
217         if (wrappedMapper != null) {
218             executeNullElement(aContext);
219         }
220     }
221     
222     //Logging facilities ------------------------------------------------------------------
223

224     private static final void addParameters(StringBuffer JavaDoc bffr , Object JavaDoc[] paramObjects, String JavaDoc[] typeNames) {
225             bffr.append("(");
226             for (int i = 0 ; i < paramObjects.length ; i++) {
227                 if (i != 0) { bffr.append(" , "); }
228                 bffr.append(normalizeToString(paramObjects[i]));
229             }
230             bffr.append(")");
231     }
232     
233     private static String JavaDoc normalizeToString(Object JavaDoc obj) {
234         if (obj instanceof Number JavaDoc) { return obj.toString(); }
235         return getVariableName(obj);
236     }
237     
238     private static HashMap JavaDoc variables = new HashMap JavaDoc() , inverse_table = new HashMap JavaDoc();
239     
240     private static String JavaDoc getResultString(Object JavaDoc obj) {
241         String JavaDoc rslt = getVariableName(obj);
242         if (rslt == null) { return ""; }
243         return rslt + " = ";
244     }
245     
246     private static String JavaDoc getVariableName(Object JavaDoc obj) {
247         
248         if (obj == null) { return null; }
249         
250         Object JavaDoc vl = inverse_table.get(obj);
251         if (vl != null) { return (String JavaDoc) vl; }
252         
253         String JavaDoc clss_nm = obj.getClass().getName();
254         String JavaDoc nm = clss_nm.substring(clss_nm.lastIndexOf('.') + 1);
255         nm = nm.toLowerCase();
256         Object JavaDoc allrd_thr = variables.get(nm);
257         int nb = 0;
258         while (allrd_thr != null) {
259             allrd_thr = variables.get(nm + (++nb));
260         }
261         if (nb != 0) { nm = nm + nb; }
262         variables.put (nm , obj);
263         inverse_table.put(obj , nm);
264         
265         logger.log(BasicLevel.INFO , "");
266         logger.log(BasicLevel.INFO , obj.getClass().getName() + " " + nm + ";");
267         
268         return nm;
269     }
270 }
Popular Tags