13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

142Chapter 5The Sun Microsystems Java Software <str<strong>on</strong>g>Development</str<strong>on</strong>g> KitNext, we have the server main() method. It first sets a security manager.The security manager c<strong>on</strong>trols what the VM is allowed to do. A number ofdefault security managers are provided, and here we use <strong>on</strong>e that is designedspecifically to give safe and reas<strong>on</strong>able defaults for RMI applicati<strong>on</strong>s. You can,of course, write your own security manager. Security managers use “policyspecificati<strong>on</strong>s” to alter their capabilities. For now, we will explain enough torun a simple example. See Secti<strong>on</strong> 5.8.4.2 for more informati<strong>on</strong> <strong>on</strong> policies forour example.Remember that main() is static, so there is no instance of Sessi<strong>on</strong>Implyet, and thus also no instance of Sessi<strong>on</strong>. We declare a variable of typeSessi<strong>on</strong>, and set it to a new instance of Sessi<strong>on</strong>Impl. (There is no needto typecast here because Sessi<strong>on</strong>Impl implements Sessi<strong>on</strong>, thereforeSessi<strong>on</strong>Impl is, am<strong>on</strong>g other things, a Sessi<strong>on</strong>.) We now have an instanceof the server class.Next, the server must make itself available to the world. It does this byregistering itself with the RMI registry (see Secti<strong>on</strong> 5.8.3). This is d<strong>on</strong>e througha static method of the java.rmi.Naming class, rebind(). Put simply, thismaps a remote object to a string name in the registry. When clients c<strong>on</strong>tact theregistry looking for a name then, if a remote object is mapped to that name,the communicati<strong>on</strong> can take place (yes, we are simplifying at the moment).The call to rebind() does not return. The server is up and running.Finally, we have the implementati<strong>on</strong> of our remote method, add().This looks like a lot of hassle to go through, and it is, but c<strong>on</strong>sider writingan interface that offers, for example, methods like getDirC<strong>on</strong>tents(),chDir(), downloadFile(), uploadFile(). You’ve just written somethinglike an FTP server. No matter how many methods you add to your interface,the complexity of the setup code does not increase. Maybe now it looks a littlemore useful?5.8.1.5 Writing the Client ClassAt this point, Example 5.12 should be fairly obvious. Our class has just a singlestatic method, main(). It, like our server side main(), sets up a security manager.It then c<strong>on</strong>tacts a registry <strong>on</strong> the machine named penfold looking for aninstance of a remote interface named Sessi<strong>on</strong> (again, lookup() is a staticmethod of the java.rmi.Naming class). We store that reference in a variableof type Sessi<strong>on</strong> called sess. We can then call the add() <strong>on</strong> sess. We’ll showthe server and client running shortly.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!