KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ____.
3  * __/\ ______| |__/\. _______
4  * __ .____| | \ | +----+ \
5  * _______| /--| | | - \ _ | : - \_________
6  * \\______: :---| : : | : | \________>
7  * |__\---\_____________:______: :____|____:_____\
8  * /_____|
9  *
10  * . . . i n j a h i a w e t r u s t . . .
11  *
12  *
13  *
14  * ----- BEGIN LICENSE BLOCK -----
15  * Version: JCSL 1.0
16  *
17  * The contents of this file are subject to the Jahia Community Source License
18  * 1.0 or later (the "License"); you may not use this file except in
19  * compliance with the License. You may obtain a copy of the License at
20  * http://www.jahia.org/license
21  *
22  * Software distributed under the License is distributed on an "AS IS" basis,
23  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
24  * for the rights, obligations and limitations governing use of the contents
25  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
26  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
27  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
28  *
29  * The Shared Modifications are Jahia View Helper.
30  *
31  * The Developer of the Shared Modifications is Jahia Solution S�rl.
32  * Portions created by the Initial Developer are Copyright (C) 2002 by the
33  * Initial Developer. All Rights Reserved.
34  *
35  * Contributor(s):
36  * 19-Aug-2003, Jahia Solutions Sarl: Fulco Houkes
37  *
38  * ----- END LICENSE BLOCK -----
39  */

40
41
42 package org.jahia.content;
43
44 import java.io.Serializable JavaDoc;
45 import java.lang.reflect.InvocationTargetException JavaDoc;
46 import java.util.HashMap JavaDoc;
47 import java.util.Map JavaDoc;
48
49 /**
50  * The purpose of this class is to construct ContentObject references that can
51  * be used both externally and internally to references Jahia content objects
52  * such as fields, containers, pages and other objects...
53  *
54  * The tentative format of this key is (subject to change in order to comply
55  * with existing norms) :
56  *
57  * type_IDInType
58  *
59  * where type is a String specifiyng the type of the object, while the IDInType
60  * is an integer that specifies which instances of the specific object type.
61  *
62  * @author Serge Huber
63  */

