This commit is contained in:
Manuele Simi 2010-03-15 21:23:13 +00:00
parent 5de9b38cb7
commit 82ec2f7c49
13 changed files with 282 additions and 204 deletions

View File

@ -26,7 +26,7 @@ public class AddResourcesOperator extends Operator {
/** /**
* Creates a new operator to manage the input resource list * Creates a new operator to manage the input resource list
* *
* @param resourceList * @param scopeState
* @param target * @param target
* @param operationID * @param operationID
*/ */

View File

@ -2,7 +2,7 @@ package org.gcube.vremanagement.resourcemanager.impl.operators;
import org.gcube.common.core.scope.GCUBEScope; import org.gcube.common.core.scope.GCUBEScope;
import org.gcube.vremanagement.resourcemanager.impl.contexts.ServiceContext; 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; import org.gcube.vremanagement.resourcemanager.impl.state.ResourceReport;
/** /**
@ -17,11 +17,11 @@ public class OperatorConfig {
public final GCUBEScope scope; 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.report = report;
this.resourceList = resourceList; this.scopeState = scopeState;
this.scope = (scope!=null && scope.length >0)? scope[0] : ServiceContext.getContext().getScope(); this.scope = (scope!=null && scope.length >0)? scope[0] : ServiceContext.getContext().getScope();
} }

View File

@ -4,14 +4,14 @@ import org.gcube.common.core.resources.GCUBEHostingNode;
import org.gcube.common.core.resources.GCUBERunningInstance; import org.gcube.common.core.resources.GCUBERunningInstance;
import org.gcube.common.core.resources.GCUBEService; import org.gcube.common.core.resources.GCUBEService;
import org.gcube.common.core.scope.GCUBEScope; 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 { public class ScopedResourceFactory {
private static ResourceList resourceList = null; private static ScopeState scopeState = null;
public static void setResourceList(final ResourceList list) { public static void setResourceList(final ScopeState list) {
resourceList = list; scopeState = list;
} }
/** /**
@ -24,9 +24,9 @@ public class ScopedResourceFactory {
*/ */
public static ScopedResource newResource(String id, String type, GCUBEScope scope) { public static ScopedResource newResource(String id, String type, GCUBEScope scope) {
if (resourceList != null) { if (scopeState != null) {
if (resourceList.containsResource(id)) if (scopeState.containsResource(id))
return resourceList.getResource(id); return scopeState.getResource(id);
} }
ScopedResource sresource = null; ScopedResource sresource = null;

View File

@ -56,9 +56,9 @@ public class ScopedResourceManagerOperator extends Operator {
this.configuration.report.addResource(sresource); this.configuration.report.addResource(sresource);
} }
if(toadd.size() > 0) if(toadd.size() > 0)
configuration.resourceList.addResources(toadd); configuration.scopeState.addResources(toadd);
if(toremove.size() > 0) if(toremove.size() > 0)
configuration.resourceList.removeResources(toremove); configuration.scopeState.removeResources(toremove);
this.configuration.report.save(); this.configuration.report.save();
} }

View File

