01: /*
02: * Copyright 2005-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package net.sf.dozer.util.mapping.factory;
17:
18: import java.lang.reflect.Method;
19:
20: import net.sf.dozer.util.mapping.BeanFactoryIF;
21: import net.sf.dozer.util.mapping.util.MappingUtils;
22: import net.sf.dozer.util.mapping.util.ReflectionUtils;
23:
24: /**
25: * Public custom bean factory that can be used by applition code when mapping XMLBean data objects
26: *
27: * @author garsombke.franz
28: */
29: public class XMLBeanFactory implements BeanFactoryIF {
30: private static Class[] emptyArglist = new Class[0];
31:
32: /**
33: * Creat a bean implementation of a xml bean interface.
34: *
35: * @param srcObj
36: * The source object
37: * @param srcObjClass
38: * The source object class
39: * @param beanId
40: * the name of the destination interface class
41: * @return A implementation of the destination interface
42: */
43: public Object createBean(Object srcObj, Class srcObjClass,
44: String beanId) {
45: Object result = null;
46: Class destClass;
47: destClass = MappingUtils.loadClass(beanId);
48: Class[] innerClasses = destClass.getClasses();
49: Class factory = null;
50: for (int i = 0; i < innerClasses.length; i++) {
51: if (innerClasses[i].getName().endsWith("Factory")) {
52: factory = innerClasses[i];
53: }
54: }
55: if (factory == null) {
56: MappingUtils
57: .throwMappingException("Factory class of Bean of type "
58: + beanId + " not found.");
59: }
60: Method newInstanceMethod = null;
61: try {
62: newInstanceMethod = ReflectionUtils.getMethod(factory,
63: "newInstance", emptyArglist);
64: } catch (NoSuchMethodException e) {
65: MappingUtils.throwMappingException(e);
66: }
67: result = ReflectionUtils.invoke(newInstanceMethod, null,
68: emptyArglist);
69: return result;
70: }
71: }
|