GeeCON Prague 2014*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes RMI and Adapter Pattern for URLyBird Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "RMI and Adapter Pattern for URLyBird" Watch "RMI and Adapter Pattern for URLyBird" New topic
Author

RMI and Adapter Pattern for URLyBird

Al McGreavy
Greenhorn

Joined: Sep 29, 2006
Posts: 5
Hi All

I am stuck with RMI and the Adapter pattern and hope someone can help with this. I have read other posts on this and javaworld and Andrew's book's example and kind of understand their adapter processes but don't know how to get my project working.

Anyhow, I have a Data class implementing interface DB.

All works fine in local mode.

For the server I have a DBRemote interface that extends Remote and DB and
a DataImpl class that extends UnicastRemoteObject and implements DBRemote - I thought that this would allow me to add RemoteExceptions to the DB methods but it doesn't.

So where do I go from here?
Do I need to have DBRemote extend Remote and another interface, say Database that copies all of DB but does something else!??

I am very stuck.

Can someone please advise or point me in the right direction?

Many thanks


Alison

Mike Ngo
Ranch Hand

Joined: Oct 16, 2006
Posts: 89
why do you want to add RemoteException? You should have the method in your RemoteDB object calls the lower-level method and traps the exception and rethrows it as a RemoteException.

For ex.

Class RemoteDB {

DB myDB;

addReservation() throws RemoteException {
try {
db.doWhatEver();
} catch (DatabaseException e) {
throw new RemoteException("Sorry. We are closed.");
}
}
}

Mark Smyth
Ranch Hand

Joined: Feb 04, 2004
Posts: 288
Originally posted by Alison McG:
Hi All

I am stuck with RMI and the Adapter pattern and hope someone can help with this. I have read other posts on this and javaworld and Andrew's book's example and kind of understand their adapter processes but don't know how to get my project working.

Anyhow, I have a Data class implementing interface DB.

All works fine in local mode.

For the server I have a DBRemote interface that extends Remote and DB and
a DataImpl class that extends UnicastRemoteObject and implements DBRemote - I thought that this would allow me to add RemoteExceptions to the DB methods but it doesn't.

So where do I go from here?
Do I need to have DBRemote extend Remote and another interface, say Database that copies all of DB but does something else!??

I am very stuck.

Can someone please advise or point me in the right direction?

Many thanks


Alison



Yes you need to create a new interface and class that exposes the methods you wish to the client. You basically have two chices about how to do this the Adapter pattern or the Facade pattern.

The adapter approach would essentially mean recreating the DB interface and adding remote exceptions to the method signatures.



The alternative is to make your remote code a bit more intelligent by using something like the the facade pattern. Here you could implement functions such as book() or search and have your client call these.


SCJP<br />SCJD
Al McGreavy
Greenhorn

Joined: Sep 29, 2006
Posts: 5
I understand it now. Thanks for your help guys.

Ken Boyd
Ranch Hand

Joined: Dec 10, 2003
Posts: 329


I have done the same but stuck where remote/DVDConnector.java (in Andrew book) class pass DBClient (in our assignment DB) to Controller class.

DataRemote doesn't implement DB where DVDConnector will pass DB when connection is Remote using RMI? (local mode works fine)

Thanks
[ March 11, 2007: Message edited by: Ken Boyd ]

SCJP, SCWCD, SCBCD, SCJD, BB Java2 and JSP1.1
Richard Levy
Greenhorn

Joined: Jan 19, 2005
Posts: 22
Hi,

chipping in late, but I've gone with a facade.

I created a facade on top of the specified DB interface. This not only removed locking from the interface (do it in the implementation of the facade interface), but talks in records not string arrays and the method prototypes also throw RemoteExceptions so that the interface can be used locally and remotely.

Works fine locally, just doing the remote bit now! Hope my scribbled UML actually works :-)

Rich
Lucy Hummel
Ranch Hand

Joined: Apr 07, 2005
Posts: 232
Hi,

Yes, I went also for a Facade and faced the same problems as described above last Saturday. I did a lot of changes and now it works fine remote and local mode.

Sorry, since I did a lot of changes, I do not know what the real fix was.

I hope you are going to be successful as I am.


----------------------------------<br />| SCJP, SCWCD, SCBCD, SCEA, SCJD |<br />----------------------------------
Ravikiran Vishnuvajhala
Ranch Hand

Joined: Jan 11, 2006
Posts: 30
Please ignore my question.
[ June 12, 2007: Message edited by: Ravikiran Vishnuvajhala ]
Swaroop Murthy
Greenhorn

Joined: Jun 23, 2007
Posts: 3
Originally posted by Mark Smyth:


Yes you need to create a new interface and class that exposes the methods you wish to the client. You basically have two chices about how to do this the Adapter pattern or the Facade pattern.

The adapter approach would essentially mean recreating the DB interface and adding remote exceptions to the method signatures.



The alternative is to make your remote code a bit more intelligent by using something like the the facade pattern. Here you could implement functions such as book() or search and have your client call these.


