Commit 6444ac3e authored by Rodrigo Carneiro Henrique's avatar Rodrigo Carneiro Henrique
Browse files

[SCS-318]: Modificar a forma como atualizamos um receptáculo no ComponentContext


git-svn-id: https://subversion.tecgraf.puc-rio.br/engdist/scs/core/java/trunk@119779 ae0415b3-e90b-0410-900d-d0be9363c56b
parent de097c0d
...@@ -11,6 +11,8 @@ import org.omg.PortableServer.Servant; ...@@ -11,6 +11,8 @@ import org.omg.PortableServer.Servant;
import scs.core.exception.FacetAlreadyExists; import scs.core.exception.FacetAlreadyExists;
import scs.core.exception.FacetDoesNotExist; import scs.core.exception.FacetDoesNotExist;
import scs.core.exception.ReceptacleAlreadyExistsException;
import scs.core.exception.ReceptacleDoesNotExistException;
import scs.core.exception.SCSException; import scs.core.exception.SCSException;
/** /**
...@@ -51,7 +53,7 @@ public class ComponentContext { ...@@ -51,7 +53,7 @@ public class ComponentContext {
* Primary constructor. The returned component instance will always have the * Primary constructor. The returned component instance will always have the
* three basic facets (IComponent, IReceptacles, IMetaInterface) instantiated. * three basic facets (IComponent, IReceptacles, IMetaInterface) instantiated.
* If the user wishes to use his own implementation of one of these facets, * If the user wishes to use his own implementation of one of these facets,
* it's possible to replace them via the putFacet method. Other facets and * it's possible to replace them via the addFacet method. Other facets and
* receptacles can also be added. * receptacles can also be added.
* *
* The returned instance of this class is considered a new SCS component * The returned instance of this class is considered a new SCS component
...@@ -149,17 +151,17 @@ public class ComponentContext { ...@@ -149,17 +151,17 @@ public class ComponentContext {
* @param interfaceName The receptacle's IDL interface. * @param interfaceName The receptacle's IDL interface.
* @param isMultiplex True if the receptacle accepts more than one connection, * @param isMultiplex True if the receptacle accepts more than one connection,
* false otherwise. * false otherwise.
* @throws ReceptacleAlreadyExistsException
*/ */
public void putReceptacle(String name, String interfaceName, public void addReceptacle(String name, String interfaceName,
boolean isMultiplex) { boolean isMultiplex) throws ReceptacleAlreadyExistsException {
Receptacle receptacle = Receptacle receptacle = this.receptacles.get(name);
new Receptacle(this, name, interfaceName, isMultiplex); if (receptacle != null) {
if (receptacles.put(name, receptacle) != null) { throw new ReceptacleAlreadyExistsException(name);
//TODO: logar que um receptaculo foi substituido e todas as suas conexões, perdidas
}
else {
//TODO: logar que um receptaculo foi adicionado
} }
receptacle = new Receptacle(this, name, interfaceName, isMultiplex);
receptacles.put(name, receptacle);
//TODO: logar que um receptaculo foi adicionado
} }
/** /**
...@@ -182,14 +184,15 @@ public class ComponentContext { ...@@ -182,14 +184,15 @@ public class ComponentContext {
* Removes a receptacle from the component. * Removes a receptacle from the component.
* *
* @param name The name of the receptacle to be removed. * @param name The name of the receptacle to be removed.
* @throws ReceptacleDoesNotExistException
*/ */
public void removeReceptacle(String name) { public void removeReceptacle(String name)
if (receptacles.containsKey(name)) { throws ReceptacleDoesNotExistException {
Receptacle receptacle = receptacles.remove(name); Receptacle receptacle = receptacles.remove(name);
if (receptacle != null) { if (receptacle == null) {
//TODO: logar que um receptaculo foi removido e todas as suas conexões, perdidas throw new ReceptacleDoesNotExistException(name);
}
} }
//TODO: logar que um receptaculo foi removido e todas as suas conexões, perdidas
} }
/** /**
......
...@@ -204,4 +204,23 @@ public class Receptacle { ...@@ -204,4 +204,23 @@ public class Receptacle {
this.connections.remove(connectionIndex); this.connections.remove(connectionIndex);
return true; return true;
} }
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (!obj.getClass().equals(this.getClass())) {
return false;
}
Receptacle receptacle = (Receptacle) obj;
return (this.name.equals(receptacle.name));
}
@Override
public int hashCode() {
return this.name.hashCode();
}
} }
...@@ -23,6 +23,7 @@ import scs.core.ComponentContext; ...@@ -23,6 +23,7 @@ import scs.core.ComponentContext;
import scs.core.ComponentId; import scs.core.ComponentId;
import scs.core.builder.exception.NoComponentIdException; import scs.core.builder.exception.NoComponentIdException;
import scs.core.exception.InvalidServantException; import scs.core.exception.InvalidServantException;
import scs.core.exception.ReceptacleAlreadyExistsException;
import scs.core.exception.SCSException; import scs.core.exception.SCSException;
/** /**
...@@ -206,8 +207,10 @@ public class XMLComponentBuilder { ...@@ -206,8 +207,10 @@ public class XMLComponentBuilder {
* *
* @param doc The document with the component specification. * @param doc The document with the component specification.
* @param context The component. * @param context The component.
* @throws ReceptacleAlreadyExistsException
*/ */
private void readAndPutReceptacles(Document doc, ComponentContext context) { private void readAndPutReceptacles(Document doc, ComponentContext context)
throws ReceptacleAlreadyExistsException {
NodeList list = doc.getElementsByTagName(RECEPTACLE_ELEMENT); NodeList list = doc.getElementsByTagName(RECEPTACLE_ELEMENT);
for (int i = 0; i < list.getLength(); i++) { for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i); Node node = list.item(i);
...@@ -217,7 +220,7 @@ public class XMLComponentBuilder { ...@@ -217,7 +220,7 @@ public class XMLComponentBuilder {
String interfaceName = getTagValue(RECEPTACLE_INTERFACE_NAME, element); String interfaceName = getTagValue(RECEPTACLE_INTERFACE_NAME, element);
boolean isMultiplex = boolean isMultiplex =
Boolean.parseBoolean(getTagValue(RECEPTACLE_MULTIPLEX, element)); Boolean.parseBoolean(getTagValue(RECEPTACLE_MULTIPLEX, element));
context.putReceptacle(name, interfaceName, isMultiplex); context.addReceptacle(name, interfaceName, isMultiplex);
} }
} }
} }
......
package scs.core; package scs.core;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set;
import junit.framework.Assert; import junit.framework.Assert;
...@@ -17,6 +15,8 @@ import org.omg.PortableServer.POAHelper; ...@@ -17,6 +15,8 @@ import org.omg.PortableServer.POAHelper;
import scs.core.exception.FacetAlreadyExists; import scs.core.exception.FacetAlreadyExists;
import scs.core.exception.FacetDoesNotExist; import scs.core.exception.FacetDoesNotExist;
import scs.core.exception.ReceptacleAlreadyExistsException;
import scs.core.exception.ReceptacleDoesNotExistException;
import scs.core.exception.SCSException; import scs.core.exception.SCSException;
public final class ComponentContextTest { public final class ComponentContextTest {
...@@ -245,23 +245,38 @@ public final class ComponentContextTest { ...@@ -245,23 +245,38 @@ public final class ComponentContextTest {
} }
@Test @Test
public void putReceptacle() throws SCSException { public void addReceptacle() throws SCSException {
String receptacleName = "nome"; String receptacleName = "nome";
ComponentContext component = new ComponentContext(orb, poa, componentId); ComponentContext component = new ComponentContext(orb, poa, componentId);
component.putReceptacle(receptacleName, IComponentHelper.id(), false); component.addReceptacle(receptacleName, IComponentHelper.id(), false);
Assert.assertNotNull(component.getReceptacleByName(receptacleName)); Assert.assertNotNull(component.getReceptacleByName(receptacleName));
} }
@Test(expected = ReceptacleAlreadyExistsException.class)
public void addReceptacle2() throws SCSException {
String receptacleName = "nome";
ComponentContext component = new ComponentContext(orb, poa, componentId);
component.addReceptacle(receptacleName, IComponentHelper.id(), false);
component.addReceptacle(receptacleName, IComponentHelper.id(), true);
}
@Test @Test
public void removeReceptacle() throws SCSException { public void removeReceptacle() throws SCSException {
String receptacleName = "nome"; String receptacleName = "nome";
ComponentContext component = new ComponentContext(orb, poa, componentId); ComponentContext component = new ComponentContext(orb, poa, componentId);
component.putReceptacle("nome", IComponentHelper.id(), false); component.addReceptacle(receptacleName, IComponentHelper.id(), false);
Assert.assertNotNull(component.getReceptacleByName(receptacleName)); Assert.assertNotNull(component.getReceptacleByName(receptacleName));
component.removeReceptacle(receptacleName); component.removeReceptacle(receptacleName);
Assert.assertNull(component.getReceptacleByName(receptacleName)); Assert.assertNull(component.getReceptacleByName(receptacleName));
} }
@Test(expected = ReceptacleDoesNotExistException.class)
public void removeReceptacle2() throws SCSException {
String receptacleName = "nome";
ComponentContext component = new ComponentContext(orb, poa, componentId);
component.removeReceptacle(receptacleName);
}
@Test @Test
public void activateComponent() throws SCSException { public void activateComponent() throws SCSException {
ComponentContext component = new ComponentContext(orb, poa, componentId); ComponentContext component = new ComponentContext(orb, poa, componentId);
......
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