Search in sources :

Example 16 with NarClassLoader

use of org.apache.pulsar.common.nar.NarClassLoader in project pulsar by apache.

the class WorkerServiceLoader method load.

/**
 * Load the worker service according to the worker service definition.
 *
 * @param metadata the worker service definition.
 * @return
 */
static WorkerServiceWithClassLoader load(WorkerServiceMetadata metadata, String narExtractionDirectory) throws IOException {
    final File narFile = metadata.getArchivePath().toAbsolutePath().toFile();
    NarClassLoader ncl = NarClassLoaderBuilder.builder().narFile(narFile).parentClassLoader(WorkerService.class.getClassLoader()).extractionDirectory(narExtractionDirectory).build();
    WorkerServiceDefinition phDef = getWorkerServiceDefinition(ncl);
    if (StringUtils.isBlank(phDef.getHandlerClass())) {
        throw new IOException("Functions Worker Service Nar Package `" + phDef.getName() + "` does NOT provide a functions worker service implementation");
    }
    try {
        Class handlerClass = ncl.loadClass(phDef.getHandlerClass());
        Object handler = handlerClass.getDeclaredConstructor().newInstance();
        if (!(handler instanceof WorkerService)) {
            throw new IOException("Class " + phDef.getHandlerClass() + " does not implement worker service interface");
        }
        WorkerService ph = (WorkerService) handler;
        return new WorkerServiceWithClassLoader(ph, ncl);
    } catch (Throwable t) {
        rethrowIOException(t);
        return null;
    }
}
Also used : NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) IOException(java.io.IOException) File(java.io.File) PulsarWorkerService(org.apache.pulsar.functions.worker.PulsarWorkerService) WorkerService(org.apache.pulsar.functions.worker.WorkerService)

Example 17 with NarClassLoader

use of org.apache.pulsar.common.nar.NarClassLoader in project pulsar by yahoo.

the class WorkerServiceLoader method load.

/**
 * Load the worker service according to the worker service definition.
 *
 * @param metadata the worker service definition.
 * @return
 */
static WorkerServiceWithClassLoader load(WorkerServiceMetadata metadata, String narExtractionDirectory) throws IOException {
    final File narFile = metadata.getArchivePath().toAbsolutePath().toFile();
    NarClassLoader ncl = NarClassLoaderBuilder.builder().narFile(narFile).parentClassLoader(WorkerService.class.getClassLoader()).extractionDirectory(narExtractionDirectory).build();
    WorkerServiceDefinition phDef = getWorkerServiceDefinition(ncl);
    if (StringUtils.isBlank(phDef.getHandlerClass())) {
        throw new IOException("Functions Worker Service Nar Package `" + phDef.getName() + "` does NOT provide a functions worker service implementation");
    }
    try {
        Class handlerClass = ncl.loadClass(phDef.getHandlerClass());
        Object handler = handlerClass.getDeclaredConstructor().newInstance();
        if (!(handler instanceof WorkerService)) {
            throw new IOException("Class " + phDef.getHandlerClass() + " does not implement worker service interface");
        }
        WorkerService ph = (WorkerService) handler;
        return new WorkerServiceWithClassLoader(ph, ncl);
    } catch (Throwable t) {
        rethrowIOException(t);
        return null;
    }
}
Also used : NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) IOException(java.io.IOException) File(java.io.File) PulsarWorkerService(org.apache.pulsar.functions.worker.PulsarWorkerService) WorkerService(org.apache.pulsar.functions.worker.WorkerService)

Example 18 with NarClassLoader

use of org.apache.pulsar.common.nar.NarClassLoader in project pulsar by yahoo.

the class FunctionUtils method getFunctionClass.

/**
 * Extract the Pulsar Function class from a functionctor archive.
 */
public static String getFunctionClass(ClassLoader classLoader) throws IOException {
    NarClassLoader ncl = (NarClassLoader) classLoader;
    String configStr = ncl.getServiceDefinition(PULSAR_IO_SERVICE_NAME);
    FunctionDefinition conf = ObjectMapperFactory.getThreadLocalYaml().readValue(configStr, FunctionDefinition.class);
    if (StringUtils.isEmpty(conf.getFunctionClass())) {
        throw new IOException(String.format("The '%s' functionctor does not provide a function implementation", conf.getName()));
    }
    try {
        // Try to load source class and check it implements Function interface
        Class functionClass = ncl.loadClass(conf.getFunctionClass());
        if (!(Function.class.isAssignableFrom(functionClass))) {
            throw new IOException("Class " + conf.getFunctionClass() + " does not implement interface " + Function.class.getName());
        }
    } catch (Throwable t) {
        Exceptions.rethrowIOException(t);
    }
    return conf.getFunctionClass();
}
Also used : NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) FunctionDefinition(org.apache.pulsar.common.functions.FunctionDefinition) UtilityClass(lombok.experimental.UtilityClass) IOException(java.io.IOException)

