001: /*
002: * CoadunationLib: The coaduntion implementation library.
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * XMLConfigurationException.java
020: *
021: * BeanLoader.java
022: *
023: * This class is responsible for loading the coadunation beans in to memory from
024: * the jar.
025: */
026:
027: package com.rift.coad.lib.deployment.bean;
028:
029: // core java imports
030: import java.util.Map;
031: import java.util.HashMap;
032: import java.util.Iterator;
033: import java.util.List;
034:
035: // logging import
036: import org.apache.log4j.Logger;
037:
038: // coad imports
039: import com.rift.coad.lib.deployment.DeploymentLoader;
040: import com.rift.coad.lib.deployment.BeanInfo;
041: import com.rift.coad.lib.deployment.DeploymentThreadInfo;
042: import com.rift.coad.lib.bean.BeanWrapper;
043: import com.rift.coad.lib.bean.BeanThread;
044: import com.rift.coad.lib.bean.BeanRunnable;
045: import com.rift.coad.lib.security.ThreadsPermissionContainer;
046: import com.rift.coad.lib.thread.CoadunationThreadGroup;
047: import com.rift.coad.lib.thread.BasicThread;
048:
049: /**
050: * This class is responsible for loading the coadunation beans in to memory from
051: * the jar file.
052: *
053: * @author Brett Chaldecott
054: */
055: public class BeanLoader extends BasicThread {
056:
057: // the class log variable
058: protected Logger log = Logger.getLogger(BeanLoader.class.getName());
059:
060: // the reference to the
061: private DeploymentLoader deploymentLoader = null;
062: private Map beans = null;
063: private ThreadsPermissionContainer permissions = null;
064: private CoadunationThreadGroup threadGroup = null;
065: private boolean successful = true;
066: private Exception exception = null;
067:
068: /**
069: * Creates a new instance of BeanLoader.
070: *
071: * @param deploymentLoader The reference to the deployment loader.
072: * @param permissions The permissions assigned to the various threads.
073: * @param threadGroup The reference to the thread group.
074: * @exception BeanException
075: * @exception Exception
076: */
077: public BeanLoader(DeploymentLoader deploymentLoader,
078: ThreadsPermissionContainer permissions,
079: CoadunationThreadGroup threadGroup) throws BeanException,
080: Exception {
081: this .deploymentLoader = deploymentLoader;
082: this .permissions = permissions;
083: this .threadGroup = threadGroup.createThreadGroup();
084: beans = new HashMap();
085: }
086:
087: /**
088: * This method finalizes the threads in this object.
089: */
090: protected void finalize() {
091: stopThreads();
092: }
093:
094: /**
095: * This method loads the beans from the deployment file.
096: *
097: * @exception BeanException
098: */
099: public void process() {
100: try {
101: log.info("Load the Beans for ["
102: + deploymentLoader.getFile().getPath() + "]");
103: Map beanList = deploymentLoader.getDeploymentInfo()
104: .getBeans();
105: for (Iterator iter = beanList.keySet().iterator(); iter
106: .hasNext();) {
107: BeanInfo beanInfo = (BeanInfo) beanList
108: .get(iter.next());
109: BeanWrapper beanWrapper = new BeanWrapper(
110: deploymentLoader, beanInfo, permissions);
111: beans.put(beanInfo.getBindName(), beanWrapper);
112: if (beanWrapper.getSubObject() instanceof BeanRunnable) {
113: BeanThread beanThread = new BeanThread(
114: (BeanRunnable) beanWrapper.getSubObject());
115: threadGroup.addThread(beanThread, beanInfo
116: .getUsername());
117: beanThread.start();
118: }
119:
120: // loop through the bean thread information
121: List threadList = beanInfo.getThreadInfoList();
122: for (int index = 0; index < threadList.size(); index++) {
123: DeploymentThreadInfo threadInfo = (DeploymentThreadInfo) threadList
124: .get(index);
125: threadGroup.startThreads(deploymentLoader
126: .getClass(threadInfo.getClassName()),
127: threadInfo.getUsername(), (int) threadInfo
128: .getThreadNumber());
129: }
130: }
131: } catch (Exception ex) {
132: log.error("Failed to instanciate the bean : "
133: + ex.getMessage(), ex);
134: stopThreads();
135: successful = false;
136: exception = new BeanException("Failed to load the beans : "
137: + ex.getMessage(), ex);
138: }
139: }
140:
141: /**
142: * Terminate the processing of this object.
143: */
144: public void terminate() {
145:
146: }
147:
148: /**
149: * This method is called to perform the unload on this bean loader object.
150: */
151: public void stopThreads() {
152: try {
153: if (threadGroup.isTerminated() == false) {
154: threadGroup.terminate();
155: }
156: } catch (Exception ex) {
157: log.error("Failed to unload the beans ["
158: + deploymentLoader.getFile().getPath()
159: + "] because : " + ex.getMessage(), ex);
160: }
161: }
162:
163: /**
164: * This method returns the loaded beans.
165: *
166: * @return The list of loaded beans.
167: */
168: public Map getBeans() {
169: return beans;
170: }
171:
172: /**
173: * This method returns true if the loading was successfull and false
174: * otherwise.
175: */
176: public boolean wasSucessfull() {
177: return successful;
178: }
179:
180: /**
181: * The exception that caused the problem
182: */
183: public Exception getException() {
184: return exception;
185: }
186: }
|