KickJava   Java API By Example, From Geeks To Geeks.

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


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  * BeanLoader.java
22  *
23  * This class is responsible for loading the coadunation beans in to memory from
24  * the jar.
25  */

26
27 package com.rift.coad.lib.deployment.bean;
28
29 // core java imports
30
import java.util.Map JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34
35 // logging import
36
import org.apache.log4j.Logger;
37
38 // coad imports
39
import com.rift.coad.lib.deployment.DeploymentLoader;
40 import com.rift.coad.lib.deployment.BeanInfo;
41 import com.rift.coad.lib.deployment.DeploymentThreadInfo;
42 import com.rift.coad.lib.bean.BeanWrapper;
43 import com.rift.coad.lib.bean.BeanThread;
44 import com.rift.coad.lib.bean.BeanRunnable;
45 import com.rift.coad.lib.security.ThreadsPermissionContainer;
46 import com.rift.coad.lib.thread.CoadunationThreadGroup;
47 import com.rift.coad.lib.thread.BasicThread;
48
49 /**
50  * This class is responsible for loading the coadunation beans in to memory from
51  * the jar file.
52  *
53  * @author Brett Chaldecott
54  */

55 public class BeanLoader extends BasicThread {
56     
57     // the class log variable
58
protected Logger log =
59         Logger.getLogger(BeanLoader.class.getName());
60     
61     // the reference to the
62
private DeploymentLoader deploymentLoader = null;
63     private Map JavaDoc beans = null;
64     private ThreadsPermissionContainer permissions = null;
65     private CoadunationThreadGroup threadGroup = null;
66     private boolean successful = true;
67     private Exception JavaDoc exception = null;
68     
69     /**
70      * Creates a new instance of BeanLoader.
71      *
72      * @param deploymentLoader The reference to the deployment loader.
73      * @param permissions The permissions assigned to the various threads.
74      * @param threadGroup The reference to the thread group.
75      * @exception BeanException
76      * @exception Exception
77      */

78     public BeanLoader(DeploymentLoader deploymentLoader,
79             ThreadsPermissionContainer permissions, CoadunationThreadGroup threadGroup)
80             throws BeanException,Exception JavaDoc {
81         this.deploymentLoader = deploymentLoader;
82         this.permissions = permissions;
83         this.threadGroup = threadGroup.createThreadGroup();
84         beans = new HashMap JavaDoc();
85     }
86     
87     
88     /**
89      * This method finalizes the threads in this object.
90      */

91     protected void finalize() {
92         stopThreads();
93     }
94     
95     
96     /**
97      * This method loads the beans from the deployment file.
98      *
99      * @exception BeanException
100      */

101     public void process() {
102         try {
103             log.info("Load the Beans for [" +
104                 deploymentLoader.getFile().getPath() + "]");
105             Map JavaDoc beanList = deploymentLoader.getDeploymentInfo().getBeans();
106             for (Iterator JavaDoc iter = beanList.keySet().iterator(); iter.hasNext();) {
107                 BeanInfo beanInfo = (BeanInfo)beanList.get(iter.next());
108                 BeanWrapper beanWrapper = new BeanWrapper(deploymentLoader,
109                         beanInfo,permissions);
110                 beans.put(beanInfo.getBindName(),beanWrapper);
111                 if (beanWrapper.getSubObject() instanceof BeanRunnable) {
112                     BeanThread beanThread = new BeanThread(
113                             (BeanRunnable)beanWrapper.getSubObject());
114                     threadGroup.addThread(beanThread,beanInfo.getUsername());
115                     beanThread.start();
116                 }
117                 
118                 // loop through the bean thread information
119
List JavaDoc threadList = beanInfo.getThreadInfoList();
120                 for (int index = 0; index < threadList.size(); index++) {
121                     DeploymentThreadInfo threadInfo =
122                             (DeploymentThreadInfo)threadList.get(index);
123                     threadGroup.startThreads(
124                             deploymentLoader.getClass(threadInfo.getClassName())
125                             ,threadInfo.getUsername(),
126                             (int)threadInfo.getThreadNumber());
127                 }
128             }
129         } catch (Exception JavaDoc ex) {
130             log.error("Failed to instanciate the bean : " + ex.getMessage(),ex);
131             stopThreads();
132             successful = false;
133             exception = new BeanException("Failed to load the beans : " +
134                     ex.getMessage(),ex);
135         }
136     }
137     
138     
139     /**
140      * Terminate the processing of this object.
141      */

142     public void terminate() {
143         
144     }
145     
146     
147     /**
148      * This method is called to perform the unload on this bean loader object.
149      */

150     public void stopThreads() {
151         try {
152             if (threadGroup.isTerminated() == false) {
153                 threadGroup.terminate();
154             }
155         } catch(Exception JavaDoc ex) {
156             log.error("Failed to unload the beans [" +
157                     deploymentLoader.getFile().getPath() + "] because : " +
158                     ex.getMessage(),ex);
159         }
160     }
161     
162     
163     /**
164      * This method returns the loaded beans.
165      *
166      * @return The list of loaded beans.
167      */

168     public Map JavaDoc getBeans() {
169         return beans;
170     }
171     
172     
173     /**
174      * This method returns true if the loading was successfull and false
175      * otherwise.
176      */

177     public boolean wasSucessfull() {
178         return successful;
179     }
180     
181     
182     /**
183      * The exception that caused the problem
184      */

185     public Exception JavaDoc getException() {
186         return exception;
187     }
188 }
189
Popular Tags