use of org.neo4j.kernel.api.procedure.CallableUserFunction in project neo4j by neo4j.
the class ProcedureCompiler method compileFunction.
List<CallableUserFunction> compileFunction(Class<?> fcnDefinition, boolean isBuiltin) throws KernelException {
try {
List<Method> functionMethods = Arrays.stream(fcnDefinition.getDeclaredMethods()).filter(m -> m.isAnnotationPresent(UserFunction.class)).collect(Collectors.toList());
if (functionMethods.isEmpty()) {
return emptyList();
}
// used for proper error handling
assertValidConstructor(fcnDefinition);
List<CallableUserFunction> out = new ArrayList<>(functionMethods.size());
for (Method method : functionMethods) {
String valueName = method.getAnnotation(UserFunction.class).value();
String definedName = method.getAnnotation(UserFunction.class).name();
QualifiedName funcName = extractName(fcnDefinition, method, valueName, definedName);
if (isBuiltin || config.isWhitelisted(funcName.toString())) {
out.add(compileFunction(fcnDefinition, method, funcName));
} else {
log.warn(String.format("The function '%s' is not on the allowlist and won't be loaded.", funcName.toString()));
}
}
out.sort(Comparator.comparing(a -> a.signature().name().toString()));
return out;
} catch (KernelException e) {
throw e;
} catch (Exception e) {
throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, e, "Failed to compile function defined in `%s`: %s", fcnDefinition.getSimpleName(), e.getMessage());
}
}
use of org.neo4j.kernel.api.procedure.CallableUserFunction in project neo4j by neo4j.
the class ProcedureCompiler method compileFunction.
private CallableUserFunction compileFunction(Class<?> procDefinition, Method method, QualifiedName procName) throws ProcedureException {
restrictions.verify(procName);
List<FieldSignature> inputSignature = inputSignatureDeterminer.signatureFor(method);
Class<?> returnType = method.getReturnType();
TypeCheckers.TypeChecker typeChecker = typeCheckers.checkerFor(returnType);
String description = description(method);
UserFunction function = method.getAnnotation(UserFunction.class);
String deprecated = deprecated(method, function::deprecatedBy, "Use of @UserFunction(deprecatedBy) without @Deprecated in " + procName);
List<FieldSetter> setters = allFieldInjections.setters(procDefinition);
if (!config.fullAccessFor(procName.toString())) {
try {
setters = safeFieldInjections.setters(procDefinition);
} catch (ComponentInjectionException e) {
description = describeAndLogLoadFailure(procName);
UserFunctionSignature signature = new UserFunctionSignature(procName, inputSignature, typeChecker.type(), deprecated, config.rolesFor(procName.toString()), description, null, false);
return new FailedLoadFunction(signature);
}
}
UserFunctionSignature signature = new UserFunctionSignature(procName, inputSignature, typeChecker.type(), deprecated, config.rolesFor(procName.toString()), description, null, false);
return ProcedureCompilation.compileFunction(signature, setters, method);
}
use of org.neo4j.kernel.api.procedure.CallableUserFunction in project neo4j by neo4j.
the class UserFunctionTest method shouldNotLoadNoneWhiteListedFunction.
@Test
void shouldNotLoadNoneWhiteListedFunction() throws Throwable {
// Given
Log log = spy(Log.class);
procedureCompiler = new ProcedureCompiler(new TypeCheckers(), components, new ComponentRegistry(), log, new ProcedureConfig(Config.defaults(GraphDatabaseSettings.procedure_allowlist, List.of("WrongName"))));
List<CallableUserFunction> method = compile(SingleReadOnlyFunction.class);
verify(log).warn("The function 'org.neo4j.procedure.impl.listCoolPeople' is not on the allowlist and won't be loaded.");
assertThat(method.size()).isEqualTo(0);
}
use of org.neo4j.kernel.api.procedure.CallableUserFunction in project neo4j by neo4j.
the class UserFunctionTest method shouldAllowOverridingProcedureName.
@Test
void shouldAllowOverridingProcedureName() throws Throwable {
// When
CallableUserFunction proc = compile(FunctionWithOverriddenName.class).get(0);
// Then
assertEquals("org.mystuff.thisisActuallyTheName", proc.signature().name().toString());
}
use of org.neo4j.kernel.api.procedure.CallableUserFunction in project neo4j by neo4j.
the class UserFunctionTest method shouldLoadWhiteListedFunction.
@Test
void shouldLoadWhiteListedFunction() throws Throwable {
// Given
procedureCompiler = new ProcedureCompiler(new TypeCheckers(), components, new ComponentRegistry(), NullLog.getInstance(), new ProcedureConfig(Config.defaults(GraphDatabaseSettings.procedure_allowlist, List.of("org.neo4j.procedure.impl.listCoolPeople"))));
CallableUserFunction method = compile(SingleReadOnlyFunction.class).get(0);
// Expect
Object out = method.apply(prepareContext(), new AnyValue[0]);
assertThat(out).isEqualTo(ValueUtils.of(Arrays.asList("Bonnie", "Clyde")));
}
Aggregations