KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > Config4Class


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o;
22
23 import com.db4o.config.*;
24 import com.db4o.foundation.*;
25 import com.db4o.reflect.*;
26
27 /**
28  * @exclude
29  */

30 public class Config4Class extends Config4Abstract implements ObjectClass,
31     DeepClone {
32     
33     private final Config4Impl _configImpl;
34
35     private final static KeySpec CALL_CONSTRUCTOR=new KeySpec(0);
36
37     private final static KeySpec EXCEPTIONAL_FIELDS=new KeySpec(null);
38
39     private final static KeySpec GENERATE_UUIDS=new KeySpec(0);
40     
41     private final static KeySpec GENERATE_VERSION_NUMBERS=new KeySpec(0);
42     
43     /**
44      * We are running into cyclic dependancies on reading the PBootRecord
45      * object, if we maintain MetaClass information there
46      */

47     private final static KeySpec MAINTAIN_METACLASS=new KeySpec(true);
48
49     private final static KeySpec MAXIMUM_ACTIVATION_DEPTH=new KeySpec(0);
50
51     private final static KeySpec MINIMUM_ACTIVATION_DEPTH=new KeySpec(0);
52
53     private final static KeySpec PERSIST_STATIC_FIELD_VALUES=new KeySpec(false);
54     
55     private final static KeySpec QUERY_ATTRIBUTE_PROVIDER=new KeySpec(null);
56     
57     private final static KeySpec STORE_TRANSIENT_FIELDS=new KeySpec(false);
58     
59     private final static KeySpec TRANSLATOR=new KeySpec(null);
60
61     private final static KeySpec TRANSLATOR_NAME=new KeySpec((String JavaDoc)null);
62     
63     private final static KeySpec UPDATE_DEPTH=new KeySpec(0);
64     
65     private final static KeySpec WRITE_AS=new KeySpec((String JavaDoc)null);
66     
67     protected Config4Class(Config4Impl configuration, KeySpecHashtable4 config) {
68         super(config);
69         _configImpl = configuration;
70     }
71
72     Config4Class(Config4Impl a_configuration, String JavaDoc a_name) {
73         _configImpl = a_configuration;
74         setName(a_name);
75     }
76
77     int adjustActivationDepth(int a_depth) {
78         if ((cascadeOnActivate() == YapConst.YES)&& a_depth < 2) {
79             a_depth = 2;
80         }
81         if((cascadeOnActivate() == YapConst.NO) && a_depth > 1){
82             a_depth = 1;
83         }
84         if (config().classActivationDepthConfigurable()) {
85             int minimumActivationDepth=_config.getAsInt(MINIMUM_ACTIVATION_DEPTH);
86             if (minimumActivationDepth != 0) {
87                 if (a_depth < minimumActivationDepth) {
88                     a_depth = minimumActivationDepth;
89                 }
90             }
91             int maximumActivationDepth=_config.getAsInt(MAXIMUM_ACTIVATION_DEPTH);
92             if (maximumActivationDepth != 0) {
93                 if (a_depth > maximumActivationDepth) {
94                     a_depth = maximumActivationDepth;
95                 }
96             }
97         }
98         return a_depth;
99     }
100     
101     public void callConstructor(boolean flag){
102         putThreeValued(CALL_CONSTRUCTOR, flag);
103     }
104
105     String JavaDoc className() {
106         return getName();
107     }
108     
109     ReflectClass classReflector() {
110         return config().reflector().forName(getName());
111     }
112
113     public void compare(ObjectAttribute comparator) {
114         _config.put(QUERY_ATTRIBUTE_PROVIDER,comparator);
115     }
116
117     Config4Field configField(String JavaDoc fieldName) {
118         Hashtable4 exceptionalFields=exceptionalFieldsOrNull();
119         if (exceptionalFields == null) {
120             return null;
121         }
122         return (Config4Field) exceptionalFields.get(fieldName);
123     }
124
125     public Object JavaDoc deepClone(Object JavaDoc param){
126         return new Config4Class((Config4Impl)param,_config);
127     }
128
129     public void enableReplication(boolean setting) {
130         generateUUIDs(setting);
131         generateVersionNumbers(setting);
132     }
133     
134     public void generateUUIDs(boolean setting) {
135         putThreeValued(GENERATE_UUIDS, setting);
136     }
137
138     public void generateVersionNumbers(boolean setting) {
139         putThreeValued(GENERATE_VERSION_NUMBERS, setting);
140     }
141
142     public ObjectTranslator getTranslator() {
143         ObjectTranslator translator=(ObjectTranslator)_config.get(TRANSLATOR);
144         if (translator != null) {
145             return translator;
146         }
147
148         String JavaDoc translatorName=_config.getAsString(TRANSLATOR_NAME);
149         if (translatorName == null) {
150             return null;
151         }
152         
153         try {
154             translator = (ObjectTranslator) config().reflector().forName(
155                 translatorName).newInstance();
156         } catch (Throwable JavaDoc t) {
157             if (! Deploy.csharp){
158                 // TODO: why?
159
try{
160                     translator = (ObjectTranslator) Class.forName(translatorName).newInstance();
161                     if(translator != null){
162                         translate(translator);
163                         return translator;
164                     }
165                 }catch(Throwable JavaDoc th){
166                 }
167             }
168             Messages.logErr(config(), 48, translatorName, null);
169             translateOnDemand(null);
170         }
171         translate(translator);
172         return translator;
173     }
174
175     Object JavaDoc instantiate(YapStream a_stream, Object JavaDoc a_toTranslate) {
176         return ((ObjectConstructor) _config.get(TRANSLATOR)).onInstantiate(a_stream,
177             a_toTranslate);
178     }
179
180     boolean instantiates() {
181         return getTranslator() instanceof ObjectConstructor;
182     }
183
184     public void maximumActivationDepth(int depth) {
185         _config.put(MAXIMUM_ACTIVATION_DEPTH,depth);
186     }
187
188     public void minimumActivationDepth(int depth) {
189         _config.put(MINIMUM_ACTIVATION_DEPTH,depth);
190     }
191     
192     public int callConstructor() {
193         if(_config.get(TRANSLATOR) != null){
194             return YapConst.YES;
195         }
196         return _config.getAsInt(CALL_CONSTRUCTOR);
197     }
198
199     private Hashtable4 exceptionalFieldsOrNull() {
200         return (Hashtable4)_config.get(EXCEPTIONAL_FIELDS);
201
202     }
203     
204     private Hashtable4 exceptionalFields() {
205         Hashtable4 exceptionalFieldsCollection=exceptionalFieldsOrNull();
206         if (exceptionalFieldsCollection == null) {
207             exceptionalFieldsCollection = new Hashtable4(16);
208             _config.put(EXCEPTIONAL_FIELDS,exceptionalFieldsCollection);
209         }
210         return exceptionalFieldsCollection;
211     }
212     
213     public ObjectField objectField(String JavaDoc fieldName) {
214         Hashtable4 exceptionalFieldsCollection=exceptionalFields();
215         Config4Field c4f = (Config4Field) exceptionalFieldsCollection.get(fieldName);
216         if (c4f == null) {
217             c4f = new Config4Field(this, fieldName);
218             exceptionalFieldsCollection.put(fieldName, c4f);
219         }
220         return c4f;
221     }
222
223     public void persistStaticFieldValues() {
224         _config.put(PERSIST_STATIC_FIELD_VALUES, true);
225     }
226
227     boolean queryEvaluation(String JavaDoc fieldName) {
228         Hashtable4 exceptionalFields=exceptionalFieldsOrNull();
229         if (exceptionalFields != null) {
230             Config4Field field = (Config4Field) exceptionalFields
231                 .get(fieldName);
232             if (field != null) {
233                 return field.queryEvaluation();
234             }
235         }
236         return true;
237     }
238
239     public void readAs(Object JavaDoc clazz) {
240        Config4Impl configRef=config();
241        ReflectClass claxx = configRef.reflectorFor(clazz);
242        if (claxx == null) {
243            return;
244        }
245        _config.put(WRITE_AS,getName());
246        configRef.readAs().put(getName(), claxx.getName());
247    }
248
249     public void rename(String JavaDoc newName) {
250         config().rename(new Rename("", getName(), newName));
251         setName(newName);
252     }
253
254     public void storeTransientFields(boolean flag) {
255         _config.put(STORE_TRANSIENT_FIELDS, flag);
256     }
257
258     public void translate(ObjectTranslator translator) {
259         if (translator == null) {
260             _config.put(TRANSLATOR_NAME, null);
261         }
262         _config.put(TRANSLATOR, translator);
263     }
264
265     void translateOnDemand(String JavaDoc a_translatorName) {
266         _config.put(TRANSLATOR_NAME,a_translatorName);
267     }
268
269     public void updateDepth(int depth) {
270         _config.put(UPDATE_DEPTH, depth);
271     }
272
273     Config4Impl config() {
274         return _configImpl;
275     }
276
277     int generateUUIDs() {
278         return _config.getAsInt(GENERATE_UUIDS);
279     }
280
281     int generateVersionNumbers() {
282         return _config.getAsInt(GENERATE_VERSION_NUMBERS);
283     }
284
285     void maintainMetaClass(boolean flag){
286         _config.put(MAINTAIN_METACLASS,flag);
287     }
288
289     boolean staticFieldValuesArePersisted() {
290         return _config.getAsBoolean(PERSIST_STATIC_FIELD_VALUES);
291     }
292
293     ObjectAttribute queryAttributeProvider() {
294         return (ObjectAttribute)_config.get(QUERY_ATTRIBUTE_PROVIDER);
295     }
296
297     boolean storeTransientFields() {
298         return _config.getAsBoolean(STORE_TRANSIENT_FIELDS);
299     }
300
301     int updateDepth() {
302         return _config.getAsInt(UPDATE_DEPTH);
303     }
304
305     String JavaDoc writeAs() {
306         return _config.getAsString(WRITE_AS);
307     }
308
309 }
Popular Tags