You can't do that. You will need to adjust your design. Happily, i don't think you need to adjust your design much.
The key thing is to be able to decide whether every object is either a remote object or a local one. Calls to the remote objects will go to the server, whereas calls to the local one will remain local. If you have one object which you want to be both, you need to split it in two, so you have a local part and a remote part. To maintain a convenient interface for clients, you can hide the remote object behind the local one.
Here's a sketch of how you could do this:
public interface RemoteDatabase extends Remote {
public void insertData(String data) throws RemoteException;
}
public class RemoteDatabaseImpl implements RemoteDatabase {
@Override
public void insertData(String data) throws RemoteException {
// whatever
}
}
public class Database implements Serializable {
private final RemoteDatabase remote;
private final String property;
public Database(RemoteDatabase remote, String property) {
this.remote = remote;
this.property = property;
}
public String getProperty() {
return property;
}
public void insertData(String data) throws RemoteException {
remote.insertData(data);
}
}
Note that when you create instances of Database
on the server, you need to give it a stub for the RemoteDatabaseImpl
implementation, rather than the real thing (i think).
You could even make these implement the same interface, to reinforce the relationship of the objects. Something like this:
public interface RemoteDatabase extends Remote {
public String getProperty() throws RemoteException;
public void insertData(String data) throws RemoteException;
}
public class RemoteDatabaseImpl implements RemoteDatabase {
@Override
public void insertData(String data) throws RemoteException {
// whatever
}
@Override
public String getProperty() {
return "whatever";
}
public Database toLocal() throws NoSuchObjectException {
return new Database((RemoteDatabase) PortableRemoteObject.toStub(this), getProperty());
}
}
public class Database implements RemoteDatabase, Serializable {
private final RemoteDatabase remote;
private final String property;
public Database(RemoteDatabase remote, String property) {
this.remote = remote;
this.property = property;
}
@Override
public String getProperty() {
return property;
}
@Override
public void insertData(String data) throws RemoteException {
remote.insertData(data);
}
}