KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > genclass > GenClassAccessor


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.speedo.genclass;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Collection JavaDoc;
25
26
27 import org.objectweb.jorm.api.PExceptionIO;
28 import org.objectweb.jorm.api.PGenClassAccessor;
29 import org.objectweb.jorm.api.PIndexedElem;
30 import org.objectweb.jorm.util.api.Loggable;
31 import org.objectweb.perseus.persistence.api.State;
32 import org.objectweb.speedo.api.Debug;
33 import org.objectweb.speedo.genclass.api.SpeedoGenClassCoherence;
34 import org.objectweb.speedo.genclass.api.SpeedoGenClassProxy;
35 import org.objectweb.speedo.mim.api.SpeedoAccessor;
36 import org.objectweb.speedo.mim.lib.BasicSpeedoAccessor;
37 import org.objectweb.speedo.pm.api.ProxyManager;
38 import org.objectweb.util.monolog.api.BasicLevel;
39 import org.objectweb.util.monolog.api.Logger;
40
41 /**
42  * This class represents an accessor of a genclass structure. This accessor is
43  * used in case of the generic class is persistent.
44  *
45  * @author S.Chassande-Barrioz
46  */

47 public abstract class GenClassAccessor
48     extends BasicSpeedoAccessor
49     implements PGenClassAccessor, SpeedoGenClassCoherence {
50
51     /**
52      * The SpeedoProxy attached to this state representation.
53      */

54     public SpeedoGenClassProxy jdoProxy;
55
56     /**
57      * Indicates if this accessor support the dela mode
58      */

59     public boolean supportDelta = true;
60
61     /**
62      * A temporary variable which is used to search an element into the
63      * "elements" list.
64      */

65     protected GenClassElement tmpelem = null;
66
67     protected Logger logger;
68
69     /**
70      * The ArrayList used to store the indexed elements of the genclass. Then
71      * this list contains also the deleted elements.
72      */

73     public List JavaDoc elements = null;
74     
75     public List JavaDoc deltaForMerge = null;
76
77     public GenClassAccessor(SpeedoGenClassProxy _jdoProxy) {
78         elements = new ArrayList JavaDoc();
79         this.jdoProxy = _jdoProxy;
80         this.proxy = jdoProxy;
81         this.tmpelem = (GenClassElement) createPIndexedElem();
82     }
83
84     public abstract void loadFieldsFromAccessor(SpeedoAccessor sa);
85     
86     public abstract PIndexedElem createPIndexedElem(GenClassAccessor gca);
87
88     public abstract void setElements(Object JavaDoc o);
89
90     public Logger getLogger() {
91         if (logger == null) {
92             logger = ((Loggable) jdoProxy).getLogger();
93         }
94         return logger;
95     }
96
97     /**
98      * When the gen class is flushed to the data support (parameter = true),
99      * the internal structure is clean: - really remove the deleted element -
100      * mark the genclass as supporting the delta
101      */

102     public void setFlushed(boolean val) {
103         super.setFlushed(val);
104         if (val == true) {
105             supportDelta = true;
106             Iterator JavaDoc it = elements.iterator();
107             while (it.hasNext()) {
108                 GenClassElement gce = (GenClassElement) it.next();
109                 if (gce.getElemStatus() == PIndexedElem.ELEM_DELETED) {
110                     //Remove the deleted element
111
it.remove();
112                 }
113             }
114         }
115     }
116
117     /**
118      * At the end of the working set, the writing mode is initialized
119      * (deltaSupported), and the reference to the persistent are unswilled
120      */

121     public void workingSetClosed() {
122         if (Debug.ON) {
123             ((Loggable) jdoProxy).getLogger().log(
124                     BasicLevel.DEBUG, "workingSetClosed()");
125         }
126         supportDelta = true;
127         for(Iterator JavaDoc it = elements.iterator(); it.hasNext();) {
128             GenClassElement gce = (GenClassElement) it.next();
129             if (gce.getElemStatus() == PIndexedElem.ELEM_DELETED) {
130                 //Remove the deleted element
131
it.remove();
132             }
133         }
134     }
135
136     public void unSwizzle() {
137         for(Iterator JavaDoc it = elements.iterator(); it.hasNext();) {
138             GenClassElement gce = (GenClassElement) it.next();
139             gce.unSwizzle();
140         }
141     }
142
143     public State merge(State oldState) {
144         if (deltaForMerge == null || !isToMerge) {
145             return super.merge(oldState);
146         }
147         GenClassAccessor gca = (GenClassAccessor) oldState;
148         for(int i=0; i<deltaForMerge.size(); i++) {
149             GenClassElement gce = (GenClassElement) deltaForMerge.get(i);
150             switch(gce.getStatusForMerge()) {
151             case PIndexedElem.ELEM_CREATED:
152                 gca.elements.add(gce);
153                 break;
154             case PIndexedElem.ELEM_DELETED:
155                 int idx = gca.elements.indexOf(gce);
156                 if (idx == -1) {
157                     logger.log(BasicLevel.WARN,
158                             "Deleted GenClass element '" + gce + "' not found during merging among "
159                             + gca.elements);
160                 } else {
161                     gca.elements.remove(idx);
162                 }
163                 break;
164             case PIndexedElem.ELEM_MODIFIED:
165                 idx = gca.elements.indexOf(gce);
166                 if (idx == -1) {
167                     logger.log(BasicLevel.WARN,
168                         "Modified GenClass element not found during merging: "
169                         + gce);
170                 } else {
171                     gca.elements.set(idx, gce);
172                 }
173                 break;
174             }
175             gce.cleanStatusForMerge();
176         }
177         deltaForMerge = null;
178         return super.merge(oldState);
179     }
180     
181     public void makeToMerge(Object JavaDoc thinLock) {
182         if (!isToMerge) {
183             for(int i=0; i<elements.size(); i++) {
184                 elements.set(i, ((GenClassElement) elements.get(i)).cloneGCE());
185             }
186         }
187         super.makeToMerge(thinLock);
188     }
189     
190     public void loadFields(ProxyManager pm, long[] fields) {
191     }
192
193     public abstract void deletePersistent(ProxyManager pm);
194
195     public abstract void makePersistent(ProxyManager pm);
196
197     public abstract void detachCopy(ProxyManager pm, Map JavaDoc map, SpeedoAccessor fieldsClone, Collection JavaDoc fgHints);
198     
199     public abstract void attachCopy(ProxyManager pm, Map JavaDoc map, SpeedoAccessor fieldsClone, boolean makeTransactional);
200     
201     public abstract void refresh(ProxyManager pm, Map JavaDoc map, Collection JavaDoc fgHints);
202     
203     public abstract void retrieve(ProxyManager pm, Map JavaDoc map, Collection JavaDoc fgHints);
204     
205     // IMPLEMENTATION OF THE SpeedoGenClassCoherence INTERFACE //
206
//---------------------------------------------------------//
207

208     public abstract boolean speedoAdd(Object JavaDoc elemToAdd, Object JavaDoc hints);
209
210     public abstract boolean speedoRemove(Object JavaDoc elemToRemove, Object JavaDoc hints);
211
212     // IMPLEMENTATION OF THE PGenClassAccessor INTERFACE //
213
//---------------------------------------------------//
214

215     public PIndexedElem createPIndexedElem() {
216         return createPIndexedElem(this);
217     }
218
219     public Object JavaDoc getMemoryInstance() {
220         return jdoProxy;
221     }
222
223     public void paAdd(PIndexedElem elem, Object JavaDoc conn) throws PExceptionIO {
224         // the elem is read from the DS, set it to unmodified.
225
((GenClassElement) elem).setStatus(PIndexedElem.ELEM_UNMODIFIED);
226         elements.add(elem);
227     }
228
229     public int paGetNbElem() {
230         return elements.size();
231     }
232
233     public Iterator JavaDoc paIterator() {
234         return elements.iterator();
235     }
236
237     public boolean paDeltaSupported() {
238         return supportDelta;
239     }
240
241     public void paSetNbElem(int nbelem) {
242         elements = (nbelem < 0 ? new ArrayList JavaDoc() : new ArrayList JavaDoc(nbelem));
243     }
244
245 }
246
Popular Tags