bobpaulin commented on code in PR #10907:
URL: https://github.com/apache/nifi/pull/10907#discussion_r2818038102


##########
nifi-connector-mock-bundle/nifi-connector-mock-server/src/main/java/org/apache/nifi/mock/connector/server/StandardConnectorMockServer.java:
##########
@@ -401,8 +428,96 @@ public void mockControllerService(final String 
controllerServiceType, final Clas
         extensionManager.addControllerService(mockControllerServiceClass);
     }
 
+    @Override
+    public int getHttpPort() {
+        if (jettyServer == null) {
+            return -1;
+        }
+
+        final ServerConnector connector = (ServerConnector) 
jettyServer.getConnectors()[0];
+        return connector.getLocalPort();
+    }
+
     @Override
     public void close() {
         stop();
     }
+
+    private void startJettyServer() {
+        final String httpPortValue = 
nifiProperties.getProperty(NiFiProperties.WEB_HTTP_PORT);
+        if (httpPortValue == null || httpPortValue.isBlank()) {
+            logger.debug("No HTTP port configured; skipping Jetty server 
startup");
+            return;
+        }
+
+        final int httpPort = Integer.parseInt(httpPortValue);
+        final Map<File, Bundle> wars = findWars(bundles);
+        if (wars.isEmpty()) {
+            logger.debug("No WAR files found in NAR bundles; skipping Jetty 
server startup");
+            return;
+        }
+
+        jettyServer = new Server();
+
+        final ServerConnector serverConnector = new 
ServerConnector(jettyServer);
+        serverConnector.setPort(httpPort);
+        jettyServer.addConnector(serverConnector);
+
+        final List<WebAppContext> webAppContexts = new ArrayList<>();
+        final ContextHandlerCollection handlers = new 
ContextHandlerCollection();
+        for (final Map.Entry<File, Bundle> entry : wars.entrySet()) {
+            final File warFile = entry.getKey();
+            final Bundle bundle = entry.getValue();
+
+            final String warName = warFile.getName();
+            final String contextPath = "/" + warName.substring(0, 
warName.length() - WAR_EXTENSION.length());
+
+            final WebAppContext webAppContext = new 
WebAppContext(warFile.getPath(), contextPath);
+            webAppContext.setClassLoader(new 
org.eclipse.jetty.ee.webapp.WebAppClassLoader(bundle.getClassLoader(), 
webAppContext));
+
+            handlers.addHandler(webAppContext);
+            webAppContexts.add(webAppContext);
+            logger.info("Deploying WAR [{}] at context path [{}]", 
warFile.getAbsolutePath(), contextPath);
+        }
+
+        jettyServer.setHandler(handlers);
+
+        try {
+            jettyServer.start();
+            logger.info("Jetty server started on port [{}]", getHttpPort());
+        } catch (final Exception e) {
+            throw new RuntimeException("Failed to start Jetty server", e);
+        }
+
+        performInjectionForConnectorUis(webAppContexts);
+    }
+
+    private void performInjectionForConnectorUis(final List<WebAppContext> 
webAppContexts) {
+        final NiFiConnectorWebContext connectorWebContext = new 
MockNiFiConnectorWebContext(connectorRepository);
+        for (final WebAppContext webAppContext : webAppContexts) {
+            final ServletContext servletContext = 
webAppContext.getServletHandler().getServletContext();
+            servletContext.setAttribute("nifi-connector-web-context", 
connectorWebContext);
+            logger.info("Injected NiFiConnectorWebContext into WAR context 
[{}]", webAppContext.getContextPath());
+        }
+    }
+
+    private Map<File, Bundle> findWars(final Set<Bundle> bundles) {
+        final Map<File, Bundle> wars = new HashMap<>();
+
+        bundles.forEach(bundle -> {
+            final BundleDetails details = bundle.getBundleDetails();
+            final Path bundledDependencies = new 
File(details.getWorkingDirectory(), NAR_DEPENDENCIES_PATH).toPath();
+            if (Files.isDirectory(bundledDependencies)) {
+                try (final Stream<Path> dependencies = 
Files.list(bundledDependencies)) {
+                    dependencies.filter(dependency -> 
dependency.getFileName().toString().endsWith(WAR_EXTENSION))
+                            .map(Path::toFile)
+                            .forEach(dependency -> wars.put(dependency, 
bundle));

Review Comment:
   Ok so the idea would be to further filter the list of wars by nars that have 
a nifi-connector file.  That seems reasonable.



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to