Skip to content

Commit

Permalink
Work in progress.
Browse files Browse the repository at this point in the history
  • Loading branch information
cmorgner committed Feb 28, 2024
1 parent 7859368 commit 242ed42
Show file tree
Hide file tree
Showing 62 changed files with 422 additions and 680 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
import java.io.IOException;
import java.util.*;
import java.util.logging.Level;
import org.structr.core.api.MethodSignature;
import org.structr.core.api.AbstractMethod;
import org.structr.core.api.Methods;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,23 @@
package org.structr.core.api;

import java.util.Map;
import org.structr.common.SecurityContext;
import org.structr.common.error.FrameworkException;
import org.structr.rest.api.RESTCall;
import org.structr.schema.action.EvaluationHints;

/**
*
*/
public abstract class MethodSignature {
public abstract class AbstractMethod {

protected String name = null;

public MethodSignature(final String name) {
public AbstractMethod(final String name) {
this.name = name;
}

public abstract boolean isStatic();

public abstract MethodCall createCall(final RESTCall parameters) throws FrameworkException;
public abstract MethodCall createCall(final Map<String, Object> parameters) throws FrameworkException;
public abstract Object execute(final SecurityContext securityContext, final Map<String, Object> arguments, final EvaluationHints hints) throws FrameworkException;

public String getName() {
return name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,8 @@
*/
package org.structr.core.api;

import org.structr.common.SecurityContext;
import org.structr.common.error.FrameworkException;
import org.structr.schema.action.EvaluationHints;

/**
*/
public interface MethodCall {

Object execute(final SecurityContext securityContext, final EvaluationHints hints) throws FrameworkException;
}
12 changes: 6 additions & 6 deletions structr-base/src/main/java/org/structr/core/api/Methods.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
*/
public class Methods {

public static List<MethodSignature> getAllMethods(final Class type) {
public static List<AbstractMethod> getAllMethods(final Class type) {

final List<MethodSignature> signatures = new LinkedList<>();
final List<AbstractMethod> signatures = new LinkedList<>();

if (type != null) {

Expand All @@ -45,7 +45,7 @@ public static List<MethodSignature> getAllMethods(final Class type) {
for (final Method method : methods.values()) {

try {
signatures.add(Methods.createMethodSignature(method, null));
signatures.add(Methods.createMethod(method, null));

} catch
(FrameworkException fex) {
Expand All @@ -69,7 +69,7 @@ public static List<MethodSignature> getAllMethods(final Class type) {
return signatures;
}

public static MethodSignature getMethodSignatureOrNull(final Class type, final GraphObject instance, final String methodName) {
public static AbstractMethod resolveMethod(final Class type, final GraphObject instance, final String methodName) {

// A method can either be a Java method, which we need to call with Method.invoke() via reflection,
// OR a scripting method which will in turn call Actions.execute(), so we want do differentiate
Expand Down Expand Up @@ -103,7 +103,7 @@ public static MethodSignature getMethodSignatureOrNull(final Class type, final G
if (method != null) {

try {
return Methods.createMethodSignature(method, instance);
return Methods.createMethod(method, instance);

} catch (FrameworkException fex) {
throw new RuntimeException(fex);
Expand All @@ -115,7 +115,7 @@ public static MethodSignature getMethodSignatureOrNull(final Class type, final G
}

// ----- private static methods -----
private static MethodSignature createMethodSignature(final Method method, final GraphObject entity) throws FrameworkException {
private static AbstractMethod createMethod(final Method method, final GraphObject entity) throws FrameworkException {

final Export exp = method.getAnnotation(Export.class);
final String id = exp.schemaMethodId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
Expand All @@ -31,12 +30,11 @@
import org.structr.common.error.AssertException;
import org.structr.common.error.FrameworkException;
import org.structr.core.GraphObject;
import org.structr.rest.api.RESTCall;
import org.structr.schema.action.EvaluationHints;

/**
*/
public class ReflectiveMethod extends MethodSignature {
public class ReflectiveMethod extends AbstractMethod {

private static final Logger logger = LoggerFactory.getLogger(ReflectiveMethod.class);

Expand All @@ -57,75 +55,49 @@ public boolean isStatic() {
}

@Override
public MethodCall createCall(final RESTCall parameters) throws FrameworkException {
public Object execute(final SecurityContext securityContext, final Map<String, Object> arguments, final EvaluationHints hints) {

final Map<String, Object> converted = new LinkedHashMap<>();
final List<Object> args = new LinkedList<>();

// convert..

return createCall(converted);
}

@Override
public MethodCall createCall(final Map<String, Object> parameters) throws FrameworkException {

// this method is called from Java code so the parameters do not need to be converted
return new ReflectiveMethodCall(parameters);
}

private class ReflectiveMethodCall implements MethodCall {

private Map<String, Object> propertySet = null;

public ReflectiveMethodCall(final Map<String, Object> propertySet) {
this.propertySet = propertySet;
if (method.getParameterCount() > 0) {
args.add(securityContext);
}

@Override
public Object execute(final SecurityContext securityContext, final EvaluationHints hints) {

final List<Object> args = new LinkedList<>();

if (method.getParameterCount() > 0) {
args.add(securityContext);
}

if (method.getParameterCount() > 1) {
args.add(propertySet);
}

try {
return method.invoke(entity, args.toArray());
if (method.getParameterCount() > 1) {
args.add(arguments);
}

} catch (IllegalArgumentException ex) {
try {
return method.invoke(entity, args.toArray());

ex.printStackTrace();
} catch (IllegalArgumentException ex) {

throw new RuntimeException(new FrameworkException(422, "Tried to call method " + method.getName() + " with invalid parameters. SchemaMethods expect their parameters to be passed as an object."));
ex.printStackTrace();

} catch (IllegalAccessException ex) {
throw new RuntimeException(new FrameworkException(422, "Tried to call method " + method.getName() + " with invalid parameters. SchemaMethods expect their parameters to be passed as an object."));

ex.printStackTrace();
} catch (IllegalAccessException ex) {

logger.error("Unexpected exception while trying to get GraphObject member.", ex);
ex.printStackTrace();

} catch (InvocationTargetException ex) {
logger.error("Unexpected exception while trying to get GraphObject member.", ex);

ex.printStackTrace();
} catch (InvocationTargetException ex) {

if (ex.getTargetException() instanceof FrameworkException) {
ex.printStackTrace();

throw new RuntimeException(ex.getTargetException());
if (ex.getTargetException() instanceof FrameworkException) {

} else if (ex.getTargetException() instanceof AssertException) {
throw new RuntimeException(ex.getTargetException());

throw ((AssertException)ex.getTargetException());
}
} else if (ex.getTargetException() instanceof AssertException) {

logger.error("Unexpected exception while trying to get GraphObject member.", ex);
throw ((AssertException)ex.getTargetException());
}

return null;
logger.error("Unexpected exception while trying to get GraphObject member.", ex);
}

return null;
}
}
53 changes: 33 additions & 20 deletions structr-base/src/main/java/org/structr/core/api/ScriptMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,18 @@
*/
package org.structr.core.api;

import java.util.LinkedHashMap;
import java.util.Map;
import org.structr.common.SecurityContext;
import org.structr.common.error.AssertException;
import org.structr.common.error.FrameworkException;
import org.structr.core.GraphObject;
import org.structr.core.entity.SchemaMethod;
import org.structr.rest.api.RESTCall;
import org.structr.schema.action.Actions;
import org.structr.schema.action.EvaluationHints;

/**
*/
public class ScriptMethod extends MethodSignature {
public class ScriptMethod extends AbstractMethod {

private GraphObject entity = null;
private SchemaMethod method = null;
Expand All @@ -48,39 +47,53 @@ public boolean isStatic() {
return method.isStaticMethod();
}

/*
@Override
public MethodCall createCall(final RESTCall parameters) throws FrameworkException {
public MethodCall createCall() throws FrameworkException {
final Map<String, Object> converted = new LinkedHashMap<>();
final List<SchemaMethodParameter> declaredParameters = new LinkedList<>();
final Map<String, Object> converted = new LinkedHashMap<>();
// convert..
// "Method xy in class/interface ABC cannot be applied to given types. Required: ..., found: ..."
return createCall(converted);
}
//FIXME: how can we implement method parameters here, via REST and via scripting?
@Override
public MethodCall createCall(final Map<String, Object> parameters) throws FrameworkException {
// this method is called from Java code so the parameters do not need to be converted
return new ScriptMethodCall(parameters);
}
declaredParameters.addAll(Iterables.toList(method.getParameters()));
Collections.sort(declaredParameters, (a, b) -> {
return a.getProperty(SchemaMethodParameter.index).compareTo(b.getProperty(SchemaMethodParameter.index));
});
for (final SchemaMethodParameter param : declaredParameters) {
private class ScriptMethodCall implements MethodCall {
final String parameterName = param.getName();
final String inputValue = restParameters.get(parameterName);
final String type = param.getParameterType();
private Map<String, Object> propertySet = null;
// handle type
final Object convertedValue = inputValue;
public ScriptMethodCall(final Map<String, Object> propertySet) {
this.propertySet = propertySet;
converted.put(parameterName, convertedValue);
}
@Override
public Object execute(final SecurityContext securityContext, final EvaluationHints hints) throws FrameworkException {
return createCall(converted);
}
*/

@Override
public Object execute(final SecurityContext securityContext, final Map<String, Object> arguments, final EvaluationHints hints) throws FrameworkException {

try {

final String methodName = method.getName();
final String codeSource = method.getUuid();
final String source = method.getProperty("source");

return Actions.execute(securityContext, entity, "${" + source.trim() + "}", propertySet, methodName, codeSource);
return Actions.execute(securityContext, entity, "${" + source.trim() + "}", arguments, methodName, codeSource);

} catch (AssertException e) {
throw new FrameworkException(e.getStatus(), e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@

import java.util.*;
import org.structr.common.helper.ValidationHelper;
import org.structr.core.api.MethodCall;
import org.structr.core.api.MethodSignature;
import org.structr.core.api.AbstractMethod;
import org.structr.core.api.Methods;

/**
Expand Down Expand Up @@ -1604,14 +1603,13 @@ public Object evaluate(final ActionContext actionContext, final String key, fina
}
}

final MethodSignature signature = Methods.getMethodSignatureOrNull(entityType, this, key);
if (signature != null) {
final AbstractMethod method = Methods.resolveMethod(entityType, this, key);
if (method != null) {

final ContextStore contextStore = actionContext.getContextStore();
final Map<String, Object> parameters = contextStore.getTemporaryParameters();
final MethodCall call = signature.createCall(parameters);

return call.execute(actionContext.getSecurityContext(), hints);
return method.execute(actionContext.getSecurityContext(), parameters, hints);
}

return Function.numberOrString(defaultValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ public class SchemaMethod extends SchemaReloadingNode implements Favoritable {
id, type, schemaNode, name, source, returnType, exceptions, callSuper, overridesExisting, doExport, codeType, isPartOfBuiltInSchema, tags, summary, description, isStatic, includeInOpenAPI, openAPIReturnType
);

public Iterable<SchemaMethodParameter> getParameters() {
return getProperty(parameters);
}

public ActionEntry getActionEntry(final Map<String, SchemaNode> schemaNodes, final AbstractSchemaNode schemaEntity) throws FrameworkException {

final ActionEntry entry = new ActionEntry("___" + SchemaHelper.cleanPropertyName(getProperty(AbstractNode.name)), getProperty(SchemaMethod.source), getProperty(SchemaMethod.codeType));
Expand Down
Loading

0 comments on commit 242ed42

Please sign in to comment.