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;
}
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);
}
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();
}
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);
}
}
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);
}
}
}
Aggregations