You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by Rainer Jung <ra...@kippdata.de> on 2013/09/09 19:24:47 UTC

Re: svn commit: r1514485 - in /tomcat/trunk/java/org/apache: coyote/ajp/AbstractAjpProcessor.java coyote/http11/AbstractOutputBuffer.java coyote/spdy/SpdyProcessor.java tomcat/util/http/HttpMessages.java

On 15.08.2013 22:51, markt@apache.org wrote:
> Author: markt
> Date: Thu Aug 15 20:51:38 2013
> New Revision: 1514485
> 
> URL: http://svn.apache.org/r1514485
> Log:
> Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=55399
> Have the message in the response line use the locale set for the response.
> 
> Modified:
>     tomcat/trunk/java/org/apache/coyote/ajp/AbstractAjpProcessor.java
>     tomcat/trunk/java/org/apache/coyote/http11/AbstractOutputBuffer.java
>     tomcat/trunk/java/org/apache/coyote/spdy/SpdyProcessor.java
>     tomcat/trunk/java/org/apache/tomcat/util/http/HttpMessages.java
> 
> Modified: tomcat/trunk/java/org/apache/tomcat/util/http/HttpMessages.java
> URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/http/HttpMessages.java?rev=1514485&r1=1514484&r2=1514485&view=diff
> ==============================================================================
> --- tomcat/trunk/java/org/apache/tomcat/util/http/HttpMessages.java (original)
> +++ tomcat/trunk/java/org/apache/tomcat/util/http/HttpMessages.java Thu Aug 15 20:51:38 2013
> @@ -16,6 +16,10 @@
>   */
>  package org.apache.tomcat.util.http;
>  
> +import java.util.Locale;
> +import java.util.Map;
> +import java.util.concurrent.ConcurrentHashMap;
> +
>  import org.apache.tomcat.util.res.StringManager;
>  
>  /**
> @@ -28,14 +32,24 @@ import org.apache.tomcat.util.res.String
>   * @author costin@eng.sun.com
>   */
>  public class HttpMessages {
> +
> +    private static final Map<Locale,HttpMessages> instances =
> +            new ConcurrentHashMap<>();
> +
> +    private static final HttpMessages DEFAULT = getInstance(Locale.getDefault());

Problem here, see below.

> +
>      // XXX move message resources in this package
> -    private static final StringManager sm =
> -        StringManager.getManager("org.apache.tomcat.util.http.res");
> +    private final StringManager sm;
> +
> +    private String st_200 = null;
> +    private String st_302 = null;
> +    private String st_400 = null;
> +    private String st_404 = null;
> +
> +    private HttpMessages(StringManager sm) {
> +        this.sm = sm;
> +    }
>  
> -    private static String st_200=null;
> -    private static String st_302=null;
> -    private static String st_400=null;
> -    private static String st_404=null;
>  
>      /** Get the status string associated with a status code.
>       *  No I18N - return the messages defined in the HTTP spec.
> @@ -45,36 +59,53 @@ public class HttpMessages {
>       *  Common messages are cached.
>       *
>       */
> -    public static String getMessage( int status ) {
> +    public String getMessage(int status) {
>          // method from Response.
>  
>          // Does HTTP requires/allow international messages or
>          // are pre-defined? The user doesn't see them most of the time
>          switch( status ) {
>          case 200:
> -            if( st_200==null ) {
> -                st_200=sm.getString( "sc.200");
> +            if(st_200 == null ) {
> +                st_200 = sm.getString("sc.200");
>              }
>              return st_200;
>          case 302:
> -            if( st_302==null ) {
> -                st_302=sm.getString( "sc.302");
> +            if(st_302 == null ) {
> +                st_302 = sm.getString("sc.302");
>              }
>              return st_302;
>          case 400:
> -            if( st_400==null ) {
> -                st_400=sm.getString( "sc.400");
> +            if(st_400 == null ) {
> +                st_400 = sm.getString("sc.400");
>              }
>              return st_400;
>          case 404:
> -            if( st_404==null ) {
> -                st_404=sm.getString( "sc.404");
> +            if(st_404 == null ) {
> +                st_404 = sm.getString("sc.404");
>              }
>              return st_404;
>          }
>          return sm.getString("sc."+ status);
>      }
>  
> +
> +    public static HttpMessages getInstance(Locale locale) {
> +        HttpMessages result = instances.get(locale);
> +        if (result == null) {
> +            StringManager sm = StringManager.getManager(
> +                    "org.apache.tomcat.util.http.res", locale);
> +            if (Locale.getDefault().equals(sm.getLocale())) {
> +                result = DEFAULT;

DEFAULT is set above by a call to this method here which already tries
to use it. On one of my systems this leads to "result" being set to null
here and ...

> +            } else {
> +                result = new HttpMessages(sm);
> +            }
> +            instances.put(locale, result);

an NPE here because "instances" is a ConcurrentHashMap that doesn't
allow null. After that no request processing is possible, because the
static initializer setting DEFAULT had failed and any further use of
this class leads to a java.lang.ExceptionInInitializerError.

One solution might be setting

private static final HttpMessages DEFAULT = new
HttpMessages(StringManager.getManager("org.apache.tomcat.util.http.res",
 Locale.getDefault()));

above. At least this worked for me.

> +        }
> +        return result;
> +    }
> +
> +

Regards,

Rainer

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org