1 26 27 package com.rift.coad.lib.deployment.bean; 29 30 import java.util.Map ; 32 import java.util.HashMap ; 33 import java.util.Iterator ; 34 import java.util.Set ; 35 import java.rmi.Remote ; 36 import javax.naming.Context ; 37 import javax.naming.InitialContext ; 38 import javax.naming.Name ; 39 import javax.rmi.PortableRemoteObject ; 40 41 import org.apache.log4j.Logger; 43 44 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 61 public class BeanManager { 62 63 68 public class BeanList { 69 70 private final static String DEFAULT_PATH = "java:comp/env/bean"; 72 private final static String BEAN_CTX = "beanctx"; 73 74 private Map beans = null; 76 private Configuration config = null; 77 private Context context = null; 78 private ContextManager contextManager = null; 79 80 85 public BeanList() throws BeanException { 86 beans = new HashMap (); 87 try { 88 config = ConfigurationFactory.getInstance(). 89 getConfig(this.getClass()); 90 context = new InitialContext (); 91 contextManager = new ContextManager( 92 config.getString(BEAN_CTX,DEFAULT_PATH)); 93 } catch (Exception ex) { 94 throw new BeanException("Failed to instanciate the BeanList : " 95 + ex.getMessage(),ex); 96 } 97 } 98 99 100 106 public synchronized void addBean(String key, Object 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 contextManager.bind(key,wrapper.getProxy()); 118 119 beans.put(key,bean); 121 } catch (Exception 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 135 public synchronized void removeBean(String key) throws BeanException { 136 try { 137 BeanWrapper wrapper = (BeanWrapper)beans.get(key); 138 if (wrapper.getTie() != null) { 139 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 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 ex) { 154 throw new BeanException( 155 "Failed to remove the bean from the list : " + 156 ex.getMessage(),ex); 157 } 158 } 159 160 161 167 public synchronized boolean contains(String key) { 168 return beans.containsKey(key); 169 } 170 171 172 179 public synchronized Object getBean(String key) { 180 return beans.get(key); 181 } 182 183 184 189 public synchronized Set getKeys() { 190 return beans.keySet(); 191 } 192 } 193 194 private final static String USERNAME = "bean_user"; 196 197 protected Logger log = 199 Logger.getLogger(BeanManager.class.getName()); 200 201 private ThreadsPermissionContainer permissions = null; 203 204 private Map entries = null; 206 private BeanList beanList = null; 207 private CoadunationThreadGroup threadGroup = null; 208 private String username = null; 209 210 217 public BeanManager(ThreadsPermissionContainer permissions, 218 CoadunationThreadGroup threadGroup) throws Exception { 219 this.permissions = permissions; 220 this.threadGroup = threadGroup.createThreadGroup(); 221 entries = new HashMap (); 222 beanList = new BeanList(); 223 try { 224 Configuration config = ConfigurationFactory.getInstance(). 225 getConfig(BeanManager.class); 226 username = config.getString(USERNAME); 227 } catch (Exception ex) { 228 throw new BeanException("Failed to start the bean manager [" + 229 ex.getMessage() + "]",ex); 230 } 231 } 232 233 234 240 public void load(DeploymentLoader deploymentLoader) throws BeanException, 241 Exception { 242 if (entries.containsKey(deploymentLoader)) { 243 throw new BeanException("This entries has been loaded before."); 244 } 245 246 beanClash(deploymentLoader.getDeploymentInfo().getBeans()); 248 249 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 Map beans = beanLoader.getBeans(); 262 for (Iterator iter = beans.keySet().iterator(); iter.hasNext();) { 263 String key = (String )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 278 public void unLoad(DeploymentLoader deploymentLoader) throws BeanException { 279 if (false == entries.containsKey(deploymentLoader)) { 280 return; 282 } 283 BeanLoader beanLoader = (BeanLoader)entries.get(deploymentLoader); 285 Map beans = beanLoader.getBeans(); 286 for (Iterator iter = beans.keySet().iterator(); iter.hasNext();) { 287 String key = (String )iter.next(); 288 log.info("Unload bean [" + key + "]"); 289 beanList.removeBean(key); 290 } 291 292 entries.remove(deploymentLoader); 294 295 beanLoader.stopThreads(); 297 } 298 299 300 305 public Set getKeys() { 306 return beanList.getKeys(); 307 } 308 309 310 316 public Object getBean(String key) { 317 return beanList.getBean(key); 318 } 319 320 321 327 private void beanClash(Map newBeans) throws BeanException { 328 329 for (Iterator 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 |