Commit de097c0d authored by Rodrigo Carneiro Henrique's avatar Rodrigo Carneiro Henrique
Browse files

[SCS-307]: Modificar a forma como atualizamos uma faceta no ComponentContext


git-svn-id: https://subversion.tecgraf.puc-rio.br/engdist/scs/core/java/trunk@119778 ae0415b3-e90b-0410-900d-d0be9363c56b
parent 1bd25b1a
package scs.core;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.omg.CORBA.ORB;
import org.omg.CORBA.UserException;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.Servant;
......@@ -97,43 +95,6 @@ public class ComponentContext {
new IMetaInterfaceServant(this));
}
private static void checkForGetComponentMethod(Servant facet) {
try {
Method getComponentMethod =
facet.getClass().getMethod("_get_component", (Class<?>[]) null);
if (getComponentMethod == null) {
//TODO: logar como warn que _get_component não foi definida.
/*
* System.err.println("_get_component nao foi definida para a classe " +
* name + "!");
*/
}
}
catch (NoSuchMethodException e) {
//TODO: logar como warn que _get_component não foi definida.
/*
* System.err.println("_get_component nao foi definida para a classe " +
* name + "!");
*/
}
catch (SecurityException e) {
//TODO: logar erro.
}
}
private void deactivateFacet(Facet facet) throws SCSException {
if (facet != null) {
try {
poa.deactivate_object(poa
.reference_to_id(facet.getDescription().facet_ref));
}
catch (UserException e) {
throw new SCSException(e);
}
}
}
/**
* Provides the ComponentId of this component instance. ComponentId's aren't
* instance identifiers; they specify a component's name, version and platform
......@@ -162,13 +123,12 @@ public class ComponentContext {
*/
public void addFacet(String name, String interfaceName, Servant servant)
throws SCSException {
checkForGetComponentMethod(servant);
Facet oldFacet = this.facets.get(name);
if (oldFacet != null) {
Facet facet = this.facets.get(name);
if (facet != null) {
throw new FacetAlreadyExists(name);
}
this.deactivateFacet(oldFacet);
this.putFacet(name, interfaceName, servant);
facet = new Facet(this.poa, name, interfaceName, servant);
facets.put(name, facet);
}
public void updateFacet(String name, Servant servant) throws SCSException {
......@@ -176,20 +136,7 @@ public class ComponentContext {
if (facet == null) {
throw new FacetDoesNotExist(name);
}
this.putFacet(name, facet.getInterfaceName(), servant);
}
private void putFacet(String name, String interfaceName, Servant servant)
throws SCSException {
try {
org.omg.CORBA.Object reference = poa.servant_to_reference(servant);
Facet facet = new Facet(name, interfaceName, reference, servant);
facets.put(name, facet);
//TODO: logar que uma faceta foi adicionada
}
catch (UserException e) {
throw new SCSException(e);
}
facet.setServant(servant);
}
/**
......@@ -223,12 +170,11 @@ public class ComponentContext {
* @throws SCSException If an UserException is catched.
*/
public void removeFacet(String name) throws SCSException {
Facet facet = facets.get(name);
Facet facet = this.facets.remove(name);
if (facet == null) {
//TODO: logar que a faceta não existe
throw new FacetDoesNotExist(name);
}
deactivateFacet(facet);
facets.remove(name);
facet.deactivate();
//TODO: logar que uma faceta foi removida
}
......@@ -257,11 +203,11 @@ public class ComponentContext {
Map<String, SCSException> errMsgs = new HashMap<String, SCSException>();
for (Facet facet : facets.values()) {
try {
this.poa.activate_object(facet.getServant());
facet.activate();
}
catch (UserException e) {
catch (SCSException e) {
//TODO: logar erro ao ativar faceta como warn
errMsgs.put(facet.getName(), new SCSException(e));
errMsgs.put(facet.getName(), e);
}
}
return errMsgs;
......@@ -283,12 +229,11 @@ public class ComponentContext {
Map<String, SCSException> errMsgs = new HashMap<String, SCSException>();
for (Facet facet : facets.values()) {
try {
poa.deactivate_object(poa
.reference_to_id(facet.getDescription().facet_ref));
facet.deactivate();
}
catch (UserException e) {
catch (SCSException e) {
//TODO: logar erro ao desativar faceta como warn
errMsgs.put(facet.getName(), new SCSException(e));
errMsgs.put(facet.getName(), e);
}
}
return errMsgs;
......
package scs.core;
import org.omg.CORBA.UserException;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.Servant;
import scs.core.exception.SCSException;
/**
* This class holds all the metadata pertinent to a facet, and represents it
* locally.
......@@ -11,6 +15,7 @@ import org.omg.PortableServer.Servant;
* fields as public but these fields should not be manipulated freely.
*/
public final class Facet {
POA poa;
/**
* The name of the facet, which acts as its identifier within the component.
*/
......@@ -21,35 +26,31 @@ public final class Facet {
*/
private String interfaceName;
/**
* The CORBA object.
*/
private org.omg.CORBA.Object reference;
/**
* The Servant instance that implements the interface.
*/
private Servant servant;
/**
* The CORBA object.
*/
private org.omg.CORBA.Object reference;
Facet(String name, String interfaceName, org.omg.CORBA.Object referece,
Servant servant) {
Facet(POA poa, String name, String interfaceName, Servant servant)
throws SCSException {
if (poa == null) {
throw new IllegalArgumentException("The poa can't be null");
}
if (name == null) {
throw new IllegalArgumentException("The name can't be null");
}
if (interfaceName == null) {
throw new IllegalArgumentException("The interface's name can't be null");
}
if (referece == null) {
throw new IllegalArgumentException("The reference can't be null");
}
if (servant == null) {
throw new IllegalArgumentException("The servant can't be null");
}
this.poa = poa;
this.name = name;
this.interfaceName = interfaceName;
this.reference = referece;
this.servant = servant;
this.setServant(servant);
}
/**
......@@ -98,6 +99,46 @@ public final class Facet {
return servant;
}
void setServant(Servant servant) throws SCSException {
if (servant == null) {
throw new IllegalArgumentException("The servant can't be null");
}
if (this.servant != null) {
this.deactivate();
}
this.servant = servant;
this.checkForGetComponent();
this.activate();
}
private void checkForGetComponent() {
try {
this.getClass().getMethod("_get_component");
}
catch (NoSuchMethodException e) {
//TODO: logar como warn que _get_component não foi definida.
}
}
void activate() throws SCSException {
try {
this.reference = this.poa.servant_to_reference(this.servant);
}
catch (UserException e) {
throw new SCSException(e);
}
}
void deactivate() throws SCSException {
try {
byte[] referenceId = this.poa.reference_to_id(this.reference);
this.poa.deactivate_object(referenceId);
}
catch (UserException e) {
throw new SCSException(e);
}
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
......
......@@ -139,7 +139,8 @@ public class XMLComponentBuilder {
}
}
if (!filled) {
throw new NoComponentIdException();
throw new NoComponentIdException(
"No foi definido nenhum identificador para o componente");
}
return id;
}
......@@ -192,7 +193,8 @@ public class XMLComponentBuilder {
context.addFacet(name, interfaceName, (Servant) servant);
}
else {
throw new InvalidServantException();
throw new InvalidServantException(String.format(
"O objeto informado como servant da faceta %s no vlido", name));
}
}
}
......
......@@ -10,11 +10,8 @@ import scs.core.exception.SCSException;
*
*/
public class NoComponentIdException extends SCSException {
/**
* Default constructor.
*/
public NoComponentIdException() {
public NoComponentIdException(String message) {
super(message);
}
/**
......
......@@ -8,9 +8,7 @@ package scs.core.exception;
* @author Tecgraf/PUC-Rio
*/
public final class FacetAlreadyExists extends SCSException {
private String facet;
public FacetAlreadyExists(String facet) {
this.facet = facet;
super(String.format("A faceta %s j existe", facet));
}
}
......@@ -8,9 +8,7 @@ package scs.core.exception;
* @author Tecgraf/PUC-Rio
*/
public final class FacetDoesNotExist extends SCSException {
private String facet;
public FacetDoesNotExist(String facet) {
this.facet = facet;
super(String.format("A faceta %s no existe", facet));
}
}
......@@ -8,11 +8,8 @@ import org.omg.CORBA.UserException;
*
*/
public class InvalidServantException extends SCSException {
/**
* Default constructor.
*/
public InvalidServantException() {
public InvalidServantException(String message) {
super(message);
}
/**
......
......@@ -8,11 +8,11 @@ import org.omg.CORBA.UserException;
*
*/
public class SCSException extends Exception {
/**
* Default constructor.
*/
public SCSException() {
public SCSException(String message) {
super(message);
}
/**
......
......@@ -15,6 +15,8 @@ import org.omg.CORBA.UserException;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;
import scs.core.exception.FacetAlreadyExists;
import scs.core.exception.FacetDoesNotExist;
import scs.core.exception.SCSException;
public final class ComponentContextTest {
......@@ -204,6 +206,26 @@ public final class ComponentContextTest {
Assert.assertNotNull(component.getFacetByName(facetName));
}
@Test(expected = FacetAlreadyExists.class)
public void addFacet2() throws SCSException {
ComponentContext component = new ComponentContext(orb, poa, componentId);
component.addFacet(ComponentContext.ICOMPONENT_FACET_NAME, IComponentHelper
.id(), new IComponentServant(component));
}
@Test
public void updateFacet() throws SCSException {
ComponentContext component = new ComponentContext(orb, poa, componentId);
component.updateFacet(ComponentContext.ICOMPONENT_FACET_NAME,
new IComponentServant(component));
}
@Test(expected = FacetDoesNotExist.class)
public void updateFacet2() throws SCSException {
ComponentContext component = new ComponentContext(orb, poa, componentId);
component.updateFacet("nome", new IComponentServant(component));
}
@Test
public void removeFacet() throws SCSException {
String facetName = "nome";
......@@ -212,10 +234,10 @@ public final class ComponentContextTest {
component));
Assert.assertNotNull(component.getFacetByName(facetName));
component.removeFacet(facetName);
Assert.assertNull(component.getFacetByName("nome"));
Assert.assertNull(component.getFacetByName(facetName));
}
@Test
@Test(expected = FacetDoesNotExist.class)
public void removeFacet2() throws SCSException {
String facetName = "nome";
ComponentContext component = new ComponentContext(orb, poa, componentId);
......@@ -244,21 +266,6 @@ public final class ComponentContextTest {
public void activateComponent() throws SCSException {
ComponentContext component = new ComponentContext(orb, poa, componentId);
Map<String, SCSException> errors = component.activateComponent();
Assert.assertEquals(component.getFacets().size(), errors.size());
}
@Test
public void activateComponent2() throws SCSException {
ComponentContext component = new ComponentContext(orb, poa, componentId);
Collection<Facet> facets = component.getFacets();
Set<String> facetsNames = new HashSet<String>(facets.size());
for (Facet facet : facets) {
facetsNames.add(facet.getName());
}
for (String facetName : facetsNames) {
component.removeFacet(facetName);
}
Map<String, SCSException> errors = component.activateComponent();
Assert.assertEquals(0, errors.size());
}
......
......@@ -14,67 +14,74 @@ import org.omg.PortableServer.Servant;
import scs.core.exception.SCSException;
public final class FacetTest {
private static ORB orb;
private static ComponentContext context;
private static String name;
private static String interfaceName;
private static Servant servant;
private static org.omg.CORBA.Object facetReference;
@BeforeClass
public static void beforeClass() throws UserException, SCSException {
ORB orb = ORB.init((String[]) null, null);
orb = ORB.init((String[]) null, null);
org.omg.CORBA.Object obj = orb.resolve_initial_references("RootPOA");
POA poa = POAHelper.narrow(obj);
poa.the_POAManager().activate();
new Thread() {
@Override
public void run() {
orb.run();
}
}.start();
ComponentId componentId =
new ComponentId("componente", (byte) 1, (byte) 0, (byte) 0, "java");
context = new ComponentContext(orb, poa, componentId);
name = "Facet";
interfaceName = IMetaInterfaceHelper.id();
servant = new IMetaInterfaceServant(context);
facetReference = context.getPOA().servant_to_reference(servant);
}
@AfterClass
public static void afterClass() {
orb.shutdown(false);
context = null;
}
@Test(expected = IllegalArgumentException.class)
public void constructFacet() {
new Facet(null, interfaceName, facetReference, servant);
public void constructFacet() throws SCSException {
new Facet(context.getPOA(), null, interfaceName, servant);
}
@Test(expected = IllegalArgumentException.class)
public void constructFacet2() {
new Facet(name, null, facetReference, servant);
public void constructFacet2() throws SCSException {
new Facet(context.getPOA(), name, null, servant);
}
@Test(expected = IllegalArgumentException.class)
public void constructFacet3() {
new Facet(name, interfaceName, null, servant);
public void constructFacet3() throws SCSException {
new Facet(null, name, interfaceName, servant);
}
@Test(expected = IllegalArgumentException.class)
public void constructFacet4() {
new Facet(name, interfaceName, facetReference, null);
public void constructFacet4() throws SCSException {
new Facet(context.getPOA(), name, interfaceName, null);
}
@Test
public void constructFacet5() {
new Facet(name, interfaceName, facetReference, servant);
public void constructFacet5() throws SCSException {
new Facet(context.getPOA(), name, interfaceName, servant);
}
@Test
public void getName() {
Facet facet = new Facet(name, interfaceName, facetReference, servant);
public void getName() throws SCSException {
Facet facet = new Facet(context.getPOA(), name, interfaceName, servant);
Assert.assertNotNull(facet.getName());
Assert.assertEquals(name, facet.getName());
Assert.assertEquals(facet.getName(), facet.getDescription().name);
}
@Test
public void getInterfaceName() {
Facet facet = new Facet(name, interfaceName, facetReference, servant);
public void getInterfaceName() throws SCSException {
Facet facet = new Facet(context.getPOA(), name, interfaceName, servant);
Assert.assertNotNull(facet.getInterfaceName());
Assert.assertEquals(interfaceName, facet.getInterfaceName());
Assert.assertEquals(facet.getInterfaceName(),
......@@ -82,14 +89,46 @@ public final class FacetTest {
}
@Test
public void getReferece() {
Facet facet = new Facet(name, interfaceName, facetReference, servant);
public void getReferece() throws SCSException {
Facet facet = new Facet(context.getPOA(), name, interfaceName, servant);
Assert.assertNotNull(facet.getReference());
}
@Test
public void getServant() {
Facet facet = new Facet(name, interfaceName, facetReference, servant);
public void getServant() throws SCSException {
Facet facet = new Facet(context.getPOA(), name, interfaceName, servant);
Assert.assertNotNull(facet.getServant());
}
@Test(expected = IllegalArgumentException.class)
public void setServant() throws SCSException {
Facet facet = new Facet(context.getPOA(), name, interfaceName, servant);
facet.setServant(null);
}
@Test
public void activate() throws SCSException {
Facet facet = new Facet(context.getPOA(), name, interfaceName, servant);
facet.activate();
}
@Test
public void activate2() throws SCSException {
Facet facet = new Facet(context.getPOA(), name, interfaceName, servant);
facet.activate();
facet.activate();
}
@Test
public void deactivate() throws SCSException {
Facet facet = new Facet(context.getPOA(), name, interfaceName, servant);
facet.deactivate();
}
@Test(expected = SCSException.class)
public void deactivate2() throws SCSException {
Facet facet = new Facet(context.getPOA(), name, interfaceName, servant);
facet.deactivate();
facet.deactivate();
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment