KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.FileWriter JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23 import java.lang.reflect.Constructor JavaDoc;
24 import java.lang.reflect.Field JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.lang.reflect.Modifier JavaDoc;
27
28 import org.objectweb.kilim.KilimException;
29 import org.objectweb.kilim.model.services.DefaultRuntimeClassLoader;
30
31 /**
32  * This mapper
33  * @author horn
34  */

35 public class JavaRuntimeMapper implements Mapper {
36     PrintWriter JavaDoc pW=null;
37     FileWriter JavaDoc fW = null;
38     
39     /**
40      * @see org.objectweb.kilim.model.mapping.Mapper#enterContext(Object). This method is empty.
41      */

42     public void enterContext(MappingContext aContext) throws KilimException { }
43     
44     /**
45      * @see org.objectweb.kilim.model.mapping.Mapper#leaveContext(). This method is empty.
46      */

47     public void leaveContext(MappingContext aContext) throws KilimException { }
48
49     /**
50      * @see org.objectweb.kilim.model.mapping.KilimMapper#getGetterValue(Object, boolean, String, MappingContext). This method returns
51      * the value of the field "filedName" of the class (if isStatic is true) or object (if isStatic is false) referenced by aSupport. The KilimException
52      * is here a wrapper to all exceptions generated by the java reflexive API when trying to perform the task.
53      */

54     public Object JavaDoc getGetterValue (Object JavaDoc aSupport, boolean isStatic, String JavaDoc fieldName, MappingContext aContext) throws KilimException {
55         
56         Class JavaDoc clazz = null;
57         if (aSupport instanceof Class JavaDoc && isStatic) {
58             clazz = (Class JavaDoc) aSupport;
59         } else {
60             clazz = aSupport.getClass();
61         }
62         
63         Field JavaDoc field = null;
64         try {
65             field = clazz.getField(fieldName);
66         } catch (Exception JavaDoc exc) {
67             throw new KilimException(exc);
68         }
69         
70         if (field == null) {
71             throw new KilimException("attempt to get the value of an unknown field " + fieldName + " in setter of " + clazz.getName());
72         }
73         
74         boolean jStatic = (field.getModifiers() & Modifier.STATIC) != 0;
75         if (isStatic && !jStatic) {
76             throw new KilimException("attempt to acces as static a non static field " + fieldName + " in " + clazz.getName());
77         }
78         
79         if (!isStatic && jStatic) {
80             throw new KilimException("attempt to acces as non static a static field " + fieldName + " in " + clazz.getName());
81         }
82         
83         Object JavaDoc resultValue = null;
84         try {
85             resultValue = field.get(aSupport);
86         } catch (Exception JavaDoc ex) {
87             throw new KilimException(ex);
88         }
89             
90         return resultValue;
91     }
92     
93     /**
94      * @see org.objectweb.kilim.model.mapping.KilimMapper#executeSetter(Object, boolean, String, Object, MappingContext). This method sets
95      * a value in the field "filedName" of the class (if isStatic is true) or object (if isStatic is false) referenced by aSupport. The KilimException
96      * is here a wrapper to all exceptions generated by the java reflexive API when trying to perform the task.
97      */

98     public void executeSetter(Object JavaDoc aSupport, boolean isStatic, String JavaDoc fieldName, Object JavaDoc toBeSet, MappingContext aContext) throws KilimException {
99         Class JavaDoc clazz = null;
100         if (aSupport instanceof Class JavaDoc && isStatic) {
101             clazz = (Class JavaDoc) aSupport;
102         } else {
103             clazz = aSupport.getClass();
104         }
105
106         Field JavaDoc field = null;
107         try {
108             field = clazz.getField(fieldName);
109         } catch (Exception JavaDoc exc) {
110             throw new KilimException(exc);
111         }
112         
113         if (field == null) {
114             throw new KilimException("attempt to access an unknown field " + fieldName + " in setter of " + clazz.getName());
115         }
116         
117         boolean jStatic = (field.getModifiers() & Modifier.STATIC) != 0;
118         if (isStatic && !jStatic) {
119             throw new KilimException("attempt to acces as static a non static field " + fieldName + " in " + clazz.getName());
120         }
121         
122         if (!isStatic && jStatic) {
123             throw new KilimException("attempt to acces as non static a static field " + fieldName + " in " + clazz.getName());
124         }
125             
126             if (toBeSet == null) {
127                 throw new KilimException("attempt to set a non initialized value to field " + fieldName + " in " + clazz.getName());
128             }
129             
130         try {
131             field.set(aSupport, toBeSet);
132         } catch (Exception JavaDoc exc) {
133             throw new KilimException(exc);
134         }
135     }
136
137     /**
138      * @see org.objectweb.kilim.model.mapping.KilimMapper#getMethodValue(Object, boolean, String, Object[], String[], MappingContext). This method returns
139      * the value returned by the execution of the method "methodName" of the class (if isStatic is true) or object (if isStatic is false) referenced by aSupport.
140      * The KilimException is here a wrapper to all exceptions generated by the java reflexive API when trying to perform the task.
141      */

142     public Object JavaDoc getMethodValue(Object JavaDoc aSupport, boolean isStatic, String JavaDoc aMethodName, Object JavaDoc[] paramObjects, String JavaDoc[] typeNames, MappingContext aContext) throws KilimException {
143     
144         if (aSupport == null) {
145             throw new KilimException("attempt to invoke method \"" + aMethodName + "()\" on a null target.");
146         }
147
148         Class JavaDoc clazz = isStatic ? (Class JavaDoc) aSupport : aSupport.getClass();
149
150         int paramNumber = paramObjects.length;
151         Class JavaDoc[] paramTypes = new Class JavaDoc[paramNumber];
152         for (int i = 0; i < paramNumber; i++) {
153             paramTypes[i] = DefaultRuntimeClassLoader.instance.getClass(typeNames[i]);
154         }
155
156         Method JavaDoc method = null;
157         try {
158             method = clazz.getMethod(aMethodName, paramTypes);
159         } catch (Exception JavaDoc exc) {
160             throw new KilimException(exc);
161         }
162
163         if (method == null) {
164             throw new KilimException("attempt to acces an unknown or ill declared method " + aMethodName + " in class " + clazz.getName());
165         }
166
167         boolean jStatic = (method.getModifiers() & Modifier.STATIC) != 0;
168         if (isStatic && !jStatic) {
169             throw new KilimException("attempt to acces a non static method as static " + aMethodName + " in class " + clazz.getName());
170         }
171
172         if (!isStatic && jStatic) {
173             throw new KilimException("attempt to acces a static method as non static " + aMethodName + " in class " + clazz.getName());
174         }
175
176         Object JavaDoc resultValue = null;
177         try {
178             resultValue = method.invoke(aSupport, paramObjects);
179         } catch (Exception JavaDoc e) {
180             e.printStackTrace();
181             StringBuffer JavaDoc buff = new StringBuffer JavaDoc("exception while calling :\n\"" + aSupport + "\"[" + aSupport.getClass() + "]." + aMethodName + "(");
182             for (int i = 0; i < paramObjects.length; i++) {
183                 buff.append("\"");
184                 if (paramObjects[i] != null) {
185                     buff.append(paramObjects[i].toString());
186                     buff.append("\"[");
187                     buff.append(paramObjects[i].getClass());
188                     buff.append("]");
189                 } else {
190                     buff.append("\"null\" [unknown]");
191                 }
192                 if (i != 0) {
193                     buff.append(",");
194                 }
195             }
196             buff.append(").");
197             throw new KilimException(buff.toString());
198         }
199
200         return resultValue;
201     }
202     
203     /**
204      * @see org.objectweb.kilim.model.mapping.KilimMapper#executeMethod(Object, boolean, String, Object[], String[], MappingContext)
205      */

206     public void executeMethod(Object JavaDoc aSupport, boolean isStatic, String JavaDoc aMethodName, Object JavaDoc[] paramObjects, String JavaDoc[] typeNames, MappingContext aContext) throws KilimException {
207         getMethodValue(aSupport, isStatic, aMethodName, paramObjects, typeNames, aContext);
208     }
209             
210     /**
211      * @see org.objectweb.kilim.model.mapping.KilimMapper#getConstructorValue(Class, Object[], String[], MappingContext)
212      */

213     public Object JavaDoc getConstructorValue(Class JavaDoc aClass, Object JavaDoc[] paramObjects, String JavaDoc[] typeNames, MappingContext aContext) throws KilimException {
214         
215         int paramNumber = paramObjects.length;
216         Class JavaDoc[] paramTypes = new Class JavaDoc[paramNumber];
217
218         for (int i = 0; i < paramNumber; i++) {
219             paramTypes[i] = DefaultRuntimeClassLoader.instance.getClass(typeNames[i]);
220         }
221
222         Constructor JavaDoc method = null;
223         try {
224             method = aClass.getConstructor(paramTypes);
225         } catch (Exception JavaDoc exc) {
226             throw new KilimException(exc);
227         }
228
229         if (method == null) {
230             throw new KilimException("attempt to use an unknown constructor in class " + aClass);
231         }
232
233         Object JavaDoc resultValue = null;
234         try {
235             resultValue = method.newInstance(paramObjects);
236         } catch (Exception JavaDoc e) {
237             e.printStackTrace();
238             StringBuffer JavaDoc buff = new StringBuffer JavaDoc("exception while calling constructor " + aClass.getName() + "(");
239             for (int i = 0; i < paramObjects.length; i++) {
240                 if (paramObjects[i] != null) {
241                     buff.append(paramObjects[i].toString());
242                     buff.append("\"[");
243                     buff.append(paramObjects[i].getClass());
244                     buff.append("]");
245                 } else {
246                     buff.append("\"null\" [unknown]");
247                 }
248                 buff.append(").");
249                 throw new KilimException(buff.toString());
250             }
251         }
252
253         return resultValue;
254     }
255     
256     /**
257      * @see org.objectweb.kilim.model.mapping.KilimMapper#executeConstructor(Class, Object[], String[], MappingContext)
258      */

259     public void executeConstructor(Class JavaDoc aClass, Object JavaDoc[] paramObjects, String JavaDoc[] typeNames, MappingContext aContext) throws KilimException {
260         getConstructorValue(aClass, paramObjects, typeNames, aContext);
261     }
262             
263     /**
264      * @see org.objectweb.kilim.model.mapping.KilimMapper#getExternalValue(Object, MappingContext)
265      */

266     public Object JavaDoc getExternalValue(Object JavaDoc value, MappingContext aContext) {
267         return value;
268     }
269     
270     /**
271      * @see org.objectweb.kilim.model.mapping.KilimMapper#getPropertyValue(Object, MappingContext)
272      */

273     public Object JavaDoc getPropertyValue(Object JavaDoc aValue, MappingContext aContext) {
274             return aValue;
275     }
276         
277     /**
278      * @see org.objectweb.kilim.model.mapping.KilimMapper#getClassValue(String, MappingContext)
279      */

280     public Object JavaDoc getClassValue(String JavaDoc aClassName, MappingContext aContext) throws KilimException {
281         return DefaultRuntimeClassLoader.instance.getClass(aClassName);
282     }
283     
284     /**
285      * @see org.objectweb.kilim.model.mapping.KilimMapper#getEventSourceValue(MappingContext)
286      */

287     public Object JavaDoc getEventSourceValue(MappingContext aContext) throws KilimException {
288         throw new KilimException("attempt to get a value directly from an event source");
289     }
290     
291     /**
292      * @see org.objectweb.kilim.model.mapping.KilimMapper#getNullElementValue(MappingContext)
293      */

294     public Object JavaDoc getNullElementValue(MappingContext aContext) {
295         return null;
296     }
297
298     /**
299      * @see org.objectweb.kilim.model.mapping.KilimMapper#executeNullElement(MappingContext)
300      */

301     public void executeNullElement(MappingContext aContext) { }
302 }
Popular Tags