KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > persist > impl > EnhancedAccessor


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: EnhancedAccessor.java,v 1.11 2006/10/30 21:14:32 bostic Exp $
7  */

8
9 package com.sleepycat.persist.impl;
10
11 import java.lang.reflect.Array JavaDoc;
12 import java.lang.reflect.Modifier JavaDoc;
13 import java.util.Collections JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16
17 /**
18  * Implements Accessor for a complex persistent class.
19  *
20  * @author Mark Hayes
21  */

22 public class EnhancedAccessor implements Accessor {
23
24     private static final Map JavaDoc<String JavaDoc,Enhanced> classRegistry =
25         Collections.synchronizedMap(new HashMap JavaDoc<String JavaDoc,Enhanced>());
26
27     /* Is public for unit tests. */
28     public static final boolean EXPECT_ENHANCED =
29         "true".equals(System.getProperty("expectEnhanced"));
30
31     private Enhanced prototype;
32     private Format priKeyFormat;
33     private Class JavaDoc type;
34
35     /**
36      * Registers a prototype instance, and should be called during
37      * initialization of the prototype class. The prototype may be null for
38      * an abstract class.
39      */

40     public static void registerClass(String JavaDoc className, Enhanced prototype) {
41         classRegistry.put(className, prototype);
42     }
43
44     /**
45      * Returns whether a given class is a (registered) enhanced class.
46      */

47     static boolean isEnhanced(Class JavaDoc type) {
48         boolean enhanced = classRegistry.containsKey(type.getName());
49         if (!enhanced && EXPECT_ENHANCED) {
50             throw new IllegalStateException JavaDoc
51                 ("Test was run with expectEnhanced=true but class " +
52                  type.getName() + " is not enhanced");
53         }
54         return enhanced;
55     }
56
57     /**
58      * Creates an accessor.
59      */

60     EnhancedAccessor(Class JavaDoc type) {
61         this.type = type;
62         prototype = classRegistry.get(type.getName());
63         assert prototype != null || Modifier.isAbstract(type.getModifiers());
64     }
65
66     /**
67      * Creates an accessor for a complex type.
68      */

69     EnhancedAccessor(Catalog catalog, Class JavaDoc type, ComplexFormat format) {
70         this(type);
71
72         /*
73          * Find the primary key format for this format or one of its superclass
74          * formats.
75          */

76         ComplexFormat declaringFormat = format;
77         while (declaringFormat != null) {
78             String JavaDoc priKeyField = declaringFormat.getPriKeyField();
79             if (priKeyField != null) {
80                 Class JavaDoc declaringType = declaringFormat.getType();
81                 Class JavaDoc fieldType;
82                 try {
83                     fieldType =
84                         declaringType.getDeclaredField(priKeyField).getType();
85                 } catch (NoSuchFieldException JavaDoc e) {
86                     throw new IllegalStateException JavaDoc(e);
87                 }
88                 priKeyFormat = catalog.getFormat(fieldType);
89                 break;
90             } else {
91                 Format superFormat = declaringFormat.getSuperFormat();
92                 declaringFormat = (ComplexFormat) superFormat;
93             }
94         }
95     }
96
97     public Object JavaDoc newInstance() {
98         if (prototype == null) {
99             /* Abstract class -- internal error. */
100             throw new IllegalStateException JavaDoc();
101         }
102         return prototype.bdbNewInstance();
103     }
104
105     public Object JavaDoc newArray(int len) {
106         if (prototype == null) {
107             /* Abstract class -- use reflection for now. */
108             return Array.newInstance(type, len);
109         }
110         return prototype.bdbNewArray(len);
111     }
112
113     public boolean isPriKeyFieldNullOrZero(Object JavaDoc o) {
114         if (priKeyFormat == null) {
115             throw new IllegalStateException JavaDoc
116                 ("No primary key: " + o.getClass().getName());
117         }
118         return ((Enhanced) o).bdbIsPriKeyFieldNullOrZero();
119     }
120
121     public void writePriKeyField(Object JavaDoc o, EntityOutput output) {
122         if (priKeyFormat == null) {
123             throw new IllegalStateException JavaDoc
124                 ("No primary key: " + o.getClass().getName());
125         }
126         ((Enhanced) o).bdbWritePriKeyField(output, priKeyFormat);
127     }
128
129     public void readPriKeyField(Object JavaDoc o, EntityInput input) {
130         if (priKeyFormat == null) {
131             throw new IllegalStateException JavaDoc
132                 ("No primary key: " + o.getClass().getName());
133         }
134         ((Enhanced) o).bdbReadPriKeyField(input, priKeyFormat);
135     }
136
137     public void writeSecKeyFields(Object JavaDoc o, EntityOutput output) {
138         ((Enhanced) o).bdbWriteSecKeyFields(output);
139     }
140     
141     public void readSecKeyFields(Object JavaDoc o,
142                                  EntityInput input,
143                                  int startField,
144                                  int endField,
145                                  int superLevel) {
146         ((Enhanced) o).bdbReadSecKeyFields
147             (input, startField, endField, superLevel);
148     }
149
150     public void writeNonKeyFields(Object JavaDoc o, EntityOutput output) {
151         ((Enhanced) o).bdbWriteNonKeyFields(output);
152     }
153     
154     public void readNonKeyFields(Object JavaDoc o,
155                                  EntityInput input,
156                                  int startField,
157                                  int endField,
158                                  int superLevel) {
159         ((Enhanced) o).bdbReadNonKeyFields
160             (input, startField, endField, superLevel);
161     }
162
163     public Object JavaDoc getField(Object JavaDoc o,
164                            int field,
165                            int superLevel,
166                            boolean isSecField) {
167         return ((Enhanced) o).bdbGetField(o, field, superLevel, isSecField);
168     }
169
170     public void setField(Object JavaDoc o,
171                          int field,
172                          int superLevel,
173                          boolean isSecField,
174                          Object JavaDoc value) {
175         ((Enhanced) o).bdbSetField(o, field, superLevel, isSecField, value);
176     }
177 }
178
Popular Tags