1 5 package ve.luz.ica.jackass.instantiator; 6 7 import java.io.File ; 8 import java.io.FilenameFilter ; 9 import java.net.MalformedURLException ; 10 import java.net.URL ; 11 import java.net.URLClassLoader ; 12 import java.util.Hashtable ; 13 import java.util.Map ; 14 15 import org.apache.commons.logging.Log; 16 import org.apache.commons.logging.LogFactory; 17 import org.apache.commons.pool.impl.GenericObjectPool; 18 19 import org.omg.CORBA.ORB ; 20 import org.omg.PortableServer.IdAssignmentPolicyValue ; 21 import org.omg.PortableServer.LifespanPolicyValue ; 22 import org.omg.PortableServer.POA ; 23 import org.omg.PortableServer.RequestProcessingPolicyValue ; 24 import org.omg.PortableServer.ServantRetentionPolicyValue ; 25 import org.omg.PortableServer.POAPackage.AdapterAlreadyExists ; 26 import org.omg.PortableServer.POAPackage.InvalidPolicy ; 27 import org.omg.PortableServer.POAPackage.WrongPolicy ; 28 29 30 import ve.luz.ica.jackass.component.ApplicationContext; 31 import ve.luz.ica.jackass.component.DeploymentRequirement; 32 import ve.luz.ica.jackass.component.StatelessContext; 33 34 43 public class JInstantiatorImpl extends JInstantiatorPOA 44 { 45 46 private static final Log LOG = LogFactory.getLog(JInstantiatorImpl.class); 47 48 private static final DeploymentRequirement[] DEPLOYMENT_REQUIREMENTS = 49 { 50 new DeploymentRequirement("Java", "1.4"), 51 new DeploymentRequirement("JacORB", "1.4.1") 52 }; 53 54 private POA rootPoa = null; 55 private ORB orb = null; 56 private Map appTable = null; 57 58 private POA statelessPoa = null; 59 private Map objectInfoTable = null; 60 61 69 public JInstantiatorImpl(ORB theOrb, POA poa) throws AdapterAlreadyExists , InvalidPolicy 70 { 71 this.orb = theOrb; 72 this.rootPoa = poa; 73 this.appTable = new Hashtable (); 74 this.objectInfoTable = new Hashtable (); 75 76 org.omg.CORBA.Policy [] policies = new org.omg.CORBA.Policy [] 78 { 79 rootPoa.create_id_assignment_policy(IdAssignmentPolicyValue.USER_ID), 80 rootPoa.create_lifespan_policy(LifespanPolicyValue.PERSISTENT), 81 rootPoa.create_servant_retention_policy(ServantRetentionPolicyValue.NON_RETAIN), 82 rootPoa.create_request_processing_policy(RequestProcessingPolicyValue.USE_SERVANT_MANAGER) 83 }; 84 statelessPoa = rootPoa.create_POA("Stateless", rootPoa.the_POAManager(), policies); 85 86 if (LOG.isDebugEnabled()) 87 { 88 LOG.debug("The statelessPoa has been created"); 89 } 90 91 StatelessServantLocator servantLocator = new StatelessServantLocator(objectInfoTable); 92 93 try 94 { 95 statelessPoa.set_servant_manager(servantLocator); 96 } 97 catch (WrongPolicy e) 98 { 99 String msg = "Wrong stateless policy configuration"; 100 if (LOG.isFatalEnabled()) 101 { 102 LOG.fatal(msg, e); 103 } 104 105 assert false : msg; 106 } 107 } 108 109 117 public void registerApplication(String appName, String binFiles) throws AlreadyRegisteredException 118 { 119 if (LOG.isInfoEnabled()) LOG.info("Registering application " + appName); 120 121 if (appTable.containsKey(appName)) 122 { 123 LOG.error("Application already registered: " + appName); 124 throw new AlreadyRegisteredException(); 125 } 126 File file = new File (binFiles); 127 JarFileFilter filter = new JarFileFilter(); 128 String [] jarFileList = file.list(filter); 129 130 if (LOG.isDebugEnabled()) LOG.debug("Found " + jarFileList.length + " jar files in " + binFiles); 131 132 URL [] urls = new URL [jarFileList.length]; 133 for (int i = 0; i < jarFileList.length; ++i) 134 { 135 try 136 { 137 if (LOG.isDebugEnabled()) LOG.debug(jarFileList[i]); 138 urls[i] = new File (binFiles, jarFileList[i]).toURL(); 139 } 140 catch (MalformedURLException e) 141 { 142 LOG.error("Error: Bad ULR. ", e); 143 } 144 } 145 URLClassLoader loader = new URLClassLoader (urls); 146 appTable.put(appName, loader); 147 } 148 149 154 public void unregisterApplication(String appName) throws NotRegisteredException 155 { 156 if (LOG.isInfoEnabled()) LOG.info("Removing application " + appName); 157 158 if (!appTable.containsKey(appName)) 159 { 160 LOG.error("Unregistering an application which is not registered: " + appName); 161 throw new NotRegisteredException(); 162 } 163 appTable.remove(appName); 164 } 165 166 178 public org.omg.CORBA.Object createStatelessObject(String appName, byte[] objectId, 179 String idlInterface, String implClassName, ApplicationContext appContext, 180 StatelessContext compContext, StatelessPoolData poolData) 181 throws UnableToCreateException 182 { 183 try 184 { 185 if (LOG.isInfoEnabled()) LOG.info("Creating object " + new String (objectId)); 186 187 ClassLoader loader = (ClassLoader ) appTable.get(appName); 189 if (loader == null) 190 { 191 LOG.error("Application " + appName + " has not been registered"); 192 throw new UnableToCreateException(); 193 } 194 195 Class servantClass = Class.forName(implClassName, true, loader); 197 198 StatelessServantFactory factory = new StatelessServantFactory(appContext, compContext, servantClass); 200 GenericObjectPool objPool = new GenericObjectPool(factory, poolData.maxActive, 201 GenericObjectPool.WHEN_EXHAUSTED_BLOCK, 0, poolData.maxIdle); 202 objPool.setMinEvictableIdleTimeMillis(poolData.checkInterval); 203 204 org.omg.CORBA.Object obj = statelessPoa.create_reference_with_id(objectId, idlInterface); 206 207 StatelessObjectInfo objInfo = new StatelessObjectInfo(servantClass, appContext, compContext, objPool); 209 objectInfoTable.put(new String (objectId), objInfo); 210 211 if (LOG.isDebugEnabled()) LOG.debug("The object has been created"); 212 213 return obj; 214 } 215 catch (ClassNotFoundException e) 216 { 217 LOG.error("Class not found exception has been thrown", e); 218 throw new UnableToCreateException(e.getMessage()); 219 } 220 } 221 222 227 public void destroyStatelessObject(byte[] oid) 228 { 229 try 230 { 231 objectInfoTable.remove(new String (oid)); 232 } 233 catch (Exception e) 234 { 235 String msg = "Exception clearing the stateless servant keyed pool"; 236 if (LOG.isFatalEnabled()) 237 { 238 LOG.fatal(msg, e); 239 } 240 241 assert false : msg; 242 } 243 } 244 245 246 250 public DeploymentRequirement[] getDeployementRequirements() 251 { 252 return DEPLOYMENT_REQUIREMENTS; 253 } 254 255 } 256 257 262 class JarFileFilter implements FilenameFilter 263 { 264 private static final String JAR_STRING = ".jar"; 265 266 269 public boolean accept(File dir, String name) 270 { 271 return name.endsWith(JAR_STRING); 272 } 273 274 } 275 | Popular Tags |