Search in sources :

Example 36 with NarClassLoader

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

the class OffloaderUtils method searchForOffloaders.

public static Offloaders searchForOffloaders(String offloadersPath, String narExtractionDirectory) throws IOException {
    Path path = Paths.get(offloadersPath).toAbsolutePath();
    log.info("Searching for offloaders in {}", path);
    Offloaders offloaders = new Offloaders();
    if (!path.toFile().exists()) {
        log.warn("Offloaders archive directory not found");
        return offloaders;
    }
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(path, "*.nar")) {
        stream.forEach(archive -> {
            try {
                OffloaderDefinition definition = getOffloaderDefinition(archive.toString(), narExtractionDirectory);
                log.info("Found offloader {} from {}", definition, archive);
                if (!StringUtils.isEmpty(definition.getOffloaderFactoryClass())) {
                    // Validate offloader factory class to be present and of the right type
                    Pair<NarClassLoader, LedgerOffloaderFactory> offloaderFactoryPair = getOffloaderFactory(archive.toString(), narExtractionDirectory);
                    if (null != offloaderFactoryPair) {
                        offloaders.getOffloaders().add(offloaderFactoryPair);
                    }
                }
            } catch (Throwable t) {
                log.warn("Failed to load offloader from {}", archive, t);
            }
        });
    }
    log.info("Found and loaded {} offloaders", offloaders.getOffloaders().size());
    return offloaders;
}
Also used : Path(java.nio.file.Path) LedgerOffloaderFactory(org.apache.bookkeeper.mledger.LedgerOffloaderFactory) NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader)

Example 37 with NarClassLoader

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

the class AdditionalServletWithClassLoaderTest method testClassLoaderSwitcher.

@Test
public void testClassLoaderSwitcher() throws Exception {
    NarClassLoader narLoader = mock(NarClassLoader.class);
    AdditionalServlet servlet = new AdditionalServlet() {

        @Override
        public void loadConfig(PulsarConfiguration pulsarConfiguration) {
            assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
        }

        @Override
        public String getBasePath() {
            assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
            return "base-path";
        }

        @Override
        public ServletHolder getServletHolder() {
            assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
            return null;
        }

        @Override
        public void close() {
            assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
        }
    };
    AdditionalServletWithClassLoader additionalServletWithClassLoader = new AdditionalServletWithClassLoader(servlet, narLoader);
    ClassLoader curClassLoader = Thread.currentThread().getContextClassLoader();
    // test class loader
    assertEquals(additionalServletWithClassLoader.getClassLoader(), narLoader);
    // test getBasePath
    assertEquals(additionalServletWithClassLoader.getBasePath(), "base-path");
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    // test loadConfig
    ServiceConfiguration conf = new ServiceConfiguration();
    additionalServletWithClassLoader.loadConfig(conf);
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    // test getServletHolder
    assertNull(additionalServletWithClassLoader.getServletHolder());
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    // test getServlet
    assertEquals(additionalServletWithClassLoader.getServlet(), servlet);
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    // test close
    additionalServletWithClassLoader.close();
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
}
Also used : ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) PulsarConfiguration(org.apache.pulsar.common.configuration.PulsarConfiguration) Test(org.testng.annotations.Test)

Example 38 with NarClassLoader

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

the class AdditionalServletWithClassLoaderTest method testWrapper.

@Test
public void testWrapper() {
    AdditionalServlet servlet = mock(AdditionalServlet.class);
    NarClassLoader loader = mock(NarClassLoader.class);
    AdditionalServletWithClassLoader wrapper = new AdditionalServletWithClassLoader(servlet, loader);
    // test getBasePath
    String basePath = "bathPath";
    when(servlet.getBasePath()).thenReturn(basePath);
    assertEquals(basePath, wrapper.getBasePath());
    verify(servlet, times(1)).getBasePath();
    // test loadConfig
    ServiceConfiguration conf = new ServiceConfiguration();
    wrapper.loadConfig(conf);
    verify(servlet, times(1)).loadConfig(same(conf));
    // test getServlet
    assertEquals(wrapper.getServlet(), servlet);
    // test getServletHolder
    ServletHolder servletHolder = new ServletHolder();
    when(servlet.getServletHolder()).thenReturn(servletHolder);
    assertEquals(wrapper.getServletHolder(), servletHolder);
    verify(servlet, times(1)).getServletHolder();
}
Also used : ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) Test(org.testng.annotations.Test)

