KickJava   Java API By Example, From Geeks To Geeks.

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


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.fractal.api.NoSuchInterfaceException;
23 import org.objectweb.fractal.api.control.IllegalBindingException;
24 import org.objectweb.perseus.cache.api.CacheEntry;
25 import org.objectweb.perseus.concurrency.api.ConcurrencyException;
26 import org.objectweb.perseus.concurrency.distributed.DistributedConcurrencyManager;
27 import org.objectweb.perseus.concurrency.pessimistic.Lock;
28 import org.objectweb.perseus.persistence.api.PersistenceException;
29 import org.objectweb.perseus.persistence.api.State;
30 import org.objectweb.perseus.persistence.api.StateManager;
31 import org.objectweb.perseus.persistence.api.StorageManager;
32 import org.objectweb.perseus.persistence.api.VirtualState;
33 import org.objectweb.perseus.persistence.api.WorkingSet;
34
35 /**
36  *
37  * @author S.Chassande-Barrioz
38  */

39 public class PDistributedConcurrencyManager
40         extends DistributedConcurrencyManager {
41
42
43
44     public final static String JavaDoc STORAGE_MANAGER_BINDING = "storage-manager";
45     public final static String JavaDoc STATE_MANAGER_BINDING = "state-manager";
46
47     protected StateManager stateManager;
48
49     protected StorageManager storageManager;
50
51     //IMPLEMENTATION OF THE UserBindingControler INTERFACE //
52
//------------------------------------------------------//
53

54     public String JavaDoc[] listFc() {
55         String JavaDoc[] super_res = super.listFc();
56         String JavaDoc res[] = new String JavaDoc[super_res.length + 2];
57         for (int cpt = 0; cpt < super_res.length; cpt++) {
58             res[cpt] = super_res[cpt];
59         }
60         res[super_res.length + 0] = STORAGE_MANAGER_BINDING;
61         res[super_res.length + 1] = STATE_MANAGER_BINDING;
62         return res;
63     }
64
65     public Object JavaDoc lookupFc(String JavaDoc s) throws NoSuchInterfaceException {
66         if (STORAGE_MANAGER_BINDING.equals(s)) {
67             return storageManager;
68         } else if (STATE_MANAGER_BINDING.equals(s)) {
69             return stateManager;
70         } else {
71             return super.lookupFc(s);
72         }
73     }
74
75     public void bindFc(String JavaDoc s, Object JavaDoc o) throws IllegalBindingException,
76             NoSuchInterfaceException {
77         if (STORAGE_MANAGER_BINDING.equals(s)) {
78             storageManager = (StorageManager) o;
79         } else if (STATE_MANAGER_BINDING.equals(s)) {
80             stateManager = (StateManager) o;
81         } else super.bindFc(s, o);
82     }
83
84     public void unbindFc(String JavaDoc s) throws NoSuchInterfaceException {
85         if (STORAGE_MANAGER_BINDING.equals(s)) {
86             storageManager = null;
87         } else if (STATE_MANAGER_BINDING.equals(s)) {
88             stateManager = null;
89         } else {
90             super.unbindFc(s);
91         }
92     }
93
94
95     public PDistributedConcurrencyManager() throws ConcurrencyException {
96         super();
97     }
98
99     public void finalize(Object JavaDoc arg0) {
100         WorkingSet ws = (WorkingSet) arg0;
101         boolean retainValues = ws.getWSRetainValues();
102         for (Iterator JavaDoc it = ws.entries().iterator(); it.hasNext();) {
103             State state = (State) it.next();
104             if (state == VirtualState.instance) {
105                 continue;
106             }
107             CacheEntry ce = state.getCacheEntry();
108             if (!stateManager.isUnexported(state)) {
109                 //The object has not been unexported (deleted)
110
if (retainValues) {
111                     if (stateManager.isDirty(state)) {
112                         if (stateManager.isToMerge(state)) {
113                             synchronized(ce) {
114                                 stateManager.setReferenceState(ce,
115                                     stateManager.merge(
116                                             stateManager.getReferenceState(ce),
117                                             state));
118                             }
119                         } else {
120                             stateManager.makeClean(state);
121                             stateManager.setReferenceState(ce, state);
122                         }
123                     }//else state is clean ==> nothing to do
124
} else if (stateManager.getReferenceState(ce) == state) {
125                     //forget the state used as reference state
126
stateManager.setReferenceState(ce, null);
127                 }
128             }
129         }
130         super.finalize(arg0);
131     }
132
133
134     protected Object JavaDoc getState(Object JavaDoc ctx,
135                               Object JavaDoc resource,
136                               Lock lock) throws ConcurrencyException {
137         CacheEntry ce = (CacheEntry) resource;
138         WorkingSet ws = (WorkingSet) ctx;
139         State state = stateManager.getReferenceState(ce);
140         if (state == null) {
141             synchronized (ce) {
142                 state = stateManager.getReferenceState(ce);
143                 if (state == null) {
144                     state = stateManager.createState(ce);
145                     stateManager.setReferenceState(state.getCacheEntry(), state);
146                     try {
147                         storageManager.read(ws, ce.getCeIdentifier(), state);
148                         stateManager.makeClean(state);
149                     } catch (PersistenceException e) {
150                         stateManager.setReferenceState(state.getCacheEntry(), null);
151                         stateManager.destroyState(state);
152                         //The instance does not exist in the data support, remove the lock
153
synchronized (locks) {
154                             if (lock.close(ctx)) {
155                                 locks.remove(lock);
156                             }
157                         }
158                         throw new NoDSIConcurrencyException(e);
159                     }
160                 }
161             }
162         }
163         return state;
164     }
165     protected void invalidateState(Object JavaDoc resId, Object JavaDoc hints) {
166         CacheEntry ce = (CacheEntry) hints;
167         State state = stateManager.getReferenceState(ce);
168         if (state != null) {
169             synchronized(ce) {
170                 state = stateManager.getReferenceState(ce);
171                 if (state != null)
172                     stateManager.destroyState(state);
173             }
174         }
175     }
176
177 }
178
Popular Tags