KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > deployment > bean > BeanManager


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * XMLConfigurationException.java
20  *
21  * BeanManager.java
22  *
23  * This object is responsible for managing the loading and unloading of
24  * coadunation beans.
25  */

26
27 // package path
28
package com.rift.coad.lib.deployment.bean;
29
30 // java imports
31
import java.util.Map JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Set JavaDoc;
35 import java.rmi.Remote JavaDoc;
36 import javax.naming.Context JavaDoc;
37 import javax.naming.InitialContext JavaDoc;
38 import javax.naming.Name JavaDoc;
39 import javax.rmi.PortableRemoteObject JavaDoc;
40
41 // logging import
42
import org.apache.log4j.Logger;
43
44 // coadunation imports
45
import com.rift.coad.lib.security.ThreadsPermissionContainer;
46 import com.rift.coad.lib.deployment.DeploymentLoader;
47 import com.rift.coad.lib.thread.CoadunationThreadGroup;
48 import com.rift.coad.lib.deployment.BeanInfo;
49 import com.rift.coad.lib.configuration.Configuration;
50 import com.rift.coad.lib.configuration.ConfigurationFactory;
51 import com.rift.coad.lib.naming.ContextManager;
52 import com.rift.coad.lib.bean.BeanWrapper;
53
54
55 /**
56  * This object is responsible for managing the loading and unloading of
57  * coadunation beans.
58  *
59  * @author Brett Chaldecott
60  */

