77 */
88package com .thoughtworks .xjb .config .ejbjar ;
99
10- import java .io .InputStreamReader ;
1110import java .io .Reader ;
1211import java .lang .reflect .Constructor ;
1312import java .lang .reflect .InvocationTargetException ;
14- import java .rmi .RemoteException ;
1513import java .util .ArrayList ;
1614import java .util .HashMap ;
1715import java .util .Iterator ;
2018
2119import javax .ejb .EJBHome ;
2220import javax .naming .Context ;
21+ import javax .naming .InitialContext ;
2322import javax .naming .NamingException ;
2423import javax .rmi .PortableRemoteObject ;
2524
26- import org .apache .log4j .Logger ;
27-
2825import com .thoughtworks .xjb .ejb .XjbHomeFactory ;
2926import com .thoughtworks .xjb .jndi .JndiRegistry ;
27+ import com .thoughtworks .xjb .jndi .XjbInitialContextFactory ;
28+ import com .thoughtworks .xjb .util .Logger ;
3029
3130/**
3231 * @author <a href="mailto:dan.north@thoughtworks.com">Dan North</a>
3332 */
34- public abstract class EjbJarConfiguratorSupport implements EjbJarConfigurator {
35- private static final Logger log = Logger .getLogger (EjbJarConfiguratorSupport .class );
33+ public class XjbEjbConfigurator implements EjbConfigurator {
34+ private static final Logger log = Logger .getLogger (XjbEjbConfigurator .class );
3635
3736 /** Represents an <ejb-ref> element */
3837 private static class EjbRef {
@@ -51,41 +50,55 @@ public EjbRef(String ejbName, String jndiName, String targetEjbName) {
5150 private final List unresolvedEjbRefs = new ArrayList ();
5251 protected final JndiRegistry jndiRegistry ;
5352 private final Context context ;
53+ private final XjbHomeFactory homeFactory ;
5454
55- protected EjbJarConfiguratorSupport (JndiRegistry jndiRegistry , Context context ) throws NamingException {
55+ public XjbEjbConfigurator (JndiRegistry jndiRegistry , Context context ) throws NamingException {
5656 this .jndiRegistry = jndiRegistry ;
5757 this .context = context ;
58+ // TODO inject this
59+ homeFactory = new XjbHomeFactory ();
5860 }
5961
60- /**
61- * Read an <tt>ejb-jar.xml</tt> deployment descriptor file.
62- *
63- * @throws RemoteException if anything goes wrong
62+ public XjbEjbConfigurator () throws NamingException {
63+ this (new XjbInitialContextFactory (), new InitialContext ());
64+ }
65+
66+ /**
67+ * Create a session bean and register it in the global context
68+ * under several common JNDI names
6469 */
65- public abstract void read (Reader in ) throws RemoteException ;
70+ public void registerSessionBean (String ejbName ,
71+ Class homeInterface , Class remoteInterface , Class ejbClass , boolean isStateless )
72+ throws InstantiationException , IllegalAccessException {
73+ EJBHome ejbHome = homeFactory .createSessionBeanHome (ejbName ,
74+ homeInterface , remoteInterface , ejbClass .newInstance (),
75+ isStateless );
76+ registerCommonGlobalNames (ejbName , remoteInterface .getName (), ejbHome );
77+ registeredBeans .put (ejbName , ejbHome );
78+ resolveOutstandingEjbRefs ();
79+ }
6680
67- /**
68- * Convenience method to read an <tt>ejb-jar.xml</tt> file from a URL.
69- * <br>
70- * The URL is resolved into a {@link Reader} using the current classloader.
71- *
72- * @throws RemoteException if anything goes wrong
73- */
74- public void read (String url ) throws RemoteException {
75- read (new InputStreamReader (getClass ().getClassLoader ().getResourceAsStream (url )));
76- }
77-
78- protected void registerSessionBean (String ejbName , Class homeInterface , Class remoteInterface , Class ejbClass , boolean isStateless ) throws InstantiationException , IllegalAccessException {
79- EJBHome home = new XjbHomeFactory ().createHome (
80- ejbName ,
81- homeInterface , remoteInterface ,
82- ejbClass .newInstance (), isStateless );
83- registerCommonGlobalNames (ejbName , remoteInterface .getName (), home );
84- registeredBeans .put (ejbName , home );
85- resolveOutstandingEjbRefs ();
81+ private void registerCommonGlobalNames (String ejbName , String remoteClassName , EJBHome ejbHome ) {
82+ // Generic
83+ String genericName = "ejb/" + ejbName ;
84+
85+ // Orion
86+ String orionName = ejbName ;
87+
88+ // WebSphere
89+ StringBuffer buf = new StringBuffer ();
90+ char [] chars = remoteClassName .toCharArray ();
91+ for (int i = 0 ; i < chars .length ; i ++) {
92+ buf .append (chars [i ] == '.' ? '/' : chars [i ]);
93+ }
94+ String websphereName = buf .toString ();
95+
96+ jndiRegistry .register (genericName , ejbHome );
97+ jndiRegistry .register (orionName , ejbHome );
98+ jndiRegistry .register (websphereName , ejbHome );
8699 }
87100
88- protected void registerEnvEntry (String ejbName , String entryName , Class entryType , String entryValue ) throws Exception {
101+ public void registerEnvEntry (String ejbName , String entryName , Class entryType , String entryValue ) throws Exception {
89102 try {
90103 Constructor ctor = entryType .getConstructor (new Class [] {String .class });
91104 Object value = ctor .newInstance (new Object [] {entryValue });
@@ -95,14 +108,14 @@ protected void registerEnvEntry(String ejbName, String entryName, Class entryTyp
95108 }
96109 }
97110
98- protected void registerResourceRef (String ejbName , String refName , Class resType ) throws ClassCastException , NamingException {
111+ public void registerResourceRef (String ejbName , String refName , Class resType ) throws ClassCastException , NamingException {
99112 log .debug ("Checking for " + refName + " for " + ejbName );
100113 Object resource = PortableRemoteObject .narrow (context .lookup (refName ), resType );
101114 log .debug ("found it!" );
102115 jndiRegistry .register (ejbName , refName , resource );
103116 }
104117
105- protected void registerEjbRef (String ejbName , String jndiName , String targetEjbName ) {
118+ public void registerEjbRef (String ejbName , String jndiName , String targetEjbName ) {
106119 EJBHome targetHome = (EJBHome )registeredBeans .get (targetEjbName );
107120 if (targetHome != null ) {
108121 log .debug ("Registering " + targetHome + " as " + jndiName + " for " + ejbName );
@@ -114,33 +127,13 @@ protected void registerEjbRef(String ejbName, String jndiName, String targetEjbN
114127 }
115128 }
116129
117- private void registerCommonGlobalNames (String ejbName , String remoteClassName , EJBHome ejbHome ) {
118- // Generic
119- String genericName = "ejb/" + ejbName ;
120-
121- // Orion
122- String orionName = ejbName ;
123-
124- // WebSphere
125- StringBuffer buf = new StringBuffer ();
126- char [] remoteChars = remoteClassName .toCharArray ();
127- for (int i = 0 ; i < remoteChars .length ; i ++) {
128- buf .append (remoteChars [i ] == '.' ? '/' : remoteChars [i ]);
129- }
130- String websphereName = buf .toString ();
131-
132- jndiRegistry .register (genericName , ejbHome );
133- jndiRegistry .register (orionName , ejbHome );
134- jndiRegistry .register (websphereName , ejbHome );
135- }
136-
137130 /**
138131 * Resolve all <tt><ejb-ref></tt> EJB references.
139132 *
140133 * This is a second pass after all the <tt>ejb-jar.xml</tt> files have been read
141- * using {@link #read(Reader)}
134+ * using {@link EjbJarParser #read(Reader)}
142135 */
143- protected void resolveOutstandingEjbRefs () {
136+ private void resolveOutstandingEjbRefs () {
144137 for (Iterator i = unresolvedEjbRefs .iterator (); i .hasNext (); ) {
145138 EjbRef ejbRef = (EjbRef ) i .next ();
146139 Object targetHome = registeredBeans .get (ejbRef .targetEjbName );
0 commit comments