user1260503
user1260503

Reputation:

java.lang.ClassCastException - Deploying WAR to GlassFish 3.1.2. - Invalid JNDI lookup, maybe?

I'm trying to decipher the standard deployment procedure for WAR files to Oracle GlassFish 3.1.2 via Eclipse EE on *NIX. I'm getting a java.lang.ClassCastException for the JNDI lookup String (as far as I know). Any suggestions?

JNDI lookup String: Object objref = ctx.lookup("java:comp/env/");

Some posts on SE have advocated using: ctx.lookup("java:comp/env/ejb/<mapped name>") So: Object objref = ctx.lookup("java:comp/env/Exchange");`

Navigating to http://localhost:8080/Exchange/ throws error:

java.lang.ClassCastException: com.sun.enterprise.naming.impl.JavaURLContext cannot be cast to org.omg.CORBA.Object

Stack trace

[#|2013-02-13T10:26:01.507-0500|WARNING|oracle-glassfish3.1.2|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=73;_ThreadName=Thread-2;|StandardWrapperValve[jsp]: PWC1406: Servlet.service() for servlet jsp threw exception
java.lang.ClassCastException: com.sun.enterprise.naming.impl.JavaURLContext cannot be cast to org.omg.CORBA.Object
    at com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:245)
    at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:153)
    at org.apache.jsp.ExchangeClient_jsp._jspService(ExchangeClient_jsp.java:87)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:722)
|#]

ejb-jar.xml:

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar>
  <enterprise-beans>
    <session>
      <ejb-name>Exchange</ejb-name>
      <home>ejb.ExchangeHome</home>
      <remote>ejb.ExchangeObject</remote>
      <ejb-class>ejb.ExchangeBean</ejb-class>
      <session-type>Stateless</session-type>
      <transaction-type>Container</transaction-type>
    </session>
  </enterprise-beans>
  <assembly-descriptor>
    <container-transaction>
      <method>
        <ejb-name>Exchange</ejb-name>
        <method-name>*</method-name>
      </method>
      <trans-attribute>Required</trans-attribute>
    </container-transaction>
  </assembly-descriptor>
</ejb-jar>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Exchange</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>ExchangeClient.jsp</welcome-file>
  </welcome-file-list>  
</web-app>

ExchangeClient.jsp

<%@page import="ejb.*" %>
<%@page import="javax.naming.InitialContext"%>
<%@page import="javax.naming.Context"%>
<%@page import="javax.rmi.PortableRemoteObject" %>

<!DOCTYPE HTML>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Currency Converter</title>
        <link rel="stylesheet" href="master.css" />
    </head>
    <body>
        <h1>WAR</h1>

        <section id="user_input">
            <form action="ExchangeClient.jsp" method="POST">
                <label>Amount</label>   <input type="text" name="initial_amount" size="16" placeholder="0.00" pattern="^([.0-9])" required/><BR>
                <label>From</label> <input type="text" name="base_trigram" size="16" placeholder="CAD" pattern="^([A-Z])" required/>
                <label>To</label>   <input type="text" name="destination_trigram" size="16" placeholder="USD" pattern="^([A-Z])" required/>

                <button id="submit" type="submit">Submit</button>   
                <button id="reset" type="reset">Clear</button>   
            </form>
        </section>

        <section id="result">
            <%!ExchangeObject exchange; %>
            <%
            String amount = request.getParameter("initial_amount");
            String from = request.getParameter("base_trigram");
            String to = request.getParameter("destination_trigram");

            Context ctx = new InitialContext(); 
            Object objref = ctx.lookup("java:comp/env/");
            ExchangeHome home = (ExchangeHome)PortableRemoteObject.narrow(objref, ExchangeHome.class);

            ExchangeObject exchange = home.create();
            out.println(exchange.convert(amount, from, to));
            out.println(exchange.getExchangeRate());
            out.println(exchange.getExchangeRateTimestamp());
            %>
        </section>
    </body>
</html>

foo@Machine ~/workspace $ tree Exchange
Exchange
├── build
│   └── classes
│       └── ejb
│           ├── ExchangeBean.class
│           ├── ExchangeHome.class
│           └── ExchangeObject.class
├── src
│   └── ejb
│       ├── ExchangeBean.java
│       ├── ExchangeHome.java
│       └── ExchangeObject.java
└── WebContent
    ├── ExchangeClient.jsp
    ├── META-INF
    │   └── MANIFEST.MF
    └── WEB-INF
        ├── lib
        │   └── ejb-jar.xml
        └── web.xml

Upvotes: 1

Views: 1129

Answers (1)

Omar MEBARKI
Omar MEBARKI

Reputation: 647

Hi in Your EJB server you have what we call JNDI Server.

The JNDI server is a diractory. It means that when you deploy and EJB, the deployer creates an entry in the JNDI tree to reference your new EJB (say ejb/MyEjb).

So to use your MyEJB, you need to look for the implementation using its name "ejb/MyEjb":

ctx.lookup("java:comp/env/ejb/ejb/MyEjb")

Try to use

ctx.lookup("java:comp/env/ejb/Exchange");

If it doesn't work, try to browse your server jndi tree and replace by the correct path to Exchange

See http://docs.oracle.com/javase/tutorial/jndi/concepts/index.html for more details

Regards,

Upvotes: 1

Related Questions