<s1 title="EJB Clients">

<s2 title="Resin-EJB">

<p>Configuring with Resin-EJB as a client uses the
BurlapContextFactory as a JNDI link.  The client will use
JNDI to lookup the EJB home interfaces.</p>

<example title="Resin-EJB with Burlap">
&lt;web-app>
  &lt;jndi-link>
    &lt;jndi-name>java:comp/env/ejb&lt;/jndi-name>
    &lt;jndi-factory>com.caucho.burlap.BurlapContextFactory&lt;/jndi-factory>
    &lt;init-param java.naming.provider.url="http://host.com/burlap"/>
  &lt;/jndi-link>
&lt;/web-app>
</example>

<p>Sample client code follows.  The EJBHome lookup is in the <code/init()/>
method for efficiency.</p>

<example title="Client Servlet">
import javax.servlet.*;
import javax.namingdservlet.*;

public class ClientServlet extends GenericServlet {
  TestHome home;

  public void init()
    throws ServletException
  {
    try {
      Context env = (Context) new InitialContext().lookup("java:comp/env");

      home = (TestHome) env.lookup("ejb/test");
    } catch (Exception e) {
      throw new ServletException(e);
    }
  }

  public void service(ServletRequest req, ServletResponse res)
    throws IOException ServletException
  {
    try {
      PrintWriter out = res.getWriter();

      Test test = home.findByPrimaryKey("test");

      out.println("test:" + test.hello());
    } catch (Exception e) {
      throw new ServletException(e);
    }
  }
}
</example>

<p>Configuring with Resin-EJB and Hessian is the same as with
Burlap.  Only the names change.</p>

<example title="Resin-EJB with Hessian">
&lt;web-app>
  &lt;jndi-link>
    &lt;jndi-name>java:comp/env/ejb&lt;/jndi-name>
    &lt;jndi-factory>com.caucho.hessian.HessianContextFactory&lt;/jndi-factory>
    &lt;init-param java.naming.provider.url="http://host.com/hessian"/>
  &lt;/jndi-link>
&lt;/web-app>
</example>

</s2>

<s2 title="WebLogic">

<example title="Sample WebLogic Configuration">
&lt;web-app>
  &lt;classpath id='/usr/local/weblogic/myserver/clientclasses'/>
  &lt;classpath id='/usr/local/weblogic/lib/weblogicaux.jar'/>
  &lt;classpath id='/usr/local/weblogic/classes'/>

  &lt;jndi-link>
    &lt;jndi-name>java:comp/env/ejb/traderHome&lt;/jndi-name>
    &lt;jndi-factory>weblogic.jndi.WLInitialContextFactory&lt;/jndi-factory>
    &lt;init-param java.naming.provider.url="t3://localhost:7001"/>
    &lt;jndi-lookup>statelessSession.TraderHome&lt;/jndi-lookup>
  &lt;/jndi-link>
&lt;/web-app>
</example>

<example title="Sample Use">
&lt;%@ page import='javax.naming.*' %>
&lt;%@ page import='examples.ejb.basic.statelessSession.*' %>
&lt;%@ page import='javax.rmi.*' %>
&lt;%
Context ic = new InitialContext();

Object homeObj = ic.lookup("java:comp/env/ejb/traderHome");
TraderHome home;
home = (TraderHome) PortableRemoteObject.narrow(homeObj, TraderHome.class);

Trader trader;
trader = (Trader) PortableRemoteObject.narrow(home.create(), Trader.class);

String stock = request.getParameter("stock");
int shares = Integer.parseInt(request.getParameter("shares"));

TradeResult result = trader.buy(stock, shares);
%>
Bought &lt;%= result.getNumberTraded() %>
       &lt;%= result.getStockSymbol() %>&lt;br>
&lt;%
trader.remove();
%>
</example>

</s2>

<s2 title="Inprise Application Server">

<p>From Damon Maria:</p>

<p>Got Resin 1.2 talking to IAS 4.1 the other day and just thought I'd share my
experiences to save someone else some time. </p>

<p>Here's what to do:</p>

<ol>
<li>Copy vbjorb.jar and vbejb.jar from AppServer/lib into resin/lib. Just adding
these into the &lt;classpath .../> in resin.conf doesn't work.

<li>Add the path to your beans into resin.conf:

<example>
  &lt;classpath id='c:/development/classes'/>
</example>

<li>Add the JNDI link from resin to the app server in resin.conf:

<example>
&lt;jndi-link>
  &lt;jndi-name>java:comp/env/ejb&lt;/jndi-name>
   &lt;jndi-factory>com.inprise.j2ee.jndi.CtxFactory&lt;/jndi-factory>
&lt;/jndi-link>
</example>

