ProxiedFormat.java [plain text]
package com.sleepycat.persist.impl;
import java.lang.reflect.Array;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Set;
import com.sleepycat.persist.model.PersistentProxy;
import com.sleepycat.persist.raw.RawObject;
public class ProxiedFormat extends Format {
private static final long serialVersionUID = -1000032651995478768L;
private Format proxyFormat;
private transient String proxyClassName;
ProxiedFormat(Class proxiedType, String proxyClassName) {
super(proxiedType);
this.proxyClassName = proxyClassName;
}
private String getProxyClassName() {
if (proxyClassName != null) {
return proxyClassName;
} else {
assert proxyFormat != null;
return proxyFormat.getClassName();
}
}
@Override
boolean areNestedRefsProhibited() {
return true;
}
@Override
void collectRelatedFormats(Catalog catalog,
Map<String,Format> newFormats) {
assert proxyClassName != null;
catalog.createFormat(proxyClassName, newFormats);
}
@Override
void initialize(Catalog catalog, int initVersion) {
if (proxyFormat == null) {
assert proxyClassName != null;
proxyFormat = catalog.getFormat(proxyClassName);
}
proxyFormat.setProxiedFormat(this);
}
@Override
Object newArray(int len) {
return Array.newInstance(getType(), len);
}
@Override
public Object newInstance(EntityInput input, boolean rawAccess) {
Reader reader = proxyFormat.getReader();
if (rawAccess) {
return reader.newInstance(null, true);
} else {
PersistentProxy proxy =
(PersistentProxy) reader.newInstance(null, false);
proxy = (PersistentProxy) reader.readObject(proxy, input, false);
return proxy.convertProxy();
}
}
@Override
public Object readObject(Object o, EntityInput input, boolean rawAccess) {
if (rawAccess) {
o = proxyFormat.getReader().readObject(o, input, true);
}
return o;
}
@Override
void writeObject(Object o, EntityOutput output, boolean rawAccess) {
if (rawAccess) {
proxyFormat.writeObject(o, output, true);
} else {
PersistentProxy proxy =
(PersistentProxy) proxyFormat.newInstance(null, false);
proxy.initializeProxy(o);
proxyFormat.writeObject(proxy, output, false);
}
}
@Override
Object convertRawObject(Catalog catalog,
boolean rawAccess,
RawObject rawObject,
IdentityHashMap converted) {
PersistentProxy proxy = (PersistentProxy) proxyFormat.convertRawObject
(catalog, rawAccess, rawObject, converted);
Object o = proxy.convertProxy();
converted.put(rawObject, o);
return o;
}
@Override
void skipContents(RecordInput input) {
proxyFormat.skipContents(input);
}
@Override
void copySecMultiKey(RecordInput input, Format keyFormat, Set results) {
CollectionProxy.copyElements(input, this, keyFormat, results);
}
@Override
boolean evolve(Format newFormatParam, Evolver evolver) {
if (!(newFormatParam instanceof ProxiedFormat)) {
evolver.addEvolveError
(this, newFormatParam, null,
"A proxied class may not be changed to a different type");
return false;
}
ProxiedFormat newFormat = (ProxiedFormat) newFormatParam;
if (!evolver.evolveFormat(proxyFormat)) {
return false;
}
Format newProxyFormat = proxyFormat.getLatestVersion();
if (!newProxyFormat.getClassName().equals
(newFormat.getProxyClassName())) {
evolver.addEvolveError
(this, newFormat, null,
"The proxy class for this type has been changed from: " +
newProxyFormat.getClassName() + " to: " +
newFormat.getProxyClassName());
return false;
}
if (newProxyFormat != proxyFormat) {
evolver.useEvolvedFormat(this, this, newFormat);
} else {
evolver.useOldFormat(this, newFormat);
}
return true;
}
}