RMI Casting problems

Hi all,

I'm developing a chatroom with a built-in shorthand feature. The client, an applet, actually accesses 2 RMI serers: chat server and shorthand server. The chat server works fine, using LinkedList's. The shorthand server uses ArrayList (because I read that it was serializable and faster. The server is works fine but I keep getting casting exceptions when I try to accass the shorthand pairs. Both RMI servers have their own port and registry name. Any ideas would b appeciated

Server Code:

--

ArrayList candidates = null;

String abrv;

String wrd;

int ARRAY_NUM=13;

String[] words = new String[ARRAY_NUM];

String[] shorts = new String[ARRAY_NUM];

intthisPort = 3231;

StringthisAddress; // = "192.168.0.3";

Registry registry;// rmi registry for lookup the remote objects.

int index = 0;

String nullString = "~NULL~";

Connection con;

public AbbrvServer(String ab, String wd) throws RemoteException {

abrv = ab;

wrd = wd;

}

.

.

.

while (rs.next()) {

try {

AbbrvServer dummyPair = new AbbrvServer(nullString,nullString);

dummyPair.abrv = rs.getString("abbrv");

dummyPair.wrd = rs.getString("word");

candidate.add(dummyPair);

//candidate.add((AbbrvServer)dummyPair);

//candidates = candidate.w

System.out.println("the abrev '"+dummyPair.abrv + " and " + dummyPair.wrd + " hazs been added");

} catch (Exception ex) {

ex.printStackTrace();

}

} // while

stmt.close();

-

Client code:

--

//RMI and local variables

String serverAddress = "192.168.0.3";

String serverPort = "3231";

String chatPort = "3232";

Registry regChat, regAbrv;

SpazzChatInterface spazzServer;

ArrayList chat = new ArrayList();

ArrayList candidates = new ArrayList();

//ArrayList followers = new ArrayList();

LinkedList members = new LinkedList();

String userName = null; //Get a parameter value

int indentTo = 20;

int startSel, endSel = 0;

Locale thisLocale = new Locale("en","US");//Helps define word boundaries

char cIn;

String wdIn = null;

AbbrvInterface abrvServer;

AbbrvServer abrvPair;

String lstWd;

boolean isWord = false;

.

.

.

stIn = getWord(carrotPos, cIn);

//stIn = getWord(cIn);

candidates = (ArrayList)abrvServer.getCandidates(stIn);

populateAbbrvList();

public void populateAbbrvList() {

abbrvList.removeAll();

// Fill memberlist

predicters.clear();

if (!candidates.isEmpty()) {

for (int j = 0; j < candidates.size(); j++) {

//if (candidates.get(j).instanceof {

try{

// abrvPair = (AbbrvServer) candidates.get(j);

AbbrvServer abrvPair = (AbbrvServer)candidates.get(j);

} catch(java.lang.ClassCastException clE){

try {

AbbrvServer abrvPair = new AbbrvServer("No data ",

"available");

} catch(RemoteException rm) {

System.err.println(rm);

}

System.err.println(clE);

}

/* try{

// abrvPair = (AbbrvServer) candidates.get(j);

abrvPair = (AbbrvServer)candidates.get(j);

} catch(java.lang.ClassCastException clE){

abrvPair.abrv = "No data ";

abrvPair.wrd = "available";

System.err.println(clE);

} */

predicters.addElement("F" + (j + 1) + ": " + abrvPair.abrv +

" - " +

abrvPair.wrd);

}

-

Thanks.

Chris

[3593 byte] By [batymohna] at [2007-11-27 5:09:35]
# 1
Format the code so we can read it easily.Only post the relevant part of the code, WITHOUT COMMENTED OUT CODE.Show us where the casting error occurs.
cooper6a at 2007-7-12 10:29:24 > top of Java-index,Core,Core APIs...
# 2

Sorry, I'm new to this forum.

if (!candidates.isEmpty()) {

for (int j = 0; j < candidates.size(); j++) {

try{

AbbrvServer abrvPair = (AbbrvServer)candidates.get(j);

} catch(java.lang.ClassCastException clE){

try {

AbbrvServer abrvPair = new AbbrvServer("No data ","available");

} catch(RemoteException rm) {

System.err.println(rm);

}

System.err.println(clE);

}

}

}

The second try - catch appear to work.

Thanks.

Chris

batymohna at 2007-7-12 10:29:24 > top of Java-index,Core,Core APIs...
# 3
The client needs to cast to the remote interface, not to the implementation class. The implementation class never leaves the server host.
ejpa at 2007-7-12 10:29:24 > top of Java-index,Core,Core APIs...
# 4

> The client needs to cast to the remote interface, not

> to the implementation class. The implementation class

> never leaves the server host.

But then I get a compile - incompatable types error. How would the program know what type of data objects are coming from the ArrayList?

I'm trying to cast a remote (server) object that is placed in an ArrayList. I'll keepplaying.

Chris

batymohna at 2007-7-12 10:29:24 > top of Java-index,Core,Core APIs...
# 5
Read what I said again. The remote server object never leaves the server. What the client receives is actually a stub object that implements the same remote interface. So the remote server type shouldn't appear anywhere in your client code, just the remote interface type.
ejpa at 2007-7-12 10:29:24 > top of Java-index,Core,Core APIs...
# 6
Thanks very much for your help and patience.You helped me solve the problem and truly understand the RMI concept..Sincerely.Chris
batymohna at 2007-7-12 10:29:24 > top of Java-index,Core,Core APIs...