Example 39 with NarClassLoader

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

the class AdditionalServletWithPulsarServiceTest method testLoadAdditionalServlet.

@Test
public void testLoadAdditionalServlet() throws Exception {
    AdditionalServletDefinition def = new AdditionalServletDefinition();
    def.setAdditionalServletClass(MockAdditionalServletWithClassLoader.class.getName());
    def.setDescription("test-additional-servlet");
    String archivePath = "/path/to/additional/servlet/nar";
    AdditionalServletMetadata metadata = new AdditionalServletMetadata();
    metadata.setDefinition(def);
    metadata.setArchivePath(Paths.get(archivePath));
    NarClassLoader mockLoader = mock(NarClassLoader.class);
    when(mockLoader.getServiceDefinition(eq(AdditionalServletUtils.ADDITIONAL_SERVLET_FILE))).thenReturn(ObjectMapperFactory.getThreadLocalYaml().writeValueAsString(def));
    Class additionalServletClass = MockAdditionalServletWithClassLoader.class;
    when(mockLoader.loadClass(eq(MockAdditionalServletWithClassLoader.class.getName()))).thenReturn(additionalServletClass);
    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);
        AdditionalServletWithClassLoader returnedASWithCL = AdditionalServletUtils.load(metadata, "");
        AdditionalServlet returnedPh = returnedASWithCL.getServlet();
        assertSame(mockLoader, returnedASWithCL.getClassLoader());
        assertTrue(returnedPh instanceof MockAdditionalServletWithClassLoader);
    }
}
Also used : NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) NarClassLoaderBuilder(org.apache.pulsar.common.nar.NarClassLoaderBuilder) Test(org.testng.annotations.Test)

Example 40 with NarClassLoader

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

the class EntryFilterProviderTest method testReadYamlFile.

@Test
public void testReadYamlFile() throws IOException {
    try (NarClassLoader cl = mock(NarClassLoader.class)) {
        when(cl.getServiceDefinition(ENTRY_FILTER_DEFINITION_FILE + ".yaml")).thenThrow(new NoSuchFileException(""));
        try {
            EntryFilterProvider.getEntryFilterDefinition(cl);
            Assert.fail();
        } catch (Exception e) {
            Assert.assertFalse(e instanceof NoSuchFileException);
        }
    }
    try (NarClassLoader cl = mock(NarClassLoader.class)) {
        when(cl.getServiceDefinition(ENTRY_FILTER_DEFINITION_FILE + ".yml")).thenThrow(new NoSuchFileException(""));
        try {
            EntryFilterProvider.getEntryFilterDefinition(cl);
            Assert.fail();
        } catch (Exception e) {
            Assert.assertFalse(e instanceof NoSuchFileException);
        }
    }
    try (NarClassLoader cl = mock(NarClassLoader.class)) {
        when(cl.getServiceDefinition(ENTRY_FILTER_DEFINITION_FILE + ".yaml")).thenThrow(new NoSuchFileException(""));
        when(cl.getServiceDefinition(ENTRY_FILTER_DEFINITION_FILE + ".yml")).thenThrow(new NoSuchFileException(""));
        try {
            EntryFilterProvider.getEntryFilterDefinition(cl);
            Assert.fail();
        } catch (Exception e) {
            Assert.assertTrue(e instanceof NoSuchFileException);
        }
    }
}
Also used : NoSuchFileException(java.nio.file.NoSuchFileException) NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException) 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