KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > perseus > concurrency > distributed > globallock > lib > BasicGlobalLockManager


1 package org.objectweb.perseus.concurrency.distributed.globallock.lib;
2
3 import org.objectweb.perseus.distribution.api.DistResUserService;
4 import org.objectweb.perseus.distribution.api.DistResServiceManager;
5 import org.objectweb.perseus.distribution.api.DistResCoordinatorFactory;
6 import org.objectweb.perseus.concurrency.distributed.globallock.api.GlobalLock;
7 import org.objectweb.perseus.concurrency.distributed.globallock.api.GlobalLockManager;
8 import org.objectweb.perseus.concurrency.distributed.globallock.api.GlobalLockException;
9 import org.objectweb.fractal.api.control.BindingController;
10 import org.objectweb.fractal.api.control.IllegalBindingException;
11 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
12 import org.objectweb.fractal.api.control.LifeCycleController;
13 import org.objectweb.fractal.api.NoSuchInterfaceException;
14
15 import java.io.Serializable JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.HashMap JavaDoc;
18
19 /**
20  * Copyright (C) 2003-2004
21  * - France Telecom R&D
22  *
23  * This library is free software; you can redistribute it and/or
24  * modify it under the terms of the GNU Lesser General Public
25  * License as published by the Free Software Foundation; either
26  * version 2 of the License, or (at your option) any later version.
27  *
28  * This library is distributed in the hope that it will be useful,
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
31  * Lesser General Public License for more details.
32  *
33  * You should have received a copy of the GNU Lesser General Public
34  * License along with this library; if not, write to the Free Software
35  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36  *
37  * Release: 1.0
38  *
39  * Authors: olivier.lobry@francetelecom.com
40  * Date: 2 juin 2004
41  * Time: 17:56:43
42  */

43 public class BasicGlobalLockManager
44         implements GlobalLockManager, BindingController, LifeCycleController {
45
46     public final static String JavaDoc SERVICE_MGR_BINDING = "dist-res-service-manager";
47     public final static String JavaDoc COORD_FACTORY_BINDING = "dist-res-coord-factory";
48
49     // required interfaces
50
protected DistResServiceManager drsm;
51     protected DistResCoordinatorFactory drcf;
52
53     // life cycle state
54
protected String JavaDoc state = LifeCycleController.STOPPED;
55
56     protected DistResUserService drus;
57     protected Map JavaDoc globalLocks;
58     protected Serializable JavaDoc nodeId;
59 /* public BasicGlobalLockManager(DistResServiceManager drsm, LockValue lockValue) {
60         this(drsm, new GlobalLockCoordinatorFactory(lockValue));
61     } */

62 /* public BasicGlobalLockManager(DistResServiceManager drsm,
63             DistResCoordinatorFactory drcf) {
64         this.nodeId = drsm.newLocalNode(drcf);
65         this.drus = drsm.getUserService(nodeId);
66     }
67 */

68     public BasicGlobalLockManager() {
69     }
70
71     // BindingController interface
72
public String JavaDoc[] listFc() {
73         return new String JavaDoc[] {SERVICE_MGR_BINDING, COORD_FACTORY_BINDING};
74     }
75
76     public Object JavaDoc lookupFc(String JavaDoc s) throws NoSuchInterfaceException {
77         if (SERVICE_MGR_BINDING.equals(s)) {
78             return drsm;
79         } else if (COORD_FACTORY_BINDING.equals(s)) {
80             return drcf;
81         }
82         return null;
83 //FIXME: throw new NoSuchInterfaceException(s);
84
}
85
86     public void bindFc(String JavaDoc s, Object JavaDoc o) throws NoSuchInterfaceException,
87             IllegalBindingException, IllegalLifeCycleException {
88         if (!LifeCycleController.STOPPED.equals(state)) {
89             throw new IllegalLifeCycleException(s);
90         }
91         try {
92             if (SERVICE_MGR_BINDING.equals(s)) {
93                 drsm = (DistResServiceManager) o;
94             } else if (COORD_FACTORY_BINDING.equals(s)) {
95                 drcf = (DistResCoordinatorFactory) o;
96             }
97         } catch (ClassCastException JavaDoc e) {
98             throw new IllegalBindingException(s + ":" + e.getMessage());
99         }
100 //FIXME: throw new NoSuchInterfaceException(s);
101
}
102
103     public void unbindFc(String JavaDoc s) throws NoSuchInterfaceException,
104             IllegalBindingException, IllegalLifeCycleException {
105         if (!LifeCycleController.STOPPED.equals(state)) {
106             throw new IllegalLifeCycleException(s);
107         }
108         if (SERVICE_MGR_BINDING.equals(s)) {
109             drus = null;
110         } else if (SERVICE_MGR_BINDING.equals(s)) {
111             drcf = null;
112         }
113 //FIXME: throw new NoSuchInterfaceException(s);
114
}
115
116     // LifeCycleController interface
117
public String JavaDoc getFcState() {
118         return state;
119     }
120
121     public void startFc() throws IllegalLifeCycleException {
122         if (LifeCycleController.STARTED.equals(state)) {
123             throw new IllegalLifeCycleException("Already started");
124         }
125         Object JavaDoc nodeId = drsm.newLocalNode(drcf);
126         drus = drsm.getUserService(nodeId);
127         globalLocks = new HashMap JavaDoc();
128         state = LifeCycleController.STARTED;
129     }
130
131     public void stopFc() throws IllegalLifeCycleException {
132         if (LifeCycleController.STOPPED.equals(state)) {
133             throw new IllegalLifeCycleException("Already stopped");
134         }
135         // TODO: could release explictly all locks
136
/* for (Iterator it = globalLocks.values().iterator(); it.hasNext();) {
137             GlobalLockUser glu = (GlobalLockUser) it.next();
138             glu.finilaze();
139         }
140         */

141         globalLocks = null;
142         drsm.removeLocalNode(drus.getNodeId());
143         drus = null;
144         state = LifeCycleController.STOPPED;
145     }
146
147     // GlobalLockManager interface
148
public GlobalLock getGlobalLock(Serializable JavaDoc oid,
149                              boolean create) throws GlobalLockException {
150         GlobalLock gl;
151         synchronized (globalLocks) {
152             gl = (GlobalLock) globalLocks.get(oid);
153             if ((gl == null) && create) {
154                 gl = createLock(oid);
155                 globalLocks.put(oid, gl);
156                 joinUsers(oid);
157             }
158         }
159         return gl;
160     }
161
162     // private/potected methods
163
protected GlobalLock createLock(Serializable JavaDoc oid) {
164         return new GlobalLockUser(oid, drus);
165     }
166
167     protected void joinUsers(Serializable JavaDoc oid) throws GlobalLockException {
168         GlobalLockUser gl = (GlobalLockUser) globalLocks.get(oid);
169         try {
170             drus.joinUsers(oid, gl);
171         } catch (Exception JavaDoc e) {
172             throw new GlobalLockException(e);
173         }
174     }
175
176
177     public Object JavaDoc getNodeId() {
178         return nodeId;
179     }
180 /* public DistResUserService getUserService() {
181         return drus;
182     }
183 */

184
185 }
186
Popular Tags