KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > dozer > util > mapping > factory > XMLBeanFactory


1 /*
2  * Copyright 2005-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc;
19
20 import net.sf.dozer.util.mapping.BeanFactoryIF;
21 import net.sf.dozer.util.mapping.MappingException;
22 /**
23  * @author garsombke.franz
24  */

25 public class XMLBeanFactory implements BeanFactoryIF {
26   private static Class JavaDoc[] emptyArglist = new Class JavaDoc[0];
27   /**
28    * Creat a bean implementation of a xml bean interface.
29    *
30    * @param srcObj
31    * The source object
32    * @param srcObjClass
33    * The source object class
34    * @param beanId
35    * the name of the destination interface class
36    * @return A implementation of the destination interface
37    */

38   public Object JavaDoc createBean(Object JavaDoc srcObj, Class JavaDoc srcObjClass, String JavaDoc beanId) {
39     Object JavaDoc result = null;
40     try {
41       Class JavaDoc destClass;
42       destClass = Class.forName(beanId);
43       Class JavaDoc[] innerClasses = destClass.getClasses();
44       Class JavaDoc factory = null;
45       for (int i = 0; i < innerClasses.length; i++) {
46         if (innerClasses[i].getName().endsWith("Factory")) {
47           factory = innerClasses[i];
48         }
49       }
50       if (factory == null) {
51         throw new MappingException("Factory class of Bean of type " + beanId + " not found.");
52       }
53       Method JavaDoc newInstance = factory.getMethod("newInstance", emptyArglist);
54       result = newInstance.invoke(null, emptyArglist);
55     } catch (ClassNotFoundException JavaDoc e) {
56       throw new MappingException("Bean of type " + beanId + " not found.", e);
57     } catch (Exception JavaDoc e) {
58       throw new MappingException(e);
59     }
60     return result;
61   }
62 }
Popular Tags