<li>Then connect to your beans in servlets/JSP, simply add "java:comp/env/ejb/"
onto any JNDI paths you use to find your beans. Eg. if I have a bean at the app
server JNDI path of "TestSession" then in my servlet I use:

<example>
javax.naming.Context  context = new javax.naming.InitialContext();
Object ref = context.lookup("java:comp/env/ejb/TestSession");
home = (TestSessionHome) javax.rmi.PortableRemoteObject.narrow(ref,
                           TestSessionHome.class);
</example>
</ol>

</s2>

<s2 title="Orion">

<p>From Luciano Montebove:</p>

<p>To use Resin as an EJB client with Orion follow these steps:</p>

<ol>
<li>Under Orion do nothing :)

<li>In Resin add orion.jar, ejb.jar, mail.jar (from orion) in your app
WEB-INF/classes dir

<li>In Resin in your app web.xml add these lines:

<def>
&lt;classpath id='WEB-INF/classes/orion.jar'/>
&lt;classpath id='WEB-INF/classes/mail.jar'/>
&lt;classpath id='WEB-INF/classes/ejb.jar'/>
&lt;jndi-link>
  &lt;jndi-name>java:comp/env/ejb&lt;/jndi-name>
  &lt;jndi-factory>com.evermind.server.rmi.RMIInitialContextFactory
  &lt;/jndi-factory>
  &lt;init-param java.naming.provider.url="ormi://myOrionHost/MyApp"/>
  &lt;init-param java.naming.security.principal="admin"/>
  &lt;init-param java.naming.security.credentials="xxx"/>
&lt;/jndi-link>
</def>

<li>Write your servlet/JSP as you would in Orion so for example if you have a
SessionBean called MyBean (in your ejb-jar.xml) your lookup should use this
string ("java:comp/env/ejb/MyBean")

<li>It's all
</ol>

</s2>

<s2 title="jBoss">

<p>From Dave Dribin:</p>

<p>To have a Resin web application use jBoss, do the following:</p>

<ol>
<li>Copy "ejb.jar", "jboss-client.jar", and "jnp-client.jar" from
$JBOSS_HOME/client into WEB-INF/lib.

<li>Copy the EJB jar file that you put in $JBOSS_HOME/deploy into
WEB-INF/lib.

<li>Put the following &lt;web-app> into resin.conf:

<example>
&lt;web-app id='/web-app-path' app-dir='/real/path/of/web-app'>
  &lt;jndi-link>
    &lt;jndi-name>java:comp/env/ejb&lt;/jndi-name>
    &lt;jndi-factory>org.jnp.interfaces.NamingContextFactory&lt;/jndi-factory>
    &lt;init-param java.naming.provider.url="localhost:1099"/>
  &lt;/jndi-link>
&lt;/web-app>
</example>

<p>Of course change the JNDI URL if you're running jBoss on a different
machine.</p>

<li>
Make sure you map all of your EJBs to the "ejb/" JNDI
context.  For example, I have this in my jboss.xml:

<example>
&lt;enterprise-beans>
  &lt;entity>
    &lt;ejb-name>UserBean&lt;/ejb-name>
    &lt;jndi-name>ejb/UserHome&lt;/jndi-name>
  &lt;/entity>

  .... other beans ....

&lt;/enterprise-beans>
</example>

This way you can lookup all the Home interfaces in the
"java:comp/env/ejb/" context.
</ol>

<p>I've attached a simple JSP that shows how to access the User EJB.</p>

<p>And don't forget that if you re-deploy your beans to jBoss, you should
copy over the jar file into WEB-INF and restart Resin.</p>

<p>Hope that helps!</p>

<p>-Dave</p>

<example>
&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
&lt;html>
  &lt;head>
    &lt;title>EJB Test&lt;/title>
  &lt;/head>

  &lt;body bgcolor="#FFFFFF">
    &lt;%@ page import='javax.naming.*' %>
    &lt;%@ page import='ejbeans.*' %>
    &lt;%@ page import='javax.rmi.*' %>
    &lt;h1 align=center>EJB Test&lt;/h1>

    &lt;%
            // Get a naming context
            InitialContext jndiContext;
            Object ref;

            // Get a reference to a UserHome Bean
            jndiContext = new InitialContext();
            ref         = jndiContext.lookup("java:comp/env/ejb/UserHome");

            int         userPK;

            UserHome    userHome;
            User        user;

            userHome = (UserHome)
                PortableRemoteObject.narrow (ref, UserHome.class);

            userPK  = Integer.parseInt(request.getParameter("pk"));
            user    = userHome.findByPrimaryKey(new UserPK(userPK));
    %>

    &lt;table border="3" width="100%">
        &lt;tr>
          &lt;th> ID &lt;/th>
          &lt;th> First Name &lt;/th>
          &lt;th> Last Name &lt;/th>
        &lt;/tr>

        &lt;tr>
          &lt;th> &lt;%= user.getId() %> &lt;/th>
          &lt;th> &lt;%= user.getFirstName() %> &lt;/th>

          &lt;th> &lt;%= user.getLastName() %> &lt;/th>
        &lt;/tr>

    &lt;/table>

  &lt;/body>
