• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Connection Factory

 
Todd Harney
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been reading the great discussions on this forum and have begun my developer assignment. I have a connection factory that binds to the RMI registry. This factory has two instance variables...the data class and a class that supports the lock and unlock methods. To keep these classes lossely coupled, I made an inner class in the factory that each client will receive. This inner class implements my remote data interface (which extends the data interface), but when I bind a client to my factory and call getConnection(), it is supposed to return an instance of my inner class as its interface type (data interface). But I receive a ClassCastException every time I do this. I assume it is because of the implicit reference to the outer class (my factory or stub class). Has anyone come accross this issue before? I am relatively new to RMI and am not sure where to proceed from here?! I guess I could have a separate class (as opposed to an inner one), but then I will have to pass references to my data instance and my locking class in its constructor?
Any help would be appreciated!
Thanks,
Todd
[ January 25, 2002: Message edited by: Todd Harney ]
[ January 25, 2002: Message edited by: Todd Harney ]
[ January 25, 2002: Message edited by: Todd Harney ]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm I am trying to think of a good answer
OK. In my getConnection method I create a new instance of my DataAccessRemote class which implements my DataAccess interface. The only difference I see between your solution and mine right now is the fact that I did not use an Inner Class, and my DataAccess interface implements Remote.
I think it is not passing the object back to your client, so your client code object is set to null which can't be cast to anything.
Just a guess, I could be dead wrong.
Mark
 
Todd Harney
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark,
Thanks for the reply. I have two interfaces. I have the data access interface which is the public methods of the data class. I also have a remote data access interface which extends both the data access interface and remote. My inner class (connection) then implements the remote data access interface and data just implements the data access interface which keeps it from being an instance of Remote. My getConnection method in the factory then is defined as:

This is implemented as return new DataConnection() where DataConnection is my inner class in the factory. I get the following stack trace:

In my client code, I am doing the following:

It is throwing the exception where I call getConnection()...any ideas anyone?
Thanks,
Todd Harney
 
Rowan Brownlee
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Todd,
Is your inner class static or non-static?
I just had a look at p 244 of Pitt and McNiff's Remote Method Invocation guide and they mention a problem concerning ClassCastExceptions and inner classes (extract follows).
cheerio
rowan
~~~~~~~~~~~~~~~~~~~~~~~~~~`
"For non-beginners, it can also be caused by trying to return a non-static inner class of a remote object as the result of a remote method. A non-static inner class has a hidden reference to the outer class, which can't be reconstructed correctly at the receiver. Change the inner class to static and the problem will disappear. If the inner class needs a reference to the enclosing remote object, you must construct it yourself, and declare its type as the remote interface type, not the remote object type."
~~~~~~~~~~~~~~~~~~
Prior to this, they say...
"This is a frequent "RMI beginners" error, typically caused by one of the following:
Trying to cast the result of Naming.lookup into a remote object implementation class instead of a remote interface.
Declaring a parameter or result of a remote method as a remote object implementation class instead of a remote interface (or an array of implementations instead of an array of interfaces)."
~~~~~~~~~~~~~~~~~~
 
Rowan Brownlee
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also found this useful in terms of general info on factory and RMI.
Applying the Factory Pattern to RMI
http://java.sun.com/j2se/1.3/docs/guide/rmi/Factory.html
I'm looking at using a connection factory to return connection objects. I was running into classCastExceptions, as I was referring to objects rather than remote interfaces, but this sounds different to the problem you're having.
cheerio
rowan
 
Todd Harney
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rowan for the great tip! I did make the inner class static, but then I had to make my data and lock manager variables static to access them...is this a good idea? Anyway, I also experimented with taking the inner class and making it a separate class of its own...RemoteDataConnection, but then I can't return that as a Remote object since the Data class itself is not serializable. Am I missing something really important here?
Any help appreciated!!
Thanks,
Todd
 
Kalichar Rangantittu
Ranch Hand
Posts: 240
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not think you need to return the RemoteDataClass. What you will be doing is really exporting the RemoteDataImpl and providing a remote accessor to the client .
 
Todd Harney
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone!! I have figured out my ignorant mistake. I didn't extend UnicastRemoteObject for my RemoteDataConnection. After I did that, all is well. The only thing I bind into the registry is the connection factory.
Thanks everyone,
Todd
 
Rowan Brownlee
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had the same problem - didn't export my remote connection object - good to have it up and running
cheerio
rowan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic