Java Reference
In-Depth Information
Finally, we can export the factory instance to the RMI registry found in the
register()
method in the
RegDvdDatabase
class. The
createRegistry()
method starts the Java RMI registry
programmatically:
public static void register(int port) throws java.rmi.RemoteException,
➥
java.net.MalformedURLException{
//the default rmi port is 1099.
java.rmi.registry.LocateRegistry.createRegistry(port);
DvdDatabaseFactoryImpl dvdFactoryImplementation
= new DvdDatabaseFactoryImpl();
//register
Naming.rebind("DvdMediator", dvdFactoryImplementation);
}
■
Note
Starting the registry programmatically is a new project requirement. Sun no longer allows starting
the RMI Registry in a separate step.
Our
RegDvdDatabase
class binds the name
DvdMediator
to our remote object instance.
When the client (soon to be a Swing GUI in the next chapter) requests a remote reference to our
database application, it will require the services of the
DVDConnector
class. The
DVDConnector
either returns a remote object or a
DvdDatabase
, which is the database wrapper for the
DvdFileAccess
class; both are
DBClient
s.
It is important to realize that we are only exporting the factory—we do not ever need to
export the
DvdDatabase
remote object. In the
DvdDatabaseFactory
class we return the remote
object
DvdDatabase
:
public DvdDatabaseRemote getClient() throws RemoteException {
return new DvdDatabaseImpl();
}
And the reason for the factory in the first place is the new instance of the database itself,
which can be found in the constructor of our
DvdDatabase
remote implementation:
public DvdDatabaseImpl() throws RemoteException {
try {
db = new DvdDatabase();
} catch (FileNotFoundException e) {
throw new RemoteException(e.getMessage());
} catch (IOException e) {
throw new RemoteException(e.getMessage());
}
}