1 13 package org.jahia.content; 14 15 import java.lang.reflect.InvocationTargetException ; 16 import java.util.HashMap ; 17 import java.util.Map ; 18 19 import org.jahia.services.version.ContentObjectEntryState; 20 import org.jahia.services.version.EntryLoadRequest; 21 import java.io.Serializable ; 22 23 28 public abstract class ContentDefinition implements Serializable { 29 30 private static org.apache.log4j.Logger logger = 31 org.apache.log4j.Logger.getLogger(ContentDefinition.class); 32 33 private static Map keyTypeClassNames = new HashMap (); 34 private ObjectKey objectKey; 35 36 protected ContentDefinition(ObjectKey objectKey) { 37 this.objectKey = objectKey; 38 } 39 40 43 protected ContentDefinition() { 44 } 45 46 52 protected void setObjectKey(ObjectKey objectKey) { 53 this.objectKey = objectKey; 54 } 55 56 69 public static void registerType(String type, String className) { 70 keyTypeClassNames.put ( type, className ); 71 } 72 73 77 public static void unregisterType(String type) { 78 keyTypeClassNames.remove(type); 79 } 80 81 86 public ObjectKey getObjectKey() { 87 return objectKey; 88 } 89 90 104 public static ContentDefinition getInstance (ObjectKey objectKey) 105 throws ClassNotFoundException { 106 107 ContentDefinition contentDefinition = null; 108 String type = objectKey.getType(); 109 String idStr = objectKey.getIDInType(); 110 if (!keyTypeClassNames.containsKey(type)) { 111 throw new ClassNotFoundException ("No class defined for type [" + 112 type + "]"); 113 } 114 try { 115 Class childClass = Class.forName( (String ) keyTypeClassNames. 116 get(type)); 117 Class [] childClassParameters = new Class [1]; 118 childClassParameters[0] = String .class; 119 java.lang.reflect.Method childClassMethod = childClass. 120 getMethod("getChildInstance", childClassParameters); 121 Object [] args = new Object [1]; 122 args[0] = idStr; 123 contentDefinition = (ContentDefinition) childClassMethod.invoke(null, args); 124 } catch (ClassNotFoundException cnfe) { 125 logger.error("Error while creating instance of object " + 126 objectKey, cnfe); 127 } catch (NoSuchMethodException nsme) { 128 logger.error("Error while creating instance of object " + 129 objectKey, nsme); 130 } catch (SecurityException se) { 131 logger.error("Error while creating instance of object " + 132 objectKey, se); 133 } catch (IllegalAccessException iae) { 134 logger.error("Error while creating instance of object " + 135 objectKey, iae); 136 } catch (IllegalArgumentException iae2) { 137 logger.error("Error while creating instance of object " + 138 objectKey, iae2); 139 } catch (InvocationTargetException ite) { 140 logger.error("Error while creating instance of object " + 141 objectKey, ite); 142 logger.error( 143 "Error while creating instance of object " + objectKey + 144 ", target exception=" 145 , ite.getTargetException()); 146 } 147 return contentDefinition; 148 } 149 150 159 public abstract String getTitle(ContentObject contentObject, 160 ContentObjectEntryState entryState) 161 throws ClassNotFoundException ; 162 163 167 public abstract String getName(); 168 169 178 public static String getObjectTitle(ContentObject contentObject, 179 ContentObjectEntryState entryState) 180 throws ClassNotFoundException { 181 182 String title = ""; 183 try { 184 ContentDefinition definition = 185 ContentDefinition.getInstance( 186 contentObject.getDefinitionKey(new EntryLoadRequest(entryState))); 187 title = definition.getTitle(contentObject,entryState); 188 } catch ( Throwable t ){ 189 190 } 191 return title; 192 } 193 } 194 | Popular Tags |