Search in sources :

Example 16 with CallableProcedure

use of org.neo4j.kernel.api.proc.CallableProcedure in project neo4j by neo4j.

the class ReflectiveProcedureCompiler method compileProcedure.

List<CallableProcedure> compileProcedure(Class<?> procDefinition, Optional<String> warning, boolean fullAccess) throws KernelException {
    try {
        List<Method> procedureMethods = Arrays.stream(procDefinition.getDeclaredMethods()).filter(m -> m.isAnnotationPresent(Procedure.class)).collect(Collectors.toList());
        if (procedureMethods.isEmpty()) {
            return emptyList();
        }
        MethodHandle constructor = constructor(procDefinition);
        ArrayList<CallableProcedure> out = new ArrayList<>(procedureMethods.size());
        for (Method method : procedureMethods) {
            String valueName = method.getAnnotation(Procedure.class).value();
            String definedName = method.getAnnotation(Procedure.class).name();
            QualifiedName procName = extractName(procDefinition, method, valueName, definedName);
            if (fullAccess || config.isWhitelisted(procName.toString())) {
                out.add(compileProcedure(procDefinition, constructor, method, warning, fullAccess, procName));
            } else {
                log.warn(String.format("The procedure '%s' is not on the whitelist and won't be loaded.", procName.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 procedure defined in `%s`: %s", procDefinition.getSimpleName(), e.getMessage());
    }
}
Also used : FailedLoadProcedure(org.neo4j.kernel.api.proc.FailedLoadProcedure) Mode(org.neo4j.procedure.Mode) MethodHandle(java.lang.invoke.MethodHandle) Arrays(java.util.Arrays) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) Log(org.neo4j.logging.Log) RawIterator(org.neo4j.collection.RawIterator) CallableProcedure(org.neo4j.kernel.api.proc.CallableProcedure) UserAggregationResult(org.neo4j.procedure.UserAggregationResult) Status(org.neo4j.kernel.api.exceptions.Status) UserAggregationUpdate(org.neo4j.procedure.UserAggregationUpdate) FailedLoadFunction(org.neo4j.kernel.api.proc.FailedLoadFunction) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Iterators.asRawIterator(org.neo4j.helpers.collection.Iterators.asRawIterator) FailedLoadAggregatedFunction(org.neo4j.kernel.api.proc.FailedLoadAggregatedFunction) ProcedureSignature(org.neo4j.kernel.api.proc.ProcedureSignature) CallableUserAggregationFunction(org.neo4j.kernel.api.proc.CallableUserAggregationFunction) Procedure(org.neo4j.procedure.Procedure) Method(java.lang.reflect.Method) ComponentInjectionException(org.neo4j.kernel.api.exceptions.ComponentInjectionException) CallableUserFunction(org.neo4j.kernel.api.proc.CallableUserFunction) Iterator(java.util.Iterator) UserFunctionSignature(org.neo4j.kernel.api.proc.UserFunctionSignature) Collections.emptyList(java.util.Collections.emptyList) MethodHandles(java.lang.invoke.MethodHandles) Description(org.neo4j.procedure.Description) Collections.emptyIterator(java.util.Collections.emptyIterator) Collectors(java.util.stream.Collectors) KernelException(org.neo4j.kernel.api.exceptions.KernelException) FieldSignature(org.neo4j.kernel.api.proc.FieldSignature) UserFunction(org.neo4j.procedure.UserFunction) List(java.util.List) Stream(java.util.stream.Stream) UserAggregationFunction(org.neo4j.procedure.UserAggregationFunction) QualifiedName(org.neo4j.kernel.api.proc.QualifiedName) Modifier(java.lang.reflect.Modifier) Optional(java.util.Optional) PerformsWrites(org.neo4j.procedure.PerformsWrites) Comparator(java.util.Comparator) Context(org.neo4j.kernel.api.proc.Context) OutputMapper(org.neo4j.kernel.impl.proc.OutputMappers.OutputMapper) QualifiedName(org.neo4j.kernel.api.proc.QualifiedName) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) ComponentInjectionException(org.neo4j.kernel.api.exceptions.ComponentInjectionException) KernelException(org.neo4j.kernel.api.exceptions.KernelException) CallableProcedure(org.neo4j.kernel.api.proc.CallableProcedure) FailedLoadProcedure(org.neo4j.kernel.api.proc.FailedLoadProcedure) CallableProcedure(org.neo4j.kernel.api.proc.CallableProcedure) Procedure(org.neo4j.procedure.Procedure) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) KernelException(org.neo4j.kernel.api.exceptions.KernelException) MethodHandle(java.lang.invoke.MethodHandle)

Example 17 with CallableProcedure

use of org.neo4j.kernel.api.proc.CallableProcedure in project neo4j by neo4j.

the class ReflectiveProcedureTest method shouldGiveHelpfulErrorOnNullMessageException.

@Test
public void shouldGiveHelpfulErrorOnNullMessageException() throws Throwable {
    // Given
    CallableProcedure proc = compile(ProcedureThatThrowsNullMsgExceptionAtInvocation.class).get(0);
    // Expect
    exception.expect(ProcedureException.class);
    exception.expectMessage("Failed to invoke procedure `org.neo4j.kernel.impl.proc.throwsAtInvocation`: " + "Caused by: java.lang.IndexOutOfBoundsException");
    // When
    proc.apply(new BasicContext(), new Object[0]);
}
Also used : BasicContext(org.neo4j.kernel.api.proc.BasicContext) CallableProcedure(org.neo4j.kernel.api.proc.CallableProcedure) Test(org.junit.Test)

Example 18 with CallableProcedure

use of org.neo4j.kernel.api.proc.CallableProcedure in project neo4j by neo4j.

the class ReflectiveProcedureTest method shouldAllowNonStaticOutput.

@Test
public void shouldAllowNonStaticOutput() throws Throwable {
    // When
    CallableProcedure proc = compile(ProcedureWithNonStaticOutputRecord.class).get(0);
    // Then
    assertEquals(1, proc.signature().outputSignature().size());
}
Also used : CallableProcedure(org.neo4j.kernel.api.proc.CallableProcedure) Test(org.junit.Test)

Example 19 with CallableProcedure

use of org.neo4j.kernel.api.proc.CallableProcedure in project neo4j by neo4j.

the class ReflectiveProcedureTest method shouldAllowOverridingProcedureNameWithoutNamespace.

@Test
public void shouldAllowOverridingProcedureNameWithoutNamespace() throws Throwable {
    // When
    CallableProcedure proc = compile(ProcedureWithSingleName.class).get(0);
    // Then
    assertEquals("singleName", proc.signature().name().toString());
}
Also used : CallableProcedure(org.neo4j.kernel.api.proc.CallableProcedure) Test(org.junit.Test)

Example 20 with CallableProcedure

use of org.neo4j.kernel.api.proc.CallableProcedure in project neo4j by neo4j.

the class ReflectiveProcedureTest method shouldInjectLogging.

@Test
public void shouldInjectLogging() throws KernelException {
    // Given
    Log log = spy(Log.class);
    components.register(Log.class, (ctx) -> log);
    CallableProcedure procedure = procedureCompiler.compileProcedure(LoggingProcedure.class, Optional.empty(), true).get(0);
    // When
    procedure.apply(new BasicContext(), new Object[0]);
    // Then
    verify(log).debug("1");
    verify(log).info("2");
    verify(log).warn("3");
    verify(log).error("4");
}
Also used : Log(org.neo4j.logging.Log) NullLog(org.neo4j.logging.NullLog) BasicContext(org.neo4j.kernel.api.proc.BasicContext) CallableProcedure(org.neo4j.kernel.api.proc.CallableProcedure) Test(org.junit.Test)

Aggregations

CallableProcedure (org.neo4j.kernel.api.proc.CallableProcedure)26 Test (org.junit.Test)22 BasicContext (org.neo4j.kernel.api.proc.BasicContext)15 ProcedureException (org.neo4j.kernel.api.exceptions.ProcedureException)10 ProcedureSignature (org.neo4j.kernel.api.proc.ProcedureSignature)7 Log (org.neo4j.logging.Log)7 NullLog (org.neo4j.logging.NullLog)6 URL (java.net.URL)4 CallableUserFunction (org.neo4j.kernel.api.proc.CallableUserFunction)3 MethodHandle (java.lang.invoke.MethodHandle)2 ComponentInjectionException (org.neo4j.kernel.api.exceptions.ComponentInjectionException)2 CallableUserAggregationFunction (org.neo4j.kernel.api.proc.CallableUserAggregationFunction)2 FailedLoadProcedure (org.neo4j.kernel.api.proc.FailedLoadProcedure)2 FieldSignature (org.neo4j.kernel.api.proc.FieldSignature)2 QualifiedName (org.neo4j.kernel.api.proc.QualifiedName)2 UserFunctionSignature (org.neo4j.kernel.api.proc.UserFunctionSignature)2 OutputMapper (org.neo4j.kernel.impl.proc.OutputMappers.OutputMapper)2 Mode (org.neo4j.procedure.Mode)2 Procedure (org.neo4j.procedure.Procedure)2 MethodHandles (java.lang.invoke.MethodHandles)1