Hi Everybody,

I'm using:
WinNT Workstation 4 SP5
BEA Weblogic 4.03
JDK1.1.7b
Stateful Session EJB Bean Code directly from their tutorials (ECart)

What I've done is replace their example client with a servlet, without changing hardly anything (source appended for both).

I can run their original client without any problems, but when I run my servlet I get a ClassCastException.

At point {*1} I am unsure whether this line is correct or not, it does not specify a binding of the ...HomeImpl class to anything, is there a problem with this?

---->< 1 snip - weblogic server - EJB startup>
Wed Jul 21 01:44:52 PDT 1999:<I> <EJB> Enterprise JavaBeans initializing
Wed Jul 21 01:44:52 PDT 1999:<I> <EJB> Read Enterprise Java Bean deployment descriptor: D:\Data\Development\Classes\ecart\ECartBeanDD.ser
{*1}Wed Jul 21 01:44:52 PDT 1999:<I> <RMI> Container "ecart.server.ECartBeanHomeImpl" bound
Wed Jul 21 01:44:52 PDT 1999:<I> <EJB> EJB home interface 'ecart.interfaces.ECartHome' bound to the JNDI name 'ECartHome'
Wed Jul 21 01:44:53 PDT 1999:<I> <ZAC> ZAC ACLs initialized
Wed Jul 21 01:44:53 PDT 1999:<I> <ZAC> ZAC packages stored in local directory exports
Wed Jul 21 01:44:53 PDT 1999:<I> <ListenThread> Listening on port: 7001
----></snip - weblogic server - EJB startup>

Here is the original client program running fine:

---->< 2 snip - original client program>
Wed Jul 21 01:45:42 PDT 1999:<I> <ListenThread> Adding address: localhost/127.0.0.1 to licensed client list
Wed Jul 21 01:45:42 PDT 1999:<I> <RJVM> Creating connection to localhost/127.0.0.1 53695836396627302
<ECart.setSessionContext (SessionContext)>
</ECart.setSessionContext (SessionContext)>
<(null:ECart).ejbCreate (String)>
</(dljfkh:ECart).ejbCreate (String)>
<(dljfkh:ECart).setPriceLimit (double)>
set cart price limit to 123.0 $
</(dljfkh:ECart).setPriceLimit (double)>
<(dljfkh:ECart).addItem (String)>
<(dljfkh:CashDesk).getItemPrice (String)>
ITEM1 price is 12.95
</(dljfkh:CashDesk).getItemPrice (String)>
check for cart price limit
add item ITEM1 which price is 12.95 $
</(dljfkh:ECart).addItem (String)>
<(dljfkh:ECart).pay (String)>
Withdrawal of 12.95 on visa card 0014-1876-5438-8666
</(dljfkh:ECart).pay (String)>
<(dljfkh:ECart).ejbRemove ()>
</(dljfkh:ECart).ejbRemove ()>
Wed Jul 21 01:46:19 PDT 1999:<I> <RJVM> Closing connection to localhost/127.0.0.1 53695836396627302
----></snip - original client program>

Here is the servlet running with problems, note that the bean is not jarred, it is as it would be after using the Weblogic EJB Deployment Wizard:

---->< 3 snip - servlet program with bean from non jar>
Wed Jul 21 01:46:23 PDT 1999:<I> <ServletContext-Servlets> Shop.Entry: init
getting new ECart object...
get JNDI initial context...
lookup the ECart home...
(*2)Exception  -->Shop.Entry:general processing: java.lang.ClassCastException: ecart.server.ECartBeanHomeImpl_ServiceStub
----></snip - servlet program with bean from non jar>

I found that when I packaged the bean into a jar file (with manifest and the ECartBeanDD.ser files) and removed the original classes from the classpath, that the servlet would then work. The only difference I could see was that when I jarred the files the two extra .ser files that the Weblogic EJB Deployment Wizard created in the bean's server directory weren't included.

