Debian Codegraph @

lang/java/src/com/sleepycat/persist/impl/ProxiedFormat.java

raw
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.util.Map;
12 import java.util.Set;
13
14 import com.sleepycat.persist.model.EntityModel;
15 import com.sleepycat.persist.model.PersistentProxy;
16 import com.sleepycat.persist.raw.RawObject;
17 import java.util.IdentityHashMap;
18
19 /**
20 * Format for types proxied by a PersistentProxy.
21 *
22 * @author Mark Hayes
23 */
24 public class ProxiedFormat extends Format {
25
26 private static final long serialVersionUID = -1000032651995478768L;
27
28 private Format proxyFormat;
29 private transient String proxyClassName;
30
31 ProxiedFormat(Catalog catalog, Class proxiedType, String proxyClassName) {
32 super(catalog, proxiedType);
33 this.proxyClassName = proxyClassName;
34 }
35
36 /**
37 * Returns the proxy class name. The proxyClassName field is non-null for
38 * a constructed object and null for a de-serialized object. Whenever the
39 * proxyClassName field is null (for a de-serialized object), the
40 * proxyFormat will be non-null.
41 */
42 private String getProxyClassName() {
43 if (proxyClassName != null) {
44 return proxyClassName;
45 } else {
46 assert proxyFormat != null;
47 return proxyFormat.getClassName();
48 }
49 }
50
51 /**
52 * In the future if we implement container proxies, which support nested
53 * references to the container, then we will return false if this is a
54 * container proxy. [#15815]
55 */
56 @Override
57 boolean areNestedRefsProhibited() {
58 return true;
59 }
60
61 @Override
62 void collectRelatedFormats(Catalog catalog,
63 Map<String, Format> newFormats) {
64 /* Collect the proxy format. */
65 assert proxyClassName != null;
66 catalog.createFormat(proxyClassName, newFormats);
67 }
68
69 @Override
70 void initialize(Catalog catalog, EntityModel model, int initVersion) {
71 /* Set the proxy format for a new (never initialized) format. */
72 if (proxyFormat == null) {
73 assert proxyClassName != null;
74 proxyFormat = catalog.getFormat(proxyClassName);
75 }
76 /* Make the linkage from proxy format to proxied format. */
77 proxyFormat.setProxiedFormat(this);
78 }
79
80 @Override
81 Object newArray(int len) {
82 return Array.newInstance(getType(), len);
83 }
84
85 @Override
86 public Object newInstance(EntityInput input, boolean rawAccess)
87 throws RefreshException {
88
89 Reader reader = proxyFormat.getReader();
90 if (rawAccess) {
91 return reader.newInstance(null, true);
92 } else {
93
94 /*
95 * Note that the read object will not be a PersistentProxy if
96 * a class converter mutation is used. In this case, the reader
97 * will be ConverterReader. ConverterReader.readObject
98 * will call ProxiedFormat.convertRawObject, which will call
99 * PersistentProxy.convertProxy to convert the proxy. So we do not
100 * need another call to the convertProxy method. [#19312]
101 */
102 Object o = reader.readObject(reader.newInstance(null, false),
103 input, false);
104 if (o instanceof PersistentProxy) {
105 o = ((PersistentProxy) o).convertProxy();
106 }
107 return o;
108 }
109 }
110
111 @Override
112 public Object readObject(Object o, EntityInput input, boolean rawAccess)
113 throws RefreshException {
114
115 if (rawAccess) {
116 o = proxyFormat.getReader().readObject(o, input, true);
117 }
118 /* Else, do nothing here -- newInstance reads the value. */
119 return o;
120 }
121
122 @Override
123 void writeObject(Object o, EntityOutput output, boolean rawAccess)
124 throws RefreshException {
125
126 if (rawAccess) {
127 proxyFormat.writeObject(o, output, true);
128 } else {
129 PersistentProxy proxy =
130 (PersistentProxy) proxyFormat.newInstance(null, false);
131 proxy.initializeProxy(o);
132 proxyFormat.writeObject(proxy, output, false);
133 }
134 }
135
136 @Override
137 Object convertRawObject(Catalog catalog,
138 boolean rawAccess,
139 RawObject rawObject,
140 IdentityHashMap converted)
141 throws RefreshException {
142
143 PersistentProxy proxy = (PersistentProxy) proxyFormat.convertRawObject
144 (catalog, rawAccess, rawObject, converted);
145 Object o = proxy.convertProxy();
146 converted.put(rawObject, o);
147 return o;
148 }
149
150 @Override
151 void skipContents(RecordInput input)
152 throws RefreshException {
153
154 proxyFormat.skipContents(input);
155 }
156
157 @Override
158 void copySecMultiKey(RecordInput input, Format keyFormat, Set results)
159 throws RefreshException {
160
161 CollectionProxy.copyElements(input, this, keyFormat, results);
162 }
163
164 @Override
165 boolean evolve(Format newFormatParam, Evolver evolver) {
166 if (!(newFormatParam instanceof ProxiedFormat)) {
167
168 /*
169 * A workaround for reading the BigDecimal data stored by
170 * BigDecimal proxy before je4.1.
171 *
172 * The BigDecimal proxy has a proxied format for BigDecimal, which
173 * is a built-in SimpleType. We will evolve this ProxiedFormat of
174 * BigDecimal to the SimpleFormat. In other words, the conversion
175 * from a BigDecimal proxied format to a BigDecimal SimpleFormat is
176 * allowed, and the old format can be used as the reader of the old
177 * data.
178 */
179 if (newFormatParam.allowEvolveFromProxy()) {
180 evolver.useEvolvedFormat(this, this, newFormatParam);
181 return true;
182 }
183 evolver.addEvolveError
184 (this, newFormatParam, null,
185 "A proxied class may not be changed to a different type");
186 return false;
187 }
188 ProxiedFormat newFormat = (ProxiedFormat) newFormatParam;
189 if (!evolver.evolveFormat(proxyFormat)) {
190 return false;
191 }
192 Format newProxyFormat = proxyFormat.getLatestVersion();
193 if (!newProxyFormat.getClassName().equals
194 (newFormat.getProxyClassName())) {
195 evolver.addEvolveError
196 (this, newFormat, null,
197 "The proxy class for this type has been changed from: " +
198 newProxyFormat.getClassName() + " to: " +
199 newFormat.getProxyClassName());
200 return false;
201 }
202 if (newProxyFormat != proxyFormat) {
203 evolver.useEvolvedFormat(this, this, newFormat);
204 } else {
205 evolver.useOldFormat(this, newFormat);
206 }
207 return true;
208 }
209 }