| 1 |
/*- |
| 2 |
* See the file LICENSE for redistribution information. |
| 3 |
* |
| 4 |
* Copyright (c) 2002, 2013 Oracle and/or its affiliates. All rights reserved. |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
package com.sleepycat.persist.impl; |
| 9 |
|
| 10 |
import java.lang.reflect.Array; |
| 11 |
import java.lang.reflect.Modifier; |
| 12 |
import java.util.Collections; |
| 13 |
import java.util.HashMap; |
| 14 |
import java.util.List; |
| 15 |
import java.util.Map; |
| 16 |
|
| 17 |
import com.sleepycat.compat.DbCompat; |
| 18 |
|
| 19 |
/** |
| 20 |
* Implements Accessor for a complex persistent class. |
| 21 |
* |
| 22 |
* @author Mark Hayes |
| 23 |
*/ |
| 24 |
public class EnhancedAccessor implements Accessor { |
| 25 |
|
| 26 |
private static final Map<String, Enhanced> classRegistry = |
| 27 |
Collections.synchronizedMap(new HashMap<String, Enhanced>()); |
| 28 |
|
| 29 |
/* Is public for unit tests. */ |
| 30 |
public static final boolean EXPECT_ENHANCED = |
| 31 |
"true".equals(System.getProperty("expectEnhanced")); |
| 32 |
|
| 33 |
private Enhanced prototype; |
| 34 |
private Format priKeyFormat; |
| 35 |
private Format[] compositeKeyFormats; |
| 36 |
private Class type; |
| 37 |
|
| 38 |
/** |
| 39 |
* Registers a prototype instance, and should be called during |
| 40 |
* initialization of the prototype class. The prototype may be null for |
| 41 |
* an abstract class. |
| 42 |
*/ |
| 43 |
public static void registerClass(String className, Enhanced prototype) { |
| 44 |
classRegistry.put(className, prototype); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Returns whether a given class is a (registered) enhanced class. |
| 49 |
*/ |
| 50 |
static boolean isEnhanced(Class type) { |
| 51 |
boolean enhanced = classRegistry.containsKey(type.getName()); |
| 52 |
if (!enhanced && EXPECT_ENHANCED) { |
| 53 |
throw new IllegalStateException |
| 54 |
("Test was run with expectEnhanced=true but class " + |
| 55 |
type.getName() + " is not enhanced"); |
| 56 |
} |
| 57 |
return enhanced; |
| 58 |
} |
| 59 |
|
| 60 |
private EnhancedAccessor(Class 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 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 |
FieldInfo priKeyField = declaringFormat.getPriKeyFieldInfo(); |
| 79 |
if (priKeyField != null) { |
| 80 |
priKeyFormat = catalog.getFormat(priKeyField.getClassName()); |
| 81 |
break; |
| 82 |
} else { |
| 83 |
declaringFormat = declaringFormat.getComplexSuper(); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Creates an accessor for a composite key type. |
| 90 |
*/ |
| 91 |
EnhancedAccessor(Catalog catalog, Class type, List<FieldInfo> fieldInfos) { |
| 92 |
this(type); |
| 93 |
final int nFields = fieldInfos.size(); |
| 94 |
compositeKeyFormats = new Format[nFields]; |
| 95 |
for (int i = 0; i < nFields; i += 1) { |
| 96 |
compositeKeyFormats[i] = |
| 97 |
catalog.getFormat(fieldInfos.get(i).getClassName()); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
public Object newInstance() { |
| 102 |
if (prototype == null) { |
| 103 |
/* Abstract class -- internal error. */ |
| 104 |
throw DbCompat.unexpectedState(); |
| 105 |
} |
| 106 |
return prototype.bdbNewInstance(); |
| 107 |
} |
| 108 |
|
| 109 |
public Object newArray(int len) { |
| 110 |
if (prototype == null) { |
| 111 |
/* Abstract class -- use reflection for now. */ |
| 112 |
return Array.newInstance(type, len); |
| 113 |
} |
| 114 |
return prototype.bdbNewArray(len); |
| 115 |
} |
| 116 |
|
| 117 |
public boolean isPriKeyFieldNullOrZero(Object o) { |
| 118 |
if (priKeyFormat == null) { |
| 119 |
throw DbCompat.unexpectedState |
| 120 |
("No primary key: " + o.getClass().getName()); |
| 121 |
} |
| 122 |
return ((Enhanced) o).bdbIsPriKeyFieldNullOrZero(); |
| 123 |
} |
| 124 |
|
| 125 |
public void writePriKeyField(Object o, EntityOutput output) |
| 126 |
throws RefreshException { |
| 127 |
|
| 128 |
if (priKeyFormat == null) { |
| 129 |
throw DbCompat.unexpectedState |
| 130 |
("No primary key: " + o.getClass().getName()); |
| 131 |
} |
| 132 |
((Enhanced) o).bdbWritePriKeyField(output, priKeyFormat); |
| 133 |
} |
| 134 |
|
| 135 |
public void readPriKeyField(Object o, EntityInput input) |
| 136 |
throws RefreshException { |
| 137 |
|
| 138 |
if (priKeyFormat == null) { |
| 139 |
throw DbCompat.unexpectedState |
| 140 |
("No primary key: " + o.getClass().getName()); |
| 141 |
} |
| 142 |
((Enhanced) o).bdbReadPriKeyField(input, priKeyFormat); |
| 143 |
} |
| 144 |
|
| 145 |
public void writeSecKeyFields(Object o, EntityOutput output) |
| 146 |
throws RefreshException { |
| 147 |
|
| 148 |
((Enhanced) o).bdbWriteSecKeyFields(output); |
| 149 |
} |
| 150 |
|
| 151 |
public void readSecKeyFields(Object o, |
| 152 |
EntityInput input, |
| 153 |
int startField, |
| 154 |
int endField, |
| 155 |
int superLevel) |
| 156 |
throws RefreshException { |
| 157 |
|
| 158 |
((Enhanced) o).bdbReadSecKeyFields |
| 159 |
(input, startField, endField, superLevel); |
| 160 |
} |
| 161 |
|
| 162 |
public void writeNonKeyFields(Object o, EntityOutput output) |
| 163 |
throws RefreshException { |
| 164 |
|
| 165 |
((Enhanced) o).bdbWriteNonKeyFields(output); |
| 166 |
} |
| 167 |
|
| 168 |
public void readNonKeyFields(Object o, |
| 169 |
EntityInput input, |
| 170 |
int startField, |
| 171 |
int endField, |
| 172 |
int superLevel) |
| 173 |
throws RefreshException { |
| 174 |
|
| 175 |
((Enhanced) o).bdbReadNonKeyFields |
| 176 |
(input, startField, endField, superLevel); |
| 177 |
} |
| 178 |
|
| 179 |
public void writeCompositeKeyFields(Object o, EntityOutput output) |
| 180 |
throws RefreshException { |
| 181 |
|
| 182 |
((Enhanced) o).bdbWriteCompositeKeyFields(output, compositeKeyFormats); |
| 183 |
} |
| 184 |
|
| 185 |
public void readCompositeKeyFields(Object o, EntityInput input) |
| 186 |
throws RefreshException { |
| 187 |
|
| 188 |
((Enhanced) o).bdbReadCompositeKeyFields(input, compositeKeyFormats); |
| 189 |
} |
| 190 |
|
| 191 |
public Object getField(Object o, |
| 192 |
int field, |
| 193 |
int superLevel, |
| 194 |
boolean isSecField) { |
| 195 |
return ((Enhanced) o).bdbGetField(o, field, superLevel, isSecField); |
| 196 |
} |
| 197 |
|
| 198 |
public void setField(Object o, |
| 199 |
int field, |
| 200 |
int superLevel, |
| 201 |
boolean isSecField, |
| 202 |
Object value) { |
| 203 |
((Enhanced) o).bdbSetField(o, field, superLevel, isSecField, value); |
| 204 |
} |
| 205 |
|
| 206 |
public void setPriField(Object o, Object value) { |
| 207 |
((Enhanced) o).bdbSetPriField(o, value); |
| 208 |
} |
| 209 |
} |