mattyb149 commented on a change in pull request #4509: URL: https://github.com/apache/nifi/pull/4509#discussion_r497069583
########## File path: nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-headless-server/src/main/java/org/apache/nifi/headless/HeadlessNiFiServer.java ########## @@ -0,0 +1,199 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.headless; + +import org.apache.nifi.NiFiServer; +import org.apache.nifi.admin.service.AuditService; +import org.apache.nifi.admin.service.impl.StandardAuditService; +import org.apache.nifi.authorization.AuthorizationRequest; +import org.apache.nifi.authorization.AuthorizationResult; +import org.apache.nifi.authorization.Authorizer; +import org.apache.nifi.authorization.AuthorizerConfigurationContext; +import org.apache.nifi.authorization.AuthorizerInitializationContext; +import org.apache.nifi.authorization.FlowParser; +import org.apache.nifi.authorization.exception.AuthorizationAccessException; +import org.apache.nifi.authorization.exception.AuthorizerCreationException; +import org.apache.nifi.authorization.exception.AuthorizerDestructionException; +import org.apache.nifi.bundle.Bundle; +import org.apache.nifi.controller.FlowController; +import org.apache.nifi.controller.StandardFlowService; +import org.apache.nifi.controller.flow.FlowManager; +import org.apache.nifi.controller.repository.FlowFileEventRepository; +import org.apache.nifi.controller.repository.metrics.RingBufferEventRepository; +import org.apache.nifi.diagnostics.DiagnosticsFactory; +import org.apache.nifi.encrypt.StringEncryptor; +import org.apache.nifi.events.VolatileBulletinRepository; +import org.apache.nifi.nar.ExtensionDiscoveringManager; +import org.apache.nifi.nar.ExtensionManagerHolder; +import org.apache.nifi.nar.ExtensionMapping; +import org.apache.nifi.nar.StandardExtensionDiscoveringManager; +import org.apache.nifi.registry.VariableRegistry; +import org.apache.nifi.registry.flow.StandardFlowRegistryClient; +import org.apache.nifi.registry.variable.FileBasedVariableRegistry; +import org.apache.nifi.reporting.BulletinRepository; +import org.apache.nifi.services.FlowService; +import org.apache.nifi.util.NiFiProperties; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; +import java.util.Set; + +/** + */ +public class HeadlessNiFiServer implements NiFiServer { + + private static final Logger logger = LoggerFactory.getLogger(HeadlessNiFiServer.class); + private NiFiProperties props; + private Bundle systemBundle; + private Set<Bundle> bundles; + private FlowService flowService; + + private static final String DEFAULT_SENSITIVE_PROPS_KEY = "nififtw!"; + + /** + * Default constructor + */ + public HeadlessNiFiServer() { + } + + public void start() { + try { + + // Create a standard extension manager and discover extensions + final ExtensionDiscoveringManager extensionManager = new StandardExtensionDiscoveringManager(); + extensionManager.discoverExtensions(systemBundle, bundles); + extensionManager.logClassLoaderMapping(); + + // Set the extension manager into the holder which makes it available to the Spring context via a factory bean + ExtensionManagerHolder.init(extensionManager); + + // Enrich the flow xml using the Extension Manager mapping + final FlowParser flowParser = new FlowParser(); + final FlowEnricher flowEnricher = new FlowEnricher(this, flowParser, props); + flowEnricher.enrichFlowWithBundleInformation(); + logger.info("Loading Flow..."); + + FlowFileEventRepository flowFileEventRepository = new RingBufferEventRepository(5); + AuditService auditService = new StandardAuditService(); + Authorizer authorizer = new Authorizer() { + @Override + public AuthorizationResult authorize(AuthorizationRequest request) throws AuthorizationAccessException { + return AuthorizationResult.approved(); + } + + @Override + public void initialize(AuthorizerInitializationContext initializationContext) throws AuthorizerCreationException { + // do nothing + } + + @Override + public void onConfigured(AuthorizerConfigurationContext configurationContext) throws AuthorizerCreationException { + // do nothing + } + + @Override + public void preDestruction() throws AuthorizerDestructionException { + // do nothing + } + }; + + final String sensitivePropAlgorithmVal = props.getProperty(StringEncryptor.NF_SENSITIVE_PROPS_ALGORITHM); + final String sensitivePropProviderVal = props.getProperty(StringEncryptor.NF_SENSITIVE_PROPS_PROVIDER); + final String sensitivePropValueNifiPropVar = props.getProperty(StringEncryptor.NF_SENSITIVE_PROPS_KEY, DEFAULT_SENSITIVE_PROPS_KEY); + + StringEncryptor encryptor = StringEncryptor.createEncryptor(sensitivePropAlgorithmVal, sensitivePropProviderVal, sensitivePropValueNifiPropVar); + VariableRegistry variableRegistry = new FileBasedVariableRegistry(props.getVariableRegistryPropertiesPaths()); + BulletinRepository bulletinRepository = new VolatileBulletinRepository(); + StandardFlowRegistryClient flowRegistryClient = new StandardFlowRegistryClient(); + flowRegistryClient.setProperties(props); + + FlowController flowController = FlowController.createStandaloneInstance( + flowFileEventRepository, + props, + authorizer, + auditService, + encryptor, + bulletinRepository, + variableRegistry, + flowRegistryClient, + extensionManager + ); + + flowService = StandardFlowService.createStandaloneInstance( + flowController, + props, + encryptor, + null, // revision manager + authorizer); + + // start and load the flow + flowService.start(); + flowService.load(null); + flowController.onFlowInitialized(true); + FlowManager flowManager = flowController.getFlowManager(); + flowManager.getGroup(flowManager.getRootGroupId()).startProcessing(); + + logger.info("Flow loaded successfully."); + } catch (Exception e) { + // ensure the flow service is terminated + if (flowService != null && flowService.isRunning()) { + flowService.stop(false); + } + startUpFailure(new Exception("Unable to load flow due to: " + e, e)); + } + } + + private void startUpFailure(Throwable t) { + System.err.println("Failed to start flow service: " + t.getMessage()); + System.err.println("Shutting down..."); + logger.warn("Failed to start headless server... shutting down.", t); + System.exit(1); + } + + @Override + public void initialize(NiFiProperties properties, Bundle systemBundle, Set<Bundle> bundles, ExtensionMapping extensionMapping) { + this.props = properties; + this.systemBundle = systemBundle; + this.bundles = bundles; + } + + public DiagnosticsFactory getDiagnosticsFactory() { + return null; + } + + public DiagnosticsFactory getThreadDumpFactory() { + return null; + } Review comment: I updated the PR to use the same diagnostics stuff that the JettyServer stuff uses, just had to create it explicitly (JettyServer gets its instance injected via Spring). ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
