NIFI-632 using NarCloseable instead of NarThreadContextClassLoader

to make sure the right classloader is being used when initializing
a component for documentation generation
This commit is contained in:
danbress 2015-06-08 22:05:49 -04:00
parent 995a01636e
commit 196da2b443
5 changed files with 21 additions and 25 deletions

View File

@ -26,10 +26,6 @@
<groupId>org.apache.nifi</groupId> <groupId>org.apache.nifi</groupId>
<artifactId>nifi-api</artifactId> <artifactId>nifi-api</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-framework-core-api</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.apache.nifi</groupId> <groupId>org.apache.nifi</groupId>
<artifactId>nifi-properties</artifactId> <artifactId>nifi-properties</artifactId>

View File

@ -33,7 +33,6 @@ import org.apache.nifi.documentation.init.ControllerServiceInitializer;
import org.apache.nifi.documentation.init.ProcessorInitializer; import org.apache.nifi.documentation.init.ProcessorInitializer;
import org.apache.nifi.documentation.init.ReportingTaskingInitializer; import org.apache.nifi.documentation.init.ReportingTaskingInitializer;
import org.apache.nifi.nar.ExtensionManager; import org.apache.nifi.nar.ExtensionManager;
import org.apache.nifi.nar.NarThreadContextClassLoader;
import org.apache.nifi.processor.Processor; import org.apache.nifi.processor.Processor;
import org.apache.nifi.reporting.InitializationException; import org.apache.nifi.reporting.InitializationException;
import org.apache.nifi.reporting.ReportingTask; import org.apache.nifi.reporting.ReportingTask;
@ -69,25 +68,16 @@ public class DocGenerator {
logger.debug("Generating documentation for: " + extensionClasses.size() + " components in: " logger.debug("Generating documentation for: " + extensionClasses.size() + " components in: "
+ explodedNiFiDocsDir); + explodedNiFiDocsDir);
// save the original class loader and use a NarThreadContextClassLoader for (final Class<?> extensionClass : extensionClasses) {
final ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); if (ConfigurableComponent.class.isAssignableFrom(extensionClass)) {
try { final Class<? extends ConfigurableComponent> componentClass = extensionClass.asSubclass(ConfigurableComponent.class);
Thread.currentThread().setContextClassLoader(NarThreadContextClassLoader.getInstance()); try {
logger.debug("Documenting: " + componentClass);
for (final Class<?> extensionClass : extensionClasses) { document(explodedNiFiDocsDir, componentClass);
if (ConfigurableComponent.class.isAssignableFrom(extensionClass)) { } catch (Exception e) {
final Class<? extends ConfigurableComponent> componentClass = extensionClass logger.warn("Unable to document: " + componentClass, e);
.asSubclass(ConfigurableComponent.class);
try {
logger.debug("Documenting: " + componentClass);
document(explodedNiFiDocsDir, componentClass);
} catch (Exception e) {
logger.warn("Unable to document: " + componentClass, e);
}
} }
} }
} finally {
Thread.currentThread().setContextClassLoader(originalClassLoader);
} }
} }

View File

@ -20,6 +20,7 @@ import org.apache.nifi.components.ConfigurableComponent;
import org.apache.nifi.controller.ControllerService; import org.apache.nifi.controller.ControllerService;
import org.apache.nifi.documentation.ConfigurableComponentInitializer; import org.apache.nifi.documentation.ConfigurableComponentInitializer;
import org.apache.nifi.documentation.mock.MockControllerServiceInitializationContext; import org.apache.nifi.documentation.mock.MockControllerServiceInitializationContext;
import org.apache.nifi.nar.NarCloseable;
import org.apache.nifi.reporting.InitializationException; import org.apache.nifi.reporting.InitializationException;
/** /**
@ -33,6 +34,9 @@ public class ControllerServiceInitializer implements ConfigurableComponentInitia
@Override @Override
public void initialize(ConfigurableComponent component) throws InitializationException { public void initialize(ConfigurableComponent component) throws InitializationException {
ControllerService controllerService = (ControllerService) component; ControllerService controllerService = (ControllerService) component;
controllerService.initialize(new MockControllerServiceInitializationContext());
try (NarCloseable narCloseable = NarCloseable.withNarLoader()) {
controllerService.initialize(new MockControllerServiceInitializationContext());
}
} }
} }

View File

@ -19,6 +19,7 @@ package org.apache.nifi.documentation.init;
import org.apache.nifi.components.ConfigurableComponent; import org.apache.nifi.components.ConfigurableComponent;
import org.apache.nifi.documentation.ConfigurableComponentInitializer; import org.apache.nifi.documentation.ConfigurableComponentInitializer;
import org.apache.nifi.documentation.mock.MockProcessorInitializationContext; import org.apache.nifi.documentation.mock.MockProcessorInitializationContext;
import org.apache.nifi.nar.NarCloseable;
import org.apache.nifi.processor.Processor; import org.apache.nifi.processor.Processor;
/** /**
@ -31,7 +32,9 @@ public class ProcessorInitializer implements ConfigurableComponentInitializer {
@Override @Override
public void initialize(ConfigurableComponent component) { public void initialize(ConfigurableComponent component) {
Processor processor = (Processor) component; Processor processor = (Processor) component;
processor.initialize(new MockProcessorInitializationContext()); try (NarCloseable narCloseable = NarCloseable.withNarLoader()) {
processor.initialize(new MockProcessorInitializationContext());
}
} }
} }

View File

@ -19,6 +19,7 @@ package org.apache.nifi.documentation.init;
import org.apache.nifi.components.ConfigurableComponent; import org.apache.nifi.components.ConfigurableComponent;
import org.apache.nifi.documentation.ConfigurableComponentInitializer; import org.apache.nifi.documentation.ConfigurableComponentInitializer;
import org.apache.nifi.documentation.mock.MockReportingInitializationContext; import org.apache.nifi.documentation.mock.MockReportingInitializationContext;
import org.apache.nifi.nar.NarCloseable;
import org.apache.nifi.reporting.InitializationException; import org.apache.nifi.reporting.InitializationException;
import org.apache.nifi.reporting.ReportingTask; import org.apache.nifi.reporting.ReportingTask;
@ -32,6 +33,8 @@ public class ReportingTaskingInitializer implements ConfigurableComponentInitial
@Override @Override
public void initialize(ConfigurableComponent component) throws InitializationException { public void initialize(ConfigurableComponent component) throws InitializationException {
ReportingTask reportingTask = (ReportingTask) component; ReportingTask reportingTask = (ReportingTask) component;
reportingTask.initialize(new MockReportingInitializationContext()); try (NarCloseable narCloseable = NarCloseable.withNarLoader()) {
reportingTask.initialize(new MockReportingInitializationContext());
}
} }
} }