KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 /**
27  * @author Vincent Jassogne
28  */

29 public class JAXBBeanFactory implements BeanFactoryIF {
30   private static final Log log = LogFactory.getLog(JAXBBeanFactory.class);
31
32   /**
33    * Creat a bean implementation of a jaxb 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 JavaDoc createBean(Object JavaDoc srcObj, Class JavaDoc srcObjClass, String JavaDoc beanId) {
44     if (log.isDebugEnabled()) {
45       log.debug("createBean(Object, Class, String) - start [" + beanId +"]");
46     }
47
48     int indexOf = beanId.indexOf('$');
49     if (indexOf > 0) {
50       beanId = beanId.substring(0, indexOf) + beanId.substring(indexOf + 1);
51       if (log.isDebugEnabled()) {
52         log.debug("createBean(Object, Class, String) - HAS BEEN CHANGED TO ["+ beanId + "]");
53       }
54     }
55     try {
56       Class JavaDoc objectFactory = Class.forName(beanId.substring(0, beanId.lastIndexOf(".")) + ".ObjectFactory");
57       Object JavaDoc factory = objectFactory.newInstance();
58       Method JavaDoc method = objectFactory.getMethod("create" + beanId.substring(beanId.lastIndexOf(".") + 1), new Class JavaDoc[] {});
59       Object JavaDoc returnObject = method.invoke(factory, new Object JavaDoc[] {});
60       if (log.isDebugEnabled()) {
61         log.debug("createBean(Object, Class, String) - end ["
62             + returnObject.getClass().getName() + "]");
63       }
64       return returnObject;
65     } catch (ClassNotFoundException JavaDoc e) {
66       log.error("createBean(Object, Class, String)", e);
67         throw new MappingException("Bean of type " + beanId + " not found.", e);
68     } catch (Exception JavaDoc e) {
69       log.error("createBean(Object, Class, String)", e);
70       throw new MappingException(e);
71     }
72   }
73 }
Popular Tags