FELIX-2158 Apply my FELIX-2158-2.patch

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@922579 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/AbstractWebConsolePlugin.java b/webconsole/src/main/java/org/apache/felix/webconsole/AbstractWebConsolePlugin.java
index eb40bdc..940091b 100644
--- a/webconsole/src/main/java/org/apache/felix/webconsole/AbstractWebConsolePlugin.java
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/AbstractWebConsolePlugin.java
@@ -538,11 +538,18 @@
 
         final String appRoot = ( String ) request.getAttribute( WebConsoleConstants.ATTR_APP_ROOT );
 
+        // support localization of the plugin title
+        String title = getTitle();
+        if ( title.startsWith( "%" ) )
+        {
+            title = "${" + title.substring( 1 ) + "}";
+        }
+
         String header = MessageFormat.format( getHeader(), new Object[]
-            { brandingPlugin.getBrandName(), getTitle(), appRoot, getLabel(),
-                toUrl( brandingPlugin.getFavIcon(), appRoot ), toUrl( brandingPlugin.getMainStyleSheet(), appRoot ),
-                brandingPlugin.getProductURL(), brandingPlugin.getProductName(),
-                toUrl( brandingPlugin.getProductImage(), appRoot ), getCssLinks( appRoot ) } );
+            { brandingPlugin.getBrandName(), title, appRoot, getLabel(), toUrl( brandingPlugin.getFavIcon(), appRoot ),
+                toUrl( brandingPlugin.getMainStyleSheet(), appRoot ), brandingPlugin.getProductURL(),
+                brandingPlugin.getProductName(), toUrl( brandingPlugin.getProductImage(), appRoot ),
+                getCssLinks( appRoot ) } );
         pw.println( header );
 
         return pw;
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationRender.java b/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationRender.java
index 1527cd6..e2fcebb 100644
--- a/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationRender.java
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationRender.java
@@ -208,7 +208,7 @@
         {
             final PrinterDesc desc = (PrinterDesc) i.next();
             final String label = desc.label;
-            final String title = desc.printer.getTitle();
+            final String title = getTitle( desc.printer );
             pw.print("<li><a href='" + pluginRoot + label + ".nfo'>" + title + "</a></li>" );
         }
         pw.println("</ul> <!-- end tabs on top -->");
@@ -273,7 +273,7 @@
     {
         if ( cfgPrinter != null )
         {
-            String sortKey = cfgPrinter.getTitle();
+            String sortKey = getTitle( cfgPrinter );
             if ( printers.containsKey( sortKey ) )
             {
                 int idx = -1;
@@ -328,7 +328,7 @@
                                             final ConfigurationPrinter cp,
                                             final String mode )
     {
-        pw.title(  cp.getTitle() );
+        pw.title( getTitle( cp ) );
         if ( cp instanceof ModeAwareConfigurationPrinter )
         {
             ((ModeAwareConfigurationPrinter)cp).printConfiguration( pw , mode);
@@ -398,6 +398,17 @@
         }
     }
 
+
+    private static final String getTitle( final ConfigurationPrinter cp )
+    {
+        final String title = cp.getTitle();
+        if ( title.startsWith( "%" ) )
+        {
+            return title.substring( 1 );
+        }
+        return title;
+    }
+
     private static class SystemPropertiesPrinter implements ConfigurationPrinter
     {
 
@@ -648,7 +659,7 @@
                     final URL[] attachments = ((AttachmentProvider)desc.printer).getAttachments(mode);
                     if ( attachments != null )
                     {
-                        cf.handleAttachments(desc.printer.getTitle(), attachments);
+                        cf.handleAttachments( getTitle( desc.printer ), attachments );
                     }
                 }
             }
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/OsgiManager.java b/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/OsgiManager.java
index 7b1c621..c9f9d7e 100644
--- a/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/OsgiManager.java
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/OsgiManager.java
@@ -155,10 +155,6 @@
     // AbstractWebConsolePlugin instances
     private Map plugins = new HashMap();
 
-    // map of labels to plugin titles: indexed by plugin label (String, values
-    // are plugin titles
-    private Map labelMap = new HashMap();
-
     private AbstractWebConsolePlugin defaultPlugin;
 
     private String defaultRenderName;
@@ -339,6 +335,8 @@
 
         if ( plugin != null )
         {
+            final Map labelMap = getLocalizedLabelMap( request.getLocale() );
+
             // the official request attributes
             req.setAttribute( WebConsoleConstants.ATTR_LABEL_MAP, labelMap );
             req.setAttribute( WebConsoleConstants.ATTR_APP_ROOT, request.getContextPath() + request.getServletPath() );
@@ -397,7 +395,6 @@
 
         // simply remove all operations, we should not be used anymore
         this.plugins.clear();
-        this.labelMap.clear();
     }
 
 
@@ -710,7 +707,6 @@
         {
             plugin.init( getServletConfig() );
             plugins.put( label, plugin );
-            labelMap.put( label, title );
 
             if ( this.defaultPlugin == null )
             {
@@ -734,8 +730,6 @@
         AbstractWebConsolePlugin plugin = ( AbstractWebConsolePlugin ) plugins.remove( label );
         if ( plugin != null )
         {
-            labelMap.remove( label );
-
             if ( this.defaultPlugin == plugin )
             {
                 if ( this.plugins.isEmpty() )
@@ -915,4 +909,44 @@
         }
         return stringConfig;
     }
+
+
+    /**
+     * Builds the map of labels to plugin titles to be stored as the
+     * <code>felix.webconsole.labelMap</code> request attribute. This map
+     * optionally localizes the plugin title using the providing bundle's
+     * resource bundle if the first character of the title is a percent
+     * sign (%). Titles not prefixed with a percent sign are added to the
+     * map unmodified.
+     *
+     * @param locale The locale to which the titles are to be localized
+     *
+     * @return The localized map of labels to titles
+     */
+    private final Map getLocalizedLabelMap( final Locale locale )
+    {
+        final Map map = new HashMap();
+        for ( Iterator pi = plugins.values().iterator(); pi.hasNext(); )
+        {
+            final AbstractWebConsolePlugin plugin = ( AbstractWebConsolePlugin ) pi.next();
+            final String label = plugin.getLabel();
+            String title = plugin.getTitle();
+            if ( title.startsWith( "%" ) )
+            {
+                try
+                {
+                    final ResourceBundle resourceBundle = resourceBundleManager.getResourceBundle( plugin.getBundle(),
+                        locale );
+                    title = resourceBundle.getString( title.substring( 1 ) );
+                }
+                catch ( Throwable e )
+                {
+                    /* ignore missing resource - use default title */
+                }
+            }
+            map.put( label, title );
+        }
+
+        return map;
+    }
 }