---->< 4 snip - servlet program with bean from jar>
Wed Jul 21 02:02:01 PDT 1999:<I> <EJB> Enterprise JavaBeans initializing
Wed Jul 21 02:02:01 PDT 1999:<I> <EJB> Read Enterprise Java Bean jar file: D:\Da
ta\Development\Classes\ecart.jar
Wed Jul 21 02:02:01 PDT 1999:<I> <EJB> EJB home interface 'ecart.interfaces.ECar
tHome' bound to the JNDI name 'ECartHome'
Wed Jul 21 02:02:01 PDT 1999:<I> <ZAC> ZAC ACLs initialized
Wed Jul 21 02:02:01 PDT 1999:<I> <ZAC> ZAC packages stored in local directory ex
ports
Wed Jul 21 02:02:01 PDT 1999:<I> <ListenThread> Listening on port: 7001
Wed Jul 21 02:02:10 PDT 1999:<I> <ListenThread> Adding address: localhost/127.0.
0.1 to licensed client list
Wed Jul 21 02:02:11 PDT 1999:<I> <ServletContext-Servlets> Shop.Entry: init
getting new ECart object...
get JNDI initial context...
lookup the ECart home...
create a named ECart object...
<ECart.setSessionContext (SessionContext)>
</ECart.setSessionContext (SessionContext)>
<(null:ECart).ejbCreate (String)>
</(Shopping Cart:ECart).ejbCreate (String)>
finished getting new ECart object...
<(Shopping Cart:ECart).setPriceLimit (double)>
set cart price limit to 100.0 $
</(Shopping Cart:ECart).setPriceLimit (double)>
adding an item to the ECart object...
<(Shopping Cart:ECart).addItem (String)>
<(Shopping Cart:CashDesk).getItemPrice (String)>
ITEM1 price is 12.95
</(Shopping Cart:CashDesk).getItemPrice (String)>
check for cart price limit
add item ITEM1 which price is 12.95 $
</(Shopping Cart:ECart).addItem (String)>
pay cart items...
<(Shopping Cart:ECart).pay (String)>
Withdrawal of 12.95 on visa card 0014-1876-5438-8666
</(Shopping Cart:ECart).pay (String)>
remove the ECart object...
<(Shopping Cart:ECart).ejbRemove ()>
</(Shopping Cart:ECart).ejbRemove ()>
----></snip - servlet program with bean from jar>

So I went back a step and pointed everything back to the original unjarred classes and removed the extra .SER files. The result was that I didn't get the extra line in the EJB startup {*1} (log listing 1) referring to the ...HomeImpl class being bound to nothing, but I still get the ClassCastException (log listing 3).

Does anybody know what may be happening here, any pointers, etc.

Appended below are the two client programs.

Regards
Andrew Evans
mailto:[EMAIL PROTECTED]

original Client program that works fine:
============================================
package ecart.client ;

import ecart.interfaces.ECartHome ;
import ecart.interfaces.ECart ;
import ecart.interfaces.NoSuchItemException ;
import ecart.interfaces.PriceLimitException ;
import javax.naming.InitialContext ;
import javax.naming.Context ;
import java.util.Vector ;
import java.util.Properties ;
import java.io.* ;

public class Client
{
    public static void main (String[] args)
    {
        System.out.println ("<Client.main (String[])>");
        try {
            ECart eCart = getNewECart () ;

            System.out.println ("set cart price limit to " + _purse + " $...") ;
            eCart.setPriceLimit (_purse) ;

            // add in cart as much items as possible (according to purse limit)
            // - client doesn't need to compute the cart price (sum of item prices)
            // - client doesn't need to check weither its purse limit is reached or not
            for (boolean continueDialog = true; continueDialog; ) {
                System.out.print ("Would you like to add an item in your cart ? yes/no: ") ;
                if (continueDialog = getYesNoAnswer ()) {  // if yes
                    System.out.print ("Enter the item name (ITEM1, ITEM2...) : ") ;
                    String itemName = _keyboard.readLine () ;
                    try {
                        System.out.println ("add item " + itemName + " in the cart...") ;
                        eCart.addItem (itemName) ;
                    } catch (PriceLimitException _) {
                        System.out.println ("Error: could not add this item according to your cart limit") ;
                    } catch (NoSuchItemException _) {
                        System.out.println ("Error: no such item !") ;
                    }
                }
            }

            System.out.println ("pay cart items...") ;
            _purse -= eCart.pay ("0014-1876-5438-8666") ;

            System.out.println ("remove the ECart object...") ;
            eCart.remove () ;
        } catch (Exception e) {
            e.printStackTrace () ;
        }
        System.out.println ("</Client.main (String[])>");
    }

    private static ECart getNewECart ()
        throws Exception
    {
        System.out.println ("get JNDI initial context...") ;
        Context context = getInitialContext () ;

        System.out.println ("lookup the ECart home...") ;
        ECartHome eCartHome = (ECartHome)(context.lookup ("ECartHome")) ;

        System.out.print ("Would you like to name your cart ? yes/no: ") ;
        boolean namedCart = getYesNoAnswer () ;
        ECart eCart ;
        if (namedCart) {  // if yes
            System.out.print ("Enter the cart name : ") ;
            String itemName = _keyboard.readLine () ;

            System.out.println ("create a named ECart object...") ;
            eCart = eCartHome.create (itemName) ;
        } else {
            System.out.println ("create an ECart object...") ;
            eCart = eCartHome.create () ;
        }

        return eCart ;
    }