Hi Mark.
I am new here, and I am still stuck on the same problem. Local mode works, RMI server works. but RMI client has problems... i am getting a DB connection object as follows:

case DIRECT:
connection = FileConnector.getLocal(dbLocation);
case RMI:
connection = RMIConnector.getRemote(dbLocation, port);

so the two interfaces have to be interconvertible. if i extend and override, i cannot add new exceptions...

am i supposed to create a new interface on top of sun's and use that one even in local mode??? andrew's book uses an interface that throws IOException s, the interface we've been given doesn't throw it :-(



Swaroop Murthy<br />SCJA, SCJP, SCJD, SCWCD, SCBCD
Cless Alvein
Ranch Hand

Joined: Jun 18, 2007
Posts: 39
Do you think that we will lose points if we use an instance of DBRemote in standalone (local) mode? Of course, there will be no RMI connection, but instead of making two versions of a class, why not just make one that implements Remote and use that for everything?

Also, if we use RMI and make some classes Remote and others Serializable, that should be ok for the "no classes can be dynamically downloaded" requirement, right? (assuming we generate stubs for Remote classes)
Ken Boyd
Ranch Hand

Joined: Dec 10, 2003
Posts: 329
for RMI create class called


Class RemoteWrapper implements DB{
contructor of the class will take {URLRemote}
}

interface URLRemote extends Remote{}

Now factory will return URLRemote object

Now class cast to URLRemote to DB when it is remote



In short you keep everything clean for local mode i.e. return new Data() for every client. Now for RMI factory will return DB wrap into URLRemote obtain from RemoteWrapper.
[ June 23, 2007: Message edited by: Ken Boyd ]
Cless Alvein
Ranch Hand

Joined: Jun 18, 2007
Posts: 39
I have decided to take a similar approach using a third business logic layer.

Interface BookingService specifies some methods
Interface RemoteBookingService extends Remote and has the same methods as BookingService, but each method throws RemoteException in addition to any other exceptions (RecordNotFoundException, BookingConflictException, etc.)

Class BookingServiceImpl implements BookingService. This is the local, standalone version.
Class RemoteBookingServiceImpl implements RemoteBookingService and creates an internal BookingServiceImpl and is essentially a binding to its methods. ex. (in RemoteBookingServiceImpl
Ken Boyd
Ranch Hand

Joined: Dec 10, 2003
Posts: 329
Originally posted by Cless Alvein:
I have decided to take a similar approach using a third business logic layer.

Interface BookingService specifies some methods
Interface RemoteBookingService extends Remote and has the same methods as BookingService, but each method throws RemoteException in addition to any other exceptions (RecordNotFoundException, BookingConflictException, etc.)

Class BookingServiceImpl implements BookingService. This is the local, standalone version.
Class RemoteBookingServiceImpl implements RemoteBookingService and creates an internal BookingServiceImpl and is essentially a binding to its methods. ex. (in RemoteBookingServiceImpl


You are implementing thin client and don't have cookie if I am not wrong
[ June 23, 2007: Message edited by: Ken Boyd ]
Swaroop Murthy
Greenhorn

Joined: Jun 23, 2007
Posts: 3

Class RemoteWrapper implements DB{
contructor of the class will take {URLRemote}
}

interface URLRemote extends Remote{}

Now factory will return URLRemote object

Now class cast to URLRemote to DB when it is remote

Sorry Ken, I still don't get it. how can you classcast URLRemote to DB??? URLRemote extends Remote, and doesnot extend DB..
Swaroop Murthy
Greenhorn

Joined: Jun 23, 2007
Posts: 3
Originally posted by Cless Alvein:
I have decided to take a similar approach using a third business logic layer.

Interface BookingService specifies some methods
Interface RemoteBookingService extends Remote and has the same methods as BookingService, but each method throws RemoteException in addition to any other exceptions (RecordNotFoundException, BookingConflictException, etc.)

Class BookingServiceImpl implements BookingService. This is the local, standalone version.
Class RemoteBookingServiceImpl implements RemoteBookingService and creates an internal BookingServiceImpl and is essentially a binding to its methods. ex. (in RemoteBookingServiceImpl


so while connecting to the database you have separate code for connecting to remote and local?
Ken Boyd
Ranch Hand

Joined: Dec 10, 2003
Posts: 329
Originally posted by Swaroop Murthy:

Sorry Ken, I still don't get it. how can you classcast URLRemote to DB??? URLRemote extends Remote, and doesnot extend DB..


if you look at carefully RemoteWrapper implements DB, so when factory pattern return URLRemote you will class caste like this in class which return remote object for database connection

DB = (DB) (RemoteWrapper(URLRemote==> this is return by RMI factory pattern))
[ June 24, 2007: Message edited by: Ken Boyd ]
Ken Boyd
Ranch Hand

Joined: Dec 10, 2003
Posts: 329
Originally posted by Swaroop Murthy:


so while connecting to the database you have separate code for connecting to remote and local?


I think one wrapper for local and remote
[ June 24, 2007: Message edited by: Ken Boyd ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: RMI and Adapter Pattern for URLyBird