From 82ec2f7c4963d06181253d330cb691c9c2989def Mon Sep 17 00:00:00 2001 From: Manuele Simi Date: Mon, 15 Mar 2010 21:23:13 +0000 Subject: [PATCH] git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/vre-management/ResourceManager@18853 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../impl/operators/AddResourcesOperator.java | 2 +- .../impl/operators/OperatorConfig.java | 8 +- .../impl/resources/ScopedResourceFactory.java | 14 +- .../ScopedResourceManagerOperator.java | 4 +- .../impl/state/InstanceState.java | 51 +++--- .../impl/state/PublishedScopeResource.java | 85 +++++----- .../impl/state/RawScopeState.java | 62 +++++++ .../state}/ResourceListTest.java | 13 +- .../{ResourceList.java => ScopeState.java} | 157 +++++++++--------- .../impl/state/observers/Executor.java | 16 +- .../impl/state/observers/Publisher.java | 20 ++- ...ceListObserver.java => ScopeObserver.java} | 20 +-- .../impl/state/observers/Serializer.java | 34 ++-- 13 files changed, 282 insertions(+), 204 deletions(-) create mode 100644 src/org/gcube/vremanagement/resourcemanager/impl/state/RawScopeState.java rename src/org/gcube/vremanagement/resourcemanager/{unit => impl/state}/ResourceListTest.java (88%) rename src/org/gcube/vremanagement/resourcemanager/impl/state/{ResourceList.java => ScopeState.java} (58%) rename src/org/gcube/vremanagement/resourcemanager/impl/state/observers/{ResourceListObserver.java => ScopeObserver.java} (53%) diff --git a/src/org/gcube/vremanagement/resourcemanager/impl/operators/AddResourcesOperator.java b/src/org/gcube/vremanagement/resourcemanager/impl/operators/AddResourcesOperator.java index e025c89..eb57607 100644 --- a/src/org/gcube/vremanagement/resourcemanager/impl/operators/AddResourcesOperator.java +++ b/src/org/gcube/vremanagement/resourcemanager/impl/operators/AddResourcesOperator.java @@ -26,7 +26,7 @@ public class AddResourcesOperator extends Operator { /** * Creates a new operator to manage the input resource list * - * @param resourceList + * @param scopeState * @param target * @param operationID */ diff --git a/src/org/gcube/vremanagement/resourcemanager/impl/operators/OperatorConfig.java b/src/org/gcube/vremanagement/resourcemanager/impl/operators/OperatorConfig.java index 47683ac..78323ca 100644 --- a/src/org/gcube/vremanagement/resourcemanager/impl/operators/OperatorConfig.java +++ b/src/org/gcube/vremanagement/resourcemanager/impl/operators/OperatorConfig.java @@ -2,7 +2,7 @@ package org.gcube.vremanagement.resourcemanager.impl.operators; import org.gcube.common.core.scope.GCUBEScope; import org.gcube.vremanagement.resourcemanager.impl.contexts.ServiceContext; -import org.gcube.vremanagement.resourcemanager.impl.state.ResourceList; +import org.gcube.vremanagement.resourcemanager.impl.state.ScopeState; import org.gcube.vremanagement.resourcemanager.impl.state.ResourceReport; /** @@ -17,11 +17,11 @@ public class OperatorConfig { public final GCUBEScope scope; - public final ResourceList resourceList; + public final ScopeState scopeState; - public OperatorConfig(ResourceReport report, ResourceList resourceList, GCUBEScope ... scope) { + public OperatorConfig(ResourceReport report, ScopeState scopeState, GCUBEScope ... scope) { this.report = report; - this.resourceList = resourceList; + this.scopeState = scopeState; this.scope = (scope!=null && scope.length >0)? scope[0] : ServiceContext.getContext().getScope(); } diff --git a/src/org/gcube/vremanagement/resourcemanager/impl/resources/ScopedResourceFactory.java b/src/org/gcube/vremanagement/resourcemanager/impl/resources/ScopedResourceFactory.java index c95d95c..df4ccf9 100644 --- a/src/org/gcube/vremanagement/resourcemanager/impl/resources/ScopedResourceFactory.java +++ b/src/org/gcube/vremanagement/resourcemanager/impl/resources/ScopedResourceFactory.java @@ -4,14 +4,14 @@ import org.gcube.common.core.resources.GCUBEHostingNode; import org.gcube.common.core.resources.GCUBERunningInstance; import org.gcube.common.core.resources.GCUBEService; import org.gcube.common.core.scope.GCUBEScope; -import org.gcube.vremanagement.resourcemanager.impl.state.ResourceList; +import org.gcube.vremanagement.resourcemanager.impl.state.ScopeState; public class ScopedResourceFactory { - private static ResourceList resourceList = null; + private static ScopeState scopeState = null; - public static void setResourceList(final ResourceList list) { - resourceList = list; + public static void setResourceList(final ScopeState list) { + scopeState = list; } /** @@ -24,9 +24,9 @@ public class ScopedResourceFactory { */ public static ScopedResource newResource(String id, String type, GCUBEScope scope) { - if (resourceList != null) { - if (resourceList.containsResource(id)) - return resourceList.getResource(id); + if (scopeState != null) { + if (scopeState.containsResource(id)) + return scopeState.getResource(id); } ScopedResource sresource = null; diff --git a/src/org/gcube/vremanagement/resourcemanager/impl/scopemanagement/ScopedResourceManagerOperator.java b/src/org/gcube/vremanagement/resourcemanager/impl/scopemanagement/ScopedResourceManagerOperator.java index 64ef981..c7d93d9 100644 --- a/src/org/gcube/vremanagement/resourcemanager/impl/scopemanagement/ScopedResourceManagerOperator.java +++ b/src/org/gcube/vremanagement/resourcemanager/impl/scopemanagement/ScopedResourceManagerOperator.java @@ -56,9 +56,9 @@ public class ScopedResourceManagerOperator extends Operator { this.configuration.report.addResource(sresource); } if(toadd.size() > 0) - configuration.resourceList.addResources(toadd); + configuration.scopeState.addResources(toadd); if(toremove.size() > 0) - configuration.resourceList.removeResources(toremove); + configuration.scopeState.removeResources(toremove); this.configuration.report.save(); } diff --git a/src/org/gcube/vremanagement/resourcemanager/impl/state/InstanceState.java b/src/org/gcube/vremanagement/resourcemanager/impl/state/InstanceState.java index 6dfbfd1..148d037 100644 --- a/src/org/gcube/vremanagement/resourcemanager/impl/state/InstanceState.java +++ b/src/org/gcube/vremanagement/resourcemanager/impl/state/InstanceState.java @@ -5,6 +5,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; +import org.gcube.common.core.contexts.GHNContext; import org.gcube.common.core.scope.GCUBEScope; import org.gcube.common.core.state.GCUBEWSResource; import org.gcube.vremanagement.resourcemanager.impl.contexts.ServiceContext; @@ -29,7 +30,7 @@ public final class InstanceState extends GCUBEWSResource { final protected GCUBEScope scope = ServiceContext.getContext().getInstance().getScopes().values().iterator().next(); - protected ResourceList resourceList; + protected ScopeState scopeState; /** report id -> report map*/ static Map id2report = Collections.synchronizedMap(new HashMap());; @@ -37,31 +38,39 @@ public final class InstanceState extends GCUBEWSResource { @Override protected void initialise(Object... params) throws Exception { this.setManagedScope(scope.toString()); - + scopeState = new ScopeState(); try { - logger.info("Loading the instance state from the local file system..."); - resourceList = Serializer.load(scope); - //synch IS list w/ resourceList - this.getPublishedScopeResource().loadFromLocalState(resourceList); //it's already done by the Publisher observer? + logger.info("Initializing the instance state from the local file system..."); + Serializer.load(scopeState, scope); + //synch IS list w/ scopeState + this.getPublishedScopeResource().loadFromLocalState(scopeState); //it's already done by the Publisher observer? } catch (IOException io) { - logger.warn("The local serialized list of scoped resources is not available"); - resourceList = new ResourceList(scope, scope.getName()); + logger.warn("The local serialized scope state is not available"); logger.info("Loading the instance state from the IS..."); if (this.getPublishedScopeResource().load()) { logger.info("Instance state harvested from the IS"); - //synch resourceList w/ IS list - this.getPublishedScopeResource().to(resourceList); - } else - logger.info("Empty list of scoped resources created"); - this.getPublishedScopeResource().loadFromLocalState(resourceList); + scopeState.initialize(scope, scope.getName(), GHNContext.getContext().isSecurityEnabled()); + //synch scopeState w/ IS list + this.getPublishedScopeResource().to(scopeState); + this.registerObservers(); + } else { + logger.info("Empty instance state created"); + //we assume that if we are running on a secure ghn, we are in a secure Scope + scopeState.initialize(scope, scope.getName(), GHNContext.getContext().isSecurityEnabled()); + this.getPublishedScopeResource().loadFromLocalState(scopeState); + this.registerObservers(); + } } - ScopedResourceFactory.setResourceList(resourceList); - //register the observers - resourceList.addObserver(new Executor()); - resourceList.addObserver(new Publisher()); - resourceList.addObserver(new Serializer()); + ScopedResourceFactory.setResourceList(scopeState); //let's notify the observers about the current list - resourceList.notifyObservers(); + scopeState.notifyObservers(); + } + + private void registerObservers() { + //register the observers + scopeState.addObserver(new Executor()); + scopeState.addObserver(new Publisher()); + scopeState.addObserver(new Serializer()); } @Override @@ -142,7 +151,7 @@ public final class InstanceState extends GCUBEWSResource { * Gets the list of {@link ScopedResource}s * @return the list of scoped resources */ - public ResourceList getResourceList() { - return resourceList; + public ScopeState getResourceList() { + return scopeState; } } diff --git a/src/org/gcube/vremanagement/resourcemanager/impl/state/PublishedScopeResource.java b/src/org/gcube/vremanagement/resourcemanager/impl/state/PublishedScopeResource.java index 03c34e1..a3b619e 100644 --- a/src/org/gcube/vremanagement/resourcemanager/impl/state/PublishedScopeResource.java +++ b/src/org/gcube/vremanagement/resourcemanager/impl/state/PublishedScopeResource.java @@ -218,7 +218,7 @@ public class PublishedScopeResource { serializer.startDocument("UTF-8", true); serializer.startTag(NS,"Scope").text(scope.toString()).endTag(NS,"Scope"); serializer.startTag(NS,"Service").text(this.service).endTag(NS,"Service"); - serializer.startTag(NS,"Creator").text(this.getManager()).endTag(NS,"Creator"); + serializer.startTag(NS,"Creator").text(this.getCreator()).endTag(NS,"Creator"); serializer.startTag(NS,"Designer").text(this.getDesigner()).endTag(NS,"Designer"); if (this.startTime == null) this.startTime = Calendar.getInstance().getTime(); @@ -241,29 +241,29 @@ public class PublishedScopeResource { /** * @return the creator */ - public String getManager() { - return creator; + public String getCreator() { + return (creator != null) ? creator : ""; } /** * @return the designer */ public String getDesigner() { - return designer; + return (designer != null)? designer : ""; } /** * @return the description */ public String getDescription() { - return this.resource.getDescription(); + return (this.resource.getDescription() != null)? this.resource.getDescription() : ""; } /** * @return the display name */ public String getDisplayName() { - return this.resource.getName(); + return (this.resource.getName() != null) ? this.resource.getName() : ""; } /** @@ -366,33 +366,39 @@ public class PublishedScopeResource { } /** - * Fills the input {@link ResourceList} with the actual content of the {@link PublishedResourceList} + * Fills the input {@link ScopeState} with the actual content of the {@link PublishedResourceList} * - * @param resourceList the list to fill + * @param scopeState the list to fill */ - public void to(ResourceList resourceList) { - resourceList.addResources(publishedResourceList.asScopedResouces()); - resourceList.setDesigner(this.getDesigner()); - resourceList.setManager(this.getManager()); - resourceList.securityEnabled = this.isSecurityEnabled(); - resourceList.changeDescription(this.getDescription()); - resourceList.setEndTime(this.endTime); - resourceList.setStartTime(this.startTime); - resourceList.setName(this.getDisplayName()); + public void to(ScopeState scopeState) { + logger.debug("To: Filling the local scope state with the published state"); + scopeState.addResources(publishedResourceList.asScopedResouces()); + scopeState.setDesigner(this.getDesigner()); + scopeState.setManager(this.getCreator()); + scopeState.setSecurity(this.isSecurityEnabled()); + scopeState.changeDescription(this.getDescription()); + scopeState.setEndTime(this.endTime); + logger.trace("Setting the scope state to " + ProfileDate.toXMLDateAndTime(this.startTime)); + scopeState.setStartTime(this.startTime); + logger.trace("Scope state set to:" + ProfileDate.toXMLDateAndTime(scopeState.getStartTime())); + scopeState.setName(this.getDisplayName()); } /** - * Fills this {@link PublishedResourceList} with the content of the input {@link ResourceList} + * Fills this {@link PublishedResourceList} with the content of the input {@link ScopeState} * - * @param resourceList the list to load + * @param scopeState the list to load * @throws Exception if the load fails */ - public synchronized void loadFromLocalState(ResourceList resourceList) throws Exception { - - publishedResourceList = new PublishedResourceList();//empty the resourceList - this.synchBasicInfo(resourceList); - for(ScopedResource resource : resourceList.getAllResources()) + public synchronized void loadFromLocalState(ScopeState scopeState) throws Exception { + logger.debug("LoadFromLocalState: Loading the published state from the local scope state"); + //reloading is needed in order to reuse the resource ID and + //to avoid to cancel the old Generic Resource from the IS (plus reusing any other information do not overridden) + this.reload(); + publishedResourceList = new PublishedResourceList();//empty the scopeState + this.synchBasicInfo(scopeState); + for(ScopedResource resource : scopeState.getAllResources()) this.addResource(resource); //initialise some resource's fields @@ -410,13 +416,14 @@ public class PublishedScopeResource { } /** - * Synchronizes this {@link PublishedResourceList} with the content of the input {@link ResourceList} + * Synchronizes this {@link PublishedResourceList} with the content of the input {@link ScopeState} * - * @param resourceList the list to synchronize with + * @param scopeState the list to synchronize with * @throws Exception if the synchronization fails */ - public synchronized void synchWithLocalState(ResourceList resourceList) throws Exception { - for(ScopedResource resource : resourceList.getAllResources()) { + public synchronized void synchWithLocalState(ScopeState scopeState) throws Exception { + logger.debug("SynchWithLocalState: Synch the published state from the scope state"); + for(ScopedResource resource : scopeState.getAllResources()) { switch (resource.getStatus()) { case ADDED: this.addResource(resource); break; case REMOVED: this.removeResource(resource); break; @@ -424,19 +431,21 @@ public class PublishedScopeResource { } } - this.synchBasicInfo(resourceList); + this.synchBasicInfo(scopeState); } - private void synchBasicInfo(ResourceList resourceList) throws Exception { - - this.setOption("MANAGER",resourceList.getManager()); - this.setOption("DESIGNER",resourceList.getDesigner()); - this.setOption("DESCRIPTION",resourceList.getDescription()); - this.setOption("DISPLAYNAME",resourceList.getName()); + private void synchBasicInfo(ScopeState scopeState) throws Exception { + logger.debug("Synchronizing basic info to publish"); + this.setOption("MANAGER",scopeState.getManager()); + this.setOption("DESIGNER",scopeState.getDesigner()); + this.setOption("DESCRIPTION",scopeState.getDescription()); + logger.trace("Resource name " + scopeState.getName()); + this.setOption("DISPLAYNAME",scopeState.getName()); //here we directly assign the values, don't want to format/unformat them via setOption() - this.endTime = resourceList.getEndTime(); - this.startTime = resourceList.getStartTime(); - this.securityEnabled = resourceList.isSecurityEnabled(); + this.endTime = scopeState.getEndTime(); + logger.trace("Start time " + ProfileDate.toXMLDateAndTime(scopeState.getStartTime())); + this.startTime = scopeState.getStartTime(); + this.securityEnabled = scopeState.isSecurityEnabled(); } diff --git a/src/org/gcube/vremanagement/resourcemanager/impl/state/RawScopeState.java b/src/org/gcube/vremanagement/resourcemanager/impl/state/RawScopeState.java new file mode 100644 index 0000000..edcb882 --- /dev/null +++ b/src/org/gcube/vremanagement/resourcemanager/impl/state/RawScopeState.java @@ -0,0 +1,62 @@ +package org.gcube.vremanagement.resourcemanager.impl.state; + +import java.util.HashMap; +import java.util.Map; + +import org.gcube.common.core.scope.GCUBEScope; +import org.gcube.vremanagement.resourcemanager.impl.resources.ScopedResource; +import org.gcube.vremanagement.resourcemanager.impl.resources.types.MultiKeysMap; + +import com.thoughtworks.xstream.annotations.XStreamAlias; + +/** + * + * The scope state. + * + * While {@link ScopeState} incorporates the behavior of a scope state, this is a separate and + * minimal class containing only the information to serialize and nothing else. + * This way, we avoid mis-serializations with XSTREAM. In fact, any time a SLOC is changed in the a serialized + * class, the old serializations are no longer valid. Here, the serialized data are separated from the state + * behavior, by minimizing the need of future changes. + * + * @author Manuele Simi (ISTI-CNR) + * + */ +@XStreamAlias("RawScopeState") +public class RawScopeState { + + /** the scope this state belongs to*/ + @XStreamAlias("Scope") + protected GCUBEScope scope; + + /** the list of resources */ + @XStreamAlias("Resources") + protected MultiKeysMap resources; + + //Open structure for information to store. By using it, we will avoid to broke XSTREAM serialization + //when we need to add more information to the class + /** any data belonging the state worthy to be serialized*/ + @XStreamAlias("Data") + protected Map data; + + protected RawScopeState () {} + + /** + * Initializes the state + * @param scope the scope this state belongs to + */ + protected void initialize(final GCUBEScope scope) { + resources = new MultiKeysMap(); + data = new HashMap(); + this.scope = scope; + } + + /** + * @return the scope this state belongs to + */ + public GCUBEScope getScope() { + return scope; + } + + +} diff --git a/src/org/gcube/vremanagement/resourcemanager/unit/ResourceListTest.java b/src/org/gcube/vremanagement/resourcemanager/impl/state/ResourceListTest.java similarity index 88% rename from src/org/gcube/vremanagement/resourcemanager/unit/ResourceListTest.java rename to src/org/gcube/vremanagement/resourcemanager/impl/state/ResourceListTest.java index e9270d2..f97ee1f 100644 --- a/src/org/gcube/vremanagement/resourcemanager/unit/ResourceListTest.java +++ b/src/org/gcube/vremanagement/resourcemanager/impl/state/ResourceListTest.java @@ -1,4 +1,4 @@ -package org.gcube.vremanagement.resourcemanager.unit; +package org.gcube.vremanagement.resourcemanager.impl.state; import java.util.HashSet; import java.util.Set; @@ -7,7 +7,6 @@ import org.gcube.common.core.scope.GCUBEScope; import org.gcube.vremanagement.resourcemanager.impl.resources.ScopedGHN; import org.gcube.vremanagement.resourcemanager.impl.resources.ScopedResource; import org.gcube.vremanagement.resourcemanager.impl.resources.ScopedRunningInstance; -import org.gcube.vremanagement.resourcemanager.impl.state.ResourceList; import com.thoughtworks.xstream.XStream; @@ -16,7 +15,7 @@ import junit.framework.TestCase; /** * - * JUnit test class for {@link ResourceList} + * JUnit test class for {@link ScopeState} * * @author Manuele Simi (ISTI-CNR) * @@ -29,11 +28,13 @@ public class ResourceListTest extends TestCase { private static final String DESCRIPTION = "This is a test for scoped resources"; - private ResourceList list; + private ScopeState list; public ResourceListTest(String name) { super(name); - list = new ResourceList(GCUBEScope.getScope(SCOPE), NAME, DESCRIPTION); + list = new ScopeState(); + list.initialize(GCUBEScope.getScope(SCOPE), NAME, false, DESCRIPTION); + } protected void setUp() throws Exception { @@ -42,7 +43,7 @@ public class ResourceListTest extends TestCase { protected void tearDown() throws Exception { XStream stream = new XStream(); - stream.processAnnotations(ResourceList.class); + stream.processAnnotations(ScopeState.class); System.out.println(stream.toXML(list)); } diff --git a/src/org/gcube/vremanagement/resourcemanager/impl/state/ResourceList.java b/src/org/gcube/vremanagement/resourcemanager/impl/state/ScopeState.java similarity index 58% rename from src/org/gcube/vremanagement/resourcemanager/impl/state/ResourceList.java rename to src/org/gcube/vremanagement/resourcemanager/impl/state/ScopeState.java index 40b8557..6f9942d 100644 --- a/src/org/gcube/vremanagement/resourcemanager/impl/state/ResourceList.java +++ b/src/org/gcube/vremanagement/resourcemanager/impl/state/ScopeState.java @@ -3,16 +3,14 @@ package org.gcube.vremanagement.resourcemanager.impl.state; import java.util.Calendar; import java.util.Collection; import java.util.Date; -import java.util.HashMap; -import java.util.Map; import java.util.Observable; import java.util.Set; import org.gcube.common.core.scope.GCUBEScope; +import org.gcube.common.core.utils.logging.GCUBELog; import org.gcube.vremanagement.resourcemanager.impl.resources.ScopedResource; import org.gcube.vremanagement.resourcemanager.impl.resources.ScopedResource.STATUS; -import org.gcube.vremanagement.resourcemanager.impl.resources.types.MultiKeysMap; -import com.thoughtworks.xstream.annotations.XStreamAlias; +import org.gcube.vremanagement.resourcemanager.impl.state.observers.Serializer; /** * @@ -21,59 +19,31 @@ import com.thoughtworks.xstream.annotations.XStreamAlias; * @author Manuele Simi (ISTI-CNR) * */ -@XStreamAlias("ResourceList") -public class ResourceList extends Observable { +public class ScopeState extends Observable { - /** the scope this list belong to*/ - @XStreamAlias("Scope") - protected GCUBEScope scope; - - @XStreamAlias("Description") - protected String description = ""; - - @XStreamAlias("Name") - protected String name; - - @XStreamAlias("Designer") - protected String designer; - - @XStreamAlias("Manager") - protected String manager; - - @XStreamAlias("StartTime") - protected Date startTime = null; + protected GCUBELog logger = new GCUBELog(this); - @XStreamAlias("StartTime") - protected Date endTime = null; - - @XStreamAlias("Security") - protected boolean securityEnabled = false; - - @XStreamAlias("Resources") - protected MultiKeysMap resources; - - //Open structure for further information to store. By using it, we will avoid to broke XSTREAM serialization - //when we need to add more information to the class - @XStreamAlias("Data") - protected Map data; - - /** last operation performed on the list*/ - @XStreamAlias("LastOperationPerformed") + + /** last operation performed on the list*/ protected OPERATION lastOperationPerformed; public enum OPERATION {CREATED, LOADED, TOBEMANAGED, PUBLISHED, SERIALIZED, EXECUTED}; - public ResourceList(GCUBEScope scope, String name, String ... description) { - this.resources = new MultiKeysMap(); - data = new HashMap(); - this.scope = scope; - this.name = name; + private RawScopeState rawState; + + protected ScopeState() {} + + protected void initialize(GCUBEScope scope, String name, boolean securityEnabled, String ... description) { + rawState = new RawScopeState(); + rawState.initialize(scope); + this.rawState.data.put("NAME", name); this.lastOperationPerformed = OPERATION.CREATED; - if (this.startTime == null) - this.startTime = Calendar.getInstance().getTime(); - this.endTime = null; - if ((description != null) && (description.length > 0)) - this.description = description[0]; + if (!this.rawState.data.containsKey("STARTTIME")) + this.rawState.data.put("STARTTIME", Calendar.getInstance().getTime()); + this.rawState.data.put("ENDTIME", null); + if ((! this.rawState.data.containsKey("DESCRIPTION")) && (description.length > 0)) + this.rawState.data.put("DESCRIPTION", description[0]); + this.rawState.data.put("SECURITYENABLED",securityEnabled); } /** @@ -81,7 +51,7 @@ public class ResourceList extends Observable { * @param manager the manager */ public synchronized void setManager(String manager) { - this.manager = manager; + this.rawState.data.put("MANAGER", manager); this.notifyObservers(); } @@ -90,7 +60,7 @@ public class ResourceList extends Observable { * @param manager the manager */ public synchronized void setDesigner(String designer) { - this.designer = designer; + this.rawState.data.put("DESIGNER", designer); this.notifyObservers(); } @@ -100,7 +70,7 @@ public class ResourceList extends Observable { * @param description the description */ public synchronized void changeDescription (String description) { - this.description = description; + this.rawState.data.put("DESCRIPTION", description); this.notifyObservers(); } @@ -110,8 +80,10 @@ public class ResourceList extends Observable { */ public synchronized void addResources(Set newresources) { for (ScopedResource resource : newresources) { - resource.setStatus(STATUS.ADDREQUESTED); - resources.put(resource.getId(), resource.getType(), resource); + resource.setStatus(STATUS.ADDREQUESTED); + if (rawState.resources.primaryKeySet().contains(resource.getId())) + rawState.resources.removeValuesByPrimaryKey(resource.getId()); + rawState.resources.put(resource.getId(), resource.getType(), resource); } this.setLastOperationPerformed(OPERATION.TOBEMANAGED); this.notifyObservers(); @@ -124,7 +96,7 @@ public class ResourceList extends Observable { * @return the collection of resources */ public Set getResourcesByType(String type) { - return resources.getValuesBySecondaryKey(type); + return rawState.resources.getValuesBySecondaryKey(type); } /** @@ -134,11 +106,11 @@ public class ResourceList extends Observable { * @return the resource */ public ScopedResource getResource(String id) { - return (ScopedResource) resources.getValuesByPrimaryKey(id).iterator().next(); + return (ScopedResource) rawState.resources.getValuesByPrimaryKey(id).iterator().next(); } public boolean containsResource(String id) { - return resources.primaryKeySet().contains(id); + return rawState.resources.primaryKeySet().contains(id); } /** @@ -147,13 +119,13 @@ public class ResourceList extends Observable { * @param type the type of resources to remove */ public synchronized void removeAllResourcesByType(String type) { - for (ScopedResource resource : resources.getValuesBySecondaryKey(type)) { + for (ScopedResource resource : rawState.resources.getValuesBySecondaryKey(type)) { resource.setStatus(STATUS.REMOVEREQUESTED); - resources.put(resource.getId(), resource.getType(), resource);//TODO: is needed? + rawState.resources.put(resource.getId(), resource.getType(), resource);//TODO: is needed? } this.setLastOperationPerformed(OPERATION.TOBEMANAGED); this.notifyObservers(); - resources.removeValuesBySecondaryKey(type); + rawState.resources.removeValuesBySecondaryKey(type); } /** @@ -165,13 +137,13 @@ public class ResourceList extends Observable { public synchronized void removeResources(Set oldresources) { for (ScopedResource resource : oldresources) { resource.setStatus(STATUS.REMOVEREQUESTED); - resources.put(resource.getId(), resource.getType(), resource);//TODO: is needed? + rawState.resources.put(resource.getId(), resource.getType(), resource);//TODO: is needed? } this.setLastOperationPerformed(OPERATION.TOBEMANAGED); this.notifyObservers(); //let the obs do their work before to remove the resources for (ScopedResource resource : oldresources) { if (resource.getStatus() == STATUS.UNPUBLISHED) - resources.removeValuesByPrimaryKey(resource.getId()); + rawState.resources.removeValuesByPrimaryKey(resource.getId()); } this.notifyObservers(); //notify about the physical removal } @@ -180,11 +152,11 @@ public class ResourceList extends Observable { * Empty the list of resources */ protected synchronized void removeAllResources() { - for (ScopedResource resource : resources.values()) + for (ScopedResource resource : rawState.resources.values()) resource.setStatus(STATUS.REMOVEREQUESTED); this.setLastOperationPerformed(OPERATION.TOBEMANAGED); this.notifyObservers(); - resources.clean(); + rawState.resources.clean(); } /** @@ -193,7 +165,7 @@ public class ResourceList extends Observable { * @return the scope */ public GCUBEScope getScope() { - return this.scope; + return rawState.scope; } public void notifyObservers(Object whatschanged) { @@ -214,7 +186,7 @@ public class ResourceList extends Observable { * @return all the {@link ScopedResource}s */ public Collection getAllResources() { - return resources.values(); + return rawState.resources.values(); } /** @@ -222,7 +194,7 @@ public class ResourceList extends Observable { * @return the scope manger */ public String getManager() { - return this.manager; + return (String) this.rawState.data.get("MANAGER"); } /** @@ -230,42 +202,44 @@ public class ResourceList extends Observable { * @return the scope designer */ public String getDesigner() { - return this.designer; + return (String) this.rawState.data.get("DESIGNER"); } public String getDescription() { - return this.description; + return (String) this.rawState.data.get("DESCRIPTION"); } public String getName() { - return this.name; + + return (String) this.rawState.data.get("NAME"); } public Date getEndTime() { - return this.endTime; + return (Date) this.rawState.data.get("ENDTIME"); } public Date getStartTime() { - return this.startTime; + return (Date) this.rawState.data.get("STARTTIME"); } public boolean isSecurityEnabled() { - return this.securityEnabled; + return (Boolean) this.rawState.data.get("SECURITYENABLED"); } public void setEndTime(Date endTime) { - this.endTime = endTime; + this.rawState.data.put("ENDTIME", endTime); this.notifyObservers(); } public void setStartTime(Date startTime) { - this.startTime = startTime; + logger.trace("setStartTime: Start time " + ProfileDate.toXMLDateAndTime(startTime)); + this.rawState.data.put("STARTTIME", startTime); this.notifyObservers(); } - public void setName(String name) { - this.name = name; + public void setName(String name) { + this.rawState.data.put("NAME", name); this.notifyObservers(); } @@ -283,11 +257,28 @@ public class ResourceList extends Observable { this.lastOperationPerformed = operation; } - public void addData(String name, Object value) { - this.data.put(name, value); + + /** + * Gets the {@link RawScopeState} + * @return the raw state + */ + public RawScopeState getRawScopeState() { + return rawState; + } + + /** + * Sets the new {@link RawScopeState} + * it usually invoked at deserialization time, see {@link Serializer#load(ScopeState, GCUBEScope)} + * @param state + */ + public void setRawScopeState(RawScopeState state) { + this.rawState = state; + + } + + public void setSecurity(boolean securityEnabled) { + this.rawState.data.put("SECURITYENABLED",securityEnabled); + } - public Object getData (String name) { - return this.data.get(name); - } } diff --git a/src/org/gcube/vremanagement/resourcemanager/impl/state/observers/Executor.java b/src/org/gcube/vremanagement/resourcemanager/impl/state/observers/Executor.java index 85fab75..23d59ab 100644 --- a/src/org/gcube/vremanagement/resourcemanager/impl/state/observers/Executor.java +++ b/src/org/gcube/vremanagement/resourcemanager/impl/state/observers/Executor.java @@ -3,8 +3,8 @@ package org.gcube.vremanagement.resourcemanager.impl.state.observers; import org.gcube.vremanagement.resourcemanager.impl.resources.ScopedResource; import org.gcube.vremanagement.resourcemanager.impl.resources.ScopedResource.STATUS; import org.gcube.vremanagement.resourcemanager.impl.scopemanagement.ScopedResourceManagerOperator.ACTION; -import org.gcube.vremanagement.resourcemanager.impl.state.ResourceList; -import org.gcube.vremanagement.resourcemanager.impl.state.ResourceList.OPERATION; +import org.gcube.vremanagement.resourcemanager.impl.state.ScopeState; +import org.gcube.vremanagement.resourcemanager.impl.state.ScopeState.OPERATION; /** * @@ -13,14 +13,14 @@ import org.gcube.vremanagement.resourcemanager.impl.state.ResourceList.OPERATION * @author Manuele Simi (ISTI-CNR) * */ -public class Executor extends ResourceListObserver { +public class Executor extends ScopeObserver { @Override - protected void listChanged(ResourceList resourceList) { - if (resourceList.getLastOperationPerformed() == OPERATION.EXECUTED) + protected void scopeChanged(ScopeState scopeState) { + if (scopeState.getLastOperationPerformed() == OPERATION.EXECUTED) return; //nothing to manage boolean managed = false; - for (ScopedResource resource : resourceList.getAllResources()) { + for (ScopedResource resource : scopeState.getAllResources()) { switch (resource.getStatus()) { case ADDREQUESTED: this.addResourceToScope(resource); managed = true; break; case REMOVEREQUESTED: this.removeResourceFromScope(resource); managed = true;break; @@ -28,8 +28,8 @@ public class Executor extends ResourceListObserver { } //notify the others for serialization and publication duties if (managed) { - resourceList.setLastOperationPerformed(OPERATION.EXECUTED); - resourceList.notifyObservers(); + scopeState.setLastOperationPerformed(OPERATION.EXECUTED); + scopeState.notifyObservers(); } } diff --git a/src/org/gcube/vremanagement/resourcemanager/impl/state/observers/Publisher.java b/src/org/gcube/vremanagement/resourcemanager/impl/state/observers/Publisher.java index 1abec2f..87d01f8 100644 --- a/src/org/gcube/vremanagement/resourcemanager/impl/state/observers/Publisher.java +++ b/src/org/gcube/vremanagement/resourcemanager/impl/state/observers/Publisher.java @@ -1,8 +1,9 @@ package org.gcube.vremanagement.resourcemanager.impl.state.observers; +import org.gcube.vremanagement.resourcemanager.impl.state.ProfileDate; import org.gcube.vremanagement.resourcemanager.impl.state.PublishedScopeResource; -import org.gcube.vremanagement.resourcemanager.impl.state.ResourceList; -import static org.gcube.vremanagement.resourcemanager.impl.state.ResourceList.OPERATION; +import org.gcube.vremanagement.resourcemanager.impl.state.ScopeState; +import static org.gcube.vremanagement.resourcemanager.impl.state.ScopeState.OPERATION; /** * * Synchronizes the resources' list with the IS @@ -10,22 +11,23 @@ import static org.gcube.vremanagement.resourcemanager.impl.state.ResourceList.OP * @author Manuele Simi (ISTI-CNR) * */ -public class Publisher extends ResourceListObserver { +public class Publisher extends ScopeObserver { @Override - protected void listChanged(ResourceList resourceList) { + protected void scopeChanged(ScopeState scopeState) { - if (resourceList.getLastOperationPerformed() == OPERATION.PUBLISHED) + if (scopeState.getLastOperationPerformed() == OPERATION.PUBLISHED) return; //no need to republish and loop on this try { - PublishedScopeResource resource = PublishedScopeResource.getResource(resourceList.getScope()); + PublishedScopeResource resource = PublishedScopeResource.getResource(scopeState.getScope()); try { - resource.synchWithLocalState(resourceList); + logger.trace("PUBLISHER: Start time " + ProfileDate.toXMLDateAndTime(scopeState.getStartTime())); + resource.synchWithLocalState(scopeState); resource.publish(); - resourceList.setLastOperationPerformed(OPERATION.PUBLISHED); - resourceList.notifyObservers();//force to serialize after publishing (not nice, thought) + scopeState.setLastOperationPerformed(OPERATION.PUBLISHED); + scopeState.notifyObservers();//force to serialize after publishing (not nice, thought) } catch (Exception e) { logger.fatal("Can't publish the Scope Resource in the IS"); throw e; diff --git a/src/org/gcube/vremanagement/resourcemanager/impl/state/observers/ResourceListObserver.java b/src/org/gcube/vremanagement/resourcemanager/impl/state/observers/ScopeObserver.java similarity index 53% rename from src/org/gcube/vremanagement/resourcemanager/impl/state/observers/ResourceListObserver.java rename to src/org/gcube/vremanagement/resourcemanager/impl/state/observers/ScopeObserver.java index 6e08f19..aeeebd1 100644 --- a/src/org/gcube/vremanagement/resourcemanager/impl/state/observers/ResourceListObserver.java +++ b/src/org/gcube/vremanagement/resourcemanager/impl/state/observers/ScopeObserver.java @@ -4,38 +4,38 @@ import java.util.Observable; import java.util.Observer; import org.gcube.common.core.utils.logging.GCUBELog; -import org.gcube.vremanagement.resourcemanager.impl.state.ResourceList; +import org.gcube.vremanagement.resourcemanager.impl.state.ScopeState; /** * - * Base observer for {@link ResourceList} + * Base observer for {@link ScopeState} * * @author Manuele Simi (ISTI-CNR) * */ -public abstract class ResourceListObserver implements Observer { +public abstract class ScopeObserver implements Observer { protected GCUBELog logger = new GCUBELog(this.getClass()); public void update(Observable observed, Object arg) { logger.trace(this.getClass().getSimpleName() + " Observer has been notified"); - ResourceList resources; + ScopeState resources; - if (ResourceList.class.isAssignableFrom(observed.getClass())) - resources = (ResourceList) observed; + if (ScopeState.class.isAssignableFrom(observed.getClass())) + resources = (ScopeState) observed; else throw new IllegalArgumentException("Can't manage the observed obj"); //notify subclasses - this.listChanged(resources); + this.scopeChanged(resources); } /** - * Manages the modified resources list + * Manages the modified scope * - * @param resourceList the new resources list + * @param scopeState the scope */ - protected abstract void listChanged(ResourceList resourceList); + protected abstract void scopeChanged(ScopeState scopeState); } diff --git a/src/org/gcube/vremanagement/resourcemanager/impl/state/observers/Serializer.java b/src/org/gcube/vremanagement/resourcemanager/impl/state/observers/Serializer.java index 448981d..5f67ff5 100644 --- a/src/org/gcube/vremanagement/resourcemanager/impl/state/observers/Serializer.java +++ b/src/org/gcube/vremanagement/resourcemanager/impl/state/observers/Serializer.java @@ -8,29 +8,30 @@ import java.io.IOException; import org.gcube.common.core.scope.GCUBEScope; import org.gcube.vremanagement.resourcemanager.impl.contexts.ServiceContext; import org.gcube.vremanagement.resourcemanager.impl.resources.ScopedResource; -import org.gcube.vremanagement.resourcemanager.impl.state.ResourceList; +import org.gcube.vremanagement.resourcemanager.impl.state.RawScopeState; +import org.gcube.vremanagement.resourcemanager.impl.state.ScopeState; import com.thoughtworks.xstream.XStream; /** * - * Serializer for {@link ResourceList} + * Serializer for {@link ScopeState} * * @author Manuele Simi (ISTI-CNR) * */ -public class Serializer extends ResourceListObserver { +public class Serializer extends ScopeObserver { final public static File persistentList = ServiceContext.getContext().getPersistentFile("ScopedResourceList.xml", true); /** * Serializes the notified list on the file system - * @param resourceList the list to serialize + * @param scopeState the list to serialize */ @Override - protected synchronized void listChanged(final ResourceList resourceList) { + protected synchronized void scopeChanged(final ScopeState scopeState) { try { - store(resourceList); - //resourceList.setLastOperationPerformed(OPERATION.SERIALIZED); //if we record this, the others observer manage the list with no need + store(scopeState); + //scopeState.setLastOperationPerformed(OPERATION.SERIALIZED); //if we record this, the others observer manage the list with no need //we do not notify the others obs here, since the serialization does not imply any change in the list } catch (IOException e) { logger.fatal("Cannot serialize the resource's list", e); @@ -38,11 +39,11 @@ public class Serializer extends ResourceListObserver { } - public synchronized static void store(ResourceList resourceList) throws IOException { + public synchronized static void store(final ScopeState scopeState) throws IOException { XStream stream = new XStream(); - stream.processAnnotations(ResourceList.class); + stream.processAnnotations(RawScopeState.class); FileOutputStream fs = new FileOutputStream(persistentList); - stream.toXML(resourceList, fs); + stream.toXML(scopeState.getRawScopeState(), fs); fs.close(); } @@ -52,17 +53,20 @@ public class Serializer extends ResourceListObserver { * @return the resource list, if any * @throws IOException if the list was not found on the file system */ - synchronized public static ResourceList load(GCUBEScope scope) throws IOException { + public synchronized static void load(ScopeState scopeState, GCUBEScope scope) throws IOException { if (! persistentList.exists()) throw new IOException(); //try to load the local list of resources XStream stream = new XStream(); - stream.processAnnotations(ResourceList.class); - ResourceList list = (ResourceList) stream.fromXML(new FileInputStream((persistentList))); + stream.processAnnotations(RawScopeState.class); + RawScopeState state = (RawScopeState) stream.fromXML(new FileInputStream((persistentList))); + //a bit of sanity checks.... - if ((list == null) || (list.getScope() == null) || (list.getScope().getName().compareTo(scope.getName()) != 0)) + if ((state == null) || (state.getScope() == null) || (state.getScope().getName().compareTo(scope.getName()) != 0)) throw new IOException(); - return list; + + //inject the state + scopeState.setRawScopeState(state); } }