    private static boolean getYesNoAnswer ()
        throws Exception
    {
        String answer = _keyboard.readLine ().toLowerCase () ;
        return (answer.equals ("yes") || answer.equals ("y")) ? true : false ;
    }

    private static Context getInitialContext ()
        throws Exception
    {
        Properties p = new Properties () ;
        p.put (Context.INITIAL_CONTEXT_FACTORY, _initialContextFactory) ;
        p.put (Context.PROVIDER_URL, _providerURL) ;
        if (_securityPrincipal != null) {
            p.put (Context.SECURITY_PRINCIPAL, _securityPrincipal) ;
            p.put (Context.SECURITY_CREDENTIALS, _securityCredentials) ;
        }
        return new InitialContext (p) ;
    }

    private static String _initialContextFactory = "weblogic.jndi.T3InitialContextFactory" ;
    private static String _providerURL           = "t3://localhost:7001" ;
    private static String _securityPrincipal ;
    private static String _securityCredentials ;
    private static double _purse                 = 123.0 ;
    private static BufferedReader _keyboard       = new BufferedReader (
                                                           new InputStreamReader (System.in)) ;
}
 

new Servlet Client program that works fine only if EJB is in JAR:
============================================
package Shop;

import ecart.interfaces.ECartHome;
import ecart.interfaces.ECart;
import ecart.interfaces.NoSuchItemException;
import ecart.interfaces.PriceLimitException;
import javax.naming.InitialContext;
import javax.naming.Context;
import java.util.Vector;
import java.util.Properties;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Entry extends HttpServlet {
    protected static double _purse = 123.45;

 public String getServletInfo() {
  return "where you enter and get your cart";
 }

 public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     // prepare output stream
  response.setContentType("text/html");
  PrintWriter out = new PrintWriter(response.getOutputStream());

        ECart eCart = null;
        try {//general processing
            System.out.println("getting new ECart object...");
            eCart = getNewECart("Shopping Cart");
            System.out.println("finished getting new ECart object...");

            eCart.setPriceLimit(100);

            System.out.println("adding an item to the ECart object...");
            try {
                eCart.addItem("ITEM1");
            } catch (PriceLimitException e) {
                System.out.println("Error: could not add this item according to your cart limit");
            } catch (NoSuchItemException e) {
                System.out.println("Error: no such item!");
            }

            System.out.println("pay cart items...");
            _purse -= eCart.pay ("0014-1876-5438-8666");

            System.out.println("remove the ECart object...");
            eCart.remove () ;
        } catch (Exception e) {
            System.out.println("Exception  -->Shop.Entry:general processing: "+e);
        }

  // output creation
  out.println("<HTML>");
  out.println("<HEAD><TITLE>Shop Entry</TITLE></HEAD>");
  out.println("<BODY>");
  out.println("<font size=+5>Welcome to my shop! This is the entry.</font>");
  out.println("</BODY>");
  out.println("</HTML>");
  out.close();
 }

    private static ECart getNewECart (String name) throws Exception {
        System.out.println("get JNDI initial context...");
        Context context = getInitialContext();
        System.out.println("lookup the ECart home...");
        ECartHome eCartHome = (ECartHome)(context.lookup("ECartHome"));
        System.out.println("create a named ECart object...");
        ECart eCart = eCartHome.create(name);
        return eCart ;
    }

    private static String _initialContextFactory = "weblogic.jndi.T3InitialContextFactory" ;
    private static String _providerURL           = "t3://localhost:7001" ;
    private static String _securityPrincipal ;
    private static String _securityCredentials ;
 
    private static Context getInitialContext () throws Exception {
        Properties p = new Properties () ;
        p.put (Context.INITIAL_CONTEXT_FACTORY, _initialContextFactory) ;
        p.put (Context.PROVIDER_URL, _providerURL) ;
        if (_securityPrincipal != null) {
            p.put (Context.SECURITY_PRINCIPAL, _securityPrincipal) ;
            p.put (Context.SECURITY_CREDENTIALS, _securityCredentials) ;
        }
        return new InitialContext (p) ;
    }
}
 

Reply via email to