引起:java.lang.ClassCastException无法转换为包装器

时间:2013-05-22 10:26:46

标签: glassfish javafx jndi lookup ejb-3.1

我正在尝试使用Javafax 2.2客户端在glassfish 3.1容器上运行远程EJB,并在“查找”远程EJB时抛出一个exeption。 我的应用程序的目的是获取/放置在服务器端保存/检索为XML文件的Javafx Client对象。

在服务器端,EJB被打包到EAR中。

控制器“scrubber_S_Controller”是无状态会话EJB

package scrubber_S_Controller;

import java.io.Serializable;

import javax.ejb.Stateless;
import javax.xml.bind.JAXBException;
import scrubber_S_Model.SimpleObject;

/**
 * Session Bean implementation class Session
 */
@Stateless
public class Session implements SessionRemote, Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -5718452084852474986L;

    /**
     * Default constructor. 
     */
    public Session() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public SimpleObject getSimpleObject() throws JAXBException {
        SimpleObject simpleobjet = new SimpleObject();
        return simpleobjet.retrieveSimpleObject();
    }

    @Override
    public void setSimpleObject(SimpleObject simpleobject) throws JAXBException {
        simpleobject.saveSimpleObject(simpleobject);

    }

}

使用的远程接口是

package scrubber_S_Controller;

import javax.ejb.Remote;
import javax.xml.bind.JAXBException;

import scrubber_S_Model.SimpleObject;

@Remote
public interface SessionRemote {
    public SimpleObject getSimpleObject() throws JAXBException;
    public void setSimpleObject(SimpleObject simpleobject) throws JAXBException;

}

SimpleObject在scrubber_S_Model包中管理:

package scrubber_S_Model;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement(name = "SimpleObject")
public class SimpleObject implements java.io.Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 306212289216139111L;

    /**
     * Used to define a simpleObject Value Object
     */
    @XmlElement(name = "scrubberValveValue")
    private int scrubberValveValue;

    @XmlElement(name = "bypassValveValue")
    private int bypassValveValue;

    @XmlElement(name = "exhaustState")
    private boolean exhaustState;

    @XmlElement(name = "layoutColor")
    private String layoutColor;

    @XmlElement(name = "textValue")
    private String textValue;

    @XmlElement(name = "textColor")
    private String textColor;

    @XmlElement(name = "pressureThreshold")
    private int pressureThreshold;

    public SimpleObject(int bypassvalvevalue, int scrubbervalvevalue,
            boolean exhauststate, String layoutcolor, String textvalue,
            String textcolor, int pressurethreshold) {

        this.bypassValveValue = bypassvalvevalue;
        this.scrubberValveValue = scrubbervalvevalue;
        this.exhaustState = exhauststate;
        this.layoutColor = layoutcolor;
        this.textValue = textvalue;
        this.textColor = textcolor;
        this.pressureThreshold = pressurethreshold;
    }

    /**
     * Empty constructor, just to enable JAXB.
     */
    public SimpleObject() {
    }

    /**
     * Gets the value of the scrubberValveValue property.
     * 
     */
    public int getScrubberValveValue() {
        return this.scrubberValveValue;
    }

    /**
     * Sets the value of the scrubberValveValue property.
     * 
     */
    public void setScrubberValveValue(int value) {
        this.scrubberValveValue = value;
    }

    /**
     * Gets the value of the bypassValveValue property.
     * 
     */

    public int getBypassValveValue() {
        return this.bypassValveValue;
    }

    /**
     * Sets the value of the bypassValveValue property.
     * 
     */
    public void setBypassValveValue(int value) {
        this.bypassValveValue = value;
    }

    /**
     * Gets the value of the exhaustState property.
     * 
     */

    public boolean isExhaustState() {
        return this.exhaustState;
    }

    /**
     * Sets the value of the exhaustState property.
     * 
     */
    public void setExhaustState(boolean value) {
        this.exhaustState = value;
    }

    /**
     * Gets the value of the layoutColor property.
     * 
     * @return possible object is {@link String }
     * 
     */
    public String getLayoutColor() {
        return this.layoutColor;
    }

    /**
     * Sets the value of the layoutColor property.
     * 
     * @param value
     *            allowed object is {@link String }
     * 
     */
    public void setLayoutColor(String value) {
        this.layoutColor = value;
    }

    /**
     * Gets the value of the textValue property.
     * 
     * @return possible object is {@link String }
     * 
     */

    public String getTextValue() {
        return this.textValue;
    }

    /**
     * Sets the value of the textValue property.
     * 
     * @param value
     *            allowed object is {@link String }
     * 
     */
    public void setTextValue(String value) {
        this.textValue = value;
    }

    /**
     * Gets the value of the textColor property.
     * 
     * @return possible object is {@link String }
     * 
     */

    public String getTextColor() {
        return this.textColor;
    }

    /**
     * Sets the value of the textColor property.
     * 
     * @param value
     *            allowed object is {@link String }
     * 
     */
    public void setTextColor(String value) {
        this.textColor = value;
    }

    /**
     * Gets the value of the pressureThreshold property.
     * 
     */

    public int getPressureThreshold() {
        return this.pressureThreshold;
    }

    /**
     * Sets the value of the pressureThreshold property.
     * 
     */
    public void setPressureThreshold(int value) {
        this.pressureThreshold = value;
    }


