1 55 package org.lateralnz.panther.container; 56 57 import java.io.File ; 58 import java.lang.reflect.Constructor ; 59 import java.util.ArrayList ; 60 import java.util.HashMap ; 61 import java.util.Iterator ; 62 import java.util.jar.JarFile ; 63 import java.util.List ; 64 import java.util.Map ; 65 import java.util.Properties ; 66 67 import javax.naming.Reference ; 68 import javax.naming.Referenceable ; 69 70 import org.apache.log4j.Logger; 71 72 import org.w3c.dom.Document ; 73 74 import org.lateralnz.common.util.Constants; 75 import org.lateralnz.common.util.StringUtils; 76 import org.lateralnz.common.util.SystemUtils; 77 import org.lateralnz.panther.deploy.SessionDescriptor; 78 import org.lateralnz.panther.deploy.EJBJarUtils; 79 import org.lateralnz.panther.util.EJBConstants; 80 import org.lateralnz.panther.wrapper.EJBMetaDataWrapper; 81 import org.lateralnz.panther.wrapper.KeyedPoolableBeanObjectFactory; 82 import org.lateralnz.panther.wrapper.SessionBeanHome; 83 84 import org.apache.commons.pool.impl.GenericKeyedObjectPool; 85 86 90 public class SSBContainer extends Container implements Constants, Referenceable { 91 private static final Logger log = Logger.getLogger(SSBContainer.class.getName()); 92 private static Reference REF = new Reference (SSBContainer.class.getName()); 93 94 private Map ssbeans = new HashMap (); private GenericKeyedObjectPool pool = null; 96 private KeyedPoolableBeanObjectFactory kpbof = null; 97 98 101 public SSBContainer(Properties props, Object parent) throws Exception { 102 super(props, parent); 103 104 kpbof = new KeyedPoolableBeanObjectFactory(); 105 pool = new GenericKeyedObjectPool(kpbof, -1); 107 108 if (log.isInfoEnabled()) { 109 log.info("deploy dir " + pantherHome + deployDir); 110 } 111 112 String [] filelist = SystemUtils.getFileList(pantherHome + deployDir, ".*\\.jar$"); 114 if (log.isInfoEnabled()) { 115 log.info(deployDir + " file list: " + filelist.length); 116 } 117 for (int i = 0; i < filelist.length; i++) { 118 if (log.isDebugEnabled()) { 119 log.debug("evaluating: " + filelist[i]); 120 } 121 deploy(pantherHome + deployDir + filelist[i]); 122 } 123 124 System.gc(); 125 if (log.isInfoEnabled()) { 126 log.info("ssbc initialisation complete"); 127 } 128 } 129 130 133 public void shutdown() { 134 ssbeans.clear(); 135 } 136 137 141 private void deploy(String filename) throws Exception { 142 File f = new File (filename); 143 JarFile jar = new JarFile (f, true, JarFile.OPEN_READ); 144 Document doc = EJBJarUtils.getEJBJarXML(jar); 145 146 if (doc == null) { 148 if (log.isInfoEnabled()) { 149 log.info("no ejb-jar.xml file found"); 150 } 151 return; 152 } 153 else if (log.isInfoEnabled()) { 154 log.info("found ejb-jar.xml"); 155 } 156 157 if (log.isInfoEnabled()) { 158 log.info("checking for session bean descriptors"); 159 } 160 161 List ejbwrappers = new ArrayList (); 163 List rmicfiles = new ArrayList (); 164 List wrapperClasses = new ArrayList (); 165 166 List l = EJBJarUtils.getSessionDescriptors(doc); 168 Iterator iter = l.iterator(); 169 while (iter.hasNext()) { 170 SessionDescriptor sd = (SessionDescriptor)iter.next(); 171 172 Class remote = Class.forName(sd.getEJBRemoteClass()); 174 Class home = Class.forName(sd.getEJBHomeClass()); 175 Class ejb = Class.forName(sd.getEJBClass()); 176 177 String packageName = sd.getEJBRemoteClass().substring(0, sd.getEJBRemoteClass().lastIndexOf('.')); 178 String packageDir = StringUtils.replace(packageName, DOT, FILE_SEPARATOR); 179 String [] tmp = StringUtils.toArray(remote.getName(), DOT); 180 181 String wrapperName = tmp[tmp.length - 1] + "Impl"; 183 String homeName = tmp[tmp.length - 1] + "HomeImpl"; 184 String ejbNameResolved = StringUtils.replace(sd.getEJBName(), FILE_SEPARATOR, UNDERSCORE); 186 187 if (log.isInfoEnabled()) { 189 log.info("loading environment entries " + envContext); 190 } 191 Map env = sd.getEnvEntries(); 192 Iterator enviter = env.keySet().iterator(); 193 while (enviter.hasNext()) { 194 String key = (String )enviter.next(); 195 if (log.isInfoEnabled()) { 196 log.info("binding " + ejbNameResolved + UNDERSCORE + key + " with value " + env.get(key)); 197 } 198 envContext.rebind(ejbNameResolved + UNDERSCORE + key, env.get(key)); 199 } 200 201 Class wrapperClass = Class.forName(remote.getPackage().getName() + DOT + wrapperName); 202 Class homeClass = Class.forName(remote.getPackage().getName() + DOT + homeName); 203 kpbof.setWrapperClass(ejbNameResolved, wrapperClass); 204 205 Constructor cons = homeClass.getConstructor(new Class []{ String .class, GenericKeyedObjectPool.class }); 206 SessionBeanHome beanhome = (SessionBeanHome)cons.newInstance(new Object []{ ejbNameResolved, pool }); 207 beanhome.setEJBMetaData(new EJBMetaDataWrapper(beanhome, homeClass, remote, (sd.getSessionType().equals(EJBConstants.STATEFUL) ? EJBConstants.STATEFUL_SESSION_BEAN : EJBConstants.STATELESS_SESSION_BEAN))); 208 String [] sub = StringUtils.toArray(sd.getEJBName(), FORWARD_SLASH); 209 try { 210 initContext.createSubcontext(sub[0]); 211 } 212 catch (Exception e) { 213 } 214 if (log.isInfoEnabled()) { 215 log.info("binding " + sd.getEJBName()); 216 } 217 initContext.rebind(sd.getEJBName(), beanhome); 218 ejbwrappers.add(beanhome); 219 ssbeans.put(sd.getEJBName(), beanhome); 220 } 221 222 if (log.isInfoEnabled()) { 223 log.info("initialising ejbs..."); 224 } 225 226 } 227 228 231 public Reference getReference() { 232 return REF; 233 } 234 235 238 private class EJBData { 239 public String className; 240 public String classFilename; 241 public String ejbName; 242 243 EJBData(String className, String classFilename, String ejbName) { 244 this.className = className; 245 this.classFilename = classFilename; 246 this.ejbName = ejbName; 247 } 248 } 249 } | Popular Tags |