1 8 9 package com.sleepycat.persist.impl; 10 11 import java.lang.reflect.Array ; 12 import java.util.IdentityHashMap ; 13 import java.util.Map ; 14 import java.util.Set ; 15 16 import com.sleepycat.persist.model.PersistentProxy; 17 import com.sleepycat.persist.raw.RawObject; 18 19 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(Class proxiedType, String proxyClassName) { 32 super(proxiedType); 33 this.proxyClassName = proxyClassName; 34 } 35 36 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 @Override 52 void collectRelatedFormats(Catalog catalog, 53 Map <String ,Format> newFormats) { 54 55 assert proxyClassName != null; 56 catalog.createFormat(proxyClassName, newFormats); 57 } 58 59 @Override 60 void initialize(Catalog catalog) { 61 62 if (proxyFormat == null) { 63 assert proxyClassName != null; 64 proxyFormat = catalog.getFormat(proxyClassName); 65 } 66 67 proxyFormat.setProxiedFormat(this); 68 } 69 70 @Override 71 Object newArray(int len) { 72 return Array.newInstance(getType(), len); 73 } 74 75 @Override 76 public Object newInstance(EntityInput input, boolean rawAccess) { 77 Reader reader = proxyFormat.getReader(); 78 if (rawAccess) { 79 return reader.newInstance(null, true); 80 } else { 81 PersistentProxy proxy = 82 (PersistentProxy) reader.newInstance(null, false); 83 proxy = (PersistentProxy) reader.readObject(proxy, input, false); 84 return proxy.convertProxy(); 85 } 86 } 87 88 @Override 89 public Object readObject(Object o, EntityInput input, boolean rawAccess) { 90 if (rawAccess) { 91 o = proxyFormat.getReader().readObject(o, input, true); 92 } 93 94 return o; 95 } 96 97 @Override 98 void writeObject(Object o, EntityOutput output, boolean rawAccess) { 99 if (rawAccess) { 100 proxyFormat.writeObject(o, output, true); 101 } else { 102 PersistentProxy proxy = 103 (PersistentProxy) proxyFormat.newInstance(null, false); 104 proxy.initializeProxy(o); 105 proxyFormat.writeObject(proxy, output, false); 106 } 107 } 108 109 @Override 110 Object convertRawObject(Catalog catalog, 111 boolean rawAccess, 112 RawObject rawObject, 113 IdentityHashMap converted) { 114 PersistentProxy proxy = (PersistentProxy) proxyFormat.convertRawObject 115 (catalog, rawAccess, rawObject, converted); 116 Object o = proxy.convertProxy(); 117 converted.put(rawObject, o); 118 return o; 119 } 120 121 @Override 122 void skipContents(RecordInput input) { 123 proxyFormat.skipContents(input); 124 } 125 126 @Override 127 void copySecMultiKey(RecordInput input, Format keyFormat, Set results) { 128 CollectionProxy.copyElements(input, this, keyFormat, results); 129 } 130 131 @Override 132 boolean evolve(Format newFormatParam, Evolver evolver) { 133 if (!(newFormatParam instanceof ProxiedFormat)) { 134 evolver.addEvolveError 135 (this, newFormatParam, null, 136 "A proxied class may not be changed to a different type"); 137 return false; 138 } 139 ProxiedFormat newFormat = (ProxiedFormat) newFormatParam; 140 if (!evolver.evolveFormat(proxyFormat)) { 141 return false; 142 } 143 Format newProxyFormat = proxyFormat.getLatestVersion(); 144 if (!newProxyFormat.getClassName().equals 145 (newFormat.getProxyClassName())) { 146 evolver.addEvolveError 147 (this, newFormat, null, 148 "The proxy class for this type has been changed from: " + 149 newProxyFormat.getClassName() + " to: " + 150 newFormat.getProxyClassName()); 151 return false; 152 } 153 if (newProxyFormat != proxyFormat) { 154 evolver.useEvolvedFormat(this, this, newFormat); 155 } else { 156 evolver.useOldFormat(this, newFormat); 157 } 158 return true; 159 } 160 } 161 | Popular Tags |