1 5 package ve.luz.ica.jackass.daemon; 6 7 import org.apache.commons.logging.Log; 8 import org.apache.commons.logging.LogFactory; 9 import org.apache.commons.pool.impl.GenericObjectPool; 10 import org.omg.CosNaming.NamingContextExt ; 11 import org.omg.PortableServer.POA ; 12 13 import ve.luz.ica.jackass.component.ApplicationContext; 14 import ve.luz.ica.jackass.component.ComponentInfo; 15 import ve.luz.ica.jackass.component.ComponentInfoManager; 16 import ve.luz.ica.jackass.component.StatelessContext; 17 import ve.luz.ica.jackass.component.StatelessContextHelper; 18 import ve.luz.ica.jackass.component.StatelessContextImpl; 19 import ve.luz.ica.jackass.component.StatelessContextPOATie; 20 import ve.luz.ica.jackass.deploy.util.Application; 21 import ve.luz.ica.jackass.deploy.util.Component; 22 import ve.luz.ica.jackass.daemon.proxy.ProxyServantFactory; 23 import ve.luz.ica.jackass.deploy.daemon.ProxyInfo; 24 import ve.luz.ica.jackass.deploy.descriptor.ica.PoolSettings; 25 import ve.luz.ica.jackass.deploy.descriptor.ica.StatelessComponentIcaType; 26 import ve.luz.ica.jackass.deploy.descriptor.standard.Properties; 27 import ve.luz.ica.jackass.deploy.descriptor.standard.Property; 28 import ve.luz.ica.jackass.deploy.descriptor.standard.StatelessComponent; 29 import ve.luz.ica.jackass.instantiator.JInstantiator; 30 import ve.luz.ica.jackass.instantiator.StatelessPoolData; 31 import ve.luz.ica.jackass.solver.ComponentProxyManager; 32 33 37 public class ComponentDeployer 38 { 39 private static final Log LOG = LogFactory.getLog(ComponentDeployer.class); 40 41 private POA contextPoa = null; 42 private POA proxyPoa = null; 43 44 private StatelessPoolData statelessPoolData = null; 45 46 52 public ComponentDeployer(POA contextPOA, POA proxyPOA, StatelessPoolData poolData) 53 { 54 this.contextPoa = contextPOA; 55 this.proxyPoa = proxyPOA; 56 this.statelessPoolData = poolData; 57 } 58 59 65 private byte[] getObjectId(String appName, String compName) 66 { 67 String objectId = appName+ComponentProxyManager.COMPONENT_ID_SEPARATOR + compName; 68 return objectId.getBytes(); 69 } 70 71 81 ProxyInfo deployComponent(JInstantiator instantiator, Application app, Component comp, 82 ApplicationContext appContext, ClassLoader proxyClassLoader) throws Exception 83 { 84 if (comp.getStandardComponent() instanceof StatelessComponent) 85 { 86 return createStatelessComponent(instantiator, app, comp, appContext, proxyClassLoader); 87 } 88 throw new Exception ("Error. Unknown component type"); 89 } 90 91 101 private ProxyInfo createStatelessComponent(JInstantiator instantiator, Application app, 102 Component comp, ApplicationContext appContext, ClassLoader proxyClassLoader) 103 throws Exception 104 { 105 if (LOG.isInfoEnabled()) LOG.info("Deploying stateless component " + comp.getName()); 106 107 StatelessContext compContext = null; 108 org.omg.CORBA.Object realObject = null; 109 byte[] oid = getObjectId(app.getName(), comp.getName()); 110 ComponentInfoManager compInfoManager = ComponentInfoManager.getManager(); 111 112 if (LOG.isDebugEnabled()) 113 { 114 LOG.debug("Creating object "+comp.getName()); 115 LOG.debug("Interface "+comp.getInterface()); 116 LOG.debug("Implementation "+comp.getImplementation()); 117 LOG.debug("Object ID "+new String (oid)); 118 } 119 120 try 121 { 122 if (LOG.isDebugEnabled()) LOG.debug("Creating the component context"); 124 compContext = this.createStatelessContext(comp, oid, appContext.getRootNamingContext()); 125 if (LOG.isDebugEnabled()) LOG.debug("Component context created"); 126 127 String compInterface = comp.getInterface(); 129 String corbaInterface = "IDL:" + compInterface.replace('.', '/') + ":1.0"; 130 String proxyClassName = compInterface + "Proxy"; 131 Class proxyClass = Class.forName(proxyClassName, true, proxyClassLoader); 132 133 int maxIdle = this.statelessPoolData.maxIdle; 135 int maxActive = this.statelessPoolData.maxActive; 136 long checkTime = this.statelessPoolData.checkInterval; 137 138 if (LOG.isDebugEnabled()) LOG.debug("Initializing pool params with default values. maxIdle = " + 139 maxIdle + " maxActive = " + maxActive + " checkTime = " + checkTime); 140 StatelessComponentIcaType icaComp = (StatelessComponentIcaType) comp.getIcaComponent(); 141 if (icaComp != null) 142 { 143 if (LOG.isDebugEnabled()) LOG.debug("Found non standard descriptor for this component"); 144 145 PoolSettings poolSettings = icaComp.getPoolSettings(); 146 if (poolSettings != null) 147 { 148 maxIdle = poolSettings.getMaxIdle(); 149 maxActive = poolSettings.getMaxActive(); 150 checkTime = poolSettings.getCheckInterval(); 151 if (LOG.isDebugEnabled()) 152 LOG.debug("Initializing pool params with values from the deployment descriptor. " + 153 " maxIdle = " + maxIdle + " maxActive = " + maxActive + " checkTime = " + checkTime); 154 } 155 } 156 StatelessPoolData poolData = new StatelessPoolData(maxActive, maxIdle, checkTime); 157 158 realObject = instantiator.createStatelessObject(app.getName(), oid, 161 corbaInterface, comp.getImplementation(), appContext, compContext, poolData); 162 163 if (LOG.isDebugEnabled()) LOG.debug("Component interface " + corbaInterface); 165 org.omg.CORBA.Object proxyObject = proxyPoa.create_reference_with_id(oid, corbaInterface); 166 167 GenericObjectPool proxyPool = new GenericObjectPool(new ProxyServantFactory(realObject, proxyClass), 169 maxActive, GenericObjectPool.WHEN_EXHAUSTED_BLOCK, 0, maxIdle); 170 proxyPool.setMinEvictableIdleTimeMillis(checkTime); 171 172 ComponentInfo compInfo = new ComponentInfo(realObject, proxyClass, proxyPool, instantiator); 175 compInfoManager.addComponentInfo(oid, compInfo); 176 177 ProxyInfo proxyInfo = new ProxyInfo(comp.getName(), proxyObject); 179 180 return proxyInfo; 181 } 182 catch (Exception e) 183 { 184 if (compContext != null) 186 { 187 contextPoa.deactivate_object(oid); 188 } 189 if (realObject != null) 190 { 191 instantiator.destroyStatelessObject(oid); 192 } 193 compInfoManager.removeComponentInfo(oid); 194 195 throw e; 196 } 197 } 198 199 207 private StatelessContext createStatelessContext(Component comp, byte[] oid, NamingContextExt rootNameContext) 208 throws Exception 209 { 210 if (LOG.isDebugEnabled()) LOG.debug("Creating the component context"); 212 213 Property[] properties = null; 214 Properties propertyList = comp.getProperties(); 215 if (propertyList != null) 216 { 217 properties = propertyList.getProperty(); 218 } 219 220 StatelessContextImpl contextImpl = new StatelessContextImpl(rootNameContext, properties); 221 StatelessContextPOATie contextServant = new StatelessContextPOATie(contextImpl); 222 org.omg.CORBA.Object contextObj = contextPoa.create_reference_with_id(oid, StatelessContextHelper.id()); 223 224 contextPoa.activate_object_with_id(oid, contextServant); 227 StatelessContext compContext = StatelessContextHelper.narrow(contextObj); 228 return compContext; 229 } 230 231 237 public void undeployComponent(Application app, Component comp) throws Exception 238 { 239 if (LOG.isInfoEnabled()) LOG.info("Undeploying component " + comp.getName()); 240 241 ComponentInfoManager compInfoManager = ComponentInfoManager.getManager(); 242 243 String appName = app.getName(); 244 String compName = comp.getName(); 245 byte[] oid = getObjectId(appName, compName); 246 247 try 249 { 250 this.contextPoa.deactivate_object(oid); 251 } 252 catch (Exception e) 253 { 254 LOG.warn("The poa hast thrown the following exception ", e); 255 } 256 257 ComponentInfo compInfo = compInfoManager.getComponentInfo(oid); 258 if (compInfo != null) 259 { 260 compInfoManager.removeComponentInfo(oid); 263 264 JInstantiator instantiator = compInfo.getInstantiator(); 266 if (comp.getStandardComponent() instanceof StatelessComponent) 267 { 268 instantiator.destroyStatelessObject(oid); 269 } 270 } 271 else 272 { 273 throw new Exception ("No ComponentInfo found for " + compName); 274 } 275 } 276 277 } 278 | Popular Tags |