You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by gn...@apache.org on 2012/04/10 17:27:24 UTC

svn commit: r1311788 - in /karaf/branches/karaf-2.3.x/shell/console/src/main: java/org/apache/karaf/shell/console/help/BrandingHelpProvider.java java/org/apache/karaf/shell/console/help/HelpAction.java resources/OSGI-INF/blueprint/karaf-console.xml

Author: gnodet
Date: Tue Apr 10 15:27:23 2012
New Revision: 1311788

URL: http://svn.apache.org/viewvc?rev=1311788&view=rev
Log:
[KARAF-1045] Remove dead code, add a branding help provider

Added:
    karaf/branches/karaf-2.3.x/shell/console/src/main/java/org/apache/karaf/shell/console/help/BrandingHelpProvider.java
Modified:
    karaf/branches/karaf-2.3.x/shell/console/src/main/java/org/apache/karaf/shell/console/help/HelpAction.java
    karaf/branches/karaf-2.3.x/shell/console/src/main/resources/OSGI-INF/blueprint/karaf-console.xml

Added: karaf/branches/karaf-2.3.x/shell/console/src/main/java/org/apache/karaf/shell/console/help/BrandingHelpProvider.java
URL: http://svn.apache.org/viewvc/karaf/branches/karaf-2.3.x/shell/console/src/main/java/org/apache/karaf/shell/console/help/BrandingHelpProvider.java?rev=1311788&view=auto
==============================================================================
--- karaf/branches/karaf-2.3.x/shell/console/src/main/java/org/apache/karaf/shell/console/help/BrandingHelpProvider.java (added)
+++ karaf/branches/karaf-2.3.x/shell/console/src/main/java/org/apache/karaf/shell/console/help/BrandingHelpProvider.java Tue Apr 10 15:27:23 2012
@@ -0,0 +1,56 @@
+/**
+ *
+ * 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.shell.console.help;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+import java.util.Properties;
+
+import jline.Terminal;
+import org.apache.felix.service.command.CommandSession;
+import org.apache.karaf.shell.console.HelpProvider;
+import org.apache.karaf.shell.console.util.Branding;
+
+import static org.apache.felix.gogo.commands.basic.DefaultActionPreparator.printFormatted;
+
+public class BrandingHelpProvider implements HelpProvider {
+
+    private Properties branding;
+
+    public BrandingHelpProvider() {
+        branding = Branding.loadBrandingProperties();
+    }
+
+    public String getHelp(CommandSession session, String path) {
+        if (path.indexOf('|') > 0) {
+            if (path.startsWith("branding|")) {
+                path = path.substring("branding|".length());
+            } else {
+                return null;
+            }
+        }
+        String str = branding.getProperty("help." + path);
+        if (str != null) {
+            Terminal term = (Terminal) session.get(".jline.terminal");
+            ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            printFormatted("", str, term != null ? term.getWidth() : 80, new PrintStream(baos, true));
+            str = baos.toString();
+        }
+        return str;
+    }
+}

Modified: karaf/branches/karaf-2.3.x/shell/console/src/main/java/org/apache/karaf/shell/console/help/HelpAction.java
URL: http://svn.apache.org/viewvc/karaf/branches/karaf-2.3.x/shell/console/src/main/java/org/apache/karaf/shell/console/help/HelpAction.java?rev=1311788&r1=1311787&r2=1311788&view=diff
==============================================================================
--- karaf/branches/karaf-2.3.x/shell/console/src/main/java/org/apache/karaf/shell/console/help/HelpAction.java (original)
+++ karaf/branches/karaf-2.3.x/shell/console/src/main/java/org/apache/karaf/shell/console/help/HelpAction.java Tue Apr 10 15:27:23 2012
@@ -17,36 +17,9 @@
  */
 package org.apache.karaf.shell.console.help;
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.PrintStream;
-import java.io.Reader;
-import java.io.StringWriter;
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-import java.net.URL;
-import java.util.Map;
-import java.util.Set;
-import java.util.SortedMap;
-import java.util.TreeMap;
-
-import jline.Terminal;
-import org.apache.felix.gogo.commands.Action;
 import org.apache.felix.gogo.commands.Argument;
 import org.apache.felix.gogo.commands.Command;
-import org.apache.felix.gogo.commands.basic.AbstractCommand;
-import org.apache.felix.gogo.commands.basic.DefaultActionPreparator;
-import org.apache.felix.service.command.Function;
 import org.apache.karaf.shell.console.AbstractAction;