@ -5,6 +5,7 @@ import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.gcube.common.core.contexts.GHNContext;
import org.gcube.common.core.scope.GCUBEScope; import org.gcube.common.core.scope.GCUBEScope;
import org.gcube.common.core.state.GCUBEWSResource; import org.gcube.common.core.state.GCUBEWSResource;
import org.gcube.vremanagement.resourcemanager.impl.contexts.ServiceContext; 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(); final protected GCUBEScope scope = ServiceContext.getContext().getInstance().getScopes().values().iterator().next();
protected ResourceList resourceList; protected ScopeState scopeState;
/** report id -> report map*/ /** report id -> report map*/
static Map<String, ResourceReport> id2report = Collections.synchronizedMap(new HashMap<String, ResourceReport>());; static Map<String, ResourceReport> id2report = Collections.synchronizedMap(new HashMap<String, ResourceReport>());;
@ -37,31 +38,39 @@ public final class InstanceState extends GCUBEWSResource {
@Override @Override
protected void initialise(Object... params) throws Exception { protected void initialise(Object... params) throws Exception {
this.setManagedScope(scope.toString()); this.setManagedScope(scope.toString());
scopeState = new ScopeState();
try { try {
logger.info("Loading the instance state from the local file system..."); logger.info("Initializing the instance state from the local file system...");
resourceList = Serializer.load(scope); Serializer.load(scopeState, scope);
//synch IS list w/ resourceList //synch IS list w/ scopeState
this.getPublishedScopeResource().loadFromLocalState(resourceList); //it's already done by the Publisher observer? this.getPublishedScopeResource().loadFromLocalState(scopeState); //it's already done by the Publisher observer?
} catch (IOException io) { } catch (IOException io) {
logger.warn("The local serialized list of scoped resources is not available"); logger.warn("The local serialized scope state is not available");
resourceList = new ResourceList(scope, scope.getName());
logger.info("Loading the instance state from the IS..."); logger.info("Loading the instance state from the IS...");
if (this.getPublishedScopeResource().load()) { if (this.getPublishedScopeResource().load()) {
logger.info("Instance state harvested from the IS"); logger.info("Instance state harvested from the IS");
//synch resourceList w/ IS list scopeState.initialize(scope, scope.getName(), GHNContext.getContext().isSecurityEnabled());
this.getPublishedScopeResource().to(resourceList); //synch scopeState w/ IS list
} else this.getPublishedScopeResource().to(scopeState);
logger.info("Empty list of scoped resources created"); this.registerObservers();
this.getPublishedScopeResource().loadFromLocalState(resourceList); } 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 ScopedResourceFactory.setResourceList(scopeState);
resourceList.addObserver(new Executor());
resourceList.addObserver(new Publisher());
resourceList.addObserver(new Serializer());
//let's notify the observers about the current list //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 @Override
@ -142,7 +151,7 @@ public final class InstanceState extends GCUBEWSResource {
* Gets the list of {@link ScopedResource}s * Gets the list of {@link ScopedResource}s
* @return the list of scoped resources * @return the list of scoped resources
*/ */
public ResourceList getResourceList() { public ScopeState getResourceList() {
return resourceList; return scopeState;
} }
} }

View File

@ -218,7 +218,7 @@ public class PublishedScopeResource {
serializer.startDocument("UTF-8", true); serializer.startDocument("UTF-8", true);
serializer.startTag(NS,"Scope").text(scope.toString()).endTag(NS,"Scope"); serializer.startTag(NS,"Scope").text(scope.toString()).endTag(NS,"Scope");
serializer.startTag(NS,"Service").text(this.service).endTag(NS,"Service"); 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"); serializer.startTag(NS,"Designer").text(this.getDesigner()).endTag(NS,"Designer");
if (this.startTime == null) if (this.startTime == null)
this.startTime = Calendar.getInstance().getTime(); this.startTime = Calendar.getInstance().getTime();
@ -241,29 +241,29 @@ public class PublishedScopeResource {
/** /**
* @return the creator * @return the creator
*/ */
public String getManager() { public String getCreator() {
return creator; return (creator != null) ? creator : "";
} }
/** /**
* @return the designer * @return the designer
*/ */
public String getDesigner() { public String getDesigner() {
return designer; return (designer != null)? designer : "";
} }
/** /**
* @return the description * @return the description
*/ */
public String getDescription() { public String getDescription() {
return this.resource.getDescription(); return (this.resource.getDescription() != null)? this.resource.getDescription() : "";
} }
/** /**
* @return the display name * @return the display name
*/ */
public String getDisplayName() { 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) { public void to(ScopeState scopeState) {
resourceList.addResources(publishedResourceList.asScopedResouces()); logger.debug("To: Filling the local scope state with the published state");
resourceList.setDesigner(this.getDesigner()); scopeState.addResources(publishedResourceList.asScopedResouces());
resourceList.setManager(this.getManager()); scopeState.setDesigner(this.getDesigner());
resourceList.securityEnabled = this.isSecurityEnabled(); scopeState.setManager(this.getCreator());
resourceList.changeDescription(this.getDescription()); scopeState.setSecurity(this.isSecurityEnabled());
resourceList.setEndTime(this.endTime); scopeState.changeDescription(this.getDescription());
resourceList.setStartTime(this.startTime); scopeState.setEndTime(this.endTime);
resourceList.setName(this.getDisplayName()); 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 * @throws Exception if the load fails
*/ */
public synchronized void loadFromLocalState(ResourceList resourceList) throws Exception { public synchronized void loadFromLocalState(ScopeState scopeState) throws Exception {
logger.debug("LoadFromLocalState: Loading the published state from the local scope state");
publishedResourceList = new PublishedResourceList();//empty the resourceList //reloading is needed in order to reuse the resource ID and
this.synchBasicInfo(resourceList); //to avoid to cancel the old Generic Resource from the IS (plus reusing any other information do not overridden)
for(ScopedResource resource : resourceList.getAllResources()) this.reload();
publishedResourceList = new PublishedResourceList();//empty the scopeState
this.synchBasicInfo(scopeState);
for(ScopedResource resource : scopeState.getAllResources())
this.addResource(resource); this.addResource(resource);
//initialise some resource's fields //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 * @throws Exception if the synchronization fails
*/ */
public synchronized void synchWithLocalState(ResourceList resourceList) throws Exception { public synchronized void synchWithLocalState(ScopeState scopeState) throws Exception {
for(ScopedResource resource : resourceList.getAllResources()) { logger.debug("SynchWithLocalState: Synch the published state from the scope state");
for(ScopedResource resource : scopeState.getAllResources()) {
switch (resource.getStatus()) { switch (resource.getStatus()) {
case ADDED: this.addResource(resource); break; case ADDED: this.addResource(resource); break;
case REMOVED: this.removeResource(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 { private void synchBasicInfo(ScopeState scopeState) throws Exception {
logger.debug("Synchronizing basic info to publish");
this.setOption("MANAGER",resourceList.getManager()); this.setOption("MANAGER",scopeState.getManager());
this.setOption("DESIGNER",resourceList.getDesigner()); this.setOption("DESIGNER",scopeState.getDesigner());
this.setOption("DESCRIPTION",resourceList.getDescription()); this.setOption("DESCRIPTION",scopeState.getDescription());
this.setOption("DISPLAYNAME",resourceList.getName()); 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() //here we directly assign the values, don't want to format/unformat them via setOption()
this.endTime = resourceList.getEndTime(); this.endTime = scopeState.getEndTime();
this.startTime = resourceList.getStartTime(); logger.trace("Start time " + ProfileDate.toXMLDateAndTime(scopeState.getStartTime()));
this.securityEnabled = resourceList.isSecurityEnabled(); this.startTime = scopeState.getStartTime();
this.securityEnabled = scopeState.isSecurityEnabled();
} }

View File

@ -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<String, String, ScopedResource> 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<String, Object> data;
protected RawScopeState () {}
/**
* Initializes the state
* @param scope the scope this state belongs to
*/
protected void initialize(final GCUBEScope scope) {
resources = new MultiKeysMap<String, String, ScopedResource>();
data = new HashMap<String, Object>();
this.scope = scope;
}
/**
* @return the scope this state belongs to
*/
public GCUBEScope getScope() {
return scope;
}
}

View File

@ -1,4 +1,4 @@
package org.gcube.vremanagement.resourcemanager.unit; package org.gcube.vremanagement.resourcemanager.impl.state;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; 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.ScopedGHN;
import org.gcube.vremanagement.resourcemanager.impl.resources.ScopedResource; import org.gcube.vremanagement.resourcemanager.impl.resources.ScopedResource;
import org.gcube.vremanagement.resourcemanager.impl.resources.ScopedRunningInstance; import org.gcube.vremanagement.resourcemanager.impl.resources.ScopedRunningInstance;
import org.gcube.vremanagement.resourcemanager.impl.state.ResourceList;
import com.thoughtworks.xstream.XStream; 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) * @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 static final String DESCRIPTION = "This is a test for scoped resources";
private ResourceList list; private ScopeState list;
public ResourceListTest(String name) { public ResourceListTest(String name) {
super(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 { protected void setUp() throws Exception {
@ -42,7 +43,7 @@ public class ResourceListTest extends TestCase {
protected void tearDown() throws Exception { protected void tearDown() throws Exception {
XStream stream = new XStream(); XStream stream = new XStream();
stream.processAnnotations(ResourceList.class); stream.processAnnotations(ScopeState.class);
System.out.println(stream.toXML(list)); System.out.println(stream.toXML(list));
} }

View File

@ -3,16 +3,14 @@ package org.gcube.vremanagement.resourcemanager.impl.state;
import java.util.Calendar; import java.util.Calendar;
import java.util.Collection; import java.util.Collection;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Observable; import java.util.Observable;
import java.util.Set; import java.util.Set;
import org.gcube.common.core.scope.GCUBEScope; 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;
import org.gcube.vremanagement.resourcemanager.impl.resources.ScopedResource.STATUS; import org.gcube.vremanagement.resourcemanager.impl.resources.ScopedResource.STATUS;
import org.gcube.vremanagement.resourcemanager.impl.resources.types.MultiKeysMap; import org.gcube.vremanagement.resourcemanager.impl.state.observers.Serializer;
import com.thoughtworks.xstream.annotations.XStreamAlias;
/** /**
* *
@ -21,59 +19,31 @@ import com.thoughtworks.xstream.annotations.XStreamAlias;
* @author Manuele Simi (ISTI-CNR) * @author Manuele Simi (ISTI-CNR)
* *
*/ */
@XStreamAlias("ResourceList") public class ScopeState extends Observable {
public class ResourceList extends Observable {
/** the scope this list belong to*/ protected GCUBELog logger = new GCUBELog(this);
@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;
@XStreamAlias("StartTime")
protected Date endTime = null;
@XStreamAlias("Security")
protected boolean securityEnabled = false;
@XStreamAlias("Resources")
protected MultiKeysMap<String, String, ScopedResource> 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<String, Object> data;
/** last operation performed on the list*/ /** last operation performed on the list*/
@XStreamAlias("LastOperationPerformed")
protected OPERATION lastOperationPerformed; protected OPERATION lastOperationPerformed;
public enum OPERATION {CREATED, LOADED, TOBEMANAGED, PUBLISHED, SERIALIZED, EXECUTED}; public enum OPERATION {CREATED, LOADED, TOBEMANAGED, PUBLISHED, SERIALIZED, EXECUTED};
public ResourceList(GCUBEScope scope, String name, String ... description) { private RawScopeState rawState;
this.resources = new MultiKeysMap<String, String, ScopedResource>();
data = new HashMap<String, Object>(); protected ScopeState() {}
this.scope = scope;
this.name = name; 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; this.lastOperationPerformed = OPERATION.CREATED;
if (this.startTime == null) if (!this.rawState.data.containsKey("STARTTIME"))
this.startTime = Calendar.getInstance().getTime(); this.rawState.data.put("STARTTIME", Calendar.getInstance().getTime());
this.endTime = null; this.rawState.data.put("ENDTIME", null);
if ((description != null) && (description.length > 0)) if ((! this.rawState.data.containsKey("DESCRIPTION")) && (description.length > 0))
this.description = description[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 * @param manager the manager
*/ */
public synchronized void setManager(String manager) { public synchronized void setManager(String manager) {
this.manager = manager; this.rawState.data.put("MANAGER", manager);
this.notifyObservers(); this.notifyObservers();
} }
@ -90,7 +60,7 @@ public class ResourceList extends Observable {
* @param manager the manager * @param manager the manager
*/ */
public synchronized void setDesigner(String designer) { public synchronized void setDesigner(String designer) {
this.designer = designer; this.rawState.data.put("DESIGNER", designer);
this.notifyObservers(); this.notifyObservers();
} }
@ -100,7 +70,7 @@ public class ResourceList extends Observable {
* @param description the description * @param description the description
*/ */
public synchronized void changeDescription (String description) { public synchronized void changeDescription (String description) {
this.description = description; this.rawState.data.put("DESCRIPTION", description);
this.notifyObservers(); this.notifyObservers();
} }
@ -111,7 +81,9 @@ public class ResourceList extends Observable {
public synchronized void addResources(Set<ScopedResource> newresources) { public synchronized void addResources(Set<ScopedResource> newresources) {
for (ScopedResource resource : newresources) { for (ScopedResource resource : newresources) {
resource.setStatus(STATUS.ADDREQUESTED); resource.setStatus(STATUS.ADDREQUESTED);
resources.put(resource.getId(), resource.getType(), resource); 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.setLastOperationPerformed(OPERATION.TOBEMANAGED);
this.notifyObservers(); this.notifyObservers();
@ -124,7 +96,7 @@ public class ResourceList extends Observable {
* @return the collection of resources * @return the collection of resources
*/ */
public Set<ScopedResource> getResourcesByType(String type) { public Set<ScopedResource> getResourcesByType(String type) {
return resources.getValuesBySecondaryKey(type); return rawState.resources.getValuesBySecondaryKey(type);
} }
/** /**
@ -134,11 +106,11 @@ public class ResourceList extends Observable {
* @return the resource * @return the resource
*/ */
public ScopedResource getResource(String id) { 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) { 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 * @param type the type of resources to remove
*/ */
public synchronized void removeAllResourcesByType(String type) { public synchronized void removeAllResourcesByType(String type) {
for (ScopedResource resource : resources.getValuesBySecondaryKey(type)) { for (ScopedResource resource : rawState.resources.getValuesBySecondaryKey(type)) {
resource.setStatus(STATUS.REMOVEREQUESTED); 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.setLastOperationPerformed(OPERATION.TOBEMANAGED);
this.notifyObservers(); this.notifyObservers();
resources.removeValuesBySecondaryKey(type); rawState.resources.removeValuesBySecondaryKey(type);
} }
/** /**
@ -165,13 +137,13 @@ public class ResourceList extends Observable {
public synchronized void removeResources(Set<ScopedResource> oldresources) { public synchronized void removeResources(Set<ScopedResource> oldresources) {
for (ScopedResource resource : oldresources) { for (ScopedResource resource : oldresources) {
resource.setStatus(STATUS.REMOVEREQUESTED); 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.setLastOperationPerformed(OPERATION.TOBEMANAGED);
this.notifyObservers(); //let the obs do their work before to remove the resources this.notifyObservers(); //let the obs do their work before to remove the resources
for (ScopedResource resource : oldresources) { for (ScopedResource resource : oldresources) {
if (resource.getStatus() == STATUS.UNPUBLISHED) if (resource.getStatus() == STATUS.UNPUBLISHED)
resources.removeValuesByPrimaryKey(resource.getId()); rawState.resources.removeValuesByPrimaryKey(resource.getId());
} }
this.notifyObservers(); //notify about the physical removal this.notifyObservers(); //notify about the physical removal
} }
@ -180,11 +152,11 @@ public class ResourceList extends Observable {
* Empty the list of resources * Empty the list of resources
*/ */
protected synchronized void removeAllResources() { protected synchronized void removeAllResources() {
for (ScopedResource resource : resources.values()) for (ScopedResource resource : rawState.resources.values())
resource.setStatus(STATUS.REMOVEREQUESTED); resource.setStatus(STATUS.REMOVEREQUESTED);
this.setLastOperationPerformed(OPERATION.TOBEMANAGED); this.setLastOperationPerformed(OPERATION.TOBEMANAGED);
this.notifyObservers(); this.notifyObservers();
resources.clean(); rawState.resources.clean();
} }
/** /**
@ -193,7 +165,7 @@ public class ResourceList extends Observable {
* @return the scope * @return the scope
*/ */
public GCUBEScope getScope() { public GCUBEScope getScope() {
return this.scope; return rawState.scope;
} }
public void notifyObservers(Object whatschanged) { public void notifyObservers(Object whatschanged) {
@ -214,7 +186,7 @@ public class ResourceList extends Observable {
* @return all the {@link ScopedResource}s * @return all the {@link ScopedResource}s
*/ */
public Collection<ScopedResource> getAllResources() { public Collection<ScopedResource> getAllResources() {
return resources.values(); return rawState.resources.values();
} }
/** /**
@ -222,7 +194,7 @@ public class ResourceList extends Observable {
* @return the scope manger * @return the scope manger
*/ */
public String getManager() { 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 * @return the scope designer
*/ */
public String getDesigner() { public String getDesigner() {
return this.designer; return (String) this.rawState.data.get("DESIGNER");
} }
public String getDescription() { public String getDescription() {
return this.description; return (String) this.rawState.data.get("DESCRIPTION");
} }
public String getName() { public String getName() {
return this.name;
return (String) this.rawState.data.get("NAME");
} }
public Date getEndTime() { public Date getEndTime() {
return this.endTime; return (Date) this.rawState.data.get("ENDTIME");
} }
public Date getStartTime() { public Date getStartTime() {
return this.startTime; return (Date) this.rawState.data.get("STARTTIME");
} }
public boolean isSecurityEnabled() { public boolean isSecurityEnabled() {
return this.securityEnabled; return (Boolean) this.rawState.data.get("SECURITYENABLED");
} }
public void setEndTime(Date endTime) { public void setEndTime(Date endTime) {
this.endTime = endTime; this.rawState.data.put("ENDTIME", endTime);
this.notifyObservers(); this.notifyObservers();
} }
public void setStartTime(Date startTime) { public void setStartTime(Date startTime) {
this.startTime = startTime; logger.trace("setStartTime: Start time " + ProfileDate.toXMLDateAndTime(startTime));
this.rawState.data.put("STARTTIME", startTime);
this.notifyObservers(); this.notifyObservers();
} }
public void setName(String name) { public void setName(String name) {
this.name = name; this.rawState.data.put("NAME", name);
this.notifyObservers(); this.notifyObservers();
} }
@ -283,11 +257,28 @@ public class ResourceList extends Observable {
this.lastOperationPerformed = operation; 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;
} }
public Object getData (String name) { /**
return this.data.get(name); * 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);
}
} }

View File

@ -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;
import org.gcube.vremanagement.resourcemanager.impl.resources.ScopedResource.STATUS; import org.gcube.vremanagement.resourcemanager.impl.resources.ScopedResource.STATUS;
import org.gcube.vremanagement.resourcemanager.impl.scopemanagement.ScopedResourceManagerOperator.ACTION; import org.gcube.vremanagement.resourcemanager.impl.scopemanagement.ScopedResourceManagerOperator.ACTION;
import org.gcube.vremanagement.resourcemanager.impl.state.ResourceList; import org.gcube.vremanagement.resourcemanager.impl.state.ScopeState;
import org.gcube.vremanagement.resourcemanager.impl.state.ResourceList.OPERATION; 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) * @author Manuele Simi (ISTI-CNR)
* *
*/ */
public class Executor extends ResourceListObserver { public class Executor extends ScopeObserver {
@Override @Override
protected void listChanged(ResourceList resourceList) { protected void scopeChanged(ScopeState scopeState) {
if (resourceList.getLastOperationPerformed() == OPERATION.EXECUTED) if (scopeState.getLastOperationPerformed() == OPERATION.EXECUTED)
return; //nothing to manage return; //nothing to manage
boolean managed = false; boolean managed = false;
for (ScopedResource resource : resourceList.getAllResources()) { for (ScopedResource resource : scopeState.getAllResources()) {
switch (resource.getStatus()) { switch (resource.getStatus()) {
case ADDREQUESTED: this.addResourceToScope(resource); managed = true; break; case ADDREQUESTED: this.addResourceToScope(resource); managed = true; break;
case REMOVEREQUESTED: this.removeResourceFromScope(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 //notify the others for serialization and publication duties
if (managed) { if (managed) {
resourceList.setLastOperationPerformed(OPERATION.EXECUTED); scopeState.setLastOperationPerformed(OPERATION.EXECUTED);
resourceList.notifyObservers(); scopeState.notifyObservers();
} }
} }

View File

@ -1,8 +1,9 @@
package org.gcube.vremanagement.resourcemanager.impl.state.observers; 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.PublishedScopeResource;
import org.gcube.vremanagement.resourcemanager.impl.state.ResourceList; import org.gcube.vremanagement.resourcemanager.impl.state.ScopeState;
import static org.gcube.vremanagement.resourcemanager.impl.state.ResourceList.OPERATION; import static org.gcube.vremanagement.resourcemanager.impl.state.ScopeState.OPERATION;
/** /**
* *
* Synchronizes the resources' list with the IS * 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) * @author Manuele Simi (ISTI-CNR)
* *
*/ */
public class Publisher extends ResourceListObserver { public class Publisher extends ScopeObserver {
@Override @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 return; //no need to republish and loop on this
try { try {
PublishedScopeResource resource = PublishedScopeResource.getResource(resourceList.getScope()); PublishedScopeResource resource = PublishedScopeResource.getResource(scopeState.getScope());
try { try {
resource.synchWithLocalState(resourceList); logger.trace("PUBLISHER: Start time " + ProfileDate.toXMLDateAndTime(scopeState.getStartTime()));
resource.synchWithLocalState(scopeState);
resource.publish(); resource.publish();
resourceList.setLastOperationPerformed(OPERATION.PUBLISHED); scopeState.setLastOperationPerformed(OPERATION.PUBLISHED);
resourceList.notifyObservers();//force to serialize after publishing (not nice, thought) scopeState.notifyObservers();//force to serialize after publishing (not nice, thought)
} catch (Exception e) { } catch (Exception e) {
logger.fatal("Can't publish the Scope Resource in the IS"); logger.fatal("Can't publish the Scope Resource in the IS");
throw e; throw e;

View File

@ -4,38 +4,38 @@ import java.util.Observable;
import java.util.Observer; import java.util.Observer;
import org.gcube.common.core.utils.logging.GCUBELog; 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) * @author Manuele Simi (ISTI-CNR)
* *
*/ */
public abstract class ResourceListObserver implements Observer { public abstract class ScopeObserver implements Observer {
protected GCUBELog logger = new GCUBELog(this.getClass()); protected GCUBELog logger = new GCUBELog(this.getClass());
public void update(Observable observed, Object arg) { public void update(Observable observed, Object arg) {
logger.trace(this.getClass().getSimpleName() + " Observer has been notified"); logger.trace(this.getClass().getSimpleName() + " Observer has been notified");
ResourceList resources; ScopeState resources;
if (ResourceList.class.isAssignableFrom(observed.getClass())) if (ScopeState.class.isAssignableFrom(observed.getClass()))
resources = (ResourceList) observed; resources = (ScopeState) observed;
else else
throw new IllegalArgumentException("Can't manage the observed obj"); throw new IllegalArgumentException("Can't manage the observed obj");
//notify subclasses //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);
} }

View File

@ -8,29 +8,30 @@ import java.io.IOException;
import org.gcube.common.core.scope.GCUBEScope; import org.gcube.common.core.scope.GCUBEScope;
import org.gcube.vremanagement.resourcemanager.impl.contexts.ServiceContext; import org.gcube.vremanagement.resourcemanager.impl.contexts.ServiceContext;
import org.gcube.vremanagement.resourcemanager.impl.resources.ScopedResource; 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; import com.thoughtworks.xstream.XStream;
/** /**
* *
* Serializer for {@link ResourceList} * Serializer for {@link ScopeState}
* *
* @author Manuele Simi (ISTI-CNR) * @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); final public static File persistentList = ServiceContext.getContext().getPersistentFile("ScopedResourceList.xml", true);
/** /**
* Serializes the notified list on the file system * Serializes the notified list on the file system
* @param resourceList the list to serialize * @param scopeState the list to serialize
*/ */
@Override @Override
protected synchronized void listChanged(final ResourceList resourceList) { protected synchronized void scopeChanged(final ScopeState scopeState) {
try { try {
store(resourceList); store(scopeState);
//resourceList.setLastOperationPerformed(OPERATION.SERIALIZED); //if we record this, the others observer manage the list with no need //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 //we do not notify the others obs here, since the serialization does not imply any change in the list
} catch (IOException e) { } catch (IOException e) {
logger.fatal("Cannot serialize the resource's list", 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(); XStream stream = new XStream();
stream.processAnnotations(ResourceList.class); stream.processAnnotations(RawScopeState.class);
FileOutputStream fs = new FileOutputStream(persistentList); FileOutputStream fs = new FileOutputStream(persistentList);
stream.toXML(resourceList, fs); stream.toXML(scopeState.getRawScopeState(), fs);
fs.close(); fs.close();
} }
@ -52,17 +53,20 @@ public class Serializer extends ResourceListObserver {
* @return the resource list, if any * @return the resource list, if any
* @throws IOException if the list was not found on the file system * @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()) if (! persistentList.exists())
throw new IOException(); throw new IOException();
//try to load the local list of resources //try to load the local list of resources
XStream stream = new XStream(); XStream stream = new XStream();
stream.processAnnotations(ResourceList.class); stream.processAnnotations(RawScopeState.class);
ResourceList list = (ResourceList) stream.fromXML(new FileInputStream((persistentList))); RawScopeState state = (RawScopeState) stream.fromXML(new FileInputStream((persistentList)));
//a bit of sanity checks.... //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(); throw new IOException();
return list;
//inject the state
scopeState.setRawScopeState(state);
} }
} }