61 public class BeanManager {
62     
63     /**
64      * This object wrapps access to the beans.
65      *
66      * @author Brett Chaldecott
67      */

68     public class BeanList {
69         
70         // class constants
71
private final static String JavaDoc DEFAULT_PATH = "java:comp/env/bean";
72         private final static String JavaDoc BEAN_CTX = "beanctx";
73         
74         // private member variables
75
private Map JavaDoc beans = null;
76         private Configuration config = null;
77         private Context JavaDoc context = null;
78         private ContextManager contextManager = null;
79         
80         /**
81          * The constructor of the bean list.
82          *
83          * @exception BeanException
84          */

85         public BeanList() throws BeanException {
86             beans = new HashMap JavaDoc();
87             try {
88                 config = ConfigurationFactory.getInstance().
89                         getConfig(this.getClass());
90                 context = new InitialContext JavaDoc();
91                 contextManager = new ContextManager(
92                         config.getString(BEAN_CTX,DEFAULT_PATH));
93             } catch (Exception JavaDoc ex) {
94                 throw new BeanException("Failed to instanciate the BeanList : "
95                         + ex.getMessage(),ex);
96             }
97         }
98         
99         
100         /**
101          * This method adds a new bean to the list of beans
102          *
103          * @param key The new key to add.
104          * @param bean The object reference to add.
105          */

106         public synchronized void addBean(String JavaDoc key, Object JavaDoc bean) throws
107                 BeanException {
108             try {
109                 BeanWrapper wrapper = (BeanWrapper)bean;
110                 if (wrapper.getTie() != null) {
111                     log.info("Adding the [" + key
112                             + "] to the portable remote object.");
113                     javax.rmi.PortableRemoteObject.exportObject(wrapper.getTie());
114                     context.bind(key,wrapper.getTie());
115                 }
116                 // local context binding
117
contextManager.bind(key,wrapper.getProxy());
118                 
119                 // add to maps
120
beans.put(key,bean);
121             } catch (Exception JavaDoc ex) {
122                 log.error("Failed to add the bean [" + key + "]",ex);
123                 throw new BeanException(
124                         "Failed to add the bean to the list : " +
125                         ex.getMessage(),ex);
126             }
127         }
128         
129         
130         /**
131          * This method removes the bean from the map.
132          *
133          * @param key The key identifying the bean to remove.
134          */

135         public synchronized void removeBean(String JavaDoc key) throws BeanException {
136             try {
137                 BeanWrapper wrapper = (BeanWrapper)beans.get(key);
138                 if (wrapper.getTie() != null) {
139                     // we do not need to remove the local reference as
140
// this gets removed when the context gets released
141
try {
142                         log.info("Unbinding an object [" + key
143                                 + "] from the portable object");
144                         context.unbind(key);
145                         javax.rmi.PortableRemoteObject.unexportObject(
146                                 wrapper.getTie());
147                     } catch (org.omg.CORBA.BAD_INV_ORDER JavaDoc ex) {
148                         log.info("Failed to unbind the object [" + key
149                                 + "] as it is not bound.",ex);
150                     }
151                 }
152                 beans.remove(key);
153             } catch (Exception JavaDoc ex) {
154                 throw new BeanException(
155                         "Failed to remove the bean from the list : " +
156                         ex.getMessage(),ex);
157             }
158         }
159         
160         
161         /**
162          * This method returns true if the bean list contains the key.
163          *
164          * @return TRUE if the bean is found FALSE if not.
165          * @param key The key to check for.
166          */

167         public synchronized boolean contains(String JavaDoc key) {
168             return beans.containsKey(key);
169         }
170         
171         
172         /**
173          * This method returns the reference to the object, if it cannot be
174          * found it returns null.
175          *
176          * @return The reference to the object.
177          * @param key The key to look for.
178          */

179         public synchronized Object JavaDoc getBean(String JavaDoc key) {
180             return beans.get(key);
181         }
182         
183         
184         /**
185          * This method returns the keys identifying the beans.
186          *
187          * @return The list of keys.
188          */

189         public synchronized Set JavaDoc getKeys() {
190             return beans.keySet();
191         }
192     }
193     
194     // class constants
195
private final static String JavaDoc USERNAME = "bean_user";
196     
197     // the class log variable
198
protected Logger log =
199             Logger.getLogger(BeanManager.class.getName());
200     
201     // the classes private member variables
202
private ThreadsPermissionContainer permissions = null;
203     
204     // the private member variables
205
private Map JavaDoc entries = null;
206     private BeanList beanList = null;
207     private CoadunationThreadGroup threadGroup = null;
208     private String JavaDoc username = null;
209     
210     /**
211      * Creates a new instance of BeanManager
212      *
213      * @param permissions The reference to the permission object.
214      * @param threadGroup The reference to the thread group.
215      * @exception Exception
216      */

217     public BeanManager(ThreadsPermissionContainer permissions,
218             CoadunationThreadGroup threadGroup) throws Exception JavaDoc {
219         this.permissions = permissions;
220         this.threadGroup = threadGroup.createThreadGroup();
221         entries = new HashMap JavaDoc();
222         beanList = new BeanList();
223         try {
224             Configuration config = ConfigurationFactory.getInstance().
225                     getConfig(BeanManager.class);
226             username = config.getString(USERNAME);
227         } catch (Exception JavaDoc ex) {
228             throw new BeanException("Failed to start the bean manager [" +
229                     ex.getMessage() + "]",ex);
230         }
231     }
232     
233     
234     /**
235      * This method will load beans from a deployment file.
236      *
237      * @param deploymentLoader The reference reference to the deployment loader.
238      * @exception BeanException
239      */

240     public void load(DeploymentLoader deploymentLoader) throws BeanException,
241             Exception JavaDoc {
242         if (entries.containsKey(deploymentLoader)) {
243             throw new BeanException("This entries has been loaded before.");
244         }
245         
246         // check for classesh
247
beanClash(deploymentLoader.getDeploymentInfo().getBeans());
248         
249         // load the beans
250
BeanLoader beanLoader = new BeanLoader(deploymentLoader,permissions,
251                 threadGroup);
252         beanLoader.setContextClassLoader(deploymentLoader.getClassLoader());
253         threadGroup.addThread(beanLoader,username);
254         beanLoader.start();
255         beanLoader.join();
256         if (!beanLoader.wasSucessfull()) {
257             throw beanLoader.getException();
258         }
259         
260         // add the new beans
261
Map JavaDoc beans = beanLoader.getBeans();
262         for (Iterator JavaDoc iter = beans.keySet().iterator(); iter.hasNext();) {
263             String JavaDoc key = (String JavaDoc)iter.next();
264             log.info("Load bean [" + key + "]");
265             beanList.addBean(key,beans.get(key));
266         }
267         entries.put(deploymentLoader,beanLoader);
268         
269     }
270     
271     
272     /**
273      * This method will unload the entry from the bean management object.
274      *
275      * @param deploymentLoader The reference to the deployment loader object.
276      * @exception BeanException
277      */

278     public void unLoad(DeploymentLoader deploymentLoader) throws BeanException {
279         if (false == entries.containsKey(deploymentLoader)) {
280             // do nothing there is nothing known about this entry
281
return;
282         }
283         // remove all the bean references.
284
BeanLoader beanLoader = (BeanLoader)entries.get(deploymentLoader);
285         Map JavaDoc beans = beanLoader.getBeans();
286         for (Iterator JavaDoc iter = beans.keySet().iterator(); iter.hasNext();) {
287             String JavaDoc key = (String JavaDoc)iter.next();
288             log.info("Unload bean [" + key + "]");
289             beanList.removeBean(key);
290         }
291         
292         // remove the bean
293
entries.remove(deploymentLoader);
294         
295         // unload the bean loader
296
beanLoader.stopThreads();
297     }
298     
299     
300     /**
301      * This method returns the list of keys.
302      *
303      * @return The list of beans managed by this object.
304      */

305     public Set JavaDoc getKeys() {
306         return beanList.getKeys();
307     }
308     
309     
310     /**
311      * Retrieve the bean that matches the key
312      *
313      * @return Return the object identified by the key.
314      * @param key The key identifying the bean.
315      */

316     public Object JavaDoc getBean(String JavaDoc key) {
317         return beanList.getBean(key);
318     }
319     
320     
321     /**
322      * This method determines if there are any bean classes based on name.
323      *
324      * @param newBeans The new beans to add to the list.
325      * @exception BeanException
326      */

327     private void beanClash(Map JavaDoc newBeans) throws BeanException {
328         
329         for (Iterator JavaDoc iter = newBeans.keySet().iterator(); iter.hasNext();) {
330             BeanInfo beanInfo = (BeanInfo)newBeans.get(iter.next());
331             if (this.beanList.contains(beanInfo.getBindName())) {
332                 throw new BeanException(
333                         "One of the beans clashes with an existing one [" +
334                         beanInfo.getBindName() + "]");
335             }
336         }
337     }
338     
339     
340 }
341
Popular Tags