158 lines
4.8 KiB
Java
158 lines
4.8 KiB
Java
package org.gcube.vremanagement.resourcemanager.impl.state;
|
|
|
|
import java.io.IOException;
|
|
import java.util.Collections;
|
|
import java.util.HashMap;
|
|
import java.util.HashSet;
|
|
import java.util.Map;
|
|
import java.util.Set;
|
|
|
|
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;
|
|
import org.gcube.vremanagement.resourcemanager.impl.resources.ScopedResource;
|
|
import org.gcube.vremanagement.resourcemanager.impl.resources.ScopedResourceFactory;
|
|
import org.gcube.vremanagement.resourcemanager.impl.state.observers.Executor;
|
|
import org.gcube.vremanagement.resourcemanager.impl.state.observers.Publisher;
|
|
import org.gcube.vremanagement.resourcemanager.impl.state.observers.Serializer;
|
|
import org.globus.wsrf.NoSuchResourceException;
|
|
|
|
/**
|
|
* The ResourceManager's stateful resource
|
|
*
|
|
* @author Manuele Simi (ISTI-CNR)
|
|
*
|
|
*/
|
|
public final class InstanceState extends GCUBEWSResource {
|
|
|
|
final protected GCUBEScope scope = ServiceContext.getContext().getInstance().getScopes().values().iterator().next();
|
|
|
|
protected ScopeState scopeState;
|
|
|
|
/** report id -> report map*/
|
|
static Map<String, ResourceReport> id2report = Collections.synchronizedMap(new HashMap<String, ResourceReport>());;
|
|
|
|
@Override
|
|
protected void initialise(Object... params) throws Exception {
|
|
scopeState = new ScopeState();
|
|
try {
|
|
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 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");
|
|
scopeState.initialize(scope, scope.getName(), GHNContext.getContext().isSecurityEnabled());
|
|
//synch scopeState w/ IS list
|
|
this.getPublishedScopeResource().to(scopeState);
|
|
} 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);
|
|
|
|
}
|
|
}
|
|
ScopedResourceFactory.setResourceList(scopeState);
|
|
this.registerObservers();
|
|
//let's notify the observers about the current scope state
|
|
scopeState.notifyObservers();
|
|
}
|
|
|
|
private void registerObservers() {
|
|
//register the observers
|
|
scopeState.addObserver(new Executor());
|
|
scopeState.addObserver(new Publisher());
|
|
scopeState.addObserver(new Serializer());
|
|
}
|
|
|
|
|
|
/**
|
|
* Gets the RP <em>ManagedScope</em>
|
|
*
|
|
* @return the scope managed by this instance
|
|
*/
|
|
synchronized public GCUBEScope getManagedScope () {
|
|
return this.scope;
|
|
}
|
|
|
|
|
|
/**
|
|
* Gets the {@link ResourceReport}
|
|
*
|
|
* @param id the report ID
|
|
* @return the report
|
|
* @throws IOException
|
|
*/
|
|
public ResourceReport getReport(String id) throws IOException {
|
|
if (! id2report.containsKey(id))
|
|
id2report.put(id, ResourceReport.load(id));
|
|
return id2report.get(id);
|
|
}
|
|
|
|
/**
|
|
* Gets the string representation of a {@link ResourceReport}
|
|
*
|
|
* @param id the report ID
|
|
* @return the string representation of the report
|
|
* @throws IOException
|
|
*/
|
|
public String getReportAsString(String id) throws IOException {
|
|
return ResourceReport.loadAsString(id);
|
|
}
|
|
|
|
|
|
/**
|
|
* Adds a new {@link ResourceReport} to the service's state
|
|
*
|
|
* @param report the report to add
|
|
*/
|
|
public void addReport(ResourceReport report) {
|
|
id2report.put(report.getId(), report);
|
|
}
|
|
|
|
/**
|
|
* Gets the {@link PublishedScopeResource}
|
|
*
|
|
* @return the {@link PublishedScopeResource}
|
|
* @throws NoSuchResourceException
|
|
*/
|
|
public PublishedScopeResource getPublishedScopeResource() throws NoSuchResourceException {
|
|
try {
|
|
return PublishedScopeResource.getResource(this.scope);
|
|
} catch (Exception e) {
|
|
logger.warn("Unable to get the PublishedScopeResource", e);
|
|
throw new NoSuchResourceException();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Gets the list of {@link ScopedResource}s
|
|
* @return the list of scoped resources
|
|
*/
|
|
public ScopeState getResourceList() {
|
|
return scopeState;
|
|
}
|
|
|
|
/**
|
|
* Disposes the current scope
|
|
*/
|
|
public void dispose() {
|
|
Set<ScopedResource> allResources = new HashSet<ScopedResource>();
|
|
for (ScopedResource resource : this.scopeState.getAllResources()) {
|
|
allResources.add(resource);
|
|
}
|
|
//remove all resources
|
|
this.scopeState.removeResources(allResources);
|
|
|
|
//TODO: unpublish the published scope resource
|
|
|
|
//TODO: undeploy myself?
|
|
|
|
}
|
|
}
|