1 18 package org.objectweb.speedo.pm.lib; 19 20 import org.objectweb.fractal.adl.Factory; 21 import org.objectweb.fractal.adl.FactoryFactory; 22 import org.objectweb.fractal.api.Component; 23 import org.objectweb.fractal.api.Interface; 24 import org.objectweb.fractal.api.control.BindingController; 25 import org.objectweb.fractal.api.control.IllegalLifeCycleException; 26 import org.objectweb.fractal.api.control.LifeCycleController; 27 import org.objectweb.fractal.util.Fractal; 28 import org.objectweb.perseus.pool.api.PoolMatchFactory; 29 import org.objectweb.perseus.pool.api.PoolException; 30 import org.objectweb.perseus.persistence.api.ConnectionHolderFactory; 31 import org.objectweb.speedo.pm.api.ProxyManager; 32 import org.objectweb.speedo.workingset.api.Transaction; 33 import org.objectweb.speedo.workingset.lib.SpeedoTransaction; 34 import org.objectweb.util.monolog.api.BasicLevel; 35 import org.objectweb.util.monolog.api.Logger; 36 37 import java.util.HashMap ; 38 import java.util.Map ; 39 import java.util.Arrays ; 40 41 58 public class SpeedoProxyManagerInstanciator 59 implements PoolMatchFactory, BindingController, LifeCycleController { 60 61 62 public final static String CONNECTION_HOLDER_FACTORY_BINDING = "connection-holder-factory"; 63 public final static String COMPONENT_BINDING = "component"; 64 public final static String PM_TEMPLATE = "org.objectweb.speedo.pm.lib.SpeedoProxyManager"; 65 public final static String T_TEMPLATE = "org.objectweb.speedo.workingset.lib.SpeedoTransaction"; 66 67 73 private ConnectionHolderFactory chf; 74 75 78 private Component thisC = null; 79 80 83 private Component pmT; 84 85 88 private Component tT; 89 90 93 private boolean started = false; 94 95 96 private Logger logger; 97 98 110 Object [] components = new Object [6]; 111 112 private final static byte PMF_COMPONENT_IDX = 0; 113 private final static byte MAPPER_COMPONENT_IDX = PMF_COMPONENT_IDX + 1; 114 private final static byte JF_COMPONENT_IDX = MAPPER_COMPONENT_IDX + 1; 115 private final static byte PNC_COMPONENT_IDX = JF_COMPONENT_IDX + 1; 116 private final static byte QM_COMPONENT_IDX = PNC_COMPONENT_IDX + 1; 117 private final static byte TPM_COMPONENT_IDX = QM_COMPONENT_IDX + 1; 118 119 123 Component speedoComponent = null; 124 125 void clearComponentRef() { 126 Arrays.fill(components, null); 127 } 128 129 133 void fetchComponentRef() throws Exception { 134 speedoComponent = Fractal.getSuperController(thisC).getFcSuperComponents()[0]; 135 Component[] children = Fractal.getContentController(speedoComponent) 136 .getFcSubComponents(); 137 for (int i = 0; i < children.length; i++) { 138 String name = Fractal.getNameController(children[i]).getFcName(); 139 if ("proxy-manager-factory".equals(name)) { 140 components[PMF_COMPONENT_IDX] = children[i].getFcInterface("proxy-manager-factory"); 141 } else if ("mapper".equals(name)) { 142 components[MAPPER_COMPONENT_IDX] = children[i].getFcInterface("mapper"); 143 components[JF_COMPONENT_IDX] = children[i].getFcInterface("jorm-factory"); 144 components[PNC_COMPONENT_IDX] = children[i].getFcInterface("pname-coder"); 145 } else if ("query-manager".equals(name)) { 146 components[QM_COMPONENT_IDX] = children[i].getFcInterface("query-manager"); 147 } else if ("tpm".equals(name)) { 148 components[TPM_COMPONENT_IDX] = children[i].getFcInterface("transactional-persistence-manager"); 149 } 150 } 151 Factory factory = FactoryFactory.getFactory(FactoryFactory.FRACTAL_BACKEND); 152 Map ctxt = new HashMap (); 153 ctxt.put("template", "true"); 154 pmT = (Component) factory.newComponent(PM_TEMPLATE, ctxt); 155 tT = (Component) factory.newComponent(T_TEMPLATE, ctxt); 156 } 157 158 161 public String getFcState() { 162 return started ? STARTED : STOPPED; 163 } 164 165 168 public void startFc() throws IllegalLifeCycleException { 169 started = true; 170 try { 171 fetchComponentRef(); 172 } catch (Exception e) { 173 if (logger != null) { 174 logger.log(BasicLevel.ERROR, "Impossible to fetch inner speedo components", e); 175 } 176 throw new IllegalLifeCycleException(e.getMessage()); 177 } 178 } 179 180 183 public void stopFc() throws IllegalLifeCycleException { 184 started = false; 185 clearComponentRef(); 186 } 187 188 189 192 public String [] listFc() { 193 return new String []{CONNECTION_HOLDER_FACTORY_BINDING}; 194 } 195 196 public Object lookupFc(String c) { 197 if (CONNECTION_HOLDER_FACTORY_BINDING.equals(c)) { 198 return chf; 199 } else { 200 return null; 201 } 202 } 203 204 public void bindFc(String c, Object s) { 205 if ("logger".equals(c)) { 206 logger = (Logger) s; 207 } else if (CONNECTION_HOLDER_FACTORY_BINDING.equals(c)) { 208 chf = (ConnectionHolderFactory) s; 209 } else if (COMPONENT_BINDING.equals(c)) { 210 thisC = (Component) s; 211 } 212 } 213 214 public void unbindFc(String c) { 215 if (CONNECTION_HOLDER_FACTORY_BINDING.equals(c)) { 216 chf = null; 217 } 218 } 219 220 221 224 233 public Object createResource(Object o) throws PoolException { 234 try { 235 Component pmC = Fractal.getFactory(pmT).newFcInstance(); 237 ProxyManager pm = (ProxyManager) pmC.getFcInterface("proxy-manager"); 238 Fractal.getNameController(pmC).setFcName("proxy-manager"); 239 BindingController pmBC = Fractal.getBindingController(pmC); 240 241 Component tC = Fractal.getFactory(tT).newFcInstance(); 243 Fractal.getNameController(tC).setFcName("transaction"); 244 Transaction t = (Transaction) tC.getFcInterface("transaction"); 245 BindingController tBC = Fractal.getBindingController(tC); 246 247 Fractal.getContentController(speedoComponent).addFcSubComponent(pmC); 249 Fractal.getContentController(speedoComponent).addFcSubComponent(tC); 250 251 pmBC.bindFc(SpeedoProxyManager.JORM_FACTORY_BINDING, 253 components[JF_COMPONENT_IDX]); 254 pmBC.bindFc(SpeedoProxyManager.PNAME_CODER_BINDING, 255 components[PNC_COMPONENT_IDX]); 256 pmBC.bindFc(SpeedoProxyManager.PROXY_MANAGER_FACTORY_BINDING, 257 components[PMF_COMPONENT_IDX]); 258 pmBC.bindFc(SpeedoProxyManager.QUERY_MANAGER_BINDING, 259 components[QM_COMPONENT_IDX]); 260 pmBC.bindFc(SpeedoProxyManager.TRANSACTIONAL_PERSISTENCE_MANAGER_BINDING, 261 components[TPM_COMPONENT_IDX]); 262 pmBC.bindFc(SpeedoProxyManager.TRANSACTION_BINDING, t); 263 264 tBC.bindFc(SpeedoTransaction.PROXY_MANAGER_BINDING, pm); 266 tBC.bindFc(SpeedoTransaction.TRANSACTIONAL_PERSISTENCE_MANAGER_BINDING, 267 components[TPM_COMPONENT_IDX]); 268 tBC.bindFc(SpeedoTransaction.MAPPER_BINDING, 269 components[MAPPER_COMPONENT_IDX]); 270 271 t.setConnectionHolder(chf.createConnectionHolder()); 273 274 Fractal.getLifeCycleController(pmC).startFc(); 276 Fractal.getLifeCycleController(tC).startFc(); 277 278 return pm; 279 } catch (Exception e) { 280 PoolException pe = new PoolException( 281 "Error during the allocation of a new ProxyManager and its WorkingSet", e); 282 logger.log(BasicLevel.ERROR, pe.getMessage(), pe); 283 throw pe; 284 } 285 } 286 287 public boolean matchResource(Object resource, Object o) { 288 return true; 289 } 290 291 296 public void destroyResource(Object resource) { 297 try { 298 Component pmC = ((Interface) resource).getFcItfOwner(); 300 BindingController pmBC = Fractal.getBindingController(pmC); 301 302 Component tC = ((Interface) 304 pmBC.lookupFc(SpeedoProxyManager.TRANSACTION_BINDING)) 305 .getFcItfOwner(); 306 BindingController tBC = Fractal.getBindingController(tC); 307 308 Fractal.getLifeCycleController(pmC).stopFc(); 310 Fractal.getLifeCycleController(tC).stopFc(); 311 312 pmBC.unbindFc(SpeedoProxyManager.JORM_FACTORY_BINDING); 314 pmBC.unbindFc(SpeedoProxyManager.PNAME_CODER_BINDING); 315 pmBC.unbindFc(SpeedoProxyManager.PROXY_MANAGER_FACTORY_BINDING); 316 pmBC.unbindFc(SpeedoProxyManager.QUERY_MANAGER_BINDING); 317 pmBC.unbindFc(SpeedoProxyManager.TRANSACTIONAL_PERSISTENCE_MANAGER_BINDING); 318 pmBC.unbindFc(SpeedoProxyManager.TRANSACTION_BINDING); 319 320 tBC.unbindFc(SpeedoTransaction.PROXY_MANAGER_BINDING); 322 tBC.unbindFc(SpeedoTransaction.TRANSACTIONAL_PERSISTENCE_MANAGER_BINDING); 323 tBC.unbindFc(SpeedoTransaction.MAPPER_BINDING); 324 325 } catch (Exception e) { 330 if (logger != null) { 331 logger.log(BasicLevel.WARN, "Error during the destroying of a ProxyManager", e); 332 } 333 } 334 } 335 336 } 337 | Popular Tags |