[FrontPage] [TitleIndex] [WordIndex

How to separate messages from java code

So we want to separate user visible messages (e.g. for exceptions) from the java code. Here's how to do it (actually this is the standard approach of Eclipse).

Create a copy of Messages.java in the appropriate package (where the messages are used). Create a message.properties files in the same package. Adapt the BUNDLE_NAME member variable (to point) to the message.properties file.

A very important feature is the use of placeholders ({0}, {1}, ...) in the messages. These can be replaced by arbitrary Strings. See the examples.

NOTE: We currently do not use a centralized properties file and need a Messages.java file for every package. Of course, it of would be nice to use a more centralized approach (and get rid of all the Messages.java files)...

Also ensure that the name of the key contains the name of the class using a message followed by a '.'. If a message is used by more than one class of a package use the package name (leaving org.deegree) as prefix.

To reach this goal, IMHO, we would need to specify:

1. Messages.java (place this in the appropriate package, adapt BUNDLE_NAME)

   1 package org.deegree.whatever;
   2 import java.text.MessageFormat;
   3 import java.util.MissingResourceException;
   4 import java.util.ResourceBundle;
   5 public class Messages {
   6     private static final String BUNDLE_NAME = "org.deegree.whatever.messages"; //$NON-NLS-1$
   7     private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle( BUNDLE_NAME );
   8     private Messages() {
   9     }
  10     public static String getString( String key ) {
  11         try {
  12             return RESOURCE_BUNDLE.getString( key );
  13         } catch (MissingResourceException e) {
  14             return '!' + key + '!';
  15         }
  16     }
  17     public static String format (String key, Object arg0) {
  18         return format (key, new Object [] {arg0});
  19     }
  20     public static String format (String key, Object arg0, Object arg1) {
  21         return format (key, new Object [] {arg0, arg1});
  22     }
  23     public static String format (String key, Object arg0, Object arg1, Object arg2) {
  24         return format (key, new Object [] {arg0, arg1, arg2});
  25     }
  26     public static String format (String key, Object[] arguments) {
  27         return MessageFormat.format(getString (key), arguments);
  28     }
  29 }

2. Example message.properties (place in same package)

   1 ogcwebservices.wfs.ERROR_FEATURE_TYPE_UNKNOWN=Queried feature type "{0}" is not served by this WFS.
   2 ogcwebservices.wfs.ERROR_FEATURE_TYPE_INVISIBLE=Queried feature type "{0}" is not served by this WFS (hint: set deegreewfs:visible in schema).
   3 ogcwebservices.wfs.ERROR_UNHANDLED_TYPE=Unhandled sql type "{0}" for property value encountered (while validating feature).
   4 ogcwebservices.wfs.ERROR_CONVERTING_PROPERTY=Cannot convert value ("{0}") of property "{1}" to "{2}".

3. Example message references

   1 // example with one variable ({0} will be replaced by variable requestedType)
   2 String msg = Messages.format("ogcwebservices.wfs.ERROR_FEATURE_TYPE_UNKNOWN", requestedType);
   3 throw new OGCWebServiceException( this.getClass().getName(), msg);
   4 // example with arbitrary number of variables
   5 String msg = Messages.getString("ogcwebservices.wfs.ERROR_CONVERTING_PROPERTY");
   6 throw new OGCWebServiceException( this.getClass().getName(), new String [] {value, propertyName, targetType });

4. What NOT to do

Do not use quotes around parameters, '{0}' will yield {0} and not the text you specify. Please note that the eclipse editor will still highlight the quoted parameters!


CategoryHowTo CategoryDeegree2


2018-04-20 12:04