KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > content > JahiaObject


1 package org.jahia.content;
2
3 import java.util.Map JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.lang.reflect.InvocationTargetException JavaDoc;
6 import java.io.Serializable JavaDoc;
7
8 /**
9  * <p>Title: Root class for all Jahia objects that use object keys and that
10  * can be instantiated through those keys.</p>
11  * <p>Description: Objects should derive from this class if they may be
12  * instantiated or referenced through ObjectKey instances. </p>
13  * <p>Copyright: Copyright (c) 2002</p>
14  * <p>Company: Jahia Ltd</p>
15  * @author Serge Huber
16  * @version 1.0
17  */

18
19 public abstract class JahiaObject implements Serializable JavaDoc {
20
21     private static org.apache.log4j.Logger logger =
22         org.apache.log4j.Logger.getLogger(JahiaObject.class);
23
24     private static Map JavaDoc keyTypeClassNames = new HashMap JavaDoc();
25
26     private ObjectKey objectKey;
27
28     /**
29      * Default constructor that sets the object's key.
30      * @param objectKey the object key that uniquely identifies this instance
31      * of JahiaObject.
32      */

33     public JahiaObject(ObjectKey objectKey) {
34         this.objectKey = objectKey;
35     }
36
37     /**
38      * No arg constructor used for serialization compliance.
39      */

40     protected JahiaObject() {
41     }
42
43     /**
44      * Registers a new type for a sub class of JahiaObject in the factory
45      * hash table. Use this if you want to be able to create instance of the
46      * new class by using a complete content object key. The new sub class MUST
47      * implement a static method with the following signature :
48      * public static JahiaObject getChildInstance(String IDInType);
49      *
50      * @param type the name of the type, usually defined as a constant in the
51      * equivalent ObjectKey sub class (such as ContentPageKey)
52      * @param className the name of the class. Can be obtained with a called
53      * similar to JahiaObject.class.getName() (replacing the JahiaObject
54      * with the subclass of course.
55      */

56     public static void registerType(String JavaDoc type, String JavaDoc className) {
57         logger.debug("Registering type [" + type + "] with class name [" +
58                      className + "]");
59         keyTypeClassNames.put ( type, className);
60     }
61
62     /**
63      * Removes a type registration. See the registerType method for more details
64      * @param type the name of the type to unregister
65      */

66     public static void unregisterType(String JavaDoc type) {
67         keyTypeClassNames.remove(type);
68     }
69
70     /**
71      * Instance generator. Build an instance of the appropriate
72      * class corresponding to the ObjectKey passed described.
73      *
74      * @param objectKey an ObjectKey instance for the object we want to retrieve
75      * an instance of.
76      * @returns a JahiaObject sub class instance that corresponds to the given
77      * object key.
78      *
79      * @throws ClassNotFoundException if no class could be found for the type
80      * passed in the object key
81      */

82     public static JahiaObject getInstance (ObjectKey objectKey)
83         throws ClassNotFoundException JavaDoc {
84         JahiaObject resultObject = null;
85         String JavaDoc type = objectKey.getType();
86         String JavaDoc idStr = objectKey.getIDInType();
87         if (!keyTypeClassNames.containsKey(type)) {
88             throw new ClassNotFoundException JavaDoc("No class defined for type [" +
89                                              type + "]");
90         }
91         try {
92             Class JavaDoc childClass = Class.forName( (String JavaDoc) keyTypeClassNames.
93                                              get(type));
94             Class JavaDoc[] childClassParameters = new Class JavaDoc[1];
95             childClassParameters[0] = String JavaDoc.class;
96             java.lang.reflect.Method JavaDoc childClassMethod = childClass.
97                 getMethod("getChildInstance", childClassParameters);
98             Object JavaDoc[] args = new Object JavaDoc[1];
99             args[0] = idStr;
100             resultObject = (JahiaObject) childClassMethod.invoke(null, args);
101         } catch (ClassNotFoundException JavaDoc cnfe) {
102             logger.error("Error while creating instance of object " +
103                          objectKey, cnfe);
104         } catch (NoSuchMethodException JavaDoc nsme) {
105             logger.error("Error while creating instance of object " +
106                          objectKey, nsme);
107         } catch (SecurityException JavaDoc se) {
108             logger.error("Error while creating instance of object " +
109                          objectKey, se);
110         } catch (IllegalAccessException JavaDoc iae) {
111             logger.error("Error while creating instance of object " +
112                          objectKey, iae);
113         } catch (IllegalArgumentException JavaDoc iae2) {
114             logger.error("Error while creating instance of object " +
115                          objectKey, iae2);
116         } catch (InvocationTargetException JavaDoc 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     /**
128      * Returns the object key that fully identifies the content object within
129      * a Jahia system.
130      * @return an ObjectKey class that uniquely identifies the object.
131      */

132     public ObjectKey getObjectKey() {
133         return objectKey;
134     }
135
136 }
137
Popular Tags