1 package org.jahia.data.beans; 2 3 import java.util.Map ; 4 import java.util.HashMap ; 5 import org.jahia.content.JahiaObject; 6 import java.lang.reflect.InvocationTargetException ; 7 import org.jahia.params.ParamBean; 8 9 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 jahiaObjectToBeanClassNames = new HashMap (); 28 29 42 public static void registerType(String jahiaObjectClassName, String 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 53 public static void unregisterType(String jahiaObjectClassName) { 54 jahiaObjectToBeanClassNames.remove(jahiaObjectClassName); 55 } 56 57 67 public static AbstractJahiaObjectBean getInstance (JahiaObject jahiaObject, ParamBean paramBean) 68 throws ClassNotFoundException { 69 AbstractJahiaObjectBean resultObject = null; 70 if (!jahiaObjectToBeanClassNames.containsKey(jahiaObject.getClass().getName())) { 71 throw new ClassNotFoundException ("No class defined for JahiaObject class name [" + 72 jahiaObject.getClass().getName() + "]"); 73 } 74 try { 75 Class childClass = Class.forName( (String ) jahiaObjectToBeanClassNames. 76 get(jahiaObject.getClass().getName())); 77 Class [] childClassParameters = new Class [2]; 78 childClassParameters[0] = JahiaObject.class; 79 childClassParameters[1] = ParamBean.class; 80 java.lang.reflect.Method childClassMethod = childClass. 81 getMethod("getChildInstance", childClassParameters); 82 Object [] args = new Object [2]; 83 args[0] = jahiaObject; 84 args[1] = paramBean; 85 resultObject = (AbstractJahiaObjectBean) childClassMethod.invoke(null, args); 86 } catch (ClassNotFoundException cnfe) { 87 logger.error("Error while creating instance of object " + 88 jahiaObject, cnfe); 89 } catch (NoSuchMethodException nsme) { 90 logger.error("Error while creating instance of object " + 91 jahiaObject, nsme); 92 } catch (SecurityException se) { 93 logger.error("Error while creating instance of object " + 94 jahiaObject, se); 95 } catch (IllegalAccessException iae) { 96 logger.error("Error while creating instance of object " + 97 jahiaObject, iae); 98 } catch (IllegalArgumentException iae2) { 99 logger.error("Error while creating instance of object " + 100 jahiaObject, iae2); 101 } catch (InvocationTargetException 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 |