KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdo > sco > PersistenceDelegateManager


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdo.sco;
13
14 import java.beans.DefaultPersistenceDelegate JavaDoc;
15 import java.beans.Encoder JavaDoc;
16 import java.beans.Expression JavaDoc;
17 import java.beans.Statement JavaDoc;
18 import java.util.*;
19
20 /**
21  * This is a util class to assist with java.beans persistence for JDO Genie
22  * SCOs. Call register with an Encoder to instal persistence delegates to
23  * handle all the JDO Genie SCO classes.
24  *
25  * @see #register(java.beans.Encoder)
26  * @see java.beans.Encoder
27  * @see java.beans.XMLEncoder
28  */

29 public class PersistenceDelegateManager {
30
31     /**
32      * Set persistence delegates on provided encoder for JDO Genie SCO instances.
33      */

34     public static void register(Encoder JavaDoc encoder) {
35         encoder.setPersistenceDelegate(com.versant.core.jdo.sco.Date.class,
36                 new SCOPersistenceDelegate(java.util.Date JavaDoc.class));
37
38         encoder.setPersistenceDelegate(SCOList.class, new SCOListPD(ArrayList.class));
39         encoder.setPersistenceDelegate(SCOArrayList.class, new SCOListPD(ArrayList.class));
40         encoder.setPersistenceDelegate(SCOVector.class, new SCOListPD(Vector.class));
41
42         encoder.setPersistenceDelegate(SCOHashMap.class, new SCOMapPD(HashMap.class));
43         encoder.setPersistenceDelegate(SCOHashtable.class, new SCOMapPD(Hashtable.class));
44         encoder.setPersistenceDelegate(SCOTreeMap.class, new SCOMapPD(TreeMap.class));
45         encoder.setPersistenceDelegate(SCOTreeSet.class, new SCOMapPD(TreeSet.class));
46
47         encoder.setPersistenceDelegate(SCOHashSet.class, new SCOCollectionPD(HashSet.class));
48         encoder.setPersistenceDelegate(SCOLinkedList.class, new SCOCollectionPD(LinkedList.class));
49     }
50
51     private static void invokeStatement(Object JavaDoc instance, String JavaDoc methodName,
52                                         Object JavaDoc[] args, Encoder JavaDoc out) {
53         out.writeStatement(new Statement JavaDoc(instance, methodName, args));
54     }
55
56     private static boolean equals(Object JavaDoc o1, Object JavaDoc o2) {
57         return (o1 == null) ? (o2 == null) : o1.equals(o2);
58     }
59
60     public static class SCOPersistenceDelegate extends DefaultPersistenceDelegate JavaDoc {
61
62         Class JavaDoc javaType;
63
64         public SCOPersistenceDelegate(Class JavaDoc javaType) {
65             this.javaType = javaType;
66         }
67
68         protected Expression JavaDoc instantiate(Object JavaDoc oldInstance, Encoder JavaDoc out) {
69
70             return new Expression JavaDoc(oldInstance,
71                     javaType,
72                     "new",
73                     new Object JavaDoc[]{});
74         }
75
76         protected boolean mutatesTo(Object JavaDoc oldInstance, Object JavaDoc newInstance) {
77             if (oldInstance != null && newInstance != null) {
78                 return true;
79             }
80             return super.mutatesTo(oldInstance, newInstance);
81         }
82     }
83
84     public static class SCOCollectionPD extends SCOPersistenceDelegate {
85
86         public SCOCollectionPD(Class JavaDoc javaType) {
87             super(javaType);
88         }
89
90         protected void initialize(Class JavaDoc type, Object JavaDoc oldInstance,
91                                   Object JavaDoc newInstance, Encoder JavaDoc out) {
92             java.util.Collection JavaDoc oldO = (java.util.Collection JavaDoc) oldInstance;
93             java.util.Collection JavaDoc newO = (java.util.Collection JavaDoc) newInstance;
94
95             if (newO.size() != 0) {
96                 PersistenceDelegateManager.invokeStatement(oldInstance, "clear", new Object JavaDoc[]{}, out);
97             }
98             for (Iterator i = oldO.iterator(); i.hasNext();) {
99                 PersistenceDelegateManager.invokeStatement(oldInstance, "add", new Object JavaDoc[]{
100                     i.next()}, out);
101             }
102         }
103     }
104
105     public static class SCOListPD extends SCOPersistenceDelegate {
106
107         public SCOListPD(Class JavaDoc javaType) {
108             super(javaType);
109         }
110
111         protected void initialize(Class JavaDoc type, Object JavaDoc oldInstance,
112                                   Object JavaDoc newInstance, Encoder JavaDoc out) {
113
114             java.util.List JavaDoc oldO = (java.util.List JavaDoc) oldInstance;
115             java.util.List JavaDoc newO = (java.util.List JavaDoc) newInstance;
116             int oldSize = oldO.size();
117             int newSize = (newO == null) ? 0 : newO.size();
118             if (oldSize < newSize) {
119                 PersistenceDelegateManager.invokeStatement(oldInstance, "clear", new Object JavaDoc[]{}, out);
120                 newSize = 0;
121             }
122             for (int i = 0; i < newSize; i++) {
123                 Object JavaDoc index = new Integer JavaDoc(i);
124
125                 Expression JavaDoc oldGetExp = new Expression JavaDoc(oldInstance, "get", new Object JavaDoc[]{
126                     index});
127                 Expression JavaDoc newGetExp = new Expression JavaDoc(newInstance, "get", new Object JavaDoc[]{
128                     index});
129                 try {
130                     Object JavaDoc oldValue = oldGetExp.getValue();
131                     Object JavaDoc newValue = newGetExp.getValue();
132                     out.writeExpression(oldGetExp);
133                     if (!PersistenceDelegateManager.equals(newValue, out.get(oldValue))) {
134                         PersistenceDelegateManager.invokeStatement(oldInstance, "set", new Object JavaDoc[]{
135                             index, oldValue}, out);
136                     }
137                 } catch (Exception JavaDoc e) {
138                     out.getExceptionListener().exceptionThrown(e);
139                 }
140             }
141             for (int i = newSize; i < oldSize; i++) {
142                 PersistenceDelegateManager.invokeStatement(oldInstance, "add", new Object JavaDoc[]{
143                     oldO.get(i)}, out);
144             }
145         }
146
147     }
148
149     public static class SCOMapPD extends SCOPersistenceDelegate {
150
151         public SCOMapPD(Class JavaDoc javaType) {
152             super(javaType);
153         }
154
155         protected void initialize(Class JavaDoc type, Object JavaDoc oldInstance,
156                                   Object JavaDoc newInstance, Encoder JavaDoc out) {
157             java.util.Map JavaDoc oldMap = (java.util.Map JavaDoc) oldInstance;
158             java.util.Map JavaDoc newMap = (java.util.Map JavaDoc) newInstance;
159             // Remove the new elements.
160
// Do this first otherwise we undo the adding work.
161
if (newMap != null) {
162                 java.util.Iterator JavaDoc newKeys = newMap.keySet().iterator();
163                 while (newKeys.hasNext()) {
164                     Object JavaDoc newKey = newKeys.next();
165                     // PENDING: This "key" is not in the right environment.
166
if (!oldMap.containsKey(newKey)) {
167                         PersistenceDelegateManager.invokeStatement(oldInstance, "remove", new Object JavaDoc[]{
168                             newKey}, out);
169                     }
170                 }
171             }
172             // Add the new elements.
173
java.util.Iterator JavaDoc oldKeys = oldMap.keySet().iterator();
174             while (oldKeys.hasNext()) {
175                 Object JavaDoc oldKey = oldKeys.next();
176
177                 Expression JavaDoc oldGetExp = new Expression JavaDoc(oldInstance, "get", new Object JavaDoc[]{
178                     oldKey});
179                 // Pending: should use newKey.
180
Expression JavaDoc newGetExp = new Expression JavaDoc(newInstance, "get", new Object JavaDoc[]{
181                     oldKey});
182                 try {
183                     Object JavaDoc oldValue = oldGetExp.getValue();
184                     Object JavaDoc newValue = newGetExp.getValue();
185                     out.writeExpression(oldGetExp);
186                     if (!PersistenceDelegateManager.equals(newValue, out.get(oldValue))) {
187                         PersistenceDelegateManager.invokeStatement(oldInstance, "put", new Object JavaDoc[]{
188                             oldKey, oldValue}, out);
189                     }
190                 } catch (Exception JavaDoc e) {
191                     out.getExceptionListener().exceptionThrown(e);
192                 }
193             }
194         }
195     }
196 }
197
Popular Tags