I have a business interface for data access: For example
In RMI, I have the same interface, say
Then I try to connect client across the network when server running and I try and cast and use the data layer interface
I get following error:
"java.lang.ClassCastException: Proxy cannot be cast to suncertify.db.HotelReservationDB"
Any reason why I can't cast, even though interfaces are same apart from remote exception?
Look, HotelReservationDB Interface is totally different of HotelReservationDBRemote interface.
You cannot to cast like this: "(HotelReservationDB)hotelReservation". To do it, is the same you want to transform "orange" in "apple". You can't to do this. Think about it. What do you really want to do?
Cheers, thanks. Sorry, if I give impression I'm clueless, I was hoping to have cleaner code when calling the direct or network client connection by having the 1 interface reference. Is the only approach to this referencing to use a separate reference for each of the direct client and network client connection? Thanks very much for kind replies.
Cheers, thanks for all your help. Just wondering 1 final point, in my set-up I have, package-wise say:
BusinessService and LocalBusinessServiceImpl both in the db package.
BusinessRemote and NetworkBusinessServiceImpl in the remote package.
How can I justify throwing the remoteException for the methods in the BusinessService interface while these classes are supposed to be accessed locally? I hope one understands that I'm asking.
Or would I be better packaging the application differently to support throwing the RemoteException from the methods?
Well, for everything related to the business/services layer, I'd create a package called business or services.
Now, about the exception, I see your point... but if a method is defined in an interface and has the RemoteException in its throws clause, it means that it can throw it. The implementation doesn't need to include it in its throws clause if it won't be thrown.
It is also shown in the provided: the method in the LocalBusinessServiceImpl is not throwing a RemoteException.
Regarding your package structure. I defined 3 packages: service (contains my business interface + service-exceptions), direct (for the local business implementations) and rmi (contains all classes to do the RMI-stuff).