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 971025e commit 08a0ee3
Show file tree
Hide file tree
Showing 26 changed files with 374 additions and 181 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package org.structr.core.api;

import java.util.Map;
import org.structr.common.SecurityContext;
import org.structr.common.error.FrameworkException;
import org.structr.schema.action.EvaluationHints;
Expand Down Expand Up @@ -53,8 +52,8 @@ public AbstractMethod(final String name, final String summary, final String desc
*/

public abstract boolean isStatic();
public abstract Map<String, String> getParameters();
public abstract Object execute(final SecurityContext securityContext, final Map<String, Object> arguments, final EvaluationHints hints) throws FrameworkException;
public abstract Parameters getParameters();
public abstract Object execute(final SecurityContext securityContext, final Arguments arguments, final EvaluationHints hints) throws FrameworkException;

public String getName() {
return name;
Expand All @@ -67,4 +66,37 @@ public String getSummary() {
public String getDescription() {
return description;
}

protected void checkAndConvertArguments(final Arguments arguments) throws FrameworkException {




/*
throw new RuntimeException(
new FrameworkException(
422,
"Tried to call method \"" + method.getName() + "\" with invalid arguments. SchemaMethods expect their arguments to be passed as an object."
)
);
*/

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

// The following code bridges the gap between an argument map (coming from JS scripting)
// and unnamed order-based arguments of regular Java methods.

// The first parameter is always a SecurityContext object. The second parameter MUST be
// Map<String, Object>, otherwise the method cannot be called from a scripting context.

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

if (method.getParameterCount() > 1) {
args.add(arguments);
}
//
}
}
99 changes: 99 additions & 0 deletions structr-base/src/main/java/org/structr/core/api/Arguments.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright (C) 2010-2023 Structr GmbH
*
* This file is part of Structr <http://structr.org>.
*
* Structr is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Structr is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Structr. If not, see <http://www.gnu.org/licenses/>.
*/
package org.structr.core.api;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.structr.common.error.FrameworkException;

/**
* Base class for arguments that can be passed to Method implementations.
* This class exists because we support several different ways to call a
* method, and we might need map-like argument objects as well as Java-
* like argument passing (arg0, arg1, arg2) etc.
*/
public class Arguments {

private final List<Argument> arguments = new LinkedList<>();

public void add(final Object value) {
arguments.add(new Argument(null, value));
}

public void add(final String name, final Object value) {
arguments.add(new Argument(name, value));
}

public static Arguments fromMap(final Map<String, Object> map) {

final Arguments arguments = new Arguments();

return arguments;
}

public Map<String, Object> toMap() throws FrameworkException {

// this can only work if we have named argumentss

final Map<String, Object> map = new LinkedHashMap<>();

for (final Argument a : arguments) {

final String name = a.getName();
final Object value = a.getValue();

if (name == null) {

// FIXME: error message is way too technical here..
throw new FrameworkException(422, "Cannot use unnamed arguments in map-based method call.");
}

map.put(name, value);
}

return map;
}

public Object[] toArray() {

return null;
}

private class Argument {

private String name = null;
private Object value = null;

public Argument(final String name, final Object value) {

this.name = name;
this.value = value;
}

public String getName() {
return name;
}

public Object getValue() {
return value;
}
}
}
125 changes: 125 additions & 0 deletions structr-base/src/main/java/org/structr/core/api/Parameters.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright (C) 2010-2023 Structr GmbH
*
* This file is part of Structr <http://structr.org>.
*
* Structr is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Structr is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Structr. If not, see <http://www.gnu.org/licenses/>.
*/
package org.structr.core.api;

import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.List;
import org.structr.core.entity.SchemaMethod;
import org.structr.core.entity.SchemaMethodParameter;

/**
* Base class for parameters that can be defined by Method implementations.
*/
public class Parameters {

private final List<Parameter> parameters = new LinkedList<>();

public void put(final String name, final String type) {
parameters.add(new Parameter(name, type));
}

public static Parameters fromMethod(final Method method) {

final Parameters parameters = new Parameters();

for (final java.lang.reflect.Parameter p : method.getParameters()) {

parameters.put(p.getName(), p.getType().getSimpleName());
}

return parameters;
}

public static Parameters fromSchemaMethod(final SchemaMethod method) {

final Parameters parameters = new Parameters();

for (final SchemaMethodParameter p : method.getParameters()) {

parameters.put(p.getName(), p.getParameterType());
}

return parameters;
}

private class Parameter {

private String name = null;
private String type = null;

public Parameter(final String name, final String type) {

this.name = name;
this.type = type;
}

public String getName() {
return name;
}

public String getType() {
return type;
}
}

/*
There are some instances of exported methods that must be called with non-map parameters.
We need to support this, but we should warn about it.
Example:
type.addMethod("sendMessage")
.setReturnType(RestMethodResult.class.getName())
.addParameter("ctx", SecurityContext.class.getName())
.addParameter("topic", String.class.getName())
.addParameter("message", String.class.getName())
.setSource("return " + MessageClient.class.getName() + ".sendMessage(this, topic, message, ctx);")
.addException(FrameworkException.class.getName())
.setDoExport(true);
*/

/*
protected Map<String, Object> convertArguments(final Map<String, Object> restInput) throws FrameworkException {
final Map<String, Object> convertedArguments = new LinkedHashMap<>();
final Map<String, String> declaredParameters = method.getParameters();
for (final String name : restInput.keySet()) {
final String type = declaredParameters.get(name);
final Object input = restInput.get(name);
convertedArguments.put(name, convert(input, type));
}
return convertedArguments;
}
private Object convert(final Object input, final String type) {
// TODO: implement conversion...
System.out.println("RESTMethodCallHandler: NOT converting " + input + " to " + type + ", implementation missing.");
return input;
}
*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.structr.common.SecurityContext;
Expand All @@ -40,15 +35,17 @@ public class ReflectiveMethod extends AbstractMethod {

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

private GraphObject entity = null;
private Method method = null;
private Parameters parameters = null;
private GraphObject entity = null;
private Method method = null;

public ReflectiveMethod(final Method method, final GraphObject entity) {

super(method.getName(), null, null);

this.method = method;
this.entity = entity;
this.parameters = Parameters.fromMethod(method);
this.method = method;
this.entity = entity;
}

@Override
Expand All @@ -57,39 +54,24 @@ public boolean isStatic() {
}

@Override
public Map<String, String> getParameters() {

final Map<String, String> parameters = new LinkedHashMap<>();

for (final Parameter p : method.getParameters()) {

parameters.put(p.getName(), p.getType().getSimpleName());
}

public Parameters getParameters() {
return parameters;
}

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

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

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

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

try {
return method.invoke(entity, args.toArray());

return method.invoke(entity, arguments.toArray());

} catch (IllegalArgumentException ex) {

ex.printStackTrace();

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."));
throw new FrameworkException(422, "Tried to call method " + method.getName() + " with invalid arguments. SchemaMethods expect their arguments to be passed as an object.");

} catch (IllegalAccessException ex) {

Expand Down
Loading

0 comments on commit 08a0ee3

Please sign in to comment.