public void saveSimpleObject(SimpleObject simpleobjet) throws JAXBException {
        FileOutputStream fileout = null;
        JAXBContext jc = JAXBContext.newInstance(SimpleObject.class);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        try {
            fileout = new FileOutputStream("simpleobjectfile.xml");
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        marshaller.marshal(simpleobjet, fileout);
        try {
            fileout.flush();
            fileout.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

public SimpleObject retrieveSimpleObject() throws JAXBException {
        FileInputStream fileinput = null;
        JAXBContext jc = JAXBContext.newInstance(SimpleObject.class);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        try {
            fileinput = new FileInputStream("simpleobjectfile.xml");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } 
        SimpleObject simpleobjet = (SimpleObject)unmarshaller.unmarshal(fileinput);
        try {
            fileinput.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return simpleobjet;

    }

}

编组/解组的Junit测试工作正常。

EJB的部署提供以下JNDI命名:

    INFO: EJB5181:Portable JNDI names for EJB Session: [java:global/Scrubber_S_EAR/Scrubber_S/Session, java:global/Scrubber_S_EAR/Scrubber_S/Session!scrubber_S_Controller.SessionRemote]
INFO: CORE10010: Loading application Scrubber_S_EAR done in 4 406 ms

在客户端,Javafx应用程序如下:

package ScrubberView;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.xml.bind.JAXBException;
import scrubber_CView_Model.SimpleObject;
import session.SessionRemote;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class scrubberView extends Application {

    @Override
    public void start(Stage primaryStage) throws JAXBException {
        try {
            Properties propriétés = new Properties();
            propriétés.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
            Context ctx = new InitialContext(propriétés);
            SessionRemote mySession = (SessionRemote)ctx.lookup("java:global/Scrubber_S_EAR/Scrubber_S/Session");
            //Create an object to exchange 
            SimpleObject simpleObject = new SimpleObject(1, 2, true, "layoutcolor", "text", "textcolor", 10 );
            mySession.setSimpleObject(simpleObject);
            @SuppressWarnings("unused")
            SimpleObject simpleObject2 = new SimpleObject();
            simpleObject2  = mySession.getSimpleObject();           
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            System.out.println(e.toString()   );
            e.printStackTrace();
        }

        //compose the scrubberview scene and show it
        primaryStage.setTitle("scrubberView");
        BorderPane borderpane = new BorderPane();
        Scene scene = new Scene(borderpane, 350, 80, Color.GREY);
        primaryStage.setScene(scene);
        scene.getStylesheets().add("./CleanRoomControl.css");
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }
}

以下jar位于Application

的buid enc中
C:\glassfish3\glassfish\modules\auto-depends.jar
C:\glassfish3\glassfish\modules\common-util.jar
C:\glassfish3\glassfish\modules\config-api.jar
C:\glassfish3\glassfish\modules\config-types.jar
C:\glassfish3\glassfish\modules\config.jar
C:\glassfish3\glassfish\modules\deployment-common.jar
C:\glassfish3\glassfish\modules\dol.jar
C:\glassfish3\glassfish\modules\ejb-container.jar
C:\glassfish3\glassfish\modules\ejb.security.jar
C:\glassfish3\glassfish\modules\glassfish-api.jar
C:\glassfish3\glassfish\modules\glassfish-corba-asm.jar
C:\glassfish3\glassfish\modules\glassfish-corba-codegen.jar
C:\glassfish3\glassfish\modules\glassfish-corba-csiv2-idl.jar
C:\glassfish3\glassfish\modules\glassfish-corba-newtimer.jar
C:\glassfish3\glassfish\modules\glassfish-corba-omgapi.jar
C:\glassfish3\glassfish\modules\glassfish-corba-orb.jar
C:\glassfish3\glassfish\modules\glassfish-corba-orbgeneric.jar
C:\glassfish3\glassfish\modules\glassfish-naming.jar
C:\glassfish3\glassfish\modules\gmbal.jar
C:\glassfish3\glassfish\modules\hk2-core.jar
C:\glassfish3\glassfish\modules\internal-api.jar
C:\glassfish3\glassfish\modules\javax.ejb.jar
C:\glassfish3\glassfish\modules\kernel.jar
C:\glassfish3\glassfish\modules\management-api.jar
C:\glassfish3\glassfish\modules\orb-connector.jar
C:\glassfish3\glassfish\modules\orb-iiop.jar
C:\glassfish3\glassfish\modules\security.jar
C:\glassfish3\glassfish\modules\transaction-internal-api.jar

运行

SessionRemote mySession = (SessionRemote)ctx.lookup("java:global/Scrubber_S_EAR/Scrubber_S/Session"); 

行,它提出了以下例外:

Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403)
    at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
    at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
    at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.ClassCastException: scrubber_S_Controller._SessionRemote_Wrapper cannot be cast to session.SessionRemote
    at ScrubberView.scrubberView.start(scrubberView.java:27)
    at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
    at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:215)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
    at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
    ... 1 more

如果我尝试使用其他命名:

SessionRemote mySession = (SessionRemote)ctx.lookup("java:global/Scrubber_S_EAR/Scrubber_S/Session!scrubber_S_Controller.SessionRemote");

它提出了相同的例外。

如果有人可以帮我解决这个问题,那就太棒了。

非常感谢您的帮助。

我希望我的英语对于理解来说并不是太糟糕。

1 个答案:

答案 0 :(得分:1)

我通过以下方式解决了这个问题:

  • 从env中删除jar文件,只将gf-client.jar放在类路径中,如下所示 How do I access a Remote EJB component from a stand-alone java client?

  • 将“scrubber_S_Controler”重命名为“session”(作为客户端的相应包)

  • 从“SimpleObject”类中删除“retrieveSimpleObject()”和“saveSimpleObject(SimpleObject simpleobjet)”并将其添加到新的“SimpleObjectPersist类”
  • 使用“SimpleObjectPersist”保存并检索SimpleObject。

Ough!在此之后,它运行良好。