KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > access > types > ExtendedTypeMap


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.access.types;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29
30 import org.apache.cayenne.util.Util;
31
32 /**
33  * Stores ExtendedTypes, implementing an algorithm to determine the right type for a given
34  * Java class. See {@link #getRegisteredType(String)} documentation for lookup algorith
35  * details.
36  *
37  * @author Andrus Adamchik
38  */

39 public class ExtendedTypeMap {
40
41     protected Map JavaDoc typeMap = new HashMap JavaDoc();
42     protected DefaultType defaultType = new DefaultType();
43
44     Collection JavaDoc extendedTypeFactories;
45
46     // standard type factories registered by Cayenne that are consulted after the user
47
// factories.
48
Collection JavaDoc internalTypeFactories;
49
50     /**
51      * Creates new ExtendedTypeMap, populating it with default JDBC-compatible types. If
52      * JDK version is at least 1.5, also loads support for enumerated types.
53      */

54     public ExtendedTypeMap() {
55         initDefaultTypes();
56         initDefaultFactories();
57     }
58
59     /**
60      * Registers default extended types. This method is called from constructor.
61      */

62     protected void initDefaultTypes() {
63         // void placeholder
64
registerType(new VoidType());
65
66         // register default types
67
Iterator JavaDoc it = DefaultType.defaultTypes();
68         while (it.hasNext()) {
69             registerType(new DefaultType((String JavaDoc) it.next()));
70         }
71     }
72
73     /**
74      * Registers default factories for creating enum types and serializable types. Note
75      * that user-defined factories are consulted before any default factory.
76      *
77      * @since 3.0
78      */

79     protected void initDefaultFactories() {
80         internalTypeFactories = new ArrayList JavaDoc(3);
81         internalTypeFactories.add(new EnumTypeFactory());
82         internalTypeFactories.add(new ByteOrCharArrayFactory(this));
83
84         // note that Serializable type should be used as a last resort after all other
85
// alternatives are exhausted.
86
internalTypeFactories.add(new SerializableTypeFactory(this));
87     }
88
89     /**
90      * Returns ExtendedTypeFactories registered with this instance.
91      *
92      * @since 1.2
93      */

94     public Collection JavaDoc getFactories() {
95         return extendedTypeFactories != null ? Collections
96                 .unmodifiableCollection(extendedTypeFactories) : Collections.EMPTY_SET;
97     }
98
99     /**
100      * Adds an ExtendedTypeFactory that will be consulted if no direct mapping for a given
101      * class exists. This feature can be used to map interfaces.
102      * <p>
103      * <i>Note that the order in which factories are added is important, as factories are
104      * consulted in turn when an ExtendedType is looked up, and lookup is stopped when any
105      * factory provides a non-null type.</i>
106      * </p>
107      *
108      * @since 1.2
109      */

110     public void addFactory(ExtendedTypeFactory factory) {
111         if (factory == null) {
112             throw new IllegalArgumentException JavaDoc("Attempt to add null factory");
113         }
114
115         if (extendedTypeFactories == null) {
116             extendedTypeFactories = new ArrayList JavaDoc();
117         }
118
119         extendedTypeFactories.add(factory);
120     }
121
122     /**
123      * Removes a factory from the regsitered factories if it was previosly added.
124      *
125      * @since 1.2
126      */

127     public void removeFactory(ExtendedTypeFactory factory) {
128         if (factory != null && extendedTypeFactories != null) {
129             // nullify for consistency
130
if (extendedTypeFactories.remove(factory) && extendedTypeFactories.isEmpty()) {
131                 extendedTypeFactories = null;
132             }
133         }
134     }
135
136     /**
137      * Adds a new type to the list of registered types. If there is another type
138      * registered for a class described by the <code>type</code> argument, the old
139      * handler is overwriden by the new one.
140      */

141     public void registerType(ExtendedType type) {
142         typeMap.put(type.getClassName(), type);
143         
144         // factory to handle subclasses of type.className
145
addFactory(new SubclassTypeFactory(type));
146     }
147
148     /**
149      * Returns a default ExtendedType that is used to handle unmapped types.
150      */

151     public ExtendedType getDefaultType() {
152         return defaultType;
153     }
154
155     /**
156      * Returns a guranteed non-null ExtendedType instance for a given Java class name. The
157      * following lookup sequence is used to determine the type:
158      * <ul>
159      * <li>First the methods checks for an ExtendedType explicitly registered with the
160      * map for a given class name (most common types are registered by Cayenne internally;
161      * users can regsiter their own).</li>
162      * <li>Second, the method tries to obtain a type by iterating through
163      * {@link ExtendedTypeFactory} instances registered by users. If a factory returns a
164      * non-null type, it is returned to the user and the rest of the factories are
165      * ignored. </li>
166      * <li>Third, the method iterates through standard {@link ExtendedTypeFactory}
167      * instances that can dynamically construct extended types for serializable objects
168      * and JDK 1.5 enums.</li>
169      * <li>If all the methods above failed, the default type is returned that relies on
170      * default JDBC driver mapping to set and get objects.</li>
171      * </ul>
172      * <i>Note that for array types class name must be in the form 'MyClass[]'</i>.
173      */

174     public ExtendedType getRegisteredType(String JavaDoc javaClassName) {
175         ExtendedType type = getExplictlyRegisteredType(javaClassName);
176
177         if (type != null) {
178             return type;
179         }
180
181         type = getDefaultType(javaClassName);
182
183         if (type != null) {
184             // register to speed up future access
185
registerType(type);
186             return type;
187         }
188
189         return getDefaultType();
190     }
191     
192     ExtendedType getExplictlyRegisteredType(String JavaDoc className) {
193         return (ExtendedType) typeMap.get(className);
194     }
195
196     /**
197      * Returns a type registered for the class name. If no such type exists, returns the
198      * default type. It is guaranteed that this method returns a non-null ExtendedType
199      * instance.
200      */

201     public ExtendedType getRegisteredType(Class JavaDoc javaClass) {
202         String JavaDoc name = null;
203
204         if (javaClass.isArray()) {
205             // only support single dimensional arrays now
206
name = javaClass.getComponentType().getName() + "[]";
207         }
208         else {
209             name = javaClass.getName();
210         }
211
212         return getRegisteredType(name);
213     }
214
215     /**
216      * Removes registered ExtendedType object corresponding to <code>javaClassName</code>
217      * parameter.
218      */

219     public void unregisterType(String JavaDoc javaClassName) {
220         typeMap.remove(javaClassName);
221     }
222
223     /**
224      * Returns array of Java class names supported by Cayenne for JDBC mapping.
225      */

226     public String JavaDoc[] getRegisteredTypeNames() {
227         Set JavaDoc keys = typeMap.keySet();
228         int len = keys.size();
229         String JavaDoc[] types = new String JavaDoc[len];
230
231         Iterator JavaDoc it = keys.iterator();
232         for (int i = 0; i < len; i++) {
233             types[i] = (String JavaDoc) it.next();
234         }
235
236         return types;
237     }
238
239     /**
240      * @deprecated since 3.0 - use {@link #createType(String)} instead.
241      * @since 1.2
242      */

243     protected ExtendedType getDefaultType(String JavaDoc javaClassName) {
244         return createType(javaClassName);
245     }
246
247     /**
248      * Returns a default type for specific Java classes. This implementation supports
249      * dynamically loading EnumType handlers for concrete Enum classes (assuming the
250      * application runs under JDK1.5+).
251      *
252      * @return a default type for a given class or null if a class has no default type
253      * mapping.
254      * @since 1.2
255      */

256     protected ExtendedType createType(String JavaDoc className) {
257
258         if (className == null) {
259             return null;
260         }
261
262         Class JavaDoc typeClass;
263         try {
264             typeClass = Util.getJavaClass(className);
265         }
266         catch (Throwable JavaDoc th) {
267             // ignore exceptions...
268
return null;
269         }
270
271         // lookup in user factories first
272
if (extendedTypeFactories != null) {
273
274             Iterator JavaDoc it = extendedTypeFactories.iterator();
275             while (it.hasNext()) {
276                 ExtendedTypeFactory factory = (ExtendedTypeFactory) it.next();
277
278                 ExtendedType type = factory.getType(typeClass);
279                 if (type != null) {
280                     return type;
281                 }
282             }
283         }
284
285         // lookup in internal factories
286

287         Iterator JavaDoc it = internalTypeFactories.iterator();
288         while (it.hasNext()) {
289             ExtendedTypeFactory factory = (ExtendedTypeFactory) it.next();
290
291             ExtendedType type = factory.getType(typeClass);
292             if (type != null) {
293                 return type;
294             }
295         }
296
297         return null;
298     }
299 }
300
Popular Tags