This is an automated email from the ASF dual-hosted git repository.

heneveld pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brooklyn-ui.git


The following commit(s) were added to refs/heads/master by this push:
     new b1e557ec allow ui modules to be excluded from the registry
b1e557ec is described below

commit b1e557eca49d997151e3b3dc1f077df76a1cbaf5
Author: Alex Heneveld <[email protected]>
AuthorDate: Tue Jan 21 16:33:09 2025 +0000

    allow ui modules to be excluded from the registry
---
 .../ui/modularity/module/api/UiModule.java         |  8 ++-
 .../ui/modularity/module/api/UiModuleListener.java | 48 ++++++++---------
 .../module/api/internal/UiModuleImpl.java          | 14 +++++
 .../module/registry/UiModuleRegistryImpl.java      | 61 +++++++++++++++++++---
 4 files changed, 98 insertions(+), 33 deletions(-)

diff --git 
a/modularity-server/module-api/src/main/java/org/apache/brooklyn/ui/modularity/module/api/UiModule.java
 
b/modularity-server/module-api/src/main/java/org/apache/brooklyn/ui/modularity/module/api/UiModule.java
index 72ae70e6..3f64a0d4 100644
--- 
a/modularity-server/module-api/src/main/java/org/apache/brooklyn/ui/modularity/module/api/UiModule.java
+++ 
b/modularity-server/module-api/src/main/java/org/apache/brooklyn/ui/modularity/module/api/UiModule.java
@@ -28,7 +28,7 @@ public interface UiModule {
     int DEFAULT_ORDER = 10_000;
 
     /**
-     * @return The unique ID of the module
+     * @return The unique ID of the module, a UUID created by the framework
      */
     String getId();
 
@@ -81,6 +81,12 @@ public interface UiModule {
     default int getOrder(){
         return DEFAULT_ORDER;
     }
+
+    default String getBundleId(){
+        return null;
+    }
+
+
     public class Utils {
         public static UiModule copyUiModule(UiModule src) {
             return UiModuleImpl.copyOf(src);
diff --git 
a/modularity-server/module-api/src/main/java/org/apache/brooklyn/ui/modularity/module/api/UiModuleListener.java
 
b/modularity-server/module-api/src/main/java/org/apache/brooklyn/ui/modularity/module/api/UiModuleListener.java
index d07ad6b7..37975d48 100644
--- 
a/modularity-server/module-api/src/main/java/org/apache/brooklyn/ui/modularity/module/api/UiModuleListener.java
+++ 
b/modularity-server/module-api/src/main/java/org/apache/brooklyn/ui/modularity/module/api/UiModuleListener.java
@@ -18,6 +18,20 @@
  */
 package org.apache.brooklyn.ui.modularity.module.api;
 
+import org.apache.brooklyn.ui.modularity.module.api.internal.UiModuleImpl;
+import org.apache.karaf.web.WebBundle;
+import org.apache.karaf.web.WebContainerService;
+import org.ops4j.pax.web.service.spi.WebEvent;
+import org.ops4j.pax.web.service.spi.WebListener;
+import org.osgi.framework.*;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.yaml.snakeyaml.Yaml;
+
+import javax.annotation.Nullable;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
 import java.io.InputStream;
 import java.net.URL;
 import java.time.Duration;
@@ -28,24 +42,6 @@ import java.util.Map;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicReference;
 
-import javax.servlet.ServletContext;
-import javax.servlet.ServletContextEvent;
-import javax.servlet.ServletContextListener;
-
-import org.apache.brooklyn.ui.modularity.module.api.internal.UiModuleImpl;
-import org.apache.karaf.web.WebBundle;
-import org.apache.karaf.web.WebContainerService;
-import org.ops4j.pax.web.service.spi.WebEvent;
-import org.ops4j.pax.web.service.spi.WebListener;
-import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.FrameworkUtil;
-import org.osgi.framework.ServiceReference;
-import org.osgi.framework.ServiceRegistration;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.yaml.snakeyaml.Yaml;
-
 /** Invoked by modules in their web.xml to create and register the {@link 
UiModule} service for that UI module. */
 public class UiModuleListener implements ServletContextListener {
     
@@ -60,10 +56,10 @@ public class UiModuleListener implements 
ServletContextListener {
     }
 
     public void contextInitialized(ServletContextEvent servletContextEvent) {
-        final UiModule uiModule = 
createUiModule(servletContextEvent.getServletContext());
         Object moduleBundle = 
servletContextEvent.getServletContext().getAttribute("osgi-bundlecontext");
         final Bundle bundle = moduleBundle instanceof BundleContext ? 
((BundleContext)moduleBundle).getBundle() : 
FrameworkUtil.getBundle(this.getClass());
-        
+        final UiModule uiModule = 
createUiModule(servletContextEvent.getServletContext(), bundle);
+
         initWebListener(bundle);
         
         // register service against the bundle where it came from if possible 
(it always is, from what I've seen)
@@ -109,7 +105,7 @@ public class UiModuleListener implements 
ServletContextListener {
                     if (config!=null) {
                         LOG.trace("WebListener on deploying UI module 
"+event.getBundle().getSymbolicName()+" ["+event.getBundleId()+"] "
                             + "to "+event.getContextPath()+", checking whether 
any bundles need stopping");
-                        
stopAnyExistingOrSuperseded(createUiModule(config.openStream(), 
event.getContextPath()), event.getBundle());
+                        
stopAnyExistingOrSuperseded(createUiModule(config.openStream(), 
event.getContextPath(), event.getBundle()), event.getBundle());
                     }
                 }
             } catch (Exception e) {
@@ -247,19 +243,21 @@ public class UiModuleListener implements 
ServletContextListener {
         }
     }
 
-    private UiModule createUiModule(ServletContext servletContext) {
+    private UiModule createUiModule(ServletContext servletContext, @Nullable 
Bundle bundle) {
         final InputStream is = servletContext.getResourceAsStream(CONFIG_PATH);
         final String path = servletContext.getContextPath();
-        return createUiModule(is, path);
+        return createUiModule(is, path, bundle);
     }
 
-    protected UiModule createUiModule(final InputStream is, final String path) 
{
+    protected UiModule createUiModule(final InputStream is, final String path, 
@Nullable Bundle bundle) {
         if (is == null) {
             throw new RuntimeException(String.format("Module on path [%s] will 
not be registered as it does not have any configuration", path));
         }
         @SuppressWarnings("unchecked")
         Map<String, ?> config = (Map<String, ?>) new Yaml().load(is);
         LOG.debug("Creating Brooklyn UI module definition for "+path+"; 
"+config+" / "+is);
-        return UiModuleImpl.createFromMap(config).path(path);
+        UiModuleImpl ui = UiModuleImpl.createFromMap(config).path(path);
+        ui.bundleId(bundle.getSymbolicName()+":"+bundle.getVersion());
+        return ui;
     }
 }
diff --git 
a/modularity-server/module-api/src/main/java/org/apache/brooklyn/ui/modularity/module/api/internal/UiModuleImpl.java
 
b/modularity-server/module-api/src/main/java/org/apache/brooklyn/ui/modularity/module/api/internal/UiModuleImpl.java
index f06a7bc3..7022a9e9 100644
--- 
a/modularity-server/module-api/src/main/java/org/apache/brooklyn/ui/modularity/module/api/internal/UiModuleImpl.java
+++ 
b/modularity-server/module-api/src/main/java/org/apache/brooklyn/ui/modularity/module/api/internal/UiModuleImpl.java
@@ -42,6 +42,8 @@ public class UiModuleImpl implements UiModule {
     private String path;
     private List<UiModuleAction> actions = new ArrayList<>();
     private int order;
+    private String bundleId;
+
     public static UiModuleImpl copyOf(UiModule src) {
         final UiModuleImpl result = new UiModuleImpl();
         result.setId(src.getId());
@@ -234,4 +236,16 @@ public class UiModuleImpl implements UiModule {
         this.order=order;
         return this;
     }
+
+    public UiModuleImpl bundleId(final String bundleId) {
+        this.bundleId = bundleId;
+        return this;
+    }
+    public void setBundleId(final String bundleId) {
+        this.bundleId = bundleId;
+    }
+    @Override
+    public String getBundleId() {
+        return bundleId;
+    }
 }
diff --git 
a/modularity-server/module-registry/src/main/java/org/apache/brooklyn/ui/modularity/module/registry/UiModuleRegistryImpl.java
 
b/modularity-server/module-registry/src/main/java/org/apache/brooklyn/ui/modularity/module/registry/UiModuleRegistryImpl.java
index a34d32a5..9cce7f74 100644
--- 
a/modularity-server/module-registry/src/main/java/org/apache/brooklyn/ui/modularity/module/registry/UiModuleRegistryImpl.java
+++ 
b/modularity-server/module-registry/src/main/java/org/apache/brooklyn/ui/modularity/module/registry/UiModuleRegistryImpl.java
@@ -18,14 +18,22 @@
  */
 package org.apache.brooklyn.ui.modularity.module.registry;
 
-import java.util.Collection;
-import java.util.concurrent.ConcurrentHashMap;
-
+import com.google.common.io.CharStreams;
+import org.apache.brooklyn.ui.modularity.module.api.UiModule;
+import org.apache.brooklyn.ui.modularity.module.api.UiModuleRegistry;
+import org.apache.brooklyn.ui.modularity.module.api.internal.UiModuleImpl;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.yaml.snakeyaml.Yaml;
+import org.yaml.snakeyaml.events.Event;
 
-import org.apache.brooklyn.ui.modularity.module.api.UiModule;
-import org.apache.brooklyn.ui.modularity.module.api.UiModuleRegistry;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.nio.charset.Charset;
+import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
 
 public class UiModuleRegistryImpl implements UiModuleRegistry {
     private static final Logger LOG = 
LoggerFactory.getLogger(UiModuleRegistryImpl.class);
@@ -41,8 +49,47 @@ public class UiModuleRegistryImpl implements 
UiModuleRegistry {
             LOG.error("Skipping invalid Brooklyn UI module "+uiModule, new 
Throwable("source of error"));
             return;
         }
-        LOG.info("Registering new Brooklyn web component [{}] [{}]", 
uiModule.getId(), uiModule.getName());
-        registry.put(uiModule.getId(), uiModule);
+        if (isExcluded(uiModule)) {
+            LOG.info("Brooklyn web component [{}] [{}] is excluded from the 
registry in this deployment", uiModule.getId(), uiModule.getName());
+        } else {
+            LOG.info("Registering new Brooklyn web component [{}] [{}]", 
uiModule.getId(), uiModule.getName());
+            registry.put(uiModule.getId(), uiModule);
+        }
+    }
+
+    Map brooklynUiCfg;
+    public boolean isExcluded(UiModule uiModule) {
+        InputStream s = null;
+        try {
+            s = new URL("file:etc/brooklyn-ui.cfg").openStream();
+            if (s==null) return false;
+        } catch (IOException e) {
+            LOG.trace("No brooklyn-ui.cfg found. Module settings will use 
defaults.");
+        }
+        try {
+            if (brooklynUiCfg==null) {
+                brooklynUiCfg = new Yaml().load(s);
+                if (brooklynUiCfg==null) brooklynUiCfg = 
Collections.emptyMap();
+            }
+            String bundleId = uiModule.getBundleId();
+
+            if (bundleId==null) {
+                Boolean excludeNull = (Boolean) 
brooklynUiCfg.get("exclude_bundle_unset");
+                if (excludeNull == null) return false;
+                return excludeNull;
+            } else {
+                List<String> exclusions = (List<String>) 
brooklynUiCfg.get("exclude_bundle_regex");
+                if (exclusions != null) {
+                    for (String ex : exclusions) {
+                        if (bundleId.matches(ex)) return true;
+                    }
+                }
+            }
+        } catch (Exception e) {
+            LOG.warn("Invalid brooklyn-ui.cfg (ignoring): "+e, e);
+            if (brooklynUiCfg==null) brooklynUiCfg = Collections.emptyMap();
+        }
+        return false;
     }
 
     public void unregister(final UiModule uiModule) {

Reply via email to