You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by jb...@apache.org on 2021/06/08 13:11:54 UTC

[karaf] branch main updated: KARAF-7165 - config:meta doesn't work for factory PIDs

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

jbonofre pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/karaf.git


The following commit(s) were added to refs/heads/main by this push:
     new 722e98a  KARAF-7165 - config:meta doesn't work for factory PIDs
     new ba5d09b  Merge pull request #1384 from jassuncao/KARAF-7165
722e98a is described below

commit 722e98afe79148e02aa2362bc32c087de8b31297
Author: jassuncao <jo...@exploitsys.com>
AuthorDate: Wed May 26 23:27:12 2021 +0100

    KARAF-7165 - config:meta doesn't work for factory PIDs
    
    Fixes MetaCommand.getMetatype to also search for the desired PID in the
    list of factory pids of MetaTypeInformation. The command is now able to
    create default configurations for factory PIDs.
    
    Removes a few unnecessary @SuppressWarnings in config:* commands
    
    
    Change-Id: I3336e5f54b88c64c01459034f672f844c4eb76d8
---
 .../karaf/config/command/ConfigCommandSupport.java |  1 -
 .../apache/karaf/config/command/EditCommand.java   |  3 +-
 .../apache/karaf/config/command/MetaCommand.java   | 59 ++++++++++++++++------
 .../apache/karaf/config/command/UpdateCommand.java |  1 -
 4 files changed, 44 insertions(+), 20 deletions(-)

