Commit b9c38a75 authored by Carlos Eduardo Lara Augusto's avatar Carlos Eduardo Lara Augusto
Browse files

[OPENBUS-2403] Criar interop delegation para OpenBus 1.5 (Java)

- Commit parcial, falta testar os pom.xml e ajustar o puts.

git-svn-id: https://subversion.tecgraf.puc-rio.br/engdist/openbus/sdk/java/branches/OB_SDK_JAVA_v1_05_02_2011_07_28@147538 ae0415b3-e90b-0410-900d-d0be9363c56b
parent cd003992
Pipeline #485 skipped
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCrP4G+kMuB1Bh6
Bvf1SjqJVLfKWX+Pj7hnt4nk8b3rTvhWN8C8kbW8wT6Z7HBldex9aM7hQn3Rds+J
pUQUef9eAH6/rOCnoId4FCX4p66k0D3TBLAw1Cr4lPpWp5BE0N8ka9yqtFHVIvY5
lclLlyhfCnKN9lsWw905C5iZn3MI2dLEvraG1f6OxgxDBPZexbhP+UiM73M8dUte
KsyhBSViovXe9XTMH1buOnMajW9oghtzcaRJEbMUypebX2KLefqDyGCqwQ9Yt8sH
re+CtTnBEYx3F5qir2EnHE3fjJ0O+zExVsJcQk2Ys8azhQzgA7FX2ySQ3Fp45aO7
JWouSManAgMBAAECggEAC6K8CRvDiCXGJ/xzOqp5gb1M46XjIfkM+Zch+nlmgyeR
fiI5qVZpN9MVktXa6TnzAK5vaCThcasV/O9Kye1kKZn1AscXWKxyp/kOf2+9URuJ
J95orlisNsnAnMSz68kZcFsX2k14RfgjLNR9ofI6XwLRt82mcMyMKoUcMEwCP/aY
bhZWH+vjY5BB8rITpnioiN4/KD3UjKoxGpTsUJ0sFUTvF/bjg0VJNoU/lYxqlw+f
RpZE3nnk0JpyE4JtAA0FdzukaeO/KFHh/8rXneHKWyQeavv4oL/jBU1belKIf33r
8NnPpPg0q6Hb8kwSIIxdiSBRb5I56xKO3wGoBWlgOQKBgQDieQgifDn60Cixlkcm
Poxs55PvGNKUf3iLWIrGbiFf9HuAsQc73sczHN5f4SrdRufPlQGBxWWy1vqkCTYv
tcJA7gTKAFAEDjlJKPHF/kIODRK3svvbpkIYan90VIO8EVxysN/CHnVTcMWx3wmW
fxXCEaK5HhM93fgUIxEB1dcgowKBgQDBkz5MZRjOXlchXE/9DQ/m4txZ0/K+DkrG
wuDgzMK7dZG8EdG82yG6Kd3hRxFF33pQHHANFeGBvID6wnsIualhYbre14EF/4aY
T0AxEHhyMGADcQOQuSKA81IgVLTltBEuN3e4RwHyDy3ce0IBlQs/2Ji9Uc5fY/+O
7eGHJ+ZuLQKBgFBxq2kMdU/ZBstl1OoJx7mbXJnOWGZ1DO16SvkG/4XlAsWInEgw
7tMCCx9FjQNCf27lv75HzsMCx87iksPJrTGBGmRiTgnSv5h0GcU5dl8lV6O2yO2R
leK0WYeJya1RAWSf3StHxGcaJK70yaQSijkAKKvWugD8ynTkq9LaEWs/AoGBAKtf
U/QGPKPW2YRrWVVKTx21EidkgK7JsGGu/a+HF48mUtMRY4Gy7nUFw61BYYVeh8aM
ppavHGnjHLzjmFDtLYtNHkdSuqM4yUvmPYoybq+JLDdfwixJ+sX2pGqDE3OYqu0j
phW6p3pI/e0IIjOMY90uQGq7Fd1VhlZfOx6QsGwxAoGAMl3yyugjx+l49HCCqI93
QDPg80CHRq9osvpRGQ5iKAUPe6FXO9uuQmIjH5uLPAiRPedCv+SEsXzM0/hinKo3
O6KGfvRWEexxu46RLcv4PO3ppY7P/UtTRtjmJOgVYGXzm1Wil62Dg1hEo/vag+If
rYXIviZfh2yY9sI6G1gnrqY=
-----END PRIVATE KEY-----
Category {
id = "openbus_interops",
name = "Categoria para testes de interoperabilidade do OpenBus",
}
Entity {
id = "interop_delegation_java_messenger",
category = "openbus_interops",
name = "Teste de Interoperabilidade Delegation do SDK Java - Messenger",
}
Certificate {
id = "interop_delegation_java_messenger",
certificate = "InteropDelegation.crt",
}
Entity {
id = "interop_delegation_java_forwarder",
category = "openbus_interops",
name = "Teste de Interoperabilidade Delegation do SDK Java - Forwarder",
}
Certificate {
id = "interop_delegation_java_forwarder",
certificate = "InteropDelegation.crt",
}
Entity {
id = "interop_delegation_java_broadcaster",
category = "openbus_interops",
name = "Teste de Interoperabilidade Delegation do SDK Java - Broadcaster",
}
Certificate {
id = "interop_delegation_java_broadcaster",
certificate = "InteropDelegation.crt",
}
Interface {
id = "IDL:tecgraf/openbus/interop/delegation/Messenger:1.0",
}
Interface {
id = "IDL:tecgraf/openbus/interop/delegation/Broadcaster:1.0",
}
Interface {
id = "IDL:tecgraf/openbus/interop/delegation/Forwarder:1.0",
}
Grant {
id = "interop_delegation_java_messenger",
interfaces = {
"IDL:tecgraf/openbus/interop/delegation/Messenger:1.0",
}
}
Grant {
id = "interop_delegation_java_broadcaster",
interfaces = {
"IDL:tecgraf/openbus/interop/delegation/Broadcaster:1.0",
}
}
Grant {
id = "interop_delegation_java_forwarder",
interfaces = {
"IDL:tecgraf/openbus/interop/delegation/Forwarder:1.0",
}
}
\ No newline at end of file
module tecgraf {
module openbus {
module interop {
module delegation {
typedef string Identifier;
exception NoForward {};
struct PostDesc {
Identifier from;
string message;
};
typedef sequence<PostDesc> PostDescSeq;
interface Messenger {
void post(in Identifier to, in string message);
PostDescSeq receivePosts();
};
interface Broadcaster {
void post(in string message);
void subscribe();
void unsubscribe();
};
interface Forwarder {
void setForward(in Identifier to);
void cancelForward(in Identifier to);
Identifier getForward() raises (NoForward);
};
}; // delegation
}; // demo
}; // openbus
}; // tecgraf
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>tecgraf.openbus</groupId>
<artifactId>openbus-sdk-interop</artifactId>
<version>1.5.2-SNAPSHOT</version>
</parent>
<artifactId>openbus-sdk-interop-delegation</artifactId>
<packaging>jar</packaging>
<name>OpenBus - SDK - Testes de Interoperabilidade - Delegation</name>
<url>http://www.tecgraf.puc-rio.br/openbus</url>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>idlj-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<sourceDirectory>idl</sourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>tecgraf.openbus</groupId>
<artifactId>openbus-sdk-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>tecgraf.scs</groupId>
<artifactId>scs-core</artifactId>
<version>${scs.version}</version>
</dependency>
<dependency>
<groupId>org.jacorb</groupId>
<artifactId>jacorb</artifactId>
<version>${jacorb.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
bus.host.name=ubu
bus.host.port=20030
login=interop_delegation_java_client
password=interop_delegation_java_client
broadcaster=interop_delegation_java_broadcaster
forwarder=interop_delegation_java_forwarder
messenger=interop_delegation_java_messenger
private.key=admin/InteropDelegation_1_5.key
acs.certificate=admin/openbus.crt
\ No newline at end of file
package tecgraf.openbus.interop.delegation;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import tecgraf.openbus.Openbus;
import tecgraf.openbus.core.v1_05.access_control_service.Credential;
public class BroadcasterImpl extends BroadcasterPOA {
private List<String> subscribers;
private Messenger messenger;
public BroadcasterImpl(Messenger messenger) {
this.subscribers = Collections.synchronizedList(new ArrayList<String>());
this.messenger = messenger;
}
@Override
public void post(String message) {
Credential myCredential = Openbus.getInstance().getCredential();
myCredential.delegate =
Openbus.getInstance().getInterceptedCredential().owner;
Openbus.getInstance().setThreadCredential(myCredential);
synchronized (subscribers) {
for (String user : subscribers) {
messenger.post(user, message);
}
}
myCredential.delegate = "";
}
@Override
public void subscribe() {
String user = Utils.getUser();
System.out.println("inscrio de " + user);
subscribers.add(user);
}
@Override
public void unsubscribe() {
String user = Utils.getUser();
System.out.println("cancelando inscrio de " + user);
subscribers.remove(user);
}
}
package tecgraf.openbus.interop.delegation;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.security.interfaces.RSAPrivateKey;
import java.security.spec.InvalidKeySpecException;
import java.util.Properties;
import org.omg.CORBA.ORB;
import org.omg.CORBA.UserException;
import scs.core.ComponentContext;
import scs.core.ComponentId;
import scs.core.IComponent;
import scs.core.IComponentHelper;
import scs.core.exception.SCSException;
import tecgraf.openbus.Openbus;
import tecgraf.openbus.core.v1_05.registry_service.IRegistryService;
import tecgraf.openbus.core.v1_05.registry_service.Property;
import tecgraf.openbus.core.v1_05.registry_service.ServiceOffer;
import tecgraf.openbus.core.v1_05.registry_service.UnathorizedFacets;
import tecgraf.openbus.exception.ACSLoginFailureException;
import tecgraf.openbus.exception.ACSUnavailableException;
import tecgraf.openbus.exception.CORBAException;
import tecgraf.openbus.exception.InvalidCredentialException;
import tecgraf.openbus.exception.OpenBusException;
import tecgraf.openbus.exception.PKIException;
import tecgraf.openbus.exception.RSUnavailableException;
import tecgraf.openbus.exception.ServiceUnavailableException;
import tecgraf.openbus.util.CryptoUtils;
public class BroadcasterServer {
private static String registrationId;
public static void main(String[] args) throws SCSException, UserException,
IOException, InvalidKeyException, NoSuchAlgorithmException,
InvalidKeySpecException, CertificateException, ACSUnavailableException,
ACSLoginFailureException, ServiceUnavailableException, PKIException,
InvalidCredentialException, CORBAException, OpenBusException {
Properties props = new Properties();
InputStream in =
BroadcasterServer.class.getResourceAsStream("/test.properties");
if (in != null) {
try {
props.load(in);
}
finally {
in.close();
}
}
else {
System.out
.println("Erro ao abrir o arquivo de configurao test.properties.");
System.exit(-1);
}
String host = props.getProperty("bus.host.name");
String portString = props.getProperty("bus.host.port");
int port = Integer.valueOf(portString);
Properties orbProps = new Properties();
orbProps.setProperty("org.omg.CORBA.ORBClass", "org.jacorb.orb.ORB");
orbProps.setProperty("org.omg.CORBA.ORBSingletonClass",
"org.jacorb.orb.ORBSingleton");
Openbus bus = Openbus.getInstance();
bus.initWithFaultTolerance(args, orbProps, host, port);
String entityName = props.getProperty("broadcaster");
String privateKeyFile = props.getProperty("private.key");
String acsCertificateFile = props.getProperty("acs.certificate");
RSAPrivateKey privateKey = CryptoUtils.readPrivateKey(privateKeyFile);
X509Certificate acsCertificate =
CryptoUtils.readCertificate(acsCertificateFile);
ORB orb = bus.getORB();
IRegistryService registryService =
bus.connect(entityName, privateKey, acsCertificate);
if (registryService == null) {
throw new RSUnavailableException();
}
Property[] serviceProperties = new Property[1];
serviceProperties[0] =
new Property("offer.domain", new String[] { "Interoperability Tests" });
ServiceOffer[] offers =
registryService.findByCriteria(new String[] { MessengerHelper.id() },
serviceProperties);
if (offers.length <= 0) {
System.err.println("no encontrou o servio messenger");
System.exit(1);
}
if (offers.length > 1) {
System.out.println("Foram encontrados vrios servios de messenger");
}
Messenger messenger =
MessengerHelper.narrow(offers[0].member.getFacet(MessengerHelper.id()));
ComponentContext context =
new ComponentContext(orb, bus.getRootPOA(), new ComponentId(
"Broadcaster", (byte) 1, (byte) 0, (byte) 0, "Java"));
context.addFacet("broadcaster", BroadcasterHelper.id(),
new BroadcasterImpl(messenger));
org.omg.CORBA.Object obj = context.getIComponent();
IComponent component = IComponentHelper.narrow(obj);
Property[] properties = new Property[1];
properties[0] =
new Property("offer.domain", new String[] { "Interoperability Tests" });
ServiceOffer serviceOffer = new ServiceOffer(properties, component);
try {
registrationId = registryService.register(serviceOffer);
System.out.println("Broadcaster registrado.");
}
catch (UnathorizedFacets uf) {
System.out.println("No foi possvel registrar o Broadcaster.");
for (String facet : uf.facets) {
System.out.println("Faceta '" + facet + "' no autorizada");
}
System.exit(1);
}
Runtime.getRuntime().addShutdownHook(new ShutdownThread());
orb.run();
}
private static class ShutdownThread extends Thread {
@Override
public void run() {
Openbus bus = Openbus.getInstance();
IRegistryService registryService = bus.getRegistryService();
registryService.unregister(registrationId);
bus.disconnect();
}
}
}
package tecgraf.openbus.interop.delegation;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.omg.CORBA.Object;
import tecgraf.openbus.Openbus;
import tecgraf.openbus.core.v1_05.registry_service.IRegistryService;
import tecgraf.openbus.core.v1_05.registry_service.Property;
import tecgraf.openbus.core.v1_05.registry_service.ServiceOffer;
import tecgraf.openbus.exception.RSUnavailableException;
public class Client {
private static final String willian = "willian";
private static final String bill = "bill";
private static final String paul = "paul";
private static final String mary = "mary";
private static final String steve = "steve";
public static void main(String[] args) throws IOException {
Properties props = new Properties();
InputStream in = Client.class.getResourceAsStream("/test.properties");
try {
props.load(in);
}
finally {
in.close();
}
try {
String host = props.getProperty("bus.host.name");
int port = Integer.valueOf(props.getProperty("bus.host.port"));
Properties orbProps = new Properties();
orbProps.setProperty("org.omg.CORBA.ORBClass", "org.jacorb.orb.ORB");
orbProps.setProperty("org.omg.CORBA.ORBSingletonClass",
"org.jacorb.orb.ORBSingleton");
Openbus bus = Openbus.getInstance();
bus.initWithFaultTolerance(args, orbProps, host, port);
String userLogin = props.getProperty("login");
String userPassword = props.getProperty("password");
IRegistryService registryService = bus.connect(userLogin, userPassword);
if (registryService == null) {
throw new RSUnavailableException();
}
Property[] serviceProperties = new Property[1];
serviceProperties[0] =
new Property("offer.domain", new String[] { "Interoperability Tests" });
ServiceOffer[] offers =
registryService.findByCriteria(new String[] {}, serviceProperties);
if (offers.length != 3) {
System.err.println("servios ofertados errados");
return;
}
Forwarder forwarder = null;
Messenger messenger = null;
Broadcaster broadcaster = null;
for (ServiceOffer offer : offers) {
for (Property prop : offer.properties) {
if (prop.name.equals("openbus.component.interface")) {
if (prop.value[0].equals(ForwarderHelper.id())) {
Object facet = offer.member.getFacet(ForwarderHelper.id());
forwarder = ForwarderHelper.narrow(facet);
}
else if (prop.value[0].equals(MessengerHelper.id())) {
Object facet = offer.member.getFacet(MessengerHelper.id());
messenger = MessengerHelper.narrow(facet);
}
else if (prop.value[0].equals(BroadcasterHelper.id())) {
Object facet = offer.member.getFacet(BroadcasterHelper.id());
broadcaster = BroadcasterHelper.narrow(facet);
}
}
}
}
bus.disconnect();
bus.connect(bill, bill);
forwarder.setForward(willian);
broadcaster.subscribe();
bus.disconnect();
bus.connect(paul, paul);
broadcaster.subscribe();
bus.disconnect();
bus.connect(mary, mary);
broadcaster.subscribe();
bus.disconnect();
bus.connect(steve, steve);
broadcaster.subscribe();
broadcaster.post("Testing the list!");
bus.disconnect();
System.out.println("Esperando que as mensagens se propaguem...");
Thread.sleep(10000);
System.out.println("Completado!");
List<String> users = new ArrayList<String>();
users.add(willian);
users.add(bill);
users.add(paul);
users.add(mary);
users.add(steve);
for (String user : users) {
bus.connect(user, user);
showPostsOf(user, messenger.receivePosts());
broadcaster.unsubscribe();
bus.disconnect();
}
bus.connect(bill, bill);
forwarder.cancelForward(willian);
bus.disconnect();
}
catch (Exception e) {
e.printStackTrace();
}
}
static void showPostsOf(String user, PostDesc[] posts) {
System.out.println(String.format("%s recebeu %d mensagens:", user,
posts.length));
int i = 1;
for (PostDesc post : posts) {
System.out.println(String
.format("%d) %s: %s", i, post.from, post.message));
++i;
}
System.out.println();
}
}
package tecgraf.openbus.interop.delegation;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import tecgraf.openbus.Openbus;
import tecgraf.openbus.core.v1_05.access_control_service.Credential;
public class ForwarderImpl extends ForwarderPOA {
private Map<String, ForwardInfo> forwardsOf;
public ForwarderImpl() {
this.forwardsOf =
Collections.synchronizedMap(new HashMap<String, ForwardInfo>());
}
@Override
public void setForward(String to) {
String user = Utils.getUser();
System.out.println(String.format("configurando forward para '%s' por '%s'",
to, user));
this.forwardsOf.put(user, new ForwardInfo(user, to));
}
@Override
public void cancelForward(String to) {
String user = Utils.getUser();