use of org.neo4j.kernel.api.proc.CallableUserAggregationFunction in project neo4j by neo4j.
the class ReflectiveUserAggregationFunctionTest method shouldGiveHelpfulErrorOnNullMessageException.
@Test
public void shouldGiveHelpfulErrorOnNullMessageException() throws Throwable {
// Given
CallableUserAggregationFunction method = compile(FunctionThatThrowsNullMsgExceptionAtInvocation.class).get(0);
// Expect
exception.expect(ProcedureException.class);
exception.expectMessage("Failed to invoke function `org.neo4j.kernel.impl.proc.test`: " + "Caused by: java.lang.IndexOutOfBoundsException");
// When
method.create(new BasicContext()).update(new Object[] {});
}
use of org.neo4j.kernel.api.proc.CallableUserAggregationFunction in project neo4j by neo4j.
the class ReflectiveProcedureCompiler method compileAggregationFunction.
List<CallableUserAggregationFunction> compileAggregationFunction(Class<?> fcnDefinition) throws KernelException {
try {
List<Method> methods = Arrays.stream(fcnDefinition.getDeclaredMethods()).filter(m -> m.isAnnotationPresent(UserAggregationFunction.class)).collect(Collectors.toList());
if (methods.isEmpty()) {
return emptyList();
}
MethodHandle constructor = constructor(fcnDefinition);
ArrayList<CallableUserAggregationFunction> out = new ArrayList<>(methods.size());
for (Method method : methods) {
String valueName = method.getAnnotation(UserAggregationFunction.class).value();
String definedName = method.getAnnotation(UserAggregationFunction.class).name();
QualifiedName funcName = extractName(fcnDefinition, method, valueName, definedName);
if (config.isWhitelisted(funcName.toString())) {
out.add(compileAggregationFunction(fcnDefinition, constructor, method, funcName));
} else {
log.warn(String.format("The function '%s' is not on the whitelist 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.proc.CallableUserAggregationFunction in project neo4j by neo4j.
the class ReflectiveProcedureCompiler method compileAggregationFunction.
private CallableUserAggregationFunction compileAggregationFunction(Class<?> definition, MethodHandle constructor, Method method, QualifiedName funcName) throws ProcedureException, IllegalAccessException {
if (funcName.namespace() == null || funcName.namespace().length == 0) {
throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "It is not allowed to define functions in the root namespace please use a namespace, e.g. `@UserFunction(\"org.example.com.%s\")", funcName.name());
}
//find update and result method
Method update = null;
Method result = null;
Class<?> aggregator = method.getReturnType();
for (Method m : aggregator.getDeclaredMethods()) {
if (m.isAnnotationPresent(UserAggregationUpdate.class)) {
if (update != null) {
throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Class '%s' contains multiple methods annotated with '@%s'.", aggregator.getSimpleName(), UserAggregationUpdate.class.getSimpleName());
}
update = m;
}
if (m.isAnnotationPresent(UserAggregationResult.class)) {
if (result != null) {
throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Class '%s' contains multiple methods annotated with '@%s'.", aggregator.getSimpleName(), UserAggregationResult.class.getSimpleName());
}
result = m;
}
}
if (result == null || update == null) {
throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Class '%s' must contain methods annotated with both '@%s' as well as '@%s'.", aggregator.getSimpleName(), UserAggregationResult.class.getSimpleName(), UserAggregationUpdate.class.getSimpleName());
}
if (update.getReturnType() != void.class) {
throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Update method '%s' in %s has type '%s' but must have return type 'void'.", update.getName(), aggregator.getSimpleName(), update.getReturnType().getSimpleName());
}
if (!Modifier.isPublic(method.getModifiers())) {
throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Aggregation method '%s' in %s must be public.", method.getName(), definition.getSimpleName());
}
if (!Modifier.isPublic(aggregator.getModifiers())) {
throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Aggregation class '%s' must be public.", aggregator.getSimpleName());
}
if (!Modifier.isPublic(update.getModifiers())) {
throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Aggregation update method '%s' in %s must be public.", method.getName(), aggregator.getSimpleName());
}
if (!Modifier.isPublic(result.getModifiers())) {
throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Aggregation result method '%s' in %s must be public.", method.getName(), aggregator.getSimpleName());
}
List<FieldSignature> inputSignature = inputSignatureDeterminer.signatureFor(update);
Class<?> returnType = result.getReturnType();
TypeMappers.NeoValueConverter valueConverter = typeMappers.converterFor(returnType);
MethodHandle creator = lookup.unreflect(method);
MethodHandle updateMethod = lookup.unreflect(update);
MethodHandle resultMethod = lookup.unreflect(result);
Optional<String> description = description(method);
UserAggregationFunction function = method.getAnnotation(UserAggregationFunction.class);
Optional<String> deprecated = deprecated(method, function::deprecatedBy, "Use of @UserAggregationFunction(deprecatedBy) without @Deprecated in " + funcName);
List<FieldInjections.FieldSetter> setters = allFieldInjections.setters(definition);
if (!config.fullAccessFor(funcName.toString())) {
try {
setters = safeFieldInjections.setters(definition);
} catch (ComponentInjectionException e) {
description = Optional.of(funcName.toString() + " is not available due to having restricted access rights, check configuration.");
log.warn(description.get());
UserFunctionSignature signature = new UserFunctionSignature(funcName, inputSignature, valueConverter.type(), deprecated, config.rolesFor(funcName.toString()), description);
return new FailedLoadAggregatedFunction(signature);
}
}
UserFunctionSignature signature = new UserFunctionSignature(funcName, inputSignature, valueConverter.type(), deprecated, config.rolesFor(funcName.toString()), description);
return new ReflectiveUserAggregationFunction(signature, constructor, creator, updateMethod, resultMethod, valueConverter, setters);
}
use of org.neo4j.kernel.api.proc.CallableUserAggregationFunction in project neo4j by neo4j.
the class ReflectiveUserAggregationFunctionTest method shouldInjectLogging.
@Test
public void shouldInjectLogging() throws KernelException {
// Given
Log log = spy(Log.class);
components.register(Log.class, (ctx) -> log);
CallableUserAggregationFunction function = procedureCompiler.compileAggregationFunction(LoggingFunction.class).get(0);
// When
CallableUserAggregationFunction.Aggregator aggregator = function.create(new BasicContext());
aggregator.update(new Object[] {});
aggregator.result();
// Then
verify(log).debug("1");
verify(log).info("2");
verify(log).warn("3");
verify(log).error("4");
}
use of org.neo4j.kernel.api.proc.CallableUserAggregationFunction in project neo4j by neo4j.
the class ReflectiveUserAggregationFunctionTest method shouldNotLoadNoneWhiteListedFunction.
@Test
public void shouldNotLoadNoneWhiteListedFunction() throws Throwable {
// Given
Log log = spy(Log.class);
procedureCompiler = new ReflectiveProcedureCompiler(new TypeMappers(), components, new ComponentRegistry(), log, new ProcedureConfig(Config.defaults().with(MapUtil.stringMap(GraphDatabaseSettings.procedure_whitelist.name(), "WrongName"))));
List<CallableUserAggregationFunction> method = compile(SingleAggregationFunction.class);
verify(log).warn("The function 'org.neo4j.kernel.impl.proc.collectCool' is not on the whitelist and won't be loaded.");
assertThat(method.size(), equalTo(0));
}
Aggregations