Example 19 with NarClassLoader

use of org.apache.pulsar.common.nar.NarClassLoader in project pulsar by yahoo.

the class ProtocolHandlerUtilsTest method testLoadProtocolHandler.

@Test
public void testLoadProtocolHandler() throws Exception {
    ProtocolHandlerDefinition def = new ProtocolHandlerDefinition();
    def.setHandlerClass(MockProtocolHandler.class.getName());
    def.setDescription("test-protocol-handler");
    String archivePath = "/path/to/protocol/handler/nar";
    ProtocolHandlerMetadata metadata = new ProtocolHandlerMetadata();
    metadata.setDefinition(def);
    metadata.setArchivePath(Paths.get(archivePath));
    NarClassLoader mockLoader = mock(NarClassLoader.class);
    when(mockLoader.getServiceDefinition(eq(PULSAR_PROTOCOL_HANDLER_DEFINITION_FILE))).thenReturn(ObjectMapperFactory.getThreadLocalYaml().writeValueAsString(def));
    Class handlerClass = MockProtocolHandler.class;
    when(mockLoader.loadClass(eq(MockProtocolHandler.class.getName()))).thenReturn(handlerClass);
    final NarClassLoaderBuilder mockedBuilder = mock(NarClassLoaderBuilder.class, RETURNS_SELF);
    when(mockedBuilder.build()).thenReturn(mockLoader);
    try (MockedStatic<NarClassLoaderBuilder> builder = Mockito.mockStatic(NarClassLoaderBuilder.class)) {
        builder.when(() -> NarClassLoaderBuilder.builder()).thenReturn(mockedBuilder);
        ProtocolHandlerWithClassLoader returnedPhWithCL = ProtocolHandlerUtils.load(metadata, "");
        ProtocolHandler returnedPh = returnedPhWithCL.getHandler();
        assertSame(mockLoader, returnedPhWithCL.getClassLoader());
        assertTrue(returnedPh instanceof MockProtocolHandler);
    }
}
Also used : NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) NarClassLoaderBuilder(org.apache.pulsar.common.nar.NarClassLoaderBuilder) Test(org.testng.annotations.Test)

Example 20 with NarClassLoader

use of org.apache.pulsar.common.nar.NarClassLoader in project pulsar by yahoo.

the class ProtocolHandlerWithClassLoaderTest method testWrapper.

@Test
public void testWrapper() throws Exception {
    ProtocolHandler h = mock(ProtocolHandler.class);
    NarClassLoader loader = mock(NarClassLoader.class);
    ProtocolHandlerWithClassLoader wrapper = new ProtocolHandlerWithClassLoader(h, loader);
    String protocol = "kafka";
    when(h.protocolName()).thenReturn(protocol);
    assertEquals(protocol, wrapper.protocolName());
    verify(h, times(1)).protocolName();
    when(h.accept(eq(protocol))).thenReturn(true);
    assertTrue(wrapper.accept(protocol));
    verify(h, times(1)).accept(same(protocol));
    ServiceConfiguration conf = new ServiceConfiguration();
    wrapper.initialize(conf);
    verify(h, times(1)).initialize(same(conf));
    BrokerService service = mock(BrokerService.class);
    wrapper.start(service);
    verify(h, times(1)).start(service);
    String protocolData = "test-protocol-data";
    when(h.getProtocolDataToAdvertise()).thenReturn(protocolData);
    assertEquals(protocolData, wrapper.getProtocolDataToAdvertise());
    verify(h, times(1)).getProtocolDataToAdvertise();
}
Also used : ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) BrokerService(org.apache.pulsar.broker.service.BrokerService) Test(org.testng.annotations.Test)

Aggregations

NarClassLoader (org.apache.pulsar.common.nar.NarClassLoader)126 Test (org.testng.annotations.Test)84 IOException (java.io.IOException)53 NarClassLoaderBuilder (org.apache.pulsar.common.nar.NarClassLoaderBuilder)48 File (java.io.File)39 FileInputStream (java.io.FileInputStream)15 UtilityClass (lombok.experimental.UtilityClass)15 Namespace (org.apache.distributedlog.api.namespace.Namespace)15 HashMap (java.util.HashMap)14 Map (java.util.Map)12 ServiceConfiguration (org.apache.pulsar.broker.ServiceConfiguration)12 Lists (com.google.common.collect.Lists)9 InputStream (java.io.InputStream)9 Files (java.nio.file.Files)9 Paths (java.nio.file.Paths)9 StandardCopyOption (java.nio.file.StandardCopyOption)9 LinkedList (java.util.LinkedList)9 List (java.util.List)9 Consumer (java.util.function.Consumer)9 Response (javax.ws.rs.core.Response)9