users@glassfish.java.net

Re: Websphere 5.x to Glassfish App Conversion

From: <glassfish_at_javadesktop.org>
Date: Wed, 12 Nov 2008 10:09:44 PST

Again, I think I should post my code:<br>
Here is the [B]GENERAL[/B] bean handler class<br><br>[code]
public class BaseBeanHandler
{
        private static final boolean DEBUG = true;
/* { <LAZ> Conversion removed all the following}
        //JNDI context property names
  protected static final String PROP_JNDI_CACHE = "com.ibm.websphere.naming.jndicache.cacheobject";
  protected static final String PROP_PROVIDER_URL = Context.PROVIDER_URL;
  protected static final String PROP_CONTEXT_FACTORY = Context.INITIAL_CONTEXT_FACTORY;

  //Default JNDI context property values
  protected static final String DEFAULT_JNDI_CACHE = "cleared";
  protected static final String DEFAULT_CONTEXT_FACTORY = "com.ibm.websphere.naming.WsnInitialContextFactory";
    </LAZ> */
 
  //Member variables
  protected String m_JNDIUrl;
  protected String m_EJBName;
  protected Context m_Context;
 
  protected BaseBeanHandler(String jndiURL, String ejbName)
  {
    m_JNDIUrl = jndiURL;
    m_EJBName = ejbName;
    m_Context = null;
  }
 
  protected Object getBean(Class homeClass)
   throws Exception
  {
    Object bean = null;
    Method create = null;
 
    try
    {
      create = homeClass.getMethod("create", new Class[0]);
    }
    catch(Exception e)
    {
      throw e;
    }
 
    try
    {
// if(DEBUG) System.out.println("BaseBeanHandler-"+getHandlerName()+".getBean() for "+m_EJBName+" - attempt bean creation");
      
// Object home = (Object) PortableRemoteObject.narrow(getJNDIContext().lookup(m_EJBName), homeClass);
// bean = create.invoke(home, (Object) new Class[0]);
      if(DEBUG) System.out.println("BaseBeanHandler-"+getHandlerName()+".getBean() for "+m_EJBName+" - attempt no-arg bean creation");
      bean = new InitialContext().lookup(m_EJBName);
    }
    catch(Exception e)
    {
      if(DEBUG) System.out.println("BaseBeanHandler-"+getHandlerName()+".getBean() for "+m_EJBName+" - bean creation failed");
      bean = null;
      closeJNDIContext();
    }
 
    if(bean == null)
    {
      try
      {
// if(DEBUG) System.out.println(getHandlerName()+".getBean() for "+m_EJBName+" - bean is null, retrying");
// Object home = (Object) PortableRemoteObject.narrow(getJNDIContext().lookup(m_EJBName), homeClass);
// bean = create.invoke(home, (Object) new Class[0]);
        if(DEBUG) System.out.println("BaseBeanHandler-"+getHandlerName()+".getBean() for "+m_EJBName+" - attempt no-arg bean creation");
        bean = new InitialContext().lookup(m_EJBName);
      }
      catch(Exception e)
      {
        if(DEBUG) System.out.println("BaseBeanHandler-"+getHandlerName()+".getBean() for "+m_EJBName+" - bean creation failed");
        bean = null;
        closeJNDIContext();
        throw e;
      }
    }
    if(DEBUG) System.out.println("BaseBeanHandler-"+getHandlerName()+".getBean() for "+m_EJBName+" - bean is created and returned");
    return bean;
  }
[/code] <br><br> ===============
and this is a [B]SPECIFIC[/B] EJB handler that extends the above class
===============<br><br>[code]
public class CWMManagerBeanHandler extends BaseBeanHandler
{
  private static final boolean DEBUG = true;
        //Member variables
  private String m_ServerName;
  private String m_SystemID;
 
  //**
  //** Public methods
  //**
 
  public CWMManagerBeanHandler(String jndiURL, String ejbName, String systemID)
  {
    super(jndiURL, ejbName);
    m_SystemID = systemID;
    
    try
    {
      InetAddress localhost = InetAddress.getLocalHost();
      m_ServerName = localhost.getHostName();
      System.out.println("CWMManagerBeanHandler - constructor was passed: jndiURL - "+jndiURL+"; ejbName - "+ejbName+"; systemID - "+systemID+"");
      System.out.println("CWMManagerBeanHandler - constructor set member: m_SystemID - "+m_SystemID+"; m_ServerName - "+m_ServerName+"");
    }
    catch(Exception e)
    {
      m_ServerName = "UNKNOWN SERVER";
    }
  }
 
  public boolean save(String name, long value)
   throws Exception
  {
    boolean okay = false;
    CWMManager mgr = null;
    StopWatch sw = new StopWatch();
 
    System.out.println("CMWManagerBeanHandler.save() called - passing in vars: name - "+name+" ; value - "+value);
    try
    {
      MonitorData md = new MonitorData();
      md.setServerName(m_ServerName);
      md.setSystemID(m_SystemID);
      md.setName(name);
      md.setValue(value);
 
      System.out.println("CMWManagerBeanHandler.save() - creates a MonitorData object containing: ServerName - "+m_ServerName);
      System.out.println(" SystemID - "+m_SystemID);
      System.out.println(" name - "+name);
      System.out.println(" value - "+value);
      System.out.println("CWMManagerBeanHandler.save() - attempts to create CWMManagerBean object");
      mgr = getCWMManagerBean();
      System.out.println("CWMManagerBeanHandler.save() - calls save method of CWMManagerBean object passing MonitorData object");
      okay = mgr.save(md);
    }
    catch(Exception e)
    {
      sw.stop();
      dumpException("save()", sw, e);
      okay = false;
      closeJNDIContext();
      throw e;
    }
    finally
    {
      removeBean(mgr);
    }
 
    return okay;
  }
  protected CWMManager getCWMManagerBean()
   throws Exception
  {
    CWMManager bean = null;

    try
    {
            System.out.println(getHandlerName()+".getBean() for "+m_EJBName+" - bean is null, retrying");
            bean = (CWMManager) getBean(CWMManagerHome.class);[/code]<br><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^-- [B] HERE IS WHERE WE BLOW UP with the ClassCastException - AFTER the SUPER returns a bean object[/B]<br><br>[code]
            // bean = (CWMManager) InitialContext().lookup(CWMManagerHome.class);
    }
    catch(Exception e)
    {
      bean = null;
      closeJNDIContext();
      throw e;
    }

    return bean;
  }

}//public class CWMManagerBeanHandler[/code]<br><BR>

Again, I tried forcing RMI stub usage from importing the old Websphere-generated stubs and naming them exactly as the error message. This did nothing to resolve the problem. I've isolated the issue down to the statement shown above as to where the problem resides. From what I can tell, it is in the assignment of the RETURNED portion of the statement where the exception occurs where the object is cast as type [B]CWMManager[/B] and assigned to the holder variable of that type. Again, I must elaborate my ignorance, this is code that works perfectly in Websphere 5.x - but apparently, there is a difference in what is expected in the calling of this bean, passing it from one class to another and assigning it to a holder variable of the type I assume the bean to be. Please help...
<br><br>
Thanks, -- Da Lizard
[Message sent by forum member 'loungelizard' (loungelizard)]

http://forums.java.net/jive/thread.jspa?messageID=316356