Presentation is loading. Please wait.

Presentation is loading. Please wait.

Example: RMI Program How to write it.

Similar presentations


Presentation on theme: "Example: RMI Program How to write it."— Presentation transcript:

1 Example: RMI Program How to write it

2 Program an ATM Machine Allow the client to withdraw, deposit, transfer money to or from their account Also allow to obtain the current balance Some other stuff ATM machine is the client Bank with Account is the server

3 Write the code Write the Remote Interface, call it Account
Write the implementation: AccountImpl Write the server: RegAccount Write the client: AccountImpl Stubs and skeletons will be generated

4 Account.java

5 Remarks Note that Account extends Remote
Every remote method in the interface throws a RemoteException This interface is present on the server and on the client

6 AccountImpl.java

7 AccountImpl.java (ctd)

8 Remarks Since this class is remote it must implement the UnicastRemote interface The concrete methods from the remote interface all must throw a RemoteException The rest is just POJ (Plain old Java)

9 Define the Server Make an instance whose methods will be called by the client Register this object with the registry Note that it contains the main{} that defines the server No remote exceptions here

10 RegAccount.java

11 Write the Client: AccountClient.java
Need a Security Manager (later) Get a stub from the server by Naming.lookup() Make the remote calls--here getName() and getBalance() The URL parameter to the lookup routine returns the remote object it references main(){} here is the client.

12 AccountClient.java

13 Running the Example ATM
Compile the java files: javac Account*.java Create stub from AccountImpl: rmic AccountImpl Start the registry: rmiregistry Start the server: java RegAccount Start the client: java AccountClient

14 What is Missing A Security Manager A Security Policy A Codebase
Compile and Execution commands that incorporate these An appropriate deployment strategy

15 Security Manager If your application only runs locally (sandbox) then there is no need for security But RMI is designed to run on networks and the Internet Hence the need for security The Security Manager is a Java class that checks whether certain operations are permitted Is available under java.lang.SecurityManager

16 Security Manager (ctd)
Checks permissions according to a security policy Infractions of permissions cause a SecurityException Set up by issuing: System.setSecurityManager (new RMISecurityManager()); Enclose critical code in try block

17 Permissions Set by a security policy as written by the programmer
For purposes of testing a blanket granting of permissions can be issued: permission java.security.AllPermission; Then after deploying be a bit more restrictive: grant{ permission java.net.SocketPermission “*: ”, “connect, accept, resolve”; permission java.net.SocketPermission “*:80”, “connect”; };

18 Codebase When starting the server indicate the download directory by means of a codebase property: For example java -D Djava.rmi.server.codebase= xxxServ This represents the URL location from which the stubs can be downloaded Note the final slash “/” on the URL

19 Running the Client Need to include security with -D Start the Client:
java -Dsecurity.policy=sec.policy xxxClient sec.policy needs a path Do this for both server and client


Download ppt "Example: RMI Program How to write it."

Similar presentations


Ads by Google