FELIX-2125 Apply patch by Valentin Valchev (thanks) with a small change: The Comparator is created each time for the locale instead of synchronizing on the comparator and setting the locale field. IMHO this is cleaner and the cost of creating the comparator instance is close zero.
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@918431 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/internal/Util.java b/webconsole/src/main/java/org/apache/felix/webconsole/internal/Util.java
index 9c8df60..e39c4a9 100644
--- a/webconsole/src/main/java/org/apache/felix/webconsole/internal/Util.java
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/internal/Util.java
@@ -21,6 +21,7 @@
import java.util.Arrays;
import java.util.Comparator;
import java.util.Enumeration;
+import java.util.Locale;
import org.osgi.framework.Bundle;
import org.osgi.framework.Constants;
@@ -57,7 +58,6 @@
/** Parameter value */
public static final String VALUE_SHUTDOWN = "shutdown";
-
/**
* Return a display name for the given <code>bundle</code>:
* <ol>
@@ -69,11 +69,13 @@
* </ol>
*
* @param bundle the bundle which name to retrieve
+ * @param locale the locale, in which the bundle name is requested
* @return the bundle name - see the description of the method for more details.
*/
- public static String getName( Bundle bundle )
+ public static String getName( Bundle bundle, Locale locale )
{
- String name = ( String ) bundle.getHeaders().get( Constants.BUNDLE_NAME );
+ final String loc = locale == null ? null : locale.toString();
+ String name = ( String ) bundle.getHeaders( loc ).get( Constants.BUNDLE_NAME );
if ( name == null || name.length() == 0 )
{
name = bundle.getSymbolicName();
@@ -93,7 +95,7 @@
* Returns the value of the header or the empty string if the header
* is not available.
*
- * @param bundle the bundle which header to retrieve
+ * @param bundle the bundle which header to retrieve
* @param headerName the name of the header to retrieve
* @return the header or empty string if it is not set
*/
@@ -106,18 +108,20 @@
}
return "";
}
+
/**
* Orders the bundles according to their name as returned by
- * {@link #getName(Bundle)}, with the exception that the system bundle is
+ * {@link #getName(Bundle, Locale)}, with the exception that the system bundle is
* always place as the first entry. If two bundles have the same name, they
* are ordered according to their version. If they have the same version,
* the bundle with the lower bundle id comes before the other.
*
* @param bundles the bundles to sort
+ * @param locale the locale, used to obtain the localized bundle name
*/
- public static void sort( Bundle[] bundles )
+ public static void sort( Bundle[] bundles, Locale locale )
{
- Arrays.sort( bundles, BUNDLE_NAME_COMPARATOR );
+ Arrays.sort( bundles, new BundleNameComparator( locale ) );
}
@@ -139,10 +143,17 @@
return l;
}
- // ---------- inner classes ------------------------------------------------
-
- private static final Comparator BUNDLE_NAME_COMPARATOR = new Comparator()
+ private static final class BundleNameComparator implements Comparator
{
+ private final Locale locale;
+
+
+ BundleNameComparator( final Locale locale )
+ {
+ this.locale = locale;
+ }
+
+
public int compare( Object o1, Object o2 )
{
return compare( ( Bundle ) o1, ( Bundle ) o2 );
@@ -169,7 +180,7 @@
}
// compare the symbolic names
- int snComp = Util.getName( b1 ).compareToIgnoreCase( Util.getName( b2 ) );
+ int snComp = Util.getName( b1, locale ).compareToIgnoreCase( Util.getName( b2, locale ) );
if ( snComp != 0 )
{
return snComp;
@@ -193,5 +204,5 @@
// b1 id must be > b2 id because equality is already checked
return 1;
}
- };
-}
+ }
+}
\ No newline at end of file
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BundlesServlet.java b/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BundlesServlet.java
index e13332a..1a5a3bb 100644
--- a/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BundlesServlet.java
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BundlesServlet.java
@@ -31,6 +31,7 @@
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
+import java.util.Locale;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;
@@ -162,7 +163,7 @@
try
{
StringWriter w = new StringWriter();
- writeJSON( w, null, null, null, true );
+ writeJSON( w, null, null, null, true, Locale.ENGLISH );
String jsonString = w.toString();
JSONObject json = new JSONObject( jsonString );
@@ -242,7 +243,7 @@
{
final String pluginRoot = ( String ) request.getAttribute( WebConsoleConstants.ATTR_PLUGIN_ROOT );
final String servicesRoot = getServicesRoot( request );
- this.renderJSON(response, reqInfo.bundle, pluginRoot, servicesRoot);
+ this.renderJSON(response, reqInfo.bundle, pluginRoot, servicesRoot, request.getLocale());
// nothing more to do
return;
@@ -345,7 +346,7 @@
}
final String pluginRoot = ( String ) req.getAttribute( WebConsoleConstants.ATTR_PLUGIN_ROOT );
final String servicesRoot = getServicesRoot( req );
- this.renderJSON(resp, null, pluginRoot, servicesRoot);
+ this.renderJSON(resp, null, pluginRoot, servicesRoot, req.getLocale() );
}
else
{
@@ -443,41 +444,40 @@
final String pluginRoot = ( String ) request.getAttribute( WebConsoleConstants.ATTR_PLUGIN_ROOT );
final String servicesRoot = getServicesRoot ( request );
StringWriter w = new StringWriter();
- PrintWriter w2 = new PrintWriter(w);
- writeJSON(w2, reqInfo.bundle, pluginRoot, servicesRoot );
+ writeJSON(w, reqInfo.bundle, pluginRoot, servicesRoot, request.getLocale() );
vars.put( "__bundles__", w.toString());
response.getWriter().print(TEMPLATE_MAIN);
}
}
- private void renderJSON( final HttpServletResponse response, final Bundle bundle, final String pluginRoot, final String servicesRoot )
+ private void renderJSON( final HttpServletResponse response, final Bundle bundle, final String pluginRoot, final String servicesRoot, final Locale locale )
throws IOException
{
response.setContentType( "application/json" );
response.setCharacterEncoding( "UTF-8" );
final PrintWriter pw = response.getWriter();
- writeJSON(pw, bundle, pluginRoot, servicesRoot);
+ writeJSON(pw, bundle, pluginRoot, servicesRoot, locale);
}
- private void writeJSON( final PrintWriter pw, final Bundle bundle, final String pluginRoot, final String servicesRoot )
+ private void writeJSON( final Writer pw, final Bundle bundle, final String pluginRoot, final String servicesRoot, final Locale locale )
throws IOException
{
- writeJSON( pw, bundle, pluginRoot, servicesRoot, false );
+ writeJSON( pw, bundle, pluginRoot, servicesRoot, false, locale );
}
private void writeJSON( final Writer pw, final Bundle bundle, final String pluginRoot,
- final String servicesRoot, final boolean fullDetails ) throws IOException
+ final String servicesRoot, final boolean fullDetails, final Locale locale ) throws IOException
{
final Bundle[] allBundles = this.getBundles();
final Object[] status = getStatusLine(allBundles);
final String statusLine = (String) status[5];
final Bundle[] bundles = ( bundle != null ) ? new Bundle[]
{ bundle } : allBundles;
- Util.sort( bundles );
+ Util.sort( bundles, locale );
final JSONWriter jw = new JSONWriter( pw );
@@ -500,7 +500,7 @@
for ( int i = 0; i < bundles.length; i++ )
{
- bundleInfo( jw, bundles[i], fullDetails || bundle != null, pluginRoot, servicesRoot );
+ bundleInfo( jw, bundles[i], fullDetails || bundle != null, pluginRoot, servicesRoot, locale );
}
jw.endArray();
@@ -582,14 +582,14 @@
return ret;
}
- private void bundleInfo( JSONWriter jw, Bundle bundle, boolean details, final String pluginRoot, final String servicesRoot )
+ private void bundleInfo( JSONWriter jw, Bundle bundle, boolean details, final String pluginRoot, final String servicesRoot, final Locale locale )
throws JSONException
{
jw.object();
jw.key( "id" );
jw.value( bundle.getBundleId() );
jw.key( "name" );
- jw.value( Util.getName( bundle ) );
+ jw.value( Util.getName( bundle, locale ) );
jw.key( "state" );
jw.value( toStateString( bundle ) );
jw.key( "version" );
@@ -618,7 +618,7 @@
if ( details )
{
- bundleDetails( jw, bundle, pluginRoot, servicesRoot );
+ bundleDetails( jw, bundle, pluginRoot, servicesRoot, locale );
}
jw.endObject();
@@ -700,10 +700,10 @@
}
- private final void bundleDetails( JSONWriter jw, Bundle bundle, final String pluginRoot, final String servicesRoot)
+ private final void bundleDetails( JSONWriter jw, Bundle bundle, final String pluginRoot, final String servicesRoot, final Locale locale)
throws JSONException
{
- Dictionary headers = bundle.getHeaders();
+ Dictionary headers = bundle.getHeaders( locale == null ? null : locale.toString() );
jw.key( "props" );
jw.array();
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/ServicesServlet.java b/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/ServicesServlet.java
index 14629bc..132edeb 100644
--- a/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/ServicesServlet.java
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/ServicesServlet.java
@@ -24,6 +24,7 @@
import java.io.StringWriter;
import java.io.Writer;
import java.text.MessageFormat;
+import java.util.Locale;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
@@ -159,7 +160,7 @@
try
{
StringWriter w = new StringWriter();
- writeJSON( w, null, true );
+ writeJSON( w, null, true, Locale.ENGLISH );
String jsonString = w.toString();
JSONObject json = new JSONObject( jsonString );
@@ -318,14 +319,14 @@
}
- private void renderJSON( final HttpServletResponse response, final ServiceReference service )
+ private void renderJSON( final HttpServletResponse response, final ServiceReference service, final Locale locale )
throws IOException
{
response.setContentType( "application/json" );
response.setCharacterEncoding( "UTF-8" );
final PrintWriter pw = response.getWriter();
- writeJSON( pw, service );
+ writeJSON( pw, service, locale );
}
@@ -363,7 +364,7 @@
}
- private void usingBundles( JSONWriter jw, ServiceReference service ) throws JSONException
+ private void usingBundles( JSONWriter jw, ServiceReference service, Locale locale ) throws JSONException
{
jw.key( "usingBundles" );
jw.array();
@@ -374,7 +375,7 @@
for ( int i = 0; i < usingBundles.length; i++ )
{
jw.object();
- bundleInfo( jw, usingBundles[i] );
+ bundleInfo( jw, usingBundles[i], locale );
jw.endObject();
}
}
@@ -384,7 +385,7 @@
}
- private void serviceInfo( JSONWriter jw, ServiceReference service, boolean details ) throws JSONException
+ private void serviceInfo( JSONWriter jw, ServiceReference service, boolean details, final Locale locale ) throws JSONException
{
jw.object();
jw.key( "id" );
@@ -394,24 +395,24 @@
jw.key( "pid" );
jw.value( propertyAsString( service, Constants.SERVICE_PID ) );
- bundleInfo( jw, service.getBundle() );
+ bundleInfo( jw, service.getBundle(), locale );
if ( details )
{
serviceDetails( jw, service );
- usingBundles( jw, service );
+ usingBundles( jw, service, locale );
}
jw.endObject();
}
- private void bundleInfo( final JSONWriter jw, final Bundle bundle ) throws JSONException
+ private void bundleInfo( final JSONWriter jw, final Bundle bundle, final Locale locale ) throws JSONException
{
jw.key( "bundleId" );
jw.value( bundle.getBundleId() );
jw.key( "bundleName" );
- jw.value( Util.getName( bundle ) );
+ jw.value( Util.getName( bundle, locale ) );
jw.key( "bundleVersion" );
jw.value( Util.getHeaderValue( bundle, Constants.BUNDLE_VERSION ) );
jw.key( "bundleSymbolicName" );
@@ -419,13 +420,13 @@
}
- private void writeJSON( final PrintWriter pw, final ServiceReference service ) throws IOException
+ private void writeJSON( final Writer pw, final ServiceReference service, final Locale locale ) throws IOException
{
- writeJSON( pw, service, false );
+ writeJSON( pw, service, false, locale );
}
- private void writeJSON( final Writer pw, final ServiceReference service, final boolean fullDetails )
+ private void writeJSON( final Writer pw, final ServiceReference service, final boolean fullDetails, final Locale locale )
throws IOException
{
final ServiceReference[] allServices = this.getServices();
@@ -452,7 +453,7 @@
for ( int i = 0; i < services.length; i++ )
{
- serviceInfo( jw, services[i], fullDetails || service != null );
+ serviceInfo( jw, services[i], fullDetails || service != null, locale );
}
jw.endArray();
@@ -484,7 +485,7 @@
}
if ( reqInfo.extension.equals( "json" ) )
{
- this.renderJSON( response, reqInfo.service );
+ this.renderJSON( response, reqInfo.service, request.getLocale() );
// nothing more to do
return;
@@ -505,8 +506,7 @@
final String appRoot = ( String ) request.getAttribute( WebConsoleConstants.ATTR_APP_ROOT );
StringWriter w = new StringWriter();
- PrintWriter w2 = new PrintWriter(w);
- writeJSON(w2, reqInfo.service);
+ writeJSON(w, reqInfo.service, request.getLocale());
// prepare variables
DefaultVariableResolver vars = ( ( DefaultVariableResolver ) WebConsoleUtil.getVariableResolver( request ) );
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/LicenseServlet.java b/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/LicenseServlet.java
index d14a66b..fbd9a62 100644
--- a/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/LicenseServlet.java
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/LicenseServlet.java
@@ -23,6 +23,7 @@
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
+import java.util.Locale;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
@@ -98,16 +99,16 @@
protected void renderContent( HttpServletRequest request, HttpServletResponse res ) throws IOException
{
Bundle[] bundles = getBundleContext().getBundles();
- Util.sort( bundles );
+ Util.sort( bundles, request.getLocale() );
// prepare variables
DefaultVariableResolver vars = ( ( DefaultVariableResolver ) WebConsoleUtil.getVariableResolver( request ) );
- vars.put( "__data__", getBundleData(bundles).toString());
+ vars.put( "__data__", getBundleData( bundles, request.getLocale() ).toString());
res.getWriter().print(TEMPLATE);
}
- private static final JSONArray getBundleData(Bundle[] bundles) throws IOException
+ private static final JSONArray getBundleData(Bundle[] bundles, Locale locale) throws IOException
{
JSONArray ret = new JSONArray();
try
@@ -121,7 +122,7 @@
{ // has resources
JSONObject data = new JSONObject();
data.put( "bid", bundle.getBundleId() );
- data.put( "title", Util.getName( bundle ) );
+ data.put( "title", Util.getName( bundle, locale ) );
data.put( "files", files );
ret.put( data );
}