&lt;/html>
</example>

</s2>

<s2 title="JOnAS">

<p>From Pedro Costa:</p>

<p>To use Resin as an EJB client with JOnAS or Enhydra Enterprise follow these steps:</p>

<ol>
<li>Under JOnAS or Enhydra do nothing

<li>Copy JEREMIE_jonas.jar (RMI_jonas.jar if you are using rmi and not
jeremie) into WEB-INF/lib or resin1.2/lib.

<p>If you are using JOnAS on the same machine as Resin you can do the same
by adding the following line to your app on resin.conf 
(assuming JOnAS is installed on \JONAS)</p>

<example>
&lt;classpath id='/JONAS/lib/JEREMIE_jonas.jar'/>
</example>

<li>Copy the EJB jar's file containing your beans to WEB-INF/lib
If you are using JOnAS on the same machine as Resin you can do the same
by adding the following line to your app on resin.conf 
(assuming JOnAS is installed on \JONAS and your beans are on a dir called \JONAS\mybeans)

<example>
&lt;classpath id='/JONAS/mybeans' source='/JONAS/mybeans' compile='false'/>
</example>
        
<li>In resin.conf add these lines to your app (assuming you are using jeremie):

<example>
&lt;jndi-link>
  &lt;jndi-name>java:comp/env/ejb&lt;/jndi-name>
  &lt;jndi-factory>
    org.objectweb.jeremie.libs.services.registry.jndi.JRMIInitialContextFactory
  &lt;/jndi-factory>
  &lt;init-param java.naming.provider.url="jrmi://YOUR_HOST:12340"/>
  &lt;init-param java.naming.factory.url.pkgs="org.objectweb.jonas.naming"/>
&lt;/jndi-link>
</example>

<li>Don't forget to change YOUR_HOST to your hostname...
</ol>

<example title="Sample JSP showing how to access your beans">
&lt;!DOCTYPE html public "-//W3C//DTD HTML 4.0 Transitional//EN">
&lt;%@ page language="java"%>
&lt;%@ page import="javax.naming.InitialContext" %>
&lt;%@ page import="javax.naming.Context" %>
&lt;%@ page import="javax.rmi.PortableRemoteObject" %>
&lt;%@ page import="YOUR_BEANS_PACKAGE.*" %>
&lt;html>
&lt;head>
&lt;title>Test&lt;/title>
&lt;/head>
&lt;body>
&lt;%
  Context initialContext = new InitialContext();
  Object ref = initialContext.lookup("java:comp/env/ejb/ExpHome");
  ExpHome home = (ExpHome) PortableRemoteObject.narrow(ref, ExpHome.class);
  Exp t1 = home.create(); 
%>
dummy result value = &lt;%=t1.dummyMethod()%>
&lt;%
  t1.remove();    
%>
&lt;/body>
&lt;/html>
</example>

</s2>

<s2 title="CORBA (J2EE Reference Implementation)">

<ul>
<li>Copy the client class jar and the j2ee.jar file to the
WEB-INF/classes directory of your web application.

<li>Add the following to your web application configuration in the
resin.conf file:

<example>
&lt;classpath id='WEB-INF/classes/j2ee.jar'/>
&lt;classpath id='WEB-INF/classes/{your client class jar}'/>
</example>

<li>In your class, explicitly create the environment for the
InitialContext:

<example>
Hashtable vEnv = new Hashtable();
vEnv.put("org.omg.CORBA.ORBInitialHost",
         "{your server host}");
vEnv.put("org.omg.CORBA.ORBInitialPort", 
         "{your server port}");
vEnv.put("java.naming.factory.initial",
         "com.sun.enterprise.naming.SerialInitContextFactory");
</example>

Or create a <a href="jndi.xtp#jndi-link">jndi-link</a>:

<example>
&lt;jndi-link>
  &lt;jndi-name>java:comp/env/ejb&lt;/jndi-name>
  &lt;jndi-factory>com.sun.enterprise.naming.SerialInitContextFactory&lt;/jndi-factory>
  &lt;init-param org.omg.CORBA.ORBInitialHost="{your server host}"/>
  &lt;init-param org.omg.CORBA.ORBInitialPort="{your server port}"/>
&lt;/jndi-link>
</example> 

<li>Make sure the InitialContext is initialised using the environment
set above. Then access the bean as usual:

<example>
InitialContext vContext = new InitialContext(vEnv);
Object vObjRef = vContext.lookup("{your bean jndi name}");
</example>
</ul>

</s2>
</s1>
