KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.objectweb.perseus.concurrency.dbdelegate.DbDelegateConcurrencyManager;
21 import org.objectweb.perseus.concurrency.api.ConcurrencyException;
22 import org.objectweb.perseus.cache.api.CacheEntry;
23 import org.objectweb.perseus.persistence.api.StateManager;
24 import org.objectweb.perseus.persistence.api.StorageManager;
25 import org.objectweb.perseus.persistence.api.VirtualState;
26 import org.objectweb.perseus.persistence.api.WorkingSet;
27 import org.objectweb.perseus.persistence.api.State;
28 import org.objectweb.perseus.persistence.api.PersistenceException;
29 import org.objectweb.util.monolog.api.BasicLevel;
30 import org.objectweb.fractal.api.NoSuchInterfaceException;
31 import org.objectweb.fractal.api.control.IllegalBindingException;
32
33 import java.util.Iterator JavaDoc;
34
35 /**
36  *
37  * @author P. Dechamboux
38  */

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

55     public String JavaDoc[] listFc() {
56         String JavaDoc[] super_res = super.listFc();
57         String JavaDoc res[] = new String JavaDoc[super_res.length + 2];
58         for (int cpt = 0; cpt < super_res.length; cpt++) {
59             res[cpt] = super_res[cpt];
60         }
61         res[super_res.length + 0] = STORAGE_MANAGER_BINDING;
62         res[super_res.length + 1] = STATE_MANAGER_BINDING;
63         return res;
64     }
65
66     public Object JavaDoc lookupFc(String JavaDoc s) throws NoSuchInterfaceException {
67         if (STORAGE_MANAGER_BINDING.equals(s)) {
68             return storageManager;
69         } else if (STATE_MANAGER_BINDING.equals(s)) {
70             return stateManager;
71         } else {
72             return super.lookupFc(s);
73         }
74     }
75
76     public void bindFc(String JavaDoc s, Object JavaDoc o) throws NoSuchInterfaceException, IllegalBindingException {
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
82             super.bindFc(s, o);
83     }
84
85     public void unbindFc(String JavaDoc s) throws NoSuchInterfaceException {
86         if (STORAGE_MANAGER_BINDING.equals(s)) {
87             storageManager = null;
88         } else if (STATE_MANAGER_BINDING.equals(s)) {
89             stateManager = null;
90         } else {
91             super.unbindFc(s);
92         }
93     }
94     public void finalize(Object JavaDoc ctx) {
95         boolean debug = logger != null && logger.isLoggable(BasicLevel.DEBUG);
96         if (debug) {
97             logger.log(BasicLevel.DEBUG, "Finalize the context: " + ctx);
98         }
99         WorkingSet ws = (WorkingSet) ctx;
100         for(Iterator JavaDoc it = ws.entries().iterator(); it.hasNext();) {
101             State state = (State) it.next();
102             if (state == VirtualState.instance) {
103                 continue;
104             }
105             CacheEntry ce = state.getCacheEntry();
106             if (stateManager.isUnexported(state)) {
107                 stateManager.setReferenceState(ce, state);
108             } else {
109                 stateManager.setReferenceState(ce, null);
110             }
111         }
112     }
113
114     public void abort(Object JavaDoc ctx) {
115         boolean debug = logger != null && logger.isLoggable(BasicLevel.DEBUG);
116         if (debug) {
117             logger.log(BasicLevel.DEBUG, "Abort the context: " + ctx);
118         }
119         WorkingSet ws = (WorkingSet) ctx;
120         for(Iterator JavaDoc it = ws.entries().iterator(); it.hasNext();) {
121             State state = (State) it.next();
122             if (state == VirtualState.instance) {
123                 continue;
124             }
125             CacheEntry ce = state.getCacheEntry();
126             if (stateManager.isUnexported(state)) {
127                 stateManager.makeClean(state);
128                 stateManager.makeBound(ce, ce.getCeIdentifier());
129             }
130         }
131     }
132
133     protected Object JavaDoc getState(Object JavaDoc ctx,
134                               Object JavaDoc resource,
135                               Object JavaDoc thinLock) throws ConcurrencyException {
136         CacheEntry ce = (CacheEntry) resource;
137         WorkingSet ws = (WorkingSet) ctx;
138         synchronized (ce) {
139             State state = stateManager.getReferenceState(ce);
140             if (state != null) {
141                 return state;
142             }
143             state = ws.lookup(ce.getCeIdentifier());
144             if (state != null && state != VirtualState.instance) {
145                 return state;
146             }
147             state = load(ws, ce.getCeIdentifier(), ce);
148             return state;
149         }
150     }
151
152     private State load(WorkingSet ws, Object JavaDoc resourceId, CacheEntry ce)
153             throws ConcurrencyException {
154         State state = stateManager.createState(ce);
155         try {
156             storageManager.read(ws, resourceId, state);
157             stateManager.makeClean(state);
158         } catch (PersistenceException e) {
159             stateManager.destroyState(state);
160             throw new NoDSIConcurrencyException(e);
161         }
162         return state;
163     }
164 }
165
Popular Tags