1 40 41 42 package org.jahia.content; 43 44 import java.io.Serializable ; 45 import java.lang.reflect.InvocationTargetException ; 46 import java.util.HashMap ; 47 import java.util.Map ; 48 49 64 public abstract class ObjectKey implements 65 ObjectKeyInterface, 66 Serializable , Comparable { 67 68 private static org.apache.log4j.Logger logger = 69 org.apache.log4j.Logger.getLogger(ObjectKey.class); 70 71 private static Map keyTypeClassNames = new HashMap (); 72 73 protected String key; 74 public static final String OBJECT_TYPE = "object"; 75 private String type = OBJECT_TYPE; 76 private String IDInType; 77 private int idInType = -1; 78 81 private ObjectKey() { 82 } 83 84 90 protected ObjectKey(String aType, 91 String anIDInType) { 92 this.type = aType; 93 this.IDInType = anIDInType; 94 StringBuffer buf = new StringBuffer (50); 95 buf.append(aType); 96 buf.append(KEY_SEPARATOR); 97 buf.append(anIDInType); 98 this.key = buf.toString(); 99 try { 100 idInType = Integer.parseInt(anIDInType); 101 } catch (NumberFormatException e) { 102 idInType = -1; 103 } 104 } 105 106 119 public static void registerType(String type, String className) { 120 logger.debug("Registering type [" + type + "] with class name [" + 121 className + "]"); 122 keyTypeClassNames.put ( type, className ); 123 } 124 125 129 public static void unregisterType(String type) { 130 keyTypeClassNames.remove(type); 131 } 132 133 146 public static ObjectKey getInstance (String key) 147 throws ClassNotFoundException { 148 ObjectKey resultKey = null; 149 int separatorPos = key.indexOf(KEY_SEPARATOR); 150 if (separatorPos > 0) { 151 String type = key.substring(0, separatorPos); 152 String idStr = key.substring(separatorPos + KEY_SEPARATOR.length()); 153 if (!keyTypeClassNames.containsKey(type)) { 154 throw new ClassNotFoundException ("No class defined for type [" + 155 type + "]"); 156 } 157 try { 158 Class childClass = Class.forName( (String ) keyTypeClassNames. 159 get(type)); 160 Class [] childClassParameters = new Class [1]; 161 childClassParameters[0] = String .class; 162 java.lang.reflect.Method childClassMethod = childClass. 163 getMethod("getChildInstance", childClassParameters); 164 Object [] args = new Object [1]; 165 args[0] = idStr; 166 resultKey = (ObjectKey) childClassMethod.invoke(null, args); 167 } catch (ClassNotFoundException cnfe) { 168 logger.error("Error while creating instance of object key " + 169 key, cnfe); 170 } catch (NoSuchMethodException nsme) { 171 logger.error("Error while creating instance of object key " + 172 key, nsme); 173 } catch (SecurityException se) { 174 logger.error("Error while creating instance of object key " + 175 key, se); 176 } catch (IllegalAccessException iae) { 177 logger.error("Error while creating instance of object key " + 178 key, iae); 179 } catch (IllegalArgumentException iae2) { 180 logger.error("Error while creating instance of object key " + 181 key, iae2); 182 } catch (InvocationTargetException ite) { 183 logger.error("Error while creating instance of object key " + 184 key, ite); 185 logger.error( 186 "Error while creating instance of object key, target exception " + 187 key, ite.getTargetException()); 188 } 189 } 190 return resultKey; 191 } 192 193 194 195 public String getKey(){ 196 return key; 197 } 198 199 public String getType(){ 200 return this.type; 201 } 202 203 public String getIDInType() { 204 return this.IDInType; 205 } 206 207 public int getIdInType() { 208 if(idInType==-1) { 209 try { 210 idInType = Integer.parseInt(IDInType); 211 } catch (NumberFormatException e) { 212 idInType = -1; 213 } 214 } 215 return idInType; 216 } 217 218 226 public boolean equals(Object obj) { 227 if (obj instanceof ObjectKey) { 229 ObjectKey destObj = (ObjectKey) obj; 230 if (this.key.equals(destObj.getKey())) { 231 return true; 232 } else { 233 return false; 234 } 235 } else { 236 return false; 237 } 238 } 239 240 250 public int compareTo(Object o) throws ClassCastException { 251 ObjectKey destObjectKey = (ObjectKey) o; 252 return this.key.compareTo(destObjectKey.getKey()); 253 } 254 255 260 public int hashCode() { 261 return key.hashCode(); 263 } 264 265 270 public String toString() { 271 return key; 272 } 273 274 } 275 | Popular Tags |