KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > client > TomcatEjbFactory


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact dev@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://www.openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: TomcatEjbFactory.java 1912 2005-06-16 22:29:56Z jlaskowski $
44  */

45 package org.openejb.client;
46
47 import java.util.Enumeration JavaDoc;
48 import java.util.Hashtable JavaDoc;
49 import java.util.Properties JavaDoc;
50
51 import javax.naming.Context JavaDoc;
52 import javax.naming.InitialContext JavaDoc;
53 import javax.naming.Name JavaDoc;
54 import javax.naming.RefAddr JavaDoc;
55 import javax.naming.Reference JavaDoc;
56 import javax.naming.spi.ObjectFactory JavaDoc;
57
58 /**
59  * Tomcat EJB factory. The factory allows a web application deployed in Tomcat to look up a bean deployed in OpenEJB.
60  * Depending on the factory's configuration OpenEJB will start up in the local mode (inside the JVM Tomcat runs in) or
61  * the request for the bean will be passed along to OpenEJB remote instance.
62  * <p>
63  * Declaration of the factory in Tomcat's server.xml:
64  * <blockquote><pre>
65  * &lt;Context ...&gt;
66  * ...
67  * &lt;Ejb name="ejb/hello"
68  * type="Session"
69  * home="org.acme.HelloHome"
70  * remote="org.acme.Hello"/&gt;
71  * &lt;ResourceParams name="ejb/hello"&gt;
72  * &lt;parameter&gt;
73  * &lt;name&gt;factory&lt;/name&gt;
74  * &lt;value&gt;org.openejb.client.TomcatEjbFactory&lt;/value&gt;
75  * &lt;/parameter&gt;
76  * &lt;parameter&gt;
77  * &lt;name&gt;openejb.naming.factory.initial&lt;/name&gt;
78  * &lt;value&gt;org.openejb.client.RemoteInitialContextFactory&lt;/value&gt;
79  * &lt;/parameter&gt;
80  * &lt;parameter&gt;
81  * &lt;name&gt;openejb.naming.security.principal&lt;/name&gt;
82  * &lt;value&gt;username&lt;/value&gt;
83  * &lt;/parameter&gt;
84  * &lt;parameter&gt;
85  * &lt;name&gt;openejb.naming.security.credentials&lt;/name&gt;
86  * &lt;value&gt;password&lt;/value&gt;
87  * &lt;/parameter&gt;
88  * &lt;parameter&gt;
89  * &lt;name&gt;openejb.naming.provider.url&lt;/name&gt;
90  * &lt;value&gt;localhost:4201&lt;/value&gt;
91  * &lt;/parameter&gt;
92  * &lt;parameter&gt;
93  * &lt;name&gt;openejb.ejb-link&lt;/name&gt;
94  * &lt;value&gt;Hello&lt;/value&gt;
95  * &lt;/parameter&gt;
96  * &lt;/ResourceParams&gt;
97  * ...
98  * &lt;/Context&gt;
99  * </pre></blockquote>
100  * Changing RemoteInitialContextFactory (<i>openejb.naming.factory.initial</i> parametr's value) into LocalInitialContextFactory incurs starting OpenEJB in the local mode.
101  * <p>
102  * Make sure to read OpenEJB documentation for more information on the factory -
103  * <a HREF="http://www.openejb.org">http://www.openejb.org/tomcat.html</a>
104  *
105  * @author <a HREF="mailto:jacek.laskowski@hp.com">Jacek Laskowski</a>
106  * @since 01/12/2003
107  */

108 public final class TomcatEjbFactory implements ObjectFactory JavaDoc
109 {
110     private final static String JavaDoc OPENEJB_PREFIX = "openejb.";
111
112     private final static String JavaDoc JAVA_PREFIX = "java.";
113
114     private final static String JavaDoc OPENEJB_EJB_LINK = "openejb.ejb-link";
115
116     private final static int OPENEJB_PREFIX_LENGTH = OPENEJB_PREFIX.length();
117
118     public Object JavaDoc getObjectInstance( Object JavaDoc obj,
119                                      Name JavaDoc name,
120                                      Context JavaDoc nameCtx,
121                                      Hashtable JavaDoc environment )
122             throws Exception JavaDoc
123     {
124         Object JavaDoc beanObj = null;
125         Class JavaDoc ejbRefClass = Class.forName( "org.apache.naming.EjbRef" );
126         if ( ejbRefClass.isAssignableFrom( obj.getClass() ) )
127         {
128             RefAddr JavaDoc refAddr = null;
129             String JavaDoc addrType = null;
130             Properties JavaDoc env = new Properties JavaDoc();
131             String JavaDoc bean = null;
132
133             Reference JavaDoc ref = ( Reference JavaDoc ) obj;
134
135             Enumeration JavaDoc addresses = ref.getAll();
136             while ( addresses.hasMoreElements() )
137             {
138                 refAddr = ( RefAddr JavaDoc ) addresses.nextElement();
139                 addrType = refAddr.getType();
140                 if ( addrType.startsWith( OPENEJB_PREFIX ) )
141                 {
142                     String JavaDoc value = refAddr.getContent().toString();
143                     if ( addrType.equals( OPENEJB_EJB_LINK ) )
144                     {
145                         bean = value;
146                         continue;
147                     }
148                     String JavaDoc key = addrType.substring( OPENEJB_PREFIX_LENGTH );
149                     key = JAVA_PREFIX + key;
150                     env.put( key, value );
151                 }
152             }
153
154             if ( bean != null )
155             {
156                 beanObj = ( new InitialContext JavaDoc( env ) ).lookup( bean );
157             }
158         }
159         return beanObj;
160     }
161 }
162
Popular Tags