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