Java Swing, Corba Objects - How to store Corba obj

2019-08-05 07:31发布

问题:

I have such IDL interface:

interface User
{
    string toString();
    //..
};

interface Group
{
    typedef sequence<User> Users;
    Users getUsers();

};

When I translated it to C++ I got sth like this:

// ...
Group::Users* GroupImpl::getUsers()
{
    // ..return sequence of 'User'-objects
}

On client side (written in Java) I want to show my users. I do sth like this:

public void showAllUsers() 
{
    User[] users = interface_obj.getUsers();
    if(users.length != 0)
    {
        DefaultListModel model = new DefaultListModel();
        for(int i=0; i<users.length; i++)
            model.addElement(users[i]);
        this.usersList.setModel(model);
    }
}

this.usersList is a JList.

When I do this like I wrote, I see only IORs of my Users-object:

IOR :0123405948239481293812312903891208320131293812381023
IOR: 0092930912617819919191818173666288810010199181919919

and so on ...

How to make it that way, to see their toString(); representation in DefaultListModel? I dont want to do this:

model.addElement(users[i].toString());

thats not the point. When I use RMI instead of CORBA, model.addElement(users[i]); is exactly what I need cause I see users string representation. But I need to use CORBA and store in DefaultListModel corba-user-objects, not strings. Please, help.

回答1:

One way to do it would be to make a UserView class whose instances you'd put in the list model:

public class UserView {

    private final User corbaUser;

    public UserView(User corbaUser) {
        this.corbaUser = corbaUser
    }

    @Override
    public String toString() {
       String ret = null;
       // construct the string as you want here
       return ret;
    }
}

EDIT:

as pointed out by JB Nizet be careful with the code you put in toString() since it is called every time the list needs to be shown - or the showing of the freshest data might be exactly what you want.



回答2:

I guess that the toString() method of the stub doesn't actually call the toString() method of the remote CORBA object. Try using another method name (like getName()), and use a custom renderer which calls this method.

That said, is it really a good idea to model a User as a remote CORBA object? That will cause a lot or remote method calls just to display the names of the users, and thse method calls are basically out of your control, since the Swing components will make them. Shouldn't you use DTOs instead?