mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

kenneth_suter
16.56.2007 69e53a50506083bb934a886ad67e6933b67a92c7
This addresses a few errors related to the messaging framework:

- Fixes an error introduced in with Application.ErrorPrintStream no longer overrode println(String) as was causing a stack overflow.
- Fixes a problem with Message in which raw format strings were run through the formatter with illegal argument specifiers which would throw an exception.

3 files modified
57 ■■■■■ changed files
opends/src/messages/src/org/opends/messages/Message.java 43 ●●●●● patch | view | raw | blame | history
opends/src/quicksetup/org/opends/quicksetup/Application.java 8 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/messages/MessageTest.java 6 ●●●●● patch | view | raw | blame | history
opends/src/messages/src/org/opends/messages/Message.java
@@ -30,6 +30,7 @@
import java.util.Locale;
import java.util.Formatter;
import java.util.Formattable;
import java.util.UnknownFormatConversionException;
/**
 * Renders sensitive textural strings.  In most cases message are intended
@@ -153,9 +154,16 @@
   * @return String representation of this message
   */
  public String toString(Locale locale) {
    String s = descriptor.getFormatString(locale);
    if (needsFormatting(s)) {
      s = new Formatter(locale).format(locale, s, args).toString();
    String s;
    String fmt = descriptor.getFormatString(locale);
    if (needsFormatting(fmt)) {
      try {
        s = new Formatter(locale).format(locale, fmt, args).toString();
      } catch (UnknownFormatConversionException e) {
        s = fmt; // This shouldn't happen but just in case...
      }
    } else {
      s = fmt;
    }
    if (s == null) s = "";
    return s;
@@ -316,19 +324,6 @@
  }
  /**
   * Indicates whether or not formatting should be applied
   * to the given format string.  Note that a format string
   * might have literal specifiers (%% or %n for example)that
   * require formatting but are not replaced by arguments.
   * @param s candiate for formatting
   * @return boolean where true indicates that the format
   *         string requires formatting
   */
  private boolean needsFormatting(String s) {
    return s != null && (args != null || s.indexOf('%') > 0);
  }
  /**
   * {@inheritDoc}
   */
  public int compareTo(Object o) {
@@ -356,4 +351,20 @@
    result = 31 * toString().hashCode();
    return result;
  }
  /**
   * Indicates whether or not formatting should be applied
   * to the given format string.  Note that a format string
   * might have literal specifiers (%% or %n for example) that
   * require formatting but are not replaced by arguments.
   * @param s candiate for formatting
   * @return boolean where true indicates that the format
   *         string requires formatting
   */
  protected boolean needsFormatting(String s) {
    return s != null &&
            ((args != null && args.length > 0)
                   || s.matches(".*%[n|%].*")); // match Formatter literals
  }
}
opends/src/quicksetup/org/opends/quicksetup/Application.java
@@ -768,16 +768,17 @@
    /**
     * {@inheritDoc}
     */
    public void println(Message msg)
    @Override
    public void println(String msg)
    {
      MessageBuilder mb = new MessageBuilder();
      if (isFirstLine)
      {
        mb.append(getFormattedLogError(msg));
        mb.append(getFormattedLogError(Message.raw(msg)));
      } else
      {
        mb.append(formatter.getLineBreak());
        mb.append(getFormattedLogError(msg));
        mb.append(getFormattedLogError(Message.raw(msg)));
      }
      notifyListeners(mb.toMessage());
      isFirstLine = false;
@@ -786,6 +787,7 @@
    /**
     * {@inheritDoc}
     */
    @Override
    public void write(byte[] b, int off, int len)
    {
      if (b == null)
opends/tests/unit-tests-testng/src/server/org/opends/messages/MessageTest.java
@@ -47,9 +47,11 @@
    return new Object[][]{
            {"Hello %s", "Hello World", new Object[]{"World"}},
            {"Hel%nlo %s", "Hel\nlo World", new Object[]{"World"}},
         {"Hel%%lo %s", "Hel%lo World", new Object[]{"World"}},
            {"Hel%%lo %s", "Hel%lo World", new Object[]{"World"}},
            {"Hel%%lo", "Hel%lo", new Object[]{}},
            {"Hel%nlo", "Hel\nlo", new Object[]{}}
            {"Hel%nlo", "Hel\nlo", new Object[]{}},
            {"Hel%Dlo", "Hel%Dlo", new Object[]{}},
            {"Hel%Dlo", "Hel%Dlo", new Object[]{ "abc"}},
    };
  }