1 26 27 package com.rift.coad.lib.deployment.bean; 28 29 import java.util.Map ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 35 import org.apache.log4j.Logger; 37 38 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 55 public class BeanLoader extends BasicThread { 56 57 protected Logger log = 59 Logger.getLogger(BeanLoader.class.getName()); 60 61 private DeploymentLoader deploymentLoader = null; 63 private Map beans = null; 64 private ThreadsPermissionContainer permissions = null; 65 private CoadunationThreadGroup threadGroup = null; 66 private boolean successful = true; 67 private Exception exception = null; 68 69 78 public BeanLoader(DeploymentLoader deploymentLoader, 79 ThreadsPermissionContainer permissions, CoadunationThreadGroup threadGroup) 80 throws BeanException,Exception { 81 this.deploymentLoader = deploymentLoader; 82 this.permissions = permissions; 83 this.threadGroup = threadGroup.createThreadGroup(); 84 beans = new HashMap (); 85 } 86 87 88 91 protected void finalize() { 92 stopThreads(); 93 } 94 95 96 101 public void process() { 102 try { 103 log.info("Load the Beans for [" + 104 deploymentLoader.getFile().getPath() + "]"); 105 Map beanList = deploymentLoader.getDeploymentInfo().getBeans(); 106 for (Iterator 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 List 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 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 142 public void terminate() { 143 144 } 145 146 147 150 public void stopThreads() { 151 try { 152 if (threadGroup.isTerminated() == false) { 153 threadGroup.terminate(); 154 } 155 } catch(Exception ex) { 156 log.error("Failed to unload the beans [" + 157 deploymentLoader.getFile().getPath() + "] because : " + 158 ex.getMessage(),ex); 159 } 160 } 161 162 163 168 public Map getBeans() { 169 return beans; 170 } 171 172 173 177 public boolean wasSucessfull() { 178 return successful; 179 } 180 181 182 185 public Exception getException() { 186 return exception; 187 } 188 } 189 | Popular Tags |