64 public abstract class ObjectKey implements
65     ObjectKeyInterface,
66     Serializable JavaDoc, Comparable JavaDoc {
67
68     private static org.apache.log4j.Logger logger =
69             org.apache.log4j.Logger.getLogger(ObjectKey.class);
70
71     private static Map JavaDoc keyTypeClassNames = new HashMap JavaDoc();
72
73     protected String JavaDoc key;
74     public static final String JavaDoc OBJECT_TYPE = "object";
75     private String JavaDoc type = OBJECT_TYPE;
76     private String JavaDoc IDInType;
77     private int idInType = -1;
78     /**
79      * Making default constructor private
80      */

81     private ObjectKey() {
82     }
83
84     /**
85      * Object constructor method.
86      * @param type a String specifying the object type. Normally this is used
87      * mostly by children of this class but could be used for some "hacks".
88      * @param IDInType the unique identifier within the type.
89      */

90     protected ObjectKey(String JavaDoc aType,
91                      String JavaDoc anIDInType) {
92         this.type = aType;
93         this.IDInType = anIDInType;
94         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(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 JavaDoc e) {
102             idInType = -1;
103         }
104     }
105
106     /**
107      * Registers a new type for a sub class of ObjectKey in the factory
108      * hash table. Use this if you want to be able to create instance of the
109      * new class by using a complete key. The new sub class MUST implement a
110      * static method with the following signature :
111      * public static ObjectKey getChildInstance(String IDInType);
112      *
113      * @param type the name of the type, usually defined as a constant in the
114      * ObjectKey sub class.
115      * @param className the name of the class. Can be obtained with a called
116      * similar to ObjectKey.class.getName() (replacing the ObjectKey with the
117      * subclass of course.
118      */

119     public static void registerType(String JavaDoc type, String JavaDoc className) {
120         logger.debug("Registering type [" + type + "] with class name [" +
121                      className + "]");
122         keyTypeClassNames.put ( type, className );
123     }
124
125     /**
126      * Removes a type registration. See the registerType method for more details
127      * @param type the name of the type to unregister
128      */

129     public static void unregisterType(String JavaDoc type) {
130         keyTypeClassNames.remove(type);
131     }
132
133     /**
134      * Instance generator. Uses a string representation of the key to extract
135      * the type and IDInType parameters. Build an instance of the appropriate
136      * class corresponding to the type described.
137      *
138      * @param key a String in the format : type + KEY_SEPARATOR + IDInType
139      *
140      * @return an ObjectKey sub class instance that corresponds to the given
141      * key.
142      *
143      * @throws ClassNotFoundException if no class could be found for the type
144      * passed in parameter.
145      */

146     public static ObjectKey getInstance (String JavaDoc key)
147         throws ClassNotFoundException JavaDoc {
148         ObjectKey resultKey = null;
149         int separatorPos = key.indexOf(KEY_SEPARATOR);
150         if (separatorPos > 0) {
151             String JavaDoc type = key.substring(0, separatorPos);
152             String JavaDoc idStr = key.substring(separatorPos + KEY_SEPARATOR.length());
153             if (!keyTypeClassNames.containsKey(type)) {
154                 throw new ClassNotFoundException JavaDoc("No class defined for type [" +
155                                                  type + "]");
156             }
157             try {
158                 Class JavaDoc childClass = Class.forName( (String JavaDoc) keyTypeClassNames.
159                                                  get(type));
160                 Class JavaDoc[] childClassParameters = new Class JavaDoc[1];
161                 childClassParameters[0] = String JavaDoc.class;
162                 java.lang.reflect.Method JavaDoc childClassMethod = childClass.
163                     getMethod("getChildInstance", childClassParameters);
164                 Object JavaDoc[] args = new Object JavaDoc[1];
165                 args[0] = idStr;
166                 resultKey = (ObjectKey) childClassMethod.invoke(null, args);
167             } catch (ClassNotFoundException JavaDoc cnfe) {
168                 logger.error("Error while creating instance of object key " +
169                              key, cnfe);
170             } catch (NoSuchMethodException JavaDoc nsme) {
171                 logger.error("Error while creating instance of object key " +
172                              key, nsme);
173             } catch (SecurityException JavaDoc se) {
174                 logger.error("Error while creating instance of object key " +
175                              key, se);
176             } catch (IllegalAccessException JavaDoc iae) {
177                 logger.error("Error while creating instance of object key " +
178                              key, iae);
179             } catch (IllegalArgumentException JavaDoc iae2) {
180                 logger.error("Error while creating instance of object key " +
181                              key, iae2);
182             } catch (InvocationTargetException JavaDoc 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     /* --- ACCESSOR METHODS ------------------------------------------------- */
194
195     public String JavaDoc getKey(){
196         return key;
197     }
198
199     public String JavaDoc getType(){
200         return this.type;
201     }
202
203     public String JavaDoc 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 JavaDoc e) {
212                 idInType = -1;
213             }
214         }
215         return idInType;
216     }
217     
218     /**
219      * Redefinition of the equality comparison, since ObjectKey objects are
220      * very likely to be used as keys in Hashtables, or other ordered sets,
221      * maps or lists.
222      * @param obj the Object to compare this object to.
223      * @return true if the object is an instance of the ObjectKey class and
224      * the value of the key is the same, false otherwise.
225      */

226     public boolean equals(Object JavaDoc obj) {
227         // JahiaConsole.println("ObjectKey.equals", "Starting...");
228
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     /**
241      * Implementation of the comparable interface so that object keys may be
242      * used in sets that need a comparable interface.
243      * @param o the ObjectKey to compare this object to
244      * @return a negative integer, zero, or a positive integer as this
245      * object's key string is less than, equal to, or greater than the
246      * specified object key string
247      * @throws ClassCastException thrown if the parameter is not of the
248      * ObjectKey type.
249      */

250     public int compareTo(Object JavaDoc o) throws ClassCastException JavaDoc {
251         ObjectKey destObjectKey = (ObjectKey) o;
252         return this.key.compareTo(destObjectKey.getKey());
253     }
254
255     /**
256      * Hash code implementation to make object compatible with hash table
257      * usage.
258      * @return an unique hash code for a unique key.
259      */

260     public int hashCode() {
261         // logger.debug("Returning hashcode=" + key.hashCode());
262
return key.hashCode();
263     }
264
265     /**
266      * Converts the object to a string only to be used for debug display,
267      * not serialization !
268      * @return a String containing the object key.
269      */

270     public String JavaDoc toString() {
271         return key;
272     }
273
274 }
275
Popular Tags