KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > perseus > persistence > concurrency > POptimisticConcurrencyManager


1 /**
2  * Copyright (C) 2003 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.perseus.persistence.concurrency;
19
20 import java.util.Iterator JavaDoc;
21
22 import org.objectweb.perseus.concurrency.optimistic.OptimisticConcurrencyManager;
23 import org.objectweb.perseus.concurrency.lib.TimeStamp;
24 import org.objectweb.perseus.concurrency.api.ConcurrencyException;
25 import org.objectweb.perseus.persistence.api.*;
26 import org.objectweb.perseus.cache.api.CacheEntry;
27 import org.objectweb.util.monolog.api.BasicLevel;
28
29 /**
30  *
31  * @author S.Chassande-Barrioz
32  */

33 public class POptimisticConcurrencyManager extends OptimisticConcurrencyManager {
34
35
36     public final static String JavaDoc STORAGE_MANAGER_BINDING = "storage-manager";
37     public final static String JavaDoc STATE_MANAGER_BINDING = "state-manager";
38
39     protected StateManager stateManager;
40
41     protected StorageManager storageManager;
42
43     //IMPLEMENTATION OF THE UserBindingControler INTERFACE //
44
//------------------------------------------------------//
45

46     public String JavaDoc[] listFc() {
47         String JavaDoc[] super_res = super.listFc();
48         String JavaDoc res[] = new String JavaDoc[super_res.length + 2];
49         for (int cpt = 0; cpt < super_res.length; cpt++) {
50             res[cpt] = super_res[cpt];
51         }
52         res[super_res.length + 0] = STORAGE_MANAGER_BINDING;
53         res[super_res.length + 1] = STATE_MANAGER_BINDING;
54         return res;
55     }
56
57     public Object JavaDoc lookupFc(String JavaDoc s) {
58         if (STORAGE_MANAGER_BINDING.equals(s)) {
59             return storageManager;
60         } else if (STATE_MANAGER_BINDING.equals(s)) {
61             return stateManager;
62         } else {
63             return super.lookupFc(s);
64         }
65     }
66
67     public void bindFc(String JavaDoc s, Object JavaDoc o) {
68         if (STORAGE_MANAGER_BINDING.equals(s)) {
69             storageManager = (StorageManager) o;
70         } else if (STATE_MANAGER_BINDING.equals(s)) {
71             stateManager = (StateManager) o;
72         } else super.bindFc(s, o);
73     }
74
75     public void unbindFc(String JavaDoc s) {
76         if (STORAGE_MANAGER_BINDING.equals(s)) {
77             storageManager = null;
78         } else if (STATE_MANAGER_BINDING.equals(s)) {
79             stateManager = null;
80         } else {
81             super.unbindFc(s);
82         }
83     }
84     public void finalize(Object JavaDoc arg0) {
85         WorkingSet ws = (WorkingSet) arg0;
86         boolean retainValues = ws.getWSRetainValues();
87         for (Iterator JavaDoc it = ws.entries().iterator(); it.hasNext();) {
88             State state = (State) it.next();
89             if (state == VirtualState.instance) {
90                 continue;
91             }
92             CacheEntry ce = state.getCacheEntry();
93             if (!stateManager.isUnexported(state)) {
94                 //The object has not been unexported (deleted)
95
if (retainValues) {
96                     if (stateManager.isDirty(state)) {
97                         if (stateManager.isToMerge(state)) {
98                             synchronized(ce) {
99                                 stateManager.setReferenceState(ce,
100                                     stateManager.merge(
101                                             stateManager.getReferenceState(ce),
102                                             state));
103                             }
104                         } else {
105                             stateManager.makeClean(state);
106                             stateManager.setReferenceState(ce, state);
107                         }
108                     }//else state is clean ==> nothing to do
109
} else if (stateManager.getReferenceState(ce) == state) {
110                     //forget the state used as reference state
111
stateManager.setReferenceState(ce, null);
112                 }
113             } else {
114                 // Assign the current state as reference to the
115
// unexported object
116
stateManager.setReferenceState(ce, state);
117             }
118         }
119         super.finalize(arg0);
120     }
121
122     public void abort(Object JavaDoc ctx) {
123         boolean debug = logger != null && logger.isLoggable(BasicLevel.DEBUG);
124         if (debug) {
125             logger.log(BasicLevel.DEBUG, "Abort the context: " + ctx);
126         }
127         WorkingSet ws = (WorkingSet) ctx;
128         for(Iterator JavaDoc it = ws.entries().iterator(); it.hasNext();) {
129             State state = (State) it.next();
130             if (state == VirtualState.instance) {
131                 continue;
132             }
133             CacheEntry ce = state.getCacheEntry();
134             if (stateManager.isUnexported(state)) {
135                 stateManager.makeClean(state);
136                 stateManager.makeBound(ce, ce.getCeIdentifier());
137             } else if (stateManager.isExported(state)) {
138                 stateManager.makeClean(state);
139             }
140         }
141         super.abort(ctx);
142     }
143
144     public void closeTimeStamp(TimeStamp ts, Object JavaDoc ctx) {
145         WorkingSet ws = (WorkingSet) ctx;
146         State s = ws.lookup(ts.oid);
147         if (s != null && s != VirtualState.instance) {
148             stateManager.stateNoMoreUsed(s);
149         }
150     }
151     protected Object JavaDoc getState(Object JavaDoc ctx,
152                               Object JavaDoc resource,
153                               TimeStamp timeStamp,
154                               boolean isDirtyBefore,
155                               boolean isWrite) throws ConcurrencyException {
156         CacheEntry ce = (CacheEntry) resource;
157         WorkingSet ws = (WorkingSet) ctx;
158         State state = ws.lookup(ce.getCeIdentifier());
159         if (state == VirtualState.instance) {
160             state = null;
161         }
162         if (state == null) {
163             state = stateManager.getReferenceState(ce);
164             if (state == null) { //no reference state ==> load it
165
state = load(ws, ce);
166             }
167             if (isWrite) {
168                 //copy on write
169
state = stateManager.createState(state);
170             } // else read case
171
} else {
172             //There is state in the working set
173
if (isWrite) {
174                 if (!isDirtyBefore) {
175                     //copy on write
176
state = stateManager.createState(state);
177                 } //else already in write
178
} // else read case ==> use the state of the workingset
179
}
180         return state;
181     }
182
183     private State load(WorkingSet ws, CacheEntry ce)
184         throws ConcurrencyException {
185         State state = stateManager.createState(ce);
186         stateManager.setReferenceState(state.getCacheEntry(), state);
187         try {
188             storageManager.read(ws, ce.getCeIdentifier(), state);
189         } catch (PersistenceException e) {
190             stateManager.setReferenceState(state.getCacheEntry(), null);
191             stateManager.destroyState(state);
192             if (e instanceof NoDSIPersistenceException) {
193                 throw new NoDSIConcurrencyException(e);
194             } else {
195                 throw new ConcurrencyException(e);
196             }
197         }
198         stateManager.makeClean(state);
199         return state;
200     }
201     
202     protected Object JavaDoc getResourceId(Object JavaDoc object) {
203         return ((CacheEntry) object).getCeIdentifier();
204     }
205
206 }
207
Popular Tags