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 ; 16 import java.util.Map ; 17 import java.util.HashMap ; 18 19 43 public class BasicGlobalLockManager 44 implements GlobalLockManager, BindingController, LifeCycleController { 45 46 public final static String SERVICE_MGR_BINDING = "dist-res-service-manager"; 47 public final static String COORD_FACTORY_BINDING = "dist-res-coord-factory"; 48 49 protected DistResServiceManager drsm; 51 protected DistResCoordinatorFactory drcf; 52 53 protected String state = LifeCycleController.STOPPED; 55 56 protected DistResUserService drus; 57 protected Map globalLocks; 58 protected Serializable nodeId; 59 62 68 public BasicGlobalLockManager() { 69 } 70 71 public String [] listFc() { 73 return new String [] {SERVICE_MGR_BINDING, COORD_FACTORY_BINDING}; 74 } 75 76 public Object lookupFc(String 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 } 85 86 public void bindFc(String s, Object 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 e) { 98 throw new IllegalBindingException(s + ":" + e.getMessage()); 99 } 100 } 102 103 public void unbindFc(String 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 } 115 116 public String 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 nodeId = drsm.newLocalNode(drcf); 126 drus = drsm.getUserService(nodeId); 127 globalLocks = new HashMap (); 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 141 globalLocks = null; 142 drsm.removeLocalNode(drus.getNodeId()); 143 drus = null; 144 state = LifeCycleController.STOPPED; 145 } 146 147 public GlobalLock getGlobalLock(Serializable 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 protected GlobalLock createLock(Serializable oid) { 164 return new GlobalLockUser(oid, drus); 165 } 166 167 protected void joinUsers(Serializable oid) throws GlobalLockException { 168 GlobalLockUser gl = (GlobalLockUser) globalLocks.get(oid); 169 try { 170 drus.joinUsers(oid, gl); 171 } catch (Exception e) { 172 throw new GlobalLockException(e); 173 } 174 } 175 176 177 public Object getNodeId() { 178 return nodeId; 179 } 180 184 185 } 186 | Popular Tags |