1 package org.jahia.content; 2 3 import java.util.Map ; 4 import java.util.HashMap ; 5 import java.lang.reflect.InvocationTargetException ; 6 import java.io.Serializable ; 7 8 18 19 public abstract class JahiaObject implements Serializable { 20 21 private static org.apache.log4j.Logger logger = 22 org.apache.log4j.Logger.getLogger(JahiaObject.class); 23 24 private static Map keyTypeClassNames = new HashMap (); 25 26 private ObjectKey objectKey; 27 28 33 public JahiaObject(ObjectKey objectKey) { 34 this.objectKey = objectKey; 35 } 36 37 40 protected JahiaObject() { 41 } 42 43 56 public static void registerType(String type, String className) { 57 logger.debug("Registering type [" + type + "] with class name [" + 58 className + "]"); 59 keyTypeClassNames.put ( type, className); 60 } 61 62 66 public static void unregisterType(String type) { 67 keyTypeClassNames.remove(type); 68 } 69 70 82 public static JahiaObject getInstance (ObjectKey objectKey) 83 throws ClassNotFoundException { 84 JahiaObject resultObject = null; 85 String type = objectKey.getType(); 86 String idStr = objectKey.getIDInType(); 87 if (!keyTypeClassNames.containsKey(type)) { 88 throw new ClassNotFoundException ("No class defined for type [" + 89 type + "]"); 90 } 91 try { 92 Class childClass = Class.forName( (String ) keyTypeClassNames. 93 get(type)); 94 Class [] childClassParameters = new Class [1]; 95 childClassParameters[0] = String .class; 96 java.lang.reflect.Method childClassMethod = childClass. 97 getMethod("getChildInstance", childClassParameters); 98 Object [] args = new Object [1]; 99 args[0] = idStr; 100 resultObject = (JahiaObject) childClassMethod.invoke(null, args); 101 } catch (ClassNotFoundException cnfe) { 102 logger.error("Error while creating instance of object " + 103 objectKey, cnfe); 104 } catch (NoSuchMethodException nsme) { 105 logger.error("Error while creating instance of object " + 106 objectKey, nsme); 107 } catch (SecurityException se) { 108 logger.error("Error while creating instance of object " + 109 objectKey, se); 110 } catch (IllegalAccessException iae) { 111 logger.error("Error while creating instance of object " + 112 objectKey, iae); 113 } catch (IllegalArgumentException iae2) { 114 logger.error("Error while creating instance of object " + 115 objectKey, iae2); 116 } catch (InvocationTargetException ite) { 117 logger.error("Error while creating instance of object " + 118 objectKey, ite); 119 logger.error( 120 "Error while creating instance of object " + objectKey + 121 ", target exception=" 122 , ite.getTargetException()); 123 } 124 return resultObject; 125 } 126 127 132 public ObjectKey getObjectKey() { 133 return objectKey; 134 } 135 136 } 137 | Popular Tags |