Grid technology & security

Published: Last Edited:

This essay has been submitted by a student. This is not an example of the work written by our professional essay writers.

1. Interfaces

The first step is to compile the Java code for the interface. The program initializes all of the remote characteristic offered by the service:

Notice this program extends Remote, and each routine declares that it may throw a Remote Exception object.

Compile the program with the Java compiler:

javac Calculator

2. Implementation

Next, you write the implementation for the remote service. This is the CalculatorImpl class:

The CalculatorImpl class uses UnicastRemoteObject to link into the Remote Method Innovation. In this program the implementation class directly extends UnicastRemote Object. A class that does not extend UnicastRemote Object may use its exportObject() method to be linked into Remote Method Innovative..

When a class extends UnicastRemoteObject, it must provide a constructor that declares that it may throw a RemoteException object. When this constructor calls super(), it activates code in UnicastRemoteObject that performs the RMI linking and remote object initialization.

3. Stubs and Skeletons

The rmic compiler creates stub and skeleton class files (JRMP protocol), and stub and attach class files (IIOP protocol) for remote objects. These classes are generated from the compiled Java programming language contains remote object implementations. A remote object is one that execute the java.rmi.Remote.

You next run the RMI compiler, rmic, to create the stub and skeleton files.

>rmic CalculatorImpl

Try this in your directory. After you run rmic you should find the file CalculatorStub and, if you are running the Java 2 SDK, CalculatorSkel.

4. Host Server

Remote method innovative services must be hosted in a server process. The Calculator class Server is a very straightforward server that offers the bare prerequisites for hosting.

5. Running the RMI System

Now run the program. You need to start 3 consoles,

  1. CalculatorServer
  2. CalculatorClient
  3. Remote Method Innovative Registry.

Start with the remote method innovative Registry. You must be in the path that contains the classes you have compiled.

Start rmiregistry

Registry will start running and you can move to the second console.

Start the calculator server containing the CalculatorService,

java CalculatorServer

Calculator Server will initiate, and execute the implementation into memory and wait for client request.

In the third console, start the calculator client program.

java CalculatorClient 2 + 3

Java CalculatorClient 4 - 2

Java CalculatorClient 4 / 2

Java CalculatorClient 4 x 2 OR >Java CalculatorClient 4 X 2

Java CalculatorClient 4 p 2 OR >Java CalculatorClient 4 P 2

You must take care of spaces between the arguments

>Java CalculatorClient(Space)(number1)(space)(operator)(space)(number2)

Calculator.Java

public interface Calculator

extends java.rmi.Remote

{

public double calculate(double a, String o, double b)

throws java.rmi.RemoteException;

public String showOperators()

throws java.rmi.RemoteException;

}

CalculatorServer.Java

import java.rmi.Naming;

public class CalculatorServer {

public CalculatorServer() {

try {

Calculator c = new CalculatorImpl();

Naming.rebind("rmi://localhost:1099/CalculatorService", c);

} catch (Exception e) {

System.out.println("Trouble: " + e);

}

}

public static void main(String args[]) {

new CalculatorServer();

}

}

CalculatorImpl.Java

import java.util.ArrayList;

public class CalculatorImpl

extends

java.rmi.server.UnicastRemoteObject

implements Calculator {

public static ArrayList list = new ArrayList();

public CalculatorImpl()

throws java.rmi.RemoteException {

super();

}

public double calculate(double a, String o, double b)

throws java.rmi.RemoteException {

if(o.equals("+")) {

list.add(o);

if(list.size() > 5)

list.remove(0);

return a + b;

} else if(o.equals("-") )

{

list.add(o);

if(list.size() > 5)

list.remove(0);

return a - b;

} else if(o.equals("x") || o.equals("X"))

{

list.add(o);

if(list.size() > 5)

list.remove(0);

return a * b;

} else if(o.equals("/"))

{

list.add(o);

if(list.size() > 5)

list.remove(0);

return a/b;

}else if(o.equals("p")|| o.equals("P"))

{

list.add(o);

if(list.size() > 5)

list.remove(0);

return Math.pow(a,b);

} else

return 0;

}

public String showOperators()

throws java.rmi.RemoteException

{

String s="";

for(int i=0; i<list.size(); i++)

{

s = s + ", " + list.get(i);

}

return s;

}

}

CalculatorClient.Java

import java.rmi.Naming;

import java.rmi.RemoteException;

import java.net.MalformedURLException;

import java.rmi.NotBoundException;

public class CalculatorClient {

public static void main(String[] args) {

try {

Calculator c =

(Calculator)Naming.lookup("rmi://localhost/CalculatorService");

double a = Double.parseDouble(args[0]);

double b = Double.parseDouble(args[2]);

String op = args[1];

System.out.println("your calculation is : " + c.calculate(a,op,b));

System.out.println("Operators Used: " + c.showOperators());

}

catch (MalformedURLException murle) {

System.out.println();

System.out.println("MalformedURLException");

System.out.println(murle);

}

catch (RemoteException re) {

System.out.println();

System.out.println("RemoteException");

System.out.println(re);

}

catch (NotBoundException nbe) {

System.out.println();

System.out.println("NotBoundException");

System.out.println(nbe);

}

catch (

java.lang.ArithmeticException

ae) {

System.out.println();

System.out.println("java.lang.ArithmeticException");

System.out.println(ae);

}

}

}