KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdo > sco > VersantSCOFactoryRegistry


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdo.sco;
13
14 import com.versant.core.metadata.FieldMetaData;
15 import com.versant.core.metadata.MDStaticUtils;
16 import com.versant.core.metadata.MDStatics;
17 import com.versant.core.util.classhelper.ClassHelper;
18
19 import java.util.*;
20
21 import com.versant.core.common.BindingSupportImpl;
22
23 /**
24  * This registry map the SCO types to their factories.
25  */

26 public class VersantSCOFactoryRegistry {
27
28     private static final VersantSCOFactoryRegistry defaultMapping = new VersantSCOFactoryRegistry();
29
30     private HashMap typeFactory = new HashMap(32);
31
32     protected VersantSCOFactoryRegistry() {
33         put(MDStatics.DATE, new DateSCOFactory());
34         put(MDStatics.HASHSET, new SCOHashSetFactory());
35         put(MDStatics.SET, new SCOHashSetFactory());
36         put(MDStatics.TREESET, new SCOTreeSetFactory());
37         put(MDStatics.SORTEDSET, new SCOTreeSetFactory());
38         put(MDStatics.COLLECTION, new SCOListFactory());
39         put(MDStatics.LIST, new SCOListFactory());
40         put(MDStatics.ARRAYLIST, new SCOArrayListFactory());
41         put(MDStatics.LINKEDLIST, new SCOLinkedListFactory());
42
43
44         put(MDStatics.VECTOR, new SCOVectorFactory());
45
46
47         put(MDStatics.MAP, new SCOHashMapFactory());
48         put(MDStatics.HASHMAP, new SCOHashMapFactory());
49         put(MDStatics.HASHTABLE, new SCOHashtableFactory());
50         put(MDStatics.TREEMAP, new SCOTreeMapFactory());
51         put(MDStatics.SORTEDMAP, new SCOTreeMapFactory());
52     }
53
54     private void put(int type, Object JavaDoc factory) {
55         if (factory == null) return;
56         String JavaDoc name = MDStaticUtils.toSimpleName(type);
57         if (name == null) return;
58         Class JavaDoc c = MDStaticUtils.toSimpleClass(name);
59         if (c == null) return;
60         typeFactory.put(c, factory);
61     }
62
63     public VersantSCOFactoryRegistry(Map mapping, ClassLoader JavaDoc loader) {
64         this();
65         if (mapping == null) return;
66         for (Iterator it = mapping.keySet().iterator(); it.hasNext();) {
67             String JavaDoc key = (String JavaDoc) it.next();
68             String JavaDoc value = (String JavaDoc) mapping.get(key);
69             try {
70                 Class JavaDoc javaClass = ClassHelper.get().classForName(key, true, loader);
71                 Class JavaDoc scoFactoryClass = ClassHelper.get().classForName(value, true, loader);
72                 Object JavaDoc factory = scoFactoryClass.newInstance();
73                 typeFactory.put(javaClass, factory);
74             } catch (Exception JavaDoc e) {
75                 throw BindingSupportImpl.getInstance().runtime("Unable to add SCO factory mapping:\n" +
76                         e.getMessage(), e);
77             }
78         }
79     }
80
81     /**
82      * Get the factory for a simple SCO type.
83      */

84     public Object JavaDoc getFactory(FieldMetaData fmd) {
85         if (fmd == null) return null;
86         //create a new SCO data
87
switch (fmd.category) {
88             case MDStatics.CATEGORY_SIMPLE:
89                 return getJdoGenieSCOFactory(fmd);
90             case MDStatics.CATEGORY_COLLECTION:
91                 return getJDOGenieSCOCollectionFactory(fmd);
92             case MDStatics.CATEGORY_MAP:
93                 return getJDOGenieSCOMapFactory(fmd);
94         }
95         return null;
96     }
97
98     /**
99      * Get the factory for a simple SCO type.
100      */

101     public VersantSCOFactory getJdoGenieSCOFactory(FieldMetaData fmd) {
102         return (VersantSCOFactory) getFactory(fmd, MDStatics.CATEGORY_SIMPLE, VersantSCOFactory.class);
103     }
104
105     /**
106      * Get the factory for a SCO collection.
107      */

108     public VersantSCOCollectionFactory getJDOGenieSCOCollectionFactory(FieldMetaData fmd) {
109         return (VersantSCOCollectionFactory) getFactory(fmd,
110                 MDStatics.CATEGORY_COLLECTION, VersantSCOCollectionFactory.class);
111     }
112
113     /**
114      * Get the factory for a SCO map.
115      */

116     public VersantSCOMapFactory getJDOGenieSCOMapFactory(FieldMetaData fmd) {
117         return (VersantSCOMapFactory) getFactory(fmd,
118                 MDStatics.CATEGORY_MAP, VersantSCOMapFactory.class);
119     }
120
121     private Object JavaDoc getFactory(FieldMetaData fmd, int category, Class JavaDoc factoryCls) {
122         if (fmd.category != category) {
123             return null;
124         }
125         Class JavaDoc cls = fmd.type;
126         
127         Object JavaDoc o = typeFactory.get(cls);
128         
129         if (o == null) {
130             throw BindingSupportImpl.getInstance().notImplemented("No SCO factory registered for type: " +
131                     MDStaticUtils.toSimpleName(fmd.typeCode) + ".\nClass: " +
132                     fmd.classMetaData.cls.getName() + " Field: " + fmd.name);
133         }
134         if (!factoryCls.isAssignableFrom(o.getClass())) {
135             throw BindingSupportImpl.getInstance().notImplemented("Incorrect SCO factory type.\nExpected:" +
136                     factoryCls.getName() + "\nFound:" + o.getClass().getName() + ".\nfor Class: " +
137                     fmd.classMetaData.cls.getName() + " Field: " + fmd.name);
138         }
139         return o;
140     }
141
142     public static String JavaDoc getDefaultMapping(Class JavaDoc javaType) {
143         Object JavaDoc factory = defaultMapping.typeFactory.get(javaType);
144         if (factory != null) {
145             return factory.getClass().getName();
146         }
147         return null;
148     }
149
150     public static void fillMapWithDefaults(Map map) {
151         for (Iterator it = defaultMapping.typeFactory.keySet().iterator(); it.hasNext();) {
152             Class JavaDoc javaType = (Class JavaDoc) it.next();
153             Object JavaDoc factory = defaultMapping.typeFactory.get(javaType);
154             map.put(javaType, factory.getClass().getName());
155         }
156     }
157
158     public static List getValidSCOFactoryList(Class JavaDoc fieldType) {
159         List factoryList = new ArrayList();
160         for (Iterator it = defaultMapping.typeFactory.keySet().iterator(); it.hasNext();) {
161             Class JavaDoc javaType = (Class JavaDoc) it.next();
162             if (fieldType.isAssignableFrom(javaType)) {
163                 Object JavaDoc factory = defaultMapping.typeFactory.get(javaType);
164                 if (factory != null) {
165                     String JavaDoc factoryName = factory.getClass().getName();
166                     if (!factoryList.contains(factoryName)) {
167                         factoryList.add(factoryName);
168                     }
169                 }
170             }
171         }
172         Collections.sort(factoryList);
173         return factoryList;
174     }
175
176     public static void removeDefaults(Map map) {
177         for (Iterator it = defaultMapping.typeFactory.keySet().iterator(); it.hasNext();) {
178             Class JavaDoc javaType = (Class JavaDoc) it.next();
179             String JavaDoc factoryName1 = (String JavaDoc) map.get(javaType);
180             if (factoryName1 != null) {
181                 Object JavaDoc factory = defaultMapping.typeFactory.get(javaType);
182                 if (factory != null) {
183                     String JavaDoc factoryName2 = factory.getClass().getName();
184                     if (factoryName1.equals(factoryName2)) {
185                         map.remove(javaType);
186                     }
187                 }
188             }
189         }
190     }
191
192 }
193
Popular Tags