This closes #46

This commit is contained in:
Aldrin Piri 2015-05-01 10:08:56 -04:00
commit 393efc836e
3 changed files with 65 additions and 12 deletions

View File

@ -20,6 +20,7 @@ import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set;
import javax.xml.stream.FactoryConfigurationError; import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLOutputFactory;
@ -36,6 +37,7 @@ import org.apache.nifi.components.ConfigurableComponent;
import org.apache.nifi.components.PropertyDescriptor; import org.apache.nifi.components.PropertyDescriptor;
import org.apache.nifi.controller.ControllerService; import org.apache.nifi.controller.ControllerService;
import org.apache.nifi.documentation.DocumentationWriter; import org.apache.nifi.documentation.DocumentationWriter;
import org.apache.nifi.nar.ExtensionManager;
/** /**
* Generates HTML documentation for a ConfigurableComponent. This class is used * Generates HTML documentation for a ConfigurableComponent. This class is used
@ -148,9 +150,7 @@ public class HtmlDocumentationWriter implements DocumentationWriter {
xmlStreamWriter.writeCharacters(", "); xmlStreamWriter.writeCharacters(", ");
} }
final String link = "../" + linkedComponent.getCanonicalName() + "/index.html"; writeLinkForComponent(xmlStreamWriter, linkedComponent);
writeLink(xmlStreamWriter, linkedComponent.getSimpleName(), link);
++index; ++index;
} }
@ -434,12 +434,24 @@ public class HtmlDocumentationWriter implements DocumentationWriter {
} }
xmlStreamWriter.writeEndElement(); xmlStreamWriter.writeEndElement();
} else if (property.getControllerServiceDefinition() != null) { } else if (property.getControllerServiceDefinition() != null) {
Class<? extends ControllerService> controllerServiceClass = property Class<? extends ControllerService> controllerServiceClass = property.getControllerServiceDefinition();
.getControllerServiceDefinition();
writeSimpleElement(xmlStreamWriter, "strong", "Controller Service: "); writeSimpleElement(xmlStreamWriter, "strong", "Controller Service API: ");
xmlStreamWriter.writeEmptyElement("br"); xmlStreamWriter.writeEmptyElement("br");
xmlStreamWriter.writeCharacters(controllerServiceClass.getSimpleName()); xmlStreamWriter.writeCharacters(controllerServiceClass.getSimpleName());
final List<Class<? extends ControllerService>> implementations = lookupControllerServiceImpls(controllerServiceClass);
xmlStreamWriter.writeEmptyElement("br");
if (implementations.size() > 0) {
final String title = implementations.size() > 1 ? "Implementations: " : "Implementation:";
writeSimpleElement(xmlStreamWriter, "strong", title);
for (int i = 0; i < implementations.size(); i++) {
xmlStreamWriter.writeEmptyElement("br");
writeLinkForComponent(xmlStreamWriter, implementations.get(i));
}
} else {
xmlStreamWriter.writeCharacters("No implementations found.");
}
} }
} }
@ -519,4 +531,41 @@ public class HtmlDocumentationWriter implements DocumentationWriter {
xmlStreamWriter.writeCharacters(text); xmlStreamWriter.writeCharacters(text);
xmlStreamWriter.writeEndElement(); xmlStreamWriter.writeEndElement();
} }
/**
* Writes a link to another configurable component
*
* @param xmlStreamWriter the xml stream writer
* @param clazz the configurable component to link to
* @throws XMLStreamException thrown if there is a problem writing the XML
*/
protected void writeLinkForComponent(final XMLStreamWriter xmlStreamWriter, final Class<?> clazz) throws XMLStreamException {
writeLink(xmlStreamWriter, clazz.getSimpleName(), "../" + clazz.getCanonicalName() + "/index.html");
}
/**
* Uses the {@link ExtensionManager} to discover any {@link ControllerService} implementations that implement a specific
* ControllerService API.
*
* @param parent the controller service API
* @return a list of controller services that implement the controller service API
*/
private List<Class<? extends ControllerService>> lookupControllerServiceImpls(
final Class<? extends ControllerService> parent) {
final List<Class<? extends ControllerService>> implementations = new ArrayList<>();
// first get all ControllerService implementations
final Set<Class> controllerServices = ExtensionManager.getExtensions(ControllerService.class);
// then iterate over all controller services looking for any that is a child of the parent
// ControllerService API that was passed in as a parameter
for (final Class<? extends ControllerService> controllerServiceClass : controllerServices) {
if (parent.isAssignableFrom(controllerServiceClass)) {
implementations.add(controllerServiceClass);
}
}
return implementations;
}
} }

View File

@ -26,9 +26,9 @@ import org.apache.nifi.components.PropertyDescriptor;
import org.apache.nifi.controller.AbstractControllerService; import org.apache.nifi.controller.AbstractControllerService;
import org.apache.nifi.processor.util.StandardValidators; import org.apache.nifi.processor.util.StandardValidators;
@CapabilityDescription("A documented controller service that can help you do things") @CapabilityDescription("A documented controller service that can help you do things")
@Tags({"one", "two", "three"}) @Tags({"one", "two", "three"})
public class FullyDocumentedControllerService extends AbstractControllerService { public class FullyDocumentedControllerService extends AbstractControllerService implements SampleService{
public static final PropertyDescriptor KEYSTORE = new PropertyDescriptor.Builder().name("Keystore Filename") public static final PropertyDescriptor KEYSTORE = new PropertyDescriptor.Builder().name("Keystore Filename")
.description("The fully-qualified filename of the Keystore").defaultValue(null) .description("The fully-qualified filename of the Keystore").defaultValue(null)
@ -53,6 +53,10 @@ public class FullyDocumentedControllerService extends AbstractControllerService
@Override @Override
protected List<PropertyDescriptor> getSupportedPropertyDescriptors() { protected List<PropertyDescriptor> getSupportedPropertyDescriptors() {
return properties; return properties;
} }
@Override
public void doSomething() {
// TODO Auto-generated method stub
}
} }

View File

@ -30,7 +30,7 @@ import org.apache.nifi.documentation.mock.MockProcessorInitializationContext;
import org.junit.Test; import org.junit.Test;
public class ProcessorDocumentationWriterTest { public class ProcessorDocumentationWriterTest {
@Test @Test
public void testFullyDocumentedProcessor() throws IOException { public void testFullyDocumentedProcessor() throws IOException {
FullyDocumentedProcessor processor = new FullyDocumentedProcessor(); FullyDocumentedProcessor processor = new FullyDocumentedProcessor();
@ -59,7 +59,7 @@ public class ProcessorDocumentationWriterTest {
assertContains(results, FullyDocumentedProcessor.REL_SUCCESS.getDescription()); assertContains(results, FullyDocumentedProcessor.REL_SUCCESS.getDescription());
assertContains(results, FullyDocumentedProcessor.REL_FAILURE.getName()); assertContains(results, FullyDocumentedProcessor.REL_FAILURE.getName());
assertContains(results, FullyDocumentedProcessor.REL_FAILURE.getDescription()); assertContains(results, FullyDocumentedProcessor.REL_FAILURE.getDescription());
assertContains(results, "Controller Service: "); assertContains(results, "Controller Service API: ");
assertContains(results, "SampleService"); assertContains(results, "SampleService");
assertNotContains(results, "iconSecure.png"); assertNotContains(results, "iconSecure.png");
@ -97,6 +97,6 @@ public class ProcessorDocumentationWriterTest {
// relationships // relationships
assertContains(results, "This processor has no relationships."); assertContains(results, "This processor has no relationships.");
} }
} }