KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: ProxiedFormat.java,v 1.19 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.util.IdentityHashMap JavaDoc;
13 import java.util.Map JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import com.sleepycat.persist.model.PersistentProxy;
17 import com.sleepycat.persist.raw.RawObject;
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 JavaDoc proxyClassName;
30
31     ProxiedFormat(Class JavaDoc proxiedType, String JavaDoc proxyClassName) {
32         super(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 JavaDoc getProxyClassName() {
43         if (proxyClassName != null) {
44             return proxyClassName;
45         } else {
46             assert proxyFormat != null;
47             return proxyFormat.getClassName();
48         }
49     }
50
51     @Override JavaDoc
52     void collectRelatedFormats(Catalog catalog,
53                                Map JavaDoc<String JavaDoc,Format> newFormats) {
54         /* Collect the proxy format. */
55         assert proxyClassName != null;
56         catalog.createFormat(proxyClassName, newFormats);
57     }
58
59     @Override JavaDoc
60     void initialize(Catalog catalog) {
61         /* Set the proxy format for a new (never initialized) format. */
62         if (proxyFormat == null) {
63             assert proxyClassName != null;
64             proxyFormat = catalog.getFormat(proxyClassName);
65         }
66         /* Make the linkage from proxy format to proxied format. */
67         proxyFormat.setProxiedFormat(this);
68     }
69     
70     @Override JavaDoc
71     Object JavaDoc newArray(int len) {
72         return Array.newInstance(getType(), len);
73     }
74
75     @Override JavaDoc
76     public Object JavaDoc 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 JavaDoc
89     public Object JavaDoc readObject(Object JavaDoc o, EntityInput input, boolean rawAccess) {
90         if (rawAccess) {
91             o = proxyFormat.getReader().readObject(o, input, true);
92         }
93         /* Else, do nothing here -- newInstance reads the value. */
94         return o;
95     }
96
97     @Override JavaDoc
98     void writeObject(Object JavaDoc 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 JavaDoc
110     Object JavaDoc convertRawObject(Catalog catalog,
111                             boolean rawAccess,
112                             RawObject rawObject,
113                             IdentityHashMap JavaDoc converted) {
114         PersistentProxy proxy = (PersistentProxy) proxyFormat.convertRawObject
115             (catalog, rawAccess, rawObject, converted);
116         Object JavaDoc o = proxy.convertProxy();
117         converted.put(rawObject, o);
118         return o;
119     }
120
121     @Override JavaDoc
122     void skipContents(RecordInput input) {
123         proxyFormat.skipContents(input);
124     }
125
126     @Override JavaDoc
127     void copySecMultiKey(RecordInput input, Format keyFormat, Set JavaDoc results) {
128         CollectionProxy.copyElements(input, this, keyFormat, results);
129     }
130
131     @Override JavaDoc
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