Factory sample (Hello world with different languages)

This commit is contained in:
2014-12-01 00:36:16 +01:00
parent f17671da1c
commit 238c61f4d3
8 changed files with 194 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
package rmi_hello_world_factory;
import java.rmi.Remote;
import java.rmi.RemoteException;
/**
* Created by Giovanni on 30/11/2014.
*/
public interface Hello extends Remote {
String sayhello () throws RemoteException;
}

View File

@@ -0,0 +1,23 @@
package rmi_hello_world_factory;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
/**
* Created by Giovanni on 01/12/2014.
*/
public class HelloImplementedEn extends UnicastRemoteObject implements Hello {
private static final long serialVersionUID = 2025591618699941247L;
public HelloImplementedEn(String in) throws RemoteException {
nome=in;
}
@Override
public String sayhello() throws RemoteException {
return "Hello " + nome;
}
private String nome;
}

View File

@@ -0,0 +1,23 @@
package rmi_hello_world_factory;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
/**
* Created by Giovanni on 01/12/2014.
*/
public class HelloImplementedFr extends UnicastRemoteObject implements Hello {
private static final long serialVersionUID = 2025591618699941247L;
public HelloImplementedFr(String in) throws RemoteException {
nome=in;
}
@Override
public String sayhello() throws RemoteException {
return "Bonjour " + nome;
}
private String nome;
}

View File

@@ -0,0 +1,23 @@
package rmi_hello_world_factory;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
/**
* Created by Giovanni on 01/12/2014.
*/
public class HelloImplementedIt extends UnicastRemoteObject implements Hello {
private static final long serialVersionUID = 2025591618699941247L;
public HelloImplementedIt(String in) throws RemoteException {
nome=in;
}
@Override
public String sayhello() throws RemoteException {
return "Ciao " + nome;
}
private String nome;
}

View File

@@ -0,0 +1,45 @@
package rmi_hello_world_factory;
import com.sun.corba.se.spi.activation.Server;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
/**
* Created by Giovanni on 01/12/2014.
*/
public class LaunchClient {
public static void main (String[] args){
System.setSecurityManager(new SecurityManager());
try {
ServerFactory f = (ServerFactory) Naming.lookup("rmi://localhost/Factory");
Hello h = f.request_hello_object("Italy","Mario");
System.out.println(h.sayhello());
h = f.request_hello_object("France","Mario");
System.out.println(h.sayhello());
h = f.request_hello_object("Germany","Mario");
System.out.println(h.sayhello());
} catch (NotBoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,31 @@
package rmi_hello_world_factory;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
/**
* Created by Giovanni on 30/11/2014.
*/
public class LaunchServer {
public static void main (String[] args){
System.setSecurityManager(new SecurityManager());
try {
ServerFactory f = new ServerFactoryImplemented();
Registry r = LocateRegistry.getRegistry();
r.rebind("Factory",f);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,11 @@
package rmi_hello_world_factory;
import java.rmi.Remote;
import java.rmi.RemoteException;
/**
* Created by Giovanni on 30/11/2014.
*/
public interface ServerFactory extends Remote{
Hello request_hello_object(String nationality, String nome) throws RemoteException;
}

View File

@@ -0,0 +1,27 @@
package rmi_hello_world_factory;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
/**
* Created by Giovanni on 30/11/2014.
*/
public class ServerFactoryImplemented extends UnicastRemoteObject implements ServerFactory {
public ServerFactoryImplemented() throws RemoteException {
}
@Override
public Hello request_hello_object(String nationality, String nome) throws RemoteException {
if (nationality.equals("Italy")){
return new HelloImplementedIt(nome);
}else if (nationality.equals("France")){
return new HelloImplementedFr(nome);
}else {
return new HelloImplementedEn(nome);
}
}
}