Search in sources :

Example 11 with CallableProcedure

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

the class ReflectiveProcedureTest method shouldAllowVoidOutput.

@Test
public void shouldAllowVoidOutput() throws Throwable {
    // When
    CallableProcedure proc = compile(ProcedureWithVoidOutput.class).get(0);
    // Then
    assertEquals(0, proc.signature().outputSignature().size());
    assertFalse(proc.apply(null, new Object[0]).hasNext());
}
Also used : CallableProcedure(org.neo4j.kernel.api.proc.CallableProcedure) Test(org.junit.Test)

Example 12 with CallableProcedure

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

the class ReflectiveProcedureTest method shouldRunClassWithMultipleProceduresDeclared.

@Test
public void shouldRunClassWithMultipleProceduresDeclared() throws Throwable {
    // Given
    List<CallableProcedure> compiled = compile(MultiProcedureProcedure.class);
    CallableProcedure bananaPeople = compiled.get(0);
    CallableProcedure coolPeople = compiled.get(1);
    // When
    RawIterator<Object[], ProcedureException> coolOut = coolPeople.apply(new BasicContext(), new Object[0]);
    RawIterator<Object[], ProcedureException> bananaOut = bananaPeople.apply(new BasicContext(), new Object[0]);
    // Then
    assertThat(asList(coolOut), contains(new Object[] { "Bonnie" }, new Object[] { "Clyde" }));
    assertThat(asList(bananaOut), contains(new Object[] { "Jake", 18L }, new Object[] { "Pontus", 2L }));
}
Also used : BasicContext(org.neo4j.kernel.api.proc.BasicContext) CallableProcedure(org.neo4j.kernel.api.proc.CallableProcedure) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) Test(org.junit.Test)

Example 13 with CallableProcedure

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

the class ReflectiveProcedureTest method shouldRunSimpleReadOnlyProcedure.

@Test
public void shouldRunSimpleReadOnlyProcedure() throws Throwable {
    // Given
    CallableProcedure proc = compile(SingleReadOnlyProcedure.class).get(0);
    // When
    RawIterator<Object[], ProcedureException> out = proc.apply(new BasicContext(), new Object[0]);
    // Then
    assertThat(asList(out), contains(new Object[] { "Bonnie" }, new Object[] { "Clyde" }));
}
Also used : BasicContext(org.neo4j.kernel.api.proc.BasicContext) CallableProcedure(org.neo4j.kernel.api.proc.CallableProcedure) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) Test(org.junit.Test)

Example 14 with CallableProcedure

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

the class ProcedureRegistry method register.

/**
     * Register a new procedure.
     *
     * @param proc the procedure.
     */
public void register(CallableProcedure proc, boolean overrideCurrentImplementation) throws ProcedureException {
    ProcedureSignature signature = proc.signature();
    QualifiedName name = signature.name();
    String descriptiveName = signature.toString();
    validateSignature(descriptiveName, signature.inputSignature(), "input");
    validateSignature(descriptiveName, signature.outputSignature(), "output");
    if (!signature.isVoid() && signature.outputSignature().isEmpty()) {
        throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Procedures with zero output fields must be declared as VOID");
    }
    CallableProcedure oldImplementation = procedures.get(name);
    if (oldImplementation == null) {
        procedures.put(name, proc);
    } else {
        if (overrideCurrentImplementation) {
            procedures.put(name, proc);
        } else {
            throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Unable to register procedure, because the name `%s` is already in use.", name);
        }
    }
}
Also used : ProcedureSignature(org.neo4j.kernel.api.proc.ProcedureSignature) QualifiedName(org.neo4j.kernel.api.proc.QualifiedName) CallableProcedure(org.neo4j.kernel.api.proc.CallableProcedure) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException)

Example 15 with CallableProcedure

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

the class ReflectiveProcedureCompiler method compileProcedure.

private CallableProcedure compileProcedure(Class<?> procDefinition, MethodHandle constructor, Method method, Optional<String> warning, boolean fullAccess, QualifiedName procName) throws ProcedureException, IllegalAccessException {
    MethodHandle procedureMethod = lookup.unreflect(method);
    List<FieldSignature> inputSignature = inputSignatureDeterminer.signatureFor(method);
    OutputMapper outputMapper = outputMappers.mapper(method);
    Optional<String> description = description(method);
    Procedure procedure = method.getAnnotation(Procedure.class);
    Mode mode = procedure.mode();
    if (method.isAnnotationPresent(PerformsWrites.class)) {
        if (!procedure.mode().equals(org.neo4j.procedure.Mode.DEFAULT)) {
            throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Conflicting procedure annotation, cannot use PerformsWrites and mode");
        } else {
            mode = Mode.WRITE;
        }
    }
    Optional<String> deprecated = deprecated(method, procedure::deprecatedBy, "Use of @Procedure(deprecatedBy) without @Deprecated in " + procName);
    List<FieldInjections.FieldSetter> setters = allFieldInjections.setters(procDefinition);
    if (!fullAccess && !config.fullAccessFor(procName.toString())) {
        try {
            setters = safeFieldInjections.setters(procDefinition);
        } catch (ComponentInjectionException e) {
            description = Optional.of(procName.toString() + " is not available due to having restricted access rights, check configuration.");
            log.warn(description.get());
            ProcedureSignature signature = new ProcedureSignature(procName, inputSignature, outputMapper.signature(), Mode.DEFAULT, Optional.empty(), new String[0], description, warning);
            return new FailedLoadProcedure(signature);
        }
    }
    ProcedureSignature signature = new ProcedureSignature(procName, inputSignature, outputMapper.signature(), mode, deprecated, config.rolesFor(procName.toString()), description, warning);
    return new ReflectiveProcedure(signature, constructor, procedureMethod, outputMapper, setters);
}
Also used : FailedLoadProcedure(org.neo4j.kernel.api.proc.FailedLoadProcedure) Mode(org.neo4j.procedure.Mode) FieldSignature(org.neo4j.kernel.api.proc.FieldSignature) ProcedureSignature(org.neo4j.kernel.api.proc.ProcedureSignature) 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) ComponentInjectionException(org.neo4j.kernel.api.exceptions.ComponentInjectionException) OutputMapper(org.neo4j.kernel.impl.proc.OutputMappers.OutputMapper) MethodHandle(java.lang.invoke.MethodHandle)

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