KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ve > luz > ica > jackass > instantiator > JInstantiatorImpl


1 /*
2  * Copyright (c) 2003 by The Jackass Team
3  * Licensed under the Open Software License version 2.0
4  */

5 package ve.luz.ica.jackass.instantiator;
6
7 import java.io.File JavaDoc;
8 import java.io.FilenameFilter JavaDoc;
9 import java.net.MalformedURLException JavaDoc;
10 import java.net.URL JavaDoc;
11 import java.net.URLClassLoader JavaDoc;
12 import java.util.Hashtable JavaDoc;
13 import java.util.Map JavaDoc;
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 JavaDoc;
20 import org.omg.PortableServer.IdAssignmentPolicyValue JavaDoc;
21 import org.omg.PortableServer.LifespanPolicyValue JavaDoc;
22 import org.omg.PortableServer.POA JavaDoc;
23 import org.omg.PortableServer.RequestProcessingPolicyValue JavaDoc;
24 import org.omg.PortableServer.ServantRetentionPolicyValue JavaDoc;
25 import org.omg.PortableServer.POAPackage.AdapterAlreadyExists JavaDoc;
26 import org.omg.PortableServer.POAPackage.InvalidPolicy JavaDoc;
27 import org.omg.PortableServer.POAPackage.WrongPolicy JavaDoc;
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 /**
35  * StatelessFactoryImpl is the factory for local stateless objects
36  * It creates a POA with the policies required for stateless objects and uses it for creating
37  * the object references for the objects.
38  * <p>
39  * When a new object is created a pool of servants is created
40  *
41  * @author Carlos Arévalo
42  */

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 JavaDoc rootPoa = null;
55     private ORB JavaDoc orb = null;
56     private Map JavaDoc appTable = null;
57
58     private POA JavaDoc statelessPoa = null;
59     private Map JavaDoc objectInfoTable = null;
60
61     /**
62      * Class constructor.
63      * stores the references for the orb and the root poa
64      * @param theOrb a reference to the previously initialized orb
65      * @param poa the poa to be used to create the objects
66      * @throws AdapterAlreadyExists if there exists a poa the the same name
67      * @throws InvalidPolicy if the policies are not correct
68      */

69     public JInstantiatorImpl(ORB JavaDoc theOrb, POA JavaDoc poa) throws AdapterAlreadyExists JavaDoc, InvalidPolicy JavaDoc
70     {
71         this.orb = theOrb;
72         this.rootPoa = poa;
73         this.appTable = new Hashtable JavaDoc();
74         this.objectInfoTable = new Hashtable JavaDoc();
75
76         // create the stateless component poa
77
org.omg.CORBA.Policy JavaDoc[] policies = new org.omg.CORBA.Policy JavaDoc[]
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 JavaDoc e)
98         {
99             String JavaDoc 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     /**
110      * Register an application with the factory
111      * A class loader is created for the application and placed in appTable
112      * @param appName the application name
113      * @param binFiles the path name to the binary files for the application
114      * @throws AlreadyRegisteredException if an application with the given name has been
115      * registered previously
116      */

117     public void registerApplication(String JavaDoc appName, String JavaDoc 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 JavaDoc file = new File JavaDoc(binFiles);
127         JarFileFilter filter = new JarFileFilter();
128         String JavaDoc[] jarFileList = file.list(filter);
129
130         if (LOG.isDebugEnabled()) LOG.debug("Found " + jarFileList.length + " jar files in " + binFiles);
131
132         URL JavaDoc[] urls = new URL JavaDoc[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 JavaDoc(binFiles, jarFileList[i]).toURL();
139             }
140             catch (MalformedURLException JavaDoc e)
141             {
142                 LOG.error("Error: Bad ULR. ", e);
143             }
144         }
145         URLClassLoader JavaDoc loader = new URLClassLoader JavaDoc(urls);
146         appTable.put(appName, loader);
147     }
148
149     /**
150      * Remove the application class loader from appTable
151      * @param appName the name of the application to be unregistered
152      * @throws NotRegisteredException if no application is registered with the given name
153      */

154     public void unregisterApplication(String JavaDoc 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     /**
167      * Create a local object for a stateless component and return the object reference
168      * @param appName the application name
169      * @param objectId the object id
170      * @param idlInterface the idl interface that the object implements
171      * @param implClassName the implementation class name
172      * @param poolData an object containing information about the servant pool
173      * @param appContext the application context
174      * @param compContext the component context
175      * @return the reference to the created object
176      * @throws UnableToCreateException if there is an error during the creation process
177      */

178     public org.omg.CORBA.Object JavaDoc createStatelessObject(String JavaDoc appName, byte[] objectId,
179             String JavaDoc idlInterface, String JavaDoc 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 JavaDoc(objectId));
186
187             // get the class loader
188
ClassLoader JavaDoc loader = (ClassLoader JavaDoc) 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 = loader.loadClass(implClassName);
196
Class JavaDoc servantClass = Class.forName(implClassName, true, loader);
197
198             // create object pool
199
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             // create the object reference
205
org.omg.CORBA.Object JavaDoc obj = statelessPoa.create_reference_with_id(objectId, idlInterface);
206
207             // Add this component to the objectInfoTable
208
StatelessObjectInfo objInfo = new StatelessObjectInfo(servantClass, appContext, compContext, objPool);
209             objectInfoTable.put(new String JavaDoc(objectId), objInfo);
210
211             if (LOG.isDebugEnabled()) LOG.debug("The object has been created");
212
213             return obj;
214         }
215         catch (ClassNotFoundException JavaDoc e)
216         {
217             LOG.error("Class not found exception has been thrown", e);
218             throw new UnableToCreateException(e.getMessage());
219         }
220     }
221
222     /**
223      * Destroy the object. Basically remove the objects ObjectInfo entry
224      * from the ObjectInfo table
225      * @param oid the the ID of the object to be destroyed
226      */

227     public void destroyStatelessObject(byte[] oid)
228     {
229         try
230         {
231             objectInfoTable.remove(new String JavaDoc(oid));
232         }
233         catch (Exception JavaDoc e)
234         {
235             String JavaDoc 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     /**
247      * Returns the instantiator's deployment requirements
248      * @return the deployment requirements
249      */

250     public DeploymentRequirement[] getDeployementRequirements()
251     {
252         return DEPLOYMENT_REQUIREMENTS;
253     }
254
255 }
256
257 /**
258  * A filter for .jar files
259  *
260  * @author Carlos Arévalo
261  */

262 class JarFileFilter implements FilenameFilter JavaDoc
263 {
264     private static final String JavaDoc JAR_STRING = ".jar";
265
266     /**
267      * @see java.io.FilenameFilter#accept(java.io.File, java.lang.String)
268      */

269     public boolean accept(File JavaDoc dir, String JavaDoc name)
270     {
271         return name.endsWith(JAR_STRING);
272     }
273
274 }
275
Popular Tags