Factory sample (Hello world with different languages)
This commit is contained in:
11
rmi_hello_world_factory/Hello.java
Normal file
11
rmi_hello_world_factory/Hello.java
Normal 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;
|
||||||
|
}
|
||||||
23
rmi_hello_world_factory/HelloImplementedEn.java
Normal file
23
rmi_hello_world_factory/HelloImplementedEn.java
Normal 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;
|
||||||
|
}
|
||||||
23
rmi_hello_world_factory/HelloImplementedFr.java
Normal file
23
rmi_hello_world_factory/HelloImplementedFr.java
Normal 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;
|
||||||
|
}
|
||||||
23
rmi_hello_world_factory/HelloImplementedIt.java
Normal file
23
rmi_hello_world_factory/HelloImplementedIt.java
Normal 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;
|
||||||
|
}
|
||||||
45
rmi_hello_world_factory/LaunchClient.java
Normal file
45
rmi_hello_world_factory/LaunchClient.java
Normal 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
31
rmi_hello_world_factory/LaunchServer.java
Normal file
31
rmi_hello_world_factory/LaunchServer.java
Normal 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
11
rmi_hello_world_factory/ServerFactory.java
Normal file
11
rmi_hello_world_factory/ServerFactory.java
Normal 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;
|
||||||
|
}
|
||||||
27
rmi_hello_world_factory/ServerFactoryImplemented.java
Normal file
27
rmi_hello_world_factory/ServerFactoryImplemented.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user