Skip to content

Commit

Permalink
Observer is assigned and linked
Browse files Browse the repository at this point in the history
  • Loading branch information
fvilla committed Dec 2, 2024
1 parent 344ce47 commit 08fed1f
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,25 @@ public Class<? extends Scope> classify() {

/**
* All scope except a {@link UserScope} have a non-null parent scope. A {@link ContextScope} is the only
* one that can have another scope of its same class as parent.
* one that can have another scope of its same class as parent. If a specific scope class is needed from a
* ContextScope, use {@link #getParentScope(Type, Class)} instead as the parent(s) may be other
* {@link ContextScope}s.
*
* @return the parent scope, null for {@link UserScope}s.
*/
Scope getParentScope();

/**
* Retrieve the first parent scope that matches the passed type. The class isn't enough as each inner
* scope class inherits from the others.
*
* @param type
* @param scopeClass
* @param <T>
* @return
*/
<T extends Scope> T getParentScope(Scope.Type type, Class<T> scopeClass);

/**
* Each scope can carry arbitrary data linked to it.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ public void setExpiration(Persistence expiration) {
this.persistence = expiration;
}

@Override
public <T extends Scope> T getParentScope(Type type, Class<T> scopeClass) {
if (delegateChannel instanceof Scope scope) {
return scope.getParentScope(type, scopeClass);
}
return null;
}

@Override
public void close() {
delegateChannel.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.integratedmodelling.klab.api.lang.kactors.KActorsBehavior;
import org.integratedmodelling.klab.api.scope.Persistence;
import org.integratedmodelling.klab.api.scope.ReactiveScope;
import org.integratedmodelling.klab.api.scope.Scope;
import org.integratedmodelling.klab.api.services.*;
import org.integratedmodelling.klab.api.services.runtime.Message;
import org.integratedmodelling.klab.api.services.runtime.impl.MessageImpl;
Expand Down Expand Up @@ -53,6 +54,18 @@ public Message send(Object... args) {
}
}

@Override
public <T extends Scope> T getParentScope(Type type, Class<T> scopeClass) {
Scope scope = this;
while (scope != null) {
if (scope.getType() == type) {
return (T)scope;
}
scope = scope.getParentScope();
}
return null;
}

public KlabService getService(String serviceId) {
KlabService ret = null;
for (var sc : List.of(Reasoner.class, ResourcesService.class, Resolver.class, RuntimeService.class)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,13 @@ public boolean sync(File workspace, SynchronizationMonitor monitor) {
exec.add(PosixFilePermission.OWNER_WRITE);

getRemoteFilelist(remote);
getLocalFilelist(workspace,local);
getLocalFilelist(workspace, local);

// process the filelist.txt entry last, so that the distrib only returns
// isComplete when it
// got to the end.
for (String s : remote.keySet()) {
if (!local.containsKey(s) || !local.get(s).equals(remote.get(s)) || !getDestinationFile(workspace,s).exists()) {
if (!local.containsKey(s) || !local.get(s).equals(remote.get(s)) || !getDestinationFile(workspace, s).exists()) {
if (!s.equals("filelist.txt"))
toDownload.put(s, remote.get(s));
}
Expand All @@ -321,7 +321,8 @@ public boolean sync(File workspace, SynchronizationMonitor monitor) {
monitor.beforeDownload(f);
}
try {
new Downloader(new URL(distributionUrl + "/" + f), getDestinationFile(workspace,f), (sofar, total) -> {
new Downloader(new URL(distributionUrl + "/" + f), getDestinationFile(workspace, f), (sofar
, total) -> {
if (monitor != null) {
monitor.notifyFileProgress(f, sofar, total);
}
Expand Down Expand Up @@ -365,7 +366,7 @@ private void scanForDeletion(File workspace, File file, HashMap<String, String>

if (file.isDirectory()) {
for (File f : file.listFiles()) {
scanForDeletion(workspace,f, remote, toRemove);
scanForDeletion(workspace, f, remote, toRemove);
}
} else {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ public abstract class ClientContextScope extends ClientSessionScope implements C

private Observation observer;
private Observation contextObservation;
private String[] scenarios;
private String resolutionNamespace;
private Map<ResolutionConstraint.Type, ResolutionConstraint> resolutionConstraints =
new LinkedHashMap<>();

Expand All @@ -50,9 +48,11 @@ public ClientContextScope(ClientUserScope parent, String contextName, RuntimeSer

private ClientContextScope(ClientContextScope parent) {
super(parent, parent.name, parent.runtimeService);
resolutionConstraints.putAll(parent.resolutionConstraints);
// this will have been reset by super to the user's id
setId(parent.getId());
resolutionConstraints.putAll(parent.resolutionConstraints);
observer = parent.observer;
contextObservation = parent.contextObservation;
}

@Override
Expand All @@ -72,12 +72,31 @@ public Observation getContextObservation() {

@Override
public ContextScope withObserver(Observation observer) {
return this;
var ret = childContext(this);
ret.observer = observer;
return ret;
}

protected ClientContextScope childContext(final ClientContextScope parent) {
return new ClientContextScope(parent) {

@Override
public <T extends KlabService> T getService(Class<T> serviceClass) {
return parent.getService(serviceClass);
}

@Override
public <T extends KlabService> Collection<T> getServices(Class<T> serviceClass) {
return parent.getServices(serviceClass);
}
};
}

@Override
public ContextScope within(Observation contextObservation) {
return null;
var ret = childContext(this);
ret.contextObservation = contextObservation;
return ret;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.integratedmodelling.klab.modeler;

import com.jcraft.jsch.Session;
import org.integratedmodelling.common.authentication.scope.AbstractReactiveScopeImpl;
import org.integratedmodelling.common.authentication.scope.AbstractServiceDelegatingScope;
import org.integratedmodelling.common.services.client.engine.EngineImpl;
Expand Down Expand Up @@ -371,7 +372,7 @@ public SessionScope getCurrentSession() {

@Override
public void setCurrentContext(ContextScope context) {
if (context != null && (this.currentSession == null || !this.currentSession.equals(context.getParentScope()))) {
if (context != null && (this.currentSession == null || !this.currentSession.equals(context.getParentScope(Scope.Type.SESSION, SessionScope.class)))) {
throw new KlabIllegalArgumentException("Cannot set context: argument is not part of the current" +
" session");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ public int compare(KimConcept o1, KimConcept o2) {
}
urn.append(urn.isEmpty() ? "" : " ").append(ret.getUrn());

// ret.setType(tokens.getFirst().getType());
ret.setUrn(urn.toString());
ret.getTraits().addAll(traits);
ret.getRoles().addAll(roles);
Expand Down

0 comments on commit 08fed1f

Please sign in to comment.