This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes how do I return a DBAccess interface back to client? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "how do I return a DBAccess interface back to client?" Watch "how do I return a DBAccess interface back to client?" New topic
Author

how do I return a DBAccess interface back to client?

berry westover
Greenhorn

Joined: Dec 24, 2004
Posts: 5
I have a factory that returns a DBAccess interface back to the client. I want the client to use this interface to run the methods for either a local or remote database connection. It works fine for a local connection. Now I'm trying to implement the remote connection using RMI.

How do I return a DBAccess interface when I want to establish a remote connection in my fat client?

I have done the following: I have a class called Data.java that implements the DBAccess interface. I have another interface called RemoteDBAccess which lists the same declarations as DBAccess and adds "throws RemoteException" to each of them. This interface also extends Remote.
I have an adapter called RemoteDBAdapter which extends UnicastRemoteObject and implements RemoteDBAccess.

How can transform this RemoteDBAccess interface type into a DBAccess interface type?
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Originally posted by berry westover:
I have a factory that returns a DBAccess interface back to the client. I want the client to use this interface to run the methods for either a local or remote database connection. It works fine for a local connection. Now I'm trying to implement the remote connection using RMI.

How do I return a DBAccess interface when I want to establish a remote connection in my fat client?

I have done the following: I have a class called Data.java that implements the DBAccess interface. I have another interface called RemoteDBAccess which lists the same declarations as DBAccess and adds "throws RemoteException" to each of them. This interface also extends Remote.
I have an adapter called RemoteDBAdapter which extends UnicastRemoteObject and implements RemoteDBAccess.

How can transform this RemoteDBAccess interface type into a DBAccess interface type?



Please don't keep starting new topics to continue the discussion on a previous topic. I will continue to help, but be warned, if you do this on the Sun Java Forums you will get seriously flamed. This forum is more friendly so that is unlikely to happen here. From now on lets use this topic to discuss your questions about the adapters.

The Data class implements the DBAccess interface. It is capable of providing data base access to a local client on a server.

I define an interface called DataAdapter that implements Remote and adds RemoteException to all the methods in DBAccess.

I define a class called DataAdapterImpl that implements DataAdapter and extends UnicastRemoteObject. The DataAdapterImpl has a Data instance as a member variable. This lets it delegate all its actions to the Data object. This is a case of the Adapter pattern as desribed in GoF.

On the client side I have a class called DataProxy that removes the throws RemoteException from all the methods. DataProxy implements the DBAccess interface exactly. To do this it has a DataAdapter instance that is actually a remote copy of the DataAdapterImpl. Check out the Proxy pattern in GoF, a Proxy is an Adapter that has the same interface as the adapted object, and as such can substitute for it (if you own stocks you may have used a real world proxy to vote at a meeting without actually attending).

Your RemoteDBAccess interface is the same as my DataAdapter interface, it's what you use on the client side of RMI. Your RemoteDataAdapter is the same as my DataAdapterImpl, it's what runs on the server side and has a Data object to delegate to.

You will get an instance of RemoteDBAccess from RMI, probably indirectly from a factory class that is registered in the RMI registry.

Once you have that instance you can build a true Proxy by implementing the DBAccess interface in a class that has a RemoteDBAccess instance as a member variable which it will delegate to. The calls that delegate to this will look like:

In this call all you do is delegate the readRecord to the RemoteDBAccess and catch the RemoteException. You then wrap that exception in a RuntimeException and throw it. I actually use a subclass of RuntimeException to make it easier to catch in the business logic.

As you get further into the database side of this you will need to come to terms with IOException. Whatever you do with IOException is exactly what you will need to do with RemoteException. The choices are to either use an unchecked RuntimeException or use the exceptions defined in the method, such as RecordNotFoundException. I chose to use a subclass of RuntimeException since the findByCriteria method doesn't throw any exceptions at all in the UyB specification.

I hope this helps, I'm off for a couple of days, Merry Christmas and best of luck on your project.

/Peter
 
wood burning stoves
 
subject: how do I return a DBAccess interface back to client?
 
Similar Threads
NX: DBAccess and RemoteException
IOException in Data idea, pls comment
urlybird local vs remote connection
NX Contractor: Design Review ..
NX: RemoteDBAccess