KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lateralnz > panther > container > SSBContainer


1 /* ====================================================================
2  * The LateralNZ Software License, Version 1.0
3  *
4  * Copyright (c) 2003 LateralNZ. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by
21  * LateralNZ (http://www.lateralnz.org/) and other third parties."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "LateralNZ" must not be used to endorse or promote
26  * products derived from this software without prior written
27  * permission. For written permission, please
28  * contact oss@lateralnz.org.
29  *
30  * 5. Products derived from this software may not be called "Panther",
31  * or "Lateral" or "LateralNZ", nor may "PANTHER" or "LATERAL" or
32  * "LATERALNZ" appear in their name, without prior written
33  * permission of LateralNZ.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of LateralNZ. For more
51  * information on Lateral, please see http://www.lateralnz.com/ or
52  * http://www.lateralnz.org
53  *
54  */

55 package org.lateralnz.panther.container;
56
57 import java.io.File JavaDoc;
58 import java.lang.reflect.Constructor JavaDoc;
59 import java.util.ArrayList JavaDoc;
60 import java.util.HashMap JavaDoc;
61 import java.util.Iterator JavaDoc;
62 import java.util.jar.JarFile JavaDoc;
63 import java.util.List JavaDoc;
64 import java.util.Map JavaDoc;
65 import java.util.Properties JavaDoc;
66
67 import javax.naming.Reference JavaDoc;
68 import javax.naming.Referenceable JavaDoc;
69
70 import org.apache.log4j.Logger;
71
72 import org.w3c.dom.Document JavaDoc;
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 /**
87  *
88  * @author J R Briggs
89  */

90 public class SSBContainer extends Container implements Constants, Referenceable JavaDoc {
91   private static final Logger log = Logger.getLogger(SSBContainer.class.getName());
92   private static Reference JavaDoc REF = new Reference JavaDoc(SSBContainer.class.getName());
93     
94   private Map JavaDoc ssbeans = new HashMap JavaDoc(); // a map of the beans in this container
95
private GenericKeyedObjectPool pool = null;
96   private KeyedPoolableBeanObjectFactory kpbof = null;
97
98  /**
99   *
100   */

101   public SSBContainer(Properties JavaDoc props, Object JavaDoc parent) throws Exception JavaDoc {
102     super(props, parent);
103
104     kpbof = new KeyedPoolableBeanObjectFactory();
105     //pool = new GenericKeyedObjectPool(kpbof, 200, GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK, 50000, 20);
106
pool = new GenericKeyedObjectPool(kpbof, -1);
107     
108     if (log.isInfoEnabled()) {
109       log.info("deploy dir " + pantherHome + deployDir);
110     }
111     
112     // deploy ejb-jars
113
String JavaDoc[] 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  /**
131   * shutdown and cleanup the server
132   */

133   public void shutdown() {
134     ssbeans.clear();
135   }
136
137  /**
138   * deploy an ejb jar file. First checks if it is an ejb jar file (i.e. does it contain
139   * ejb-jar.xml?), then parses the contents
140   */

141   private void deploy(String JavaDoc filename) throws Exception JavaDoc {
142     File JavaDoc f = new File JavaDoc(filename);
143     JarFile JavaDoc jar = new JarFile JavaDoc(f, true, JarFile.OPEN_READ);
144     Document JavaDoc doc = EJBJarUtils.getEJBJarXML(jar);
145
146     // not an ejb jar file
147
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     // various lists needed during the parse
162
List JavaDoc ejbwrappers = new ArrayList JavaDoc();
163     List JavaDoc rmicfiles = new ArrayList JavaDoc();
164     List JavaDoc wrapperClasses = new ArrayList JavaDoc();
165     
166     // work through the session descriptors
167
List JavaDoc l = EJBJarUtils.getSessionDescriptors(doc);
168     Iterator JavaDoc iter = l.iterator();
169     while (iter.hasNext()) {
170       SessionDescriptor sd = (SessionDescriptor)iter.next();
171
172       // get the remote, ejb and home classes
173
Class JavaDoc remote = Class.forName(sd.getEJBRemoteClass());
174       Class JavaDoc home = Class.forName(sd.getEJBHomeClass());
175       Class JavaDoc ejb = Class.forName(sd.getEJBClass());
176       
177       String JavaDoc packageName = sd.getEJBRemoteClass().substring(0, sd.getEJBRemoteClass().lastIndexOf('.'));
178       String JavaDoc packageDir = StringUtils.replace(packageName, DOT, FILE_SEPARATOR);
179       String JavaDoc[] tmp = StringUtils.toArray(remote.getName(), DOT);
180       
181       // this is what we'll call our ejb wrapper
182
String JavaDoc wrapperName = tmp[tmp.length - 1] + "Impl";
183       String JavaDoc homeName = tmp[tmp.length - 1] + "HomeImpl";
184       //String wrapperFullName = StringUtils.replace(packageDir, FILE_SEPARATOR, UNDERSCORE) + UNDERSCORE + wrapperName;
185
String JavaDoc ejbNameResolved = StringUtils.replace(sd.getEJBName(), FILE_SEPARATOR, UNDERSCORE);
186                   
187       // now we load the env-entry parameters from the ejb-jar
188
if (log.isInfoEnabled()) {
189         log.info("loading environment entries " + envContext);
190       }
191       Map JavaDoc env = sd.getEnvEntries();
192       Iterator JavaDoc enviter = env.keySet().iterator();
193       while (enviter.hasNext()) {
194         String JavaDoc key = (String JavaDoc)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 JavaDoc wrapperClass = Class.forName(remote.getPackage().getName() + DOT + wrapperName);
202       Class JavaDoc homeClass = Class.forName(remote.getPackage().getName() + DOT + homeName);
203       kpbof.setWrapperClass(ejbNameResolved, wrapperClass);
204       
205       Constructor JavaDoc cons = homeClass.getConstructor(new Class JavaDoc[]{ String JavaDoc.class, GenericKeyedObjectPool.class });
206       SessionBeanHome beanhome = (SessionBeanHome)cons.newInstance(new Object JavaDoc[]{ 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 JavaDoc[] sub = StringUtils.toArray(sd.getEJBName(), FORWARD_SLASH);
209       try {
210         initContext.createSubcontext(sub[0]);
211       }
212       catch (Exception JavaDoc 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  /**
229   * get a reference to this container
230   */

231   public Reference JavaDoc getReference() {
232     return REF;
233   }
234   
235  /**
236   * a wrapper for ejb data (classname, filename, ejb name)
237   */

238   private class EJBData {
239     public String JavaDoc className;
240     public String JavaDoc classFilename;
241     public String JavaDoc ejbName;
242     
243     EJBData(String JavaDoc className, String JavaDoc classFilename, String JavaDoc ejbName) {
244       this.className = className;
245       this.classFilename = classFilename;
246       this.ejbName = ejbName;
247     }
248   }
249 }
Popular Tags