You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by cs...@apache.org on 2015/01/26 16:16:08 UTC

karaf git commit: [KARAF-1025] Adding config:meta command to display meta type information

Repository: karaf
Updated Branches:
  refs/heads/master 29cd8bb6e -> cd353eb57


[KARAF-1025] Adding config:meta command to display meta type information


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/cd353eb5
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/cd353eb5
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/cd353eb5

Branch: refs/heads/master
Commit: cd353eb57b3d36d6de078241116fbf0c6d74e61f
Parents: 29cd8bb
Author: Christian Schneider <ch...@die-schneider.net>
Authored: Mon Jan 26 16:15:55 2015 +0100
Committer: Christian Schneider <ch...@die-schneider.net>
Committed: Mon Jan 26 16:15:55 2015 +0100

----------------------------------------------------------------------
 config/pom.xml                                  |   6 +-
 .../karaf/config/command/MetaCommand.java       | 125 +++++++++++++++++++
 2 files changed, 129 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/cd353eb5/config/pom.xml
----------------------------------------------------------------------
diff --git a/config/pom.xml b/config/pom.xml
index dfcbac6..c12a911 100644
--- a/config/pom.xml
+++ b/config/pom.xml
@@ -101,10 +101,12 @@
                     <instructions>
                         <Export-Package>
                             org.apache.karaf.config.command*,
-                            org.apache.karaf.config.core
+                            org.apache.karaf.config.core,
+                            org.osgi.service.metatype
                         </Export-Package>
                         <Import-Package>
-                            *
+                            *,
+                            org.osgi.service.metatype
                         </Import-Package>
                         <Private-Package>
                             org.apache.karaf.config.core.impl,

http://git-wip-us.apache.org/repos/asf/karaf/blob/cd353eb5/config/src/main/java/org/apache/karaf/config/command/MetaCommand.java
----------------------------------------------------------------------
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
new file mode 100644
index 0000000..e558c32
--- /dev/null
+++ b/config/src/main/java/org/apache/karaf/config/command/MetaCommand.java
@@ -0,0 +1,125 @@
+/*
+ * 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.karaf.config.command;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.karaf.config.command.completers.ConfigurationCompleter;
+import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.Completion;
+import org.apache.karaf.shell.api.action.Option;
+import org.apache.karaf.shell.api.action.lifecycle.Reference;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.apache.karaf.shell.support.table.ShellTable;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.metatype.AttributeDefinition;
+import org.osgi.service.metatype.MetaTypeInformation;
+import org.osgi.service.metatype.MetaTypeService;
+import org.osgi.service.metatype.ObjectClassDefinition;
+
+@Command(scope = "config", name = "meta", description = "Lists meta type information.")
+@Service
+public class MetaCommand extends ConfigCommandSupport {
+    @Option(name = "-p", aliases = "--pid", description = "The configuration pid", required = false, multiValued = false)
+    @Completion(ConfigurationCompleter.class)
+    protected String pid;
+    
+    @Reference
+    BundleContext context;
+
+    private Map<Integer, String> typeMap;
+    
+    public MetaCommand() {
+        typeMap = new HashMap<>();
+        typeMap.put(AttributeDefinition.BOOLEAN, "boolean");
+        typeMap.put(AttributeDefinition.BYTE, "byte");
+        typeMap.put(AttributeDefinition.CHARACTER, "char");
+        typeMap.put(AttributeDefinition.DOUBLE, "double");
+        typeMap.put(AttributeDefinition.FLOAT, "float");
+        typeMap.put(AttributeDefinition.INTEGER, "int");
+        typeMap.put(AttributeDefinition.LONG, "long");
+        typeMap.put(AttributeDefinition.PASSWORD, "password");
+        typeMap.put(AttributeDefinition.SHORT, "short");
+        typeMap.put(AttributeDefinition.STRING, "String");
+    }
+
+    @Override
+    protected Object doExecute() throws Exception {
+        System.out.println("pid:" + pid);
+        ServiceReference<MetaTypeService> ref = context.getServiceReference(MetaTypeService.class);
+        if (ref == null) {
+            System.out.println("No MetaTypeService present. You need to install an implementation to use this command.");
+        }
+        MetaTypeService metaTypeService = context.getService(ref);
+        ObjectClassDefinition def = getMetatype(metaTypeService, pid);
+        context.ungetService(ref);
+        ShellTable table = new ShellTable();
+        table.column("key");
+        table.column("name");
+        table.column("type");
+        table.column("default");
+        table.column("description");
+        AttributeDefinition[] attrs = def.getAttributeDefinitions(ObjectClassDefinition.ALL);
+        for (AttributeDefinition attr : attrs) {
+            table.addRow().addContent(attr.getID(), attr.getName(), getType(attr.getType()), 
+                                      getDefaultValueStr(attr.getDefaultValue()), attr.getDescription());
+        }
+        table.print(System.out);
+        return null;
+    }
+    
+    private String getType(int type) {
+        return typeMap.get(type);
+    }
+    
+    private String getDefaultValueStr(String[] defaultValues) {
+        if (defaultValues == null) {
+            return "";
+        }
+        StringBuilder result = new StringBuilder();
+        boolean first = true;
+        for (String defaultValue : defaultValues) {
+            if (first) {
+                first = false;
+            } else {
+                result.append(",");
+            }
+            result.append(defaultValue);
+        }
+        return result.toString();
+    }
+
+    public  ObjectClassDefinition getMetatype(MetaTypeService metaTypeService, String pid) {
+        for (Bundle bundle : context.getBundles()) {
+            MetaTypeInformation info = metaTypeService.getMetaTypeInformation(bundle);
+            if (info == null) {
+                continue;
+            }
+            String[] pids = info.getPids();
+            for (String cPid : pids) {
+                if (cPid.equals(pid)) {
+                    return info.getObjectClassDefinition(cPid, null);
+                }
+            }
+        }
+        return null;
+    }
+
+}