1 18 19 package org.objectweb.kilim.model.mapping; 20 21 import java.util.HashMap ; 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 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 42 public JavaLoggerMapper(Mapper aMapper) { 43 wrappedMapper = aMapper; 44 } 45 46 49 public void enterContext(MappingContext aContext) throws KilimException { } 50 51 54 public void leaveContext(MappingContext aContext) throws KilimException { } 55 56 59 public Object getGetterValue (Object aSupport, boolean isStatic, String fieldName, MappingContext aContext) throws KilimException { 60 Object 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 75 public void executeSetter(Object aSupport, boolean isStatic, String fieldName, Object 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 89 public Object getMethodValue(Object aSupport, boolean isStatic, String aMethodName, Object [] paramObjects, String [] typeNames, MappingContext aContext) throws KilimException { 90 Object 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 bffr = new StringBuffer (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 108 public void executeMethod(Object aSupport, boolean isStatic, String aMethodName, Object [] paramObjects, String [] 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 bffr = new StringBuffer (((isStatic) ? "[static] " : "") + normalizeToString(aSupport) + "." + aMethodName); 115 addParameters(bffr , paramObjects , typeNames); 116 bffr.append(";"); 117 logger.log(BasicLevel.INFO , bffr.toString()); 118 } 119 } 120 121 124 public Object getConstructorValue(Class aClass, Object [] paramObjects, String [] typeNames, MappingContext aContext) throws KilimException { 125 Object 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 bffr = new StringBuffer (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 143 public void executeConstructor(Class aClass, Object [] paramObjects, String [] 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 bffr = new StringBuffer ("new " + aClass.getName()); 150 addParameters(bffr , paramObjects , typeNames); 151 bffr.append(";"); 152 logger.log(BasicLevel.INFO , bffr.toString()); 153 } 154 } 155 156 159 public Object getExternalValue(Object aValue, MappingContext aContext) throws KilimException { 160 Object resultValue = null; 161 if (wrappedMapper != null) { 162 resultValue = wrappedMapper.getExternalValue(aValue, aContext); 163 } 164 return resultValue; 165 } 166 167 170 public Object getPropertyValue(Object aValue, MappingContext aContext) throws KilimException { 171 Object resultValue = null; 172 if (wrappedMapper != null) { 173 resultValue = wrappedMapper.getPropertyValue(aValue, aContext); 174 } 175 return resultValue; 176 } 177 178 181 public Object getClassValue(String aClassName, MappingContext aContext) throws KilimException { 182 Object resultValue = null; 183 if (wrappedMapper != null) { 184 resultValue = wrappedMapper.getClassValue(aClassName, aContext); 185 } 186 return resultValue; 187 } 188 189 192 public Object getEventSourceValue(MappingContext aContext) throws KilimException { 193 Object resultValue = null; 194 if (wrappedMapper != null) { 195 resultValue = wrappedMapper.getEventSourceValue(aContext); 196 } 197 return resultValue; 198 } 199 200 203 public Object getNullElementValue(MappingContext aContext) throws KilimException { 204 Object resultValue = null; 205 if (wrappedMapper != null) { 206 resultValue = wrappedMapper.getNullElementValue(aContext); 207 } 208 return resultValue; 209 210 } 211 212 216 public void executeNullElement(MappingContext aContext) { 217 if (wrappedMapper != null) { 218 executeNullElement(aContext); 219 } 220 } 221 222 224 private static final void addParameters(StringBuffer bffr , Object [] paramObjects, String [] 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 normalizeToString(Object obj) { 234 if (obj instanceof Number ) { return obj.toString(); } 235 return getVariableName(obj); 236 } 237 238 private static HashMap variables = new HashMap () , inverse_table = new HashMap (); 239 240 private static String getResultString(Object obj) { 241 String rslt = getVariableName(obj); 242 if (rslt == null) { return ""; } 243 return rslt + " = "; 244 } 245 246 private static String getVariableName(Object obj) { 247 248 if (obj == null) { return null; } 249 250 Object vl = inverse_table.get(obj); 251 if (vl != null) { return (String ) vl; } 252 253 String clss_nm = obj.getClass().getName(); 254 String nm = clss_nm.substring(clss_nm.lastIndexOf('.') + 1); 255 nm = nm.toLowerCase(); 256 Object 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 |