use of org.apache.nifi.controller.ControllerService in project nifi by apache.
the class StandardControllerServiceProvider method createGhostControllerService.
private ControllerServiceNode createGhostControllerService(final String type, final String id, final BundleCoordinate bundleCoordinate) {
final ControllerServiceInvocationHandler invocationHandler = new ControllerServiceInvocationHandler() {
@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
final String methodName = method.getName();
if ("validate".equals(methodName)) {
final ValidationResult result = new ValidationResult.Builder().input("Any Property").subject("Missing Controller Service").valid(false).explanation("Controller Service could not be created because the Controller Service Type (" + type + ") could not be found").build();
return Collections.singleton(result);
} else if ("getPropertyDescriptor".equals(methodName)) {
final String propertyName = (String) args[0];
return new PropertyDescriptor.Builder().name(propertyName).description(propertyName).sensitive(true).required(true).build();
} else if ("getPropertyDescriptors".equals(methodName)) {
return Collections.emptyList();
} else if ("onPropertyModified".equals(methodName)) {
return null;
} else if ("getIdentifier".equals(methodName)) {
return id;
} else if ("toString".equals(methodName)) {
return "GhostControllerService[id=" + id + ", type=" + type + "]";
} else if ("hashCode".equals(methodName)) {
return 91 * type.hashCode() + 41 * id.hashCode();
} else if ("equals".equals(methodName)) {
return proxy == args[0];
} else {
throw new IllegalStateException("Controller Service could not be created because the Controller Service Type (" + type + ") could not be found");
}
}
@Override
public void setServiceNode(ControllerServiceNode serviceNode) {
// nothing to do
}
};
final ControllerService proxiedService = (ControllerService) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { ControllerService.class }, invocationHandler);
final String simpleClassName = type.contains(".") ? StringUtils.substringAfterLast(type, ".") : type;
final String componentType = "(Missing) " + simpleClassName;
final LoggableComponent<ControllerService> proxiedLoggableComponent = new LoggableComponent<>(proxiedService, bundleCoordinate, null);
final ComponentVariableRegistry componentVarRegistry = new StandardComponentVariableRegistry(this.variableRegistry);
final ControllerServiceNode serviceNode = new StandardControllerServiceNode(proxiedLoggableComponent, proxiedLoggableComponent, invocationHandler, id, new StandardValidationContextFactory(this, variableRegistry), this, componentType, type, componentVarRegistry, flowController, true);
serviceCache.putIfAbsent(id, serviceNode);
return serviceNode;
}
use of org.apache.nifi.controller.ControllerService in project nifi-minifi by apache.
the class ControllerServiceInitializer method teardown.
@Override
public void teardown(ConfigurableComponent component) {
try (NarCloseable narCloseable = NarCloseable.withComponentNarLoader(component.getClass(), component.getIdentifier())) {
ControllerService controllerService = (ControllerService) component;
final ComponentLog logger = new MockComponentLogger();
final MockConfigurationContext context = new MockConfigurationContext();
ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnShutdown.class, controllerService, logger, context);
} finally {
ExtensionManager.removeInstanceClassLoader(component.getIdentifier());
}
}
use of org.apache.nifi.controller.ControllerService in project nifi by apache.
the class StandardProcessGroup method removeControllerService.
@Override
public void removeControllerService(final ControllerServiceNode service) {
boolean removed = false;
writeLock.lock();
try {
final ControllerServiceNode existing = controllerServices.get(requireNonNull(service).getIdentifier());
if (existing == null) {
throw new IllegalStateException("ControllerService " + service.getIdentifier() + " is not a member of this Process Group");
}
service.verifyCanDelete();
try (final NarCloseable x = NarCloseable.withComponentNarLoader(service.getControllerServiceImplementation().getClass(), service.getIdentifier())) {
final ConfigurationContext configurationContext = new StandardConfigurationContext(service, controllerServiceProvider, null, variableRegistry);
ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnRemoved.class, service.getControllerServiceImplementation(), configurationContext);
}
for (final Map.Entry<PropertyDescriptor, String> entry : service.getProperties().entrySet()) {
final PropertyDescriptor descriptor = entry.getKey();
if (descriptor.getControllerServiceDefinition() != null) {
final String value = entry.getValue() == null ? descriptor.getDefaultValue() : entry.getValue();
if (value != null) {
final ControllerServiceNode referencedNode = getControllerService(value);
if (referencedNode != null) {
referencedNode.removeReference(service);
}
}
}
}
controllerServices.remove(service.getIdentifier());
onComponentModified();
// For any component that references this Controller Service, find the component's Process Group
// and notify the Process Group that a component has been modified. This way, we know to re-calculate
// whether or not the Process Group has local modifications.
service.getReferences().getReferencingComponents().stream().map(ConfiguredComponent::getProcessGroupIdentifier).filter(id -> !id.equals(getIdentifier())).forEach(groupId -> {
final ProcessGroup descendant = findProcessGroup(groupId);
if (descendant != null) {
descendant.onComponentModified();
}
});
flowController.getStateManagerProvider().onComponentRemoved(service.getIdentifier());
removed = true;
LOG.info("{} removed from {}", service, this);
} finally {
if (removed) {
try {
ExtensionManager.removeInstanceClassLoader(service.getIdentifier());
} catch (Throwable t) {
}
}
writeLock.unlock();
}
}
use of org.apache.nifi.controller.ControllerService in project nifi by apache.
the class NiFiRegistryFlowMapper method mapControllerServiceApis.
private List<ControllerServiceAPI> mapControllerServiceApis(final ControllerServiceNode service) {
final Class<?> serviceClass = service.getControllerServiceImplementation().getClass();
final Set<Class<?>> serviceApiClasses = new HashSet<>();
// get all of it's interfaces to determine the controller service api's it implements
final List<Class<?>> interfaces = ClassUtils.getAllInterfaces(serviceClass);
for (final Class<?> i : interfaces) {
// add all controller services that's not ControllerService itself
if (ControllerService.class.isAssignableFrom(i) && !ControllerService.class.equals(i)) {
serviceApiClasses.add(i);
}
}
final List<ControllerServiceAPI> serviceApis = new ArrayList<>();
for (final Class<?> serviceApiClass : serviceApiClasses) {
final BundleCoordinate bundleCoordinate = ExtensionManager.getBundle(serviceApiClass.getClassLoader()).getBundleDetails().getCoordinate();
final ControllerServiceAPI serviceApi = new ControllerServiceAPI();
serviceApi.setType(serviceApiClass.getName());
serviceApi.setBundle(mapBundle(bundleCoordinate));
serviceApis.add(serviceApi);
}
return serviceApis;
}
use of org.apache.nifi.controller.ControllerService in project nifi by apache.
the class ControllerServiceInitializer method initialize.
@Override
public void initialize(ConfigurableComponent component) throws InitializationException {
ControllerService controllerService = (ControllerService) component;
ControllerServiceInitializationContext context = new MockControllerServiceInitializationContext();
try (NarCloseable narCloseable = NarCloseable.withComponentNarLoader(component.getClass(), context.getIdentifier())) {
controllerService.initialize(context);
}
}
Aggregations