diff --git a/config/src/main/java/org/apache/karaf/config/command/ConfigCommandSupport.java b/config/src/main/java/org/apache/karaf/config/command/ConfigCommandSupport.java
index fec1dd4..0fade0c 100644
--- a/config/src/main/java/org/apache/karaf/config/command/ConfigCommandSupport.java
+++ b/config/src/main/java/org/apache/karaf/config/command/ConfigCommandSupport.java
@@ -51,7 +51,6 @@ public abstract class ConfigCommandSupport implements Action {
 
     protected abstract Object doExecute() throws Exception;
 
-    @SuppressWarnings("rawtypes")
     protected TypedProperties getEditedProps() throws Exception {
         return (TypedProperties) this.session.get(PROPERTY_CONFIG_PROPS);
     }
diff --git a/config/src/main/java/org/apache/karaf/config/command/EditCommand.java b/config/src/main/java/org/apache/karaf/config/command/EditCommand.java
index dd522b6..5ecfcc3 100644
--- a/config/src/main/java/org/apache/karaf/config/command/EditCommand.java
+++ b/config/src/main/java/org/apache/karaf/config/command/EditCommand.java
@@ -45,8 +45,7 @@ public class EditCommand extends ConfigCommandSupport {
     @Option(name = "--type", aliases = {}, description = "Specifies the configuration storage type (cfg or json).", required = false, multiValued = false)
     String suffix;
 
-    @Override
-    @SuppressWarnings("rawtypes")
+    @Override    
     protected Object doExecute() throws Exception {
         String oldPid = (String) this.session.get(PROPERTY_CONFIG_PID);
         if (oldPid != null && !oldPid.equals(pid) && !force) {
diff --git a/config/src/main/java/org/apache/karaf/config/command/MetaCommand.java b/config/src/main/java/org/apache/karaf/config/command/MetaCommand.java
index 0662420..3687bb7 100644
--- a/config/src/main/java/org/apache/karaf/config/command/MetaCommand.java
+++ b/config/src/main/java/org/apache/karaf/config/command/MetaCommand.java
@@ -37,19 +37,16 @@ import org.apache.karaf.shell.support.table.ShellTable;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
 import org.osgi.service.metatype.AttributeDefinition;
 import org.osgi.service.metatype.MetaTypeInformation;
 import org.osgi.service.metatype.MetaTypeService;
 import org.osgi.service.metatype.ObjectClassDefinition;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 @Command(scope = "config", name = "meta", description = "Lists meta type information.")
 @Service
 public class MetaCommand extends ConfigCommandSupport {
 
-    private static final Logger LOG = LoggerFactory.getLogger(MetaCommand.class);
-
     @Argument(name = "pid", description = "The configuration pid", required = true, multiValued = false)
     @Completion(MetaCompleter.class)
     protected String pid;
@@ -116,7 +113,7 @@ public class MetaCommand extends ConfigCommandSupport {
             return result.toString();
         }
 
-        protected ObjectClassDefinition getMetatype(MetaTypeService metaTypeService, String pid) {
+        protected MetaInfo getMetatype(MetaTypeService metaTypeService, String pid) {
             if (metaTypeService != null) {
                 for (Bundle bundle : context.getBundles()) {
                     MetaTypeInformation info = metaTypeService.getMetaTypeInformation(bundle);
@@ -126,7 +123,13 @@ public class MetaCommand extends ConfigCommandSupport {
                     String[] pids = info.getPids();
                     for (String cPid : pids) {
                         if (cPid.equals(pid)) {
-                            return info.getObjectClassDefinition(cPid, null);
+                            return new MetaInfo(info.getObjectClassDefinition(cPid, null), false);
+                        }
+                    }
+                    pids = info.getFactoryPids();
+                    for (String cPid : pids) {
+                        if (cPid.equals(pid)) {
+                            return new MetaInfo(info.getObjectClassDefinition(cPid, null), true);
                         }
                     }
                 }
@@ -138,26 +141,33 @@ public class MetaCommand extends ConfigCommandSupport {
     class Create extends AbstractMeta {
 
         public Void apply(MetaTypeService metaTypeService) {
-            ObjectClassDefinition def = getMetatype(metaTypeService, pid);
-            if (def == null) {
+            MetaInfo info = getMetatype(metaTypeService, pid);
+            if (info == null) {                
                 System.out.println("No meta type definition found for pid: " + pid);
                 return null;
             }
             
             try {
-                createDefaultConfig(pid, def);
+                createDefaultConfig(pid, info);
             } catch (IOException e) {
                  throw new RuntimeException(e.getMessage(), e);
             }
             return null;
         }
         
-        private void createDefaultConfig(String pid, ObjectClassDefinition def) throws IOException {
-            AttributeDefinition[] attrs = def.getAttributeDefinitions(ObjectClassDefinition.ALL);
+        private void createDefaultConfig(String pid, MetaInfo info) throws IOException {
+            AttributeDefinition[] attrs = info.definition.getAttributeDefinitions(ObjectClassDefinition.ALL);
             if (attrs == null) {
                 return;
             }
-            Configuration config = configRepository.getConfigAdmin().getConfiguration(pid);
+            ConfigurationAdmin configAdmin = configRepository.getConfigAdmin();
+            Configuration config;
+            if(info.factory) {
+                config = configAdmin.createFactoryConfiguration(pid, null);
+            }
+            else {
+                config = configAdmin.getConfiguration(pid, null);
+            }            
             Dictionary<String, Object> props = new Hashtable<>();
             for (AttributeDefinition attr : attrs) {
                 String valueStr = getDefaultValueStr(attr.getDefaultValue());
@@ -172,19 +182,24 @@ public class MetaCommand extends ConfigCommandSupport {
     
     class Print extends AbstractMeta {
         public Void apply(MetaTypeService metaTypeService) {
-            ObjectClassDefinition def = getMetatype(metaTypeService, pid);
-            if (def == null) {
+            MetaInfo info = getMetatype(metaTypeService, pid);
+            if (info == null) {
                 System.out.println("No meta type definition found for pid: " + pid);
                 return null;
             }
-            System.out.println("Meta type informations for pid: " + pid);
+            if(info.factory) {
+                System.out.println("Meta type informations for factory pid: " + pid);
+            }
+            else {
+                System.out.println("Meta type informations for pid: " + pid);
+            }
             ShellTable table = new ShellTable();
             table.column("key");
             table.column("name");
             table.column("type");
             table.column("default");
             table.column("description").wrap();
-            AttributeDefinition[] attrs = def.getAttributeDefinitions(ObjectClassDefinition.ALL);
+            AttributeDefinition[] attrs = info.definition.getAttributeDefinitions(ObjectClassDefinition.ALL);
             if (attrs != null) {
                 for (AttributeDefinition attr : attrs) {
                     table.addRow().addContent(attr.getID(), attr.getName(), getType(attr.getType()),
@@ -200,4 +215,16 @@ public class MetaCommand extends ConfigCommandSupport {
         }
 
     }
+    
+    private static class MetaInfo {
+        final ObjectClassDefinition definition;
+        final boolean factory;
+        
+        MetaInfo(ObjectClassDefinition definition, boolean factory) {
+
+            this.definition = definition;
+            this.factory = factory;
+        }
+        
+    }
 }
diff --git a/config/src/main/java/org/apache/karaf/config/command/UpdateCommand.java b/config/src/main/java/org/apache/karaf/config/command/UpdateCommand.java
index 20db79c..40a9403 100644
--- a/config/src/main/java/org/apache/karaf/config/command/UpdateCommand.java
+++ b/config/src/main/java/org/apache/karaf/config/command/UpdateCommand.java
@@ -25,7 +25,6 @@ import org.apache.karaf.shell.api.action.lifecycle.Service;
 public class UpdateCommand extends ConfigCommandSupport {
 
     @Override
-    @SuppressWarnings({ "rawtypes", "unchecked" })
     protected Object doExecute() throws Exception {
         TypedProperties props = getEditedProps();
         if (props == null) {