KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > data > beans > AbstractJahiaObjectBean


1 package org.jahia.data.beans;
2
3 import java.util.Map JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import org.jahia.content.JahiaObject;
6 import java.lang.reflect.InvocationTargetException JavaDoc;
7 import org.jahia.params.ParamBean;
8
9 /**
10  * <p>Title: Abstract class that is the parent class of all bean wrappers
11  * for JahiaObject classes.</p>
12  * <p>Description: This class is the parent class for all the JavaBean
13  * compliant wrappers that are part of the new template tools available since
14  * Jahia 4. Using this common abstract class we can instantiate any wrapper
15  * by using the corresponding JahiaObject and ParamBean objects.</p>
16  * <p>Copyright: Copyright (c) 2002</p>
17  * <p>Company: Jahia Ltd</p>
18  * @author Serge Huber
19  * @version 1.0
20  */

21
22 public abstract class AbstractJahiaObjectBean {
23
24     private static org.apache.log4j.Logger logger =
25         org.apache.log4j.Logger.getLogger(AbstractJahiaObjectBean.class);
26
27     private static Map JavaDoc jahiaObjectToBeanClassNames = new HashMap JavaDoc();
28
29     /**
30      * Registers a new mapping for a sub class of JahiaObject in the factory
31      * hash table. Use this if you want to be able to create instance of the
32      * new class by using a JahiaObject. The new sub class MUST
33      * implement a static method with the following signature :
34      * public static AbstractJahiaObjectBean getChildInstance(JahiaObject jahiaObject,
35      * ParamBean paramBean);
36      *
37      * @param jahiaObjectClassName the source JahiaObject class name that will
38      * be mapped to the beanClassName
39      * @param beanClassName the target AbstractJahiaObjectBean class name that
40      * will be mapped to.
41      */

42     public static void registerType(String JavaDoc jahiaObjectClassName, String JavaDoc beanClassName) {
43         logger.debug("Registering mapping from JahiaObject class name [" + jahiaObjectClassName + "] to bean class name [" +
44                      beanClassName + "]");
45         jahiaObjectToBeanClassNames.put ( jahiaObjectClassName, beanClassName);
46     }
47
48     /**
49      * Removes a JahiaObject mapping registration. See the registerType method
50      * for more details
51      * @param jahiaObjectClassName the name of the JahiaObject class name
52      */

53     public static void unregisterType(String JavaDoc jahiaObjectClassName) {
54         jahiaObjectToBeanClassNames.remove(jahiaObjectClassName);
55     }
56
57     /**
58      * Instance generator. Build an instance of the appropriate JavaBean
59      * compliant wrapper class corresponding to the JahiaObject instance and
60      * the current ParamBean that represents the current request.
61      *
62      * @param jahiaObject the JahiaObject child instance for which to create
63      * an AbstractJahiaObjectBean instance.
64      * @throws ClassNotFoundException if no class could be found for the type
65      * passed in the object key
66      */

67     public static AbstractJahiaObjectBean getInstance (JahiaObject jahiaObject, ParamBean paramBean)
68         throws ClassNotFoundException JavaDoc {
69         AbstractJahiaObjectBean resultObject = null;
70         if (!jahiaObjectToBeanClassNames.containsKey(jahiaObject.getClass().getName())) {
71             throw new ClassNotFoundException JavaDoc("No class defined for JahiaObject class name [" +
72                                              jahiaObject.getClass().getName() + "]");
73         }
74         try {
75             Class JavaDoc childClass = Class.forName( (String JavaDoc) jahiaObjectToBeanClassNames.
76                                              get(jahiaObject.getClass().getName()));
77             Class JavaDoc[] childClassParameters = new Class JavaDoc[2];
78             childClassParameters[0] = JahiaObject.class;
79             childClassParameters[1] = ParamBean.class;
80             java.lang.reflect.Method JavaDoc childClassMethod = childClass.
81                 getMethod("getChildInstance", childClassParameters);
82             Object JavaDoc[] args = new Object JavaDoc[2];
83             args[0] = jahiaObject;
84             args[1] = paramBean;
85             resultObject = (AbstractJahiaObjectBean) childClassMethod.invoke(null, args);
86         } catch (ClassNotFoundException JavaDoc cnfe) {
87             logger.error("Error while creating instance of object " +
88                          jahiaObject, cnfe);
89         } catch (NoSuchMethodException JavaDoc nsme) {
90             logger.error("Error while creating instance of object " +
91                          jahiaObject, nsme);
92         } catch (SecurityException JavaDoc se) {
93             logger.error("Error while creating instance of object " +
94                          jahiaObject, se);
95         } catch (IllegalAccessException JavaDoc iae) {
96             logger.error("Error while creating instance of object " +
97                          jahiaObject, iae);
98         } catch (IllegalArgumentException JavaDoc iae2) {
99             logger.error("Error while creating instance of object " +
100                          jahiaObject, iae2);
101         } catch (InvocationTargetException JavaDoc ite) {
102             logger.error("Error while creating instance of object " +
103                          jahiaObject, ite);
104             logger.error(
105                 "Error while creating instance of object " + jahiaObject +
106                 ", target exception="
107                 , ite.getTargetException());
108         }
109         return resultObject;
110     }
111
112 }
Popular Tags