-import org.apache.karaf.shell.console.NameScoping;
-import org.apache.karaf.shell.console.SubShell;
-import org.fusesource.jansi.Ansi;
-import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceReference;
-
-import static org.apache.felix.gogo.commands.basic.DefaultActionPreparator.printFormatted;
 
 /**
  * Displays help on the available commands
@@ -71,122 +44,4 @@ public class HelpAction extends Abstract
         return null;
     }
 
-    private SortedMap<String, String> getCommandDescriptions(Set<String> names) {
-        SortedMap<String,String> commands = new TreeMap<String,String>();
-        for (String name : names) {
-            if (command != null && !name.startsWith(command)) {
-                continue;
-            }
-            String description = null;
-            Function function = (Function) session.get(name);
-            function = unProxy(function);
-            if (function instanceof AbstractCommand) {
-                try {
-                    Method mth = AbstractCommand.class.getDeclaredMethod("createNewAction");
-                    mth.setAccessible(true);
-                    Action action = (Action) mth.invoke(function);
-                    Class<? extends Action> clazz = action.getClass();
-                    Command ann = clazz.getAnnotation(Command.class);
-                    description = ann.description();
-                } catch (Throwable e) {
-                }
-                if (name.startsWith("*:")) {
-                    name = name.substring(2);
-                }
-                commands.put(name, description);
-            }
-        }
-        return commands;
-    }
-
-    private void printMethodList(Terminal term, PrintStream out, SortedMap<String, String> commands) {
-        out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("COMMANDS").a(Ansi.Attribute.RESET));
-//        int max = 0;
-//        for (Map.Entry<String,String> entry : commands.entrySet()) {
-//            String key = NameScoping.getCommandNameWithoutGlobalPrefix(session, entry.getKey());
-//            max = Math.max(max,key.length());
-//        }
-        for (Map.Entry<String,String> entry : commands.entrySet()) {
-            out.print("        ");
-            String key = NameScoping.getCommandNameWithoutGlobalPrefix(session, entry.getKey());
-            out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a(key).a(Ansi.Attribute.RESET));
-            if (entry.getValue() != null) {
-                DefaultActionPreparator.printFormatted("                ", entry.getValue(),
-                        term != null ? term.getWidth() : 80, out);
-            }
-        }
-        out.println();
-    }
-
-    private void printSubShellHelp(Bundle bundle, SubShell subShell, PrintStream out) {
-        Terminal term = session != null ? (Terminal) session.get(".jline.terminal") : null;
-        out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("SUBSHELL").a(Ansi.Attribute.RESET));
-        out.print("        ");
-        if (subShell.getName() != null) {
-            out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a(subShell.getName()).a(Ansi.Attribute.RESET));
-            out.println();
-        }
-        out.print("\t");
-        out.println(subShell.getDescription());
-        out.println();
-        if (subShell.getDetailedDescription() != null) {
-            out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("DETAILS").a(Ansi.Attribute.RESET));
-            String desc = loadDescription(bundle, subShell.getDetailedDescription());
-            printFormatted("        ", desc, term != null ? term.getWidth() : 80, out);
-        }
-    }
-
-    protected String loadDescription(Bundle bundle, String desc) {
-        if (desc.startsWith("classpath:")) {
-            URL url = bundle.getResource(desc.substring("classpath:".length()));
-            if (url == null) {
-                desc = "Unable to load description from " + desc;
-            } else {
-                InputStream is = null;
-                try {
-                    is = url.openStream();
-                    Reader r = new InputStreamReader(is);
-                    StringWriter sw = new StringWriter();
-                    int c;
-                    while ((c = r.read()) != -1) {
-                        sw.append((char) c);
-                    }
-                    desc = sw.toString();
-                } catch (IOException e) {
-                    desc = "Unable to load description from " + desc;
-                } finally {
-                    try {
-                        is.close();
-                    } catch (IOException e) {
-                        // Ignore
-                    }
-                }
-            }
-        }
-        return desc;
-    }
-
-    protected Function unProxy(Function function) {
-        try {
-            if (function.getClass().getName().contains("CommandProxy")) {
-                Field contextField = function.getClass().getDeclaredField("context");
-                Field referenceField = function.getClass().getDeclaredField("reference");
-                contextField.setAccessible(true);
-                referenceField.setAccessible(true);
-                BundleContext context = (BundleContext) contextField.get(function);
-                ServiceReference reference = (ServiceReference) referenceField.get(function);
-                Object target = context.getService(reference);
-                try {
-                    if (target instanceof Function) {
-                        function = (Function) target;
-                    }
-                } finally {
-                    context.ungetService(reference);
-                }
-            }
-        } catch (Throwable t) {
-        }
-        return function;
-    }
-
 }

Modified: karaf/branches/karaf-2.3.x/shell/console/src/main/resources/OSGI-INF/blueprint/karaf-console.xml
URL: http://svn.apache.org/viewvc/karaf/branches/karaf-2.3.x/shell/console/src/main/resources/OSGI-INF/blueprint/karaf-console.xml?rev=1311788&r1=1311787&r2=1311788&view=diff
==============================================================================
--- karaf/branches/karaf-2.3.x/shell/console/src/main/resources/OSGI-INF/blueprint/karaf-console.xml (original)
+++ karaf/branches/karaf-2.3.x/shell/console/src/main/resources/OSGI-INF/blueprint/karaf-console.xml Tue Apr 10 15:27:23 2012
@@ -88,7 +88,7 @@
     <bean id="subShellHelpProvider" class="org.apache.karaf.shell.console.help.SubShellHelpProvider" init-method="start" destroy-method="stop">
         <property name="context" ref="blueprintBundleContext"/>
     </bean>
-    <service auto-export="interfaces" ranking="-5">
+    <service auto-export="interfaces" ranking="-7">
         <bean class="org.apache.karaf.shell.console.help.SimpleHelpProvider">
             <property name="help">
                 <map>
@@ -98,6 +98,9 @@
             </property>
         </bean>
     </service>
+    <service auto-export="interfaces" ranking="-5">
+        <bean class="org.apache.karaf.shell.console.help.BrandingHelpProvider"/>
+    </service>
 
 
 </blueprint>