Coverage Report - org.mule.util.StringMessageUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
StringMessageUtils
0%
0/98
0%
0/62
0
 
 1  
 /*
 2  
  * $Id: StringMessageUtils.java 19191 2010-08-25 21:05:23Z tcarlson $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.util;
 12  
 
 13  
 import org.mule.api.MuleMessage;
 14  
 import org.mule.api.MuleRuntimeException;
 15  
 import org.mule.api.transport.PropertyScope;
 16  
 import org.mule.config.i18n.CoreMessages;
 17  
 
 18  
 import java.io.UnsupportedEncodingException;
 19  
 import java.text.MessageFormat;
 20  
 import java.util.ArrayList;
 21  
 import java.util.Arrays;
 22  
 import java.util.Collection;
 23  
 import java.util.List;
 24  
 import java.util.Map;
 25  
 import java.util.Set;
 26  
 import java.util.TreeSet;
 27  
 
 28  
 /**
 29  
  * Useful methods for formatting message strings for logging or exceptions.
 30  
  */
 31  
 // @ThreadSafe
 32  
 public final class StringMessageUtils
 33  
 {
 34  
     // The maximum number of Collection and Array elements used for messages
 35  
     public static final int MAX_ELEMENTS = 50;
 36  
     public static final int DEFAULT_MESSAGE_WIDTH = 80;
 37  
 
 38  
     /** Do not instanciate. */
 39  
     private StringMessageUtils ()
 40  0
     {
 41  
         // no-op
 42  0
     }
 43  
 
 44  
     public static String getFormattedMessage(String msg, Object[] arguments)
 45  
     {
 46  0
         if (arguments != null)
 47  
         {
 48  0
             for (int i = 0; i < arguments.length; i++)
 49  
             {
 50  0
                 arguments[i] = toString(arguments[i]);
 51  
             }
 52  
         }
 53  0
         return MessageFormat.format(msg, arguments);
 54  
     }
 55  
 
 56  
     public static String getBoilerPlate(String message)
 57  
     {
 58  0
         return getBoilerPlate(message, '*', DEFAULT_MESSAGE_WIDTH);
 59  
     }
 60  
 
 61  
     public static String getBoilerPlate(String message, char c, int maxlength)
 62  
     {
 63  0
         return getBoilerPlate(new ArrayList(Arrays.asList(new String[]{message})), c, maxlength);
 64  
     }
 65  
 
 66  
     public static String getBoilerPlate(List messages, char c, int maxlength)
 67  
     {
 68  
         int size;
 69  0
         StringBuffer buf = new StringBuffer(messages.size() * maxlength);
 70  0
         int trimLength = maxlength - (c == ' ' ? 2 : 4);
 71  
 
 72  0
         for (int i = 0; i < messages.size(); i++)
 73  
         {
 74  0
             size = messages.get(i).toString().length();
 75  0
             if (size > trimLength)
 76  
             {
 77  0
                 String temp = messages.get(i).toString();
 78  0
                 int k = i;
 79  
                 int x;
 80  
                 int len;
 81  0
                 messages.remove(i);
 82  0
                 while (temp.length() > 0)
 83  
                 {
 84  0
                     len = (trimLength <= temp.length() ? trimLength : temp.length());
 85  0
                     String msg = temp.substring(0, len);
 86  0
                     x = msg.indexOf(SystemUtils.LINE_SEPARATOR);
 87  
 
 88  0
                     if (x > -1)
 89  
                     {
 90  0
                         msg = msg.substring(0, x);
 91  0
                         len = x + 1;
 92  
                     }
 93  
                     else
 94  
                     {
 95  0
                         x = msg.lastIndexOf(' ');
 96  0
                         if (x > -1 && len == trimLength)
 97  
                         {
 98  0
                             msg = msg.substring(0, x);
 99  0
                             len = x + 1;
 100  
                         }
 101  
                     }
 102  0
                     if (msg.startsWith(" "))
 103  
                     {
 104  0
                         msg = msg.substring(1);
 105  
                     }
 106  
 
 107  0
                     temp = temp.substring(len);
 108  0
                     messages.add(k, msg);
 109  0
                     k++;
 110  0
                 }
 111  
             }
 112  
         }
 113  
 
 114  0
         buf.append(SystemUtils.LINE_SEPARATOR);
 115  0
         if (c != ' ')
 116  
         {
 117  0
             buf.append(StringUtils.repeat(c, maxlength));
 118  
         }
 119  
 
 120  0
         for (int i = 0; i < messages.size(); i++)
 121  
         {
 122  0
             buf.append(SystemUtils.LINE_SEPARATOR);
 123  0
             if (c != ' ')
 124  
             {
 125  0
                 buf.append(c);
 126  
             }
 127  0
             buf.append(" ");
 128  0
             buf.append(messages.get(i));
 129  
 
 130  0
             String osEncoding = CharSetUtils.defaultCharsetName();
 131  
             int padding;
 132  
             try
 133  
             {
 134  0
                 padding = trimLength - messages.get(i).toString().getBytes(osEncoding).length;
 135  
             }
 136  0
             catch (UnsupportedEncodingException ueex)
 137  
             {
 138  0
                 throw new MuleRuntimeException(CoreMessages.failedToConvertStringUsingEncoding(osEncoding), ueex);
 139  0
             }
 140  0
             if (padding > 0)
 141  
             {
 142  0
                 buf.append(StringUtils.repeat(' ', padding));
 143  
             }
 144  0
             buf.append(' ');
 145  0
             if (c != ' ')
 146  
             {
 147  0
                 buf.append(c);
 148  
             }
 149  
         }
 150  0
         buf.append(SystemUtils.LINE_SEPARATOR);
 151  0
         if (c != ' ')
 152  
         {
 153  0
             buf.append(StringUtils.repeat(c, maxlength));
 154  
         }
 155  0
         return buf.toString();
 156  
     }
 157  
 
 158  
     public static String truncate(String message, int length, boolean includeCount)
 159  
     {
 160  0
         if (message == null)
 161  
         {
 162  0
             return null;
 163  
         }
 164  0
         if (message.length() <= length)
 165  
         {
 166  0
             return message;
 167  
         }
 168  0
         String result = message.substring(0, length) + "...";
 169  0
         if (includeCount)
 170  
         {
 171  0
             result += "[" + length + " of " + message.length() + "]";
 172  
         }
 173  0
         return result;
 174  
     }
 175  
 
 176  
     public static byte[] getBytes(String string)
 177  
     {
 178  
         try
 179  
         {
 180  0
             return string.getBytes(FileUtils.DEFAULT_ENCODING);
 181  
         }
 182  0
         catch (UnsupportedEncodingException e)
 183  
         {
 184  
             // We can ignore this as the encoding is validated on start up
 185  0
             return null;
 186  
         }
 187  
     }
 188  
 
 189  
     public static String getString(byte[] bytes, String encoding)
 190  
     {
 191  
         try
 192  
         {
 193  0
             return new String(bytes, encoding);
 194  
         }
 195  0
         catch (UnsupportedEncodingException e)
 196  
         {
 197  
             // We can ignore this as the encoding is validated on start up
 198  0
             return null;
 199  
         }
 200  
     }
 201  
 
 202  
     /**
 203  
      * @see ArrayUtils#toString(Object, int)
 204  
      * @see CollectionUtils#toString(Collection, int)
 205  
      * @see MapUtils#toString(Map, boolean)
 206  
      */
 207  
     public static String toString(Object o)
 208  
     {
 209  0
         if (o == null)
 210  
         {
 211  0
             return "null";
 212  
         }
 213  0
         else if (o instanceof Class)
 214  
         {
 215  0
             return ((Class) o).getName();
 216  
         }
 217  0
         else if (o instanceof Map)
 218  
         {
 219  0
             return MapUtils.toString((Map) o, false);
 220  
         }
 221  0
         else if (o.getClass().isArray())
 222  
         {
 223  0
             return ArrayUtils.toString(o, MAX_ELEMENTS);
 224  
         }
 225  0
         else if (o instanceof Collection)
 226  
         {
 227  0
             return CollectionUtils.toString((Collection) o, MAX_ELEMENTS);
 228  
         }
 229  
         else
 230  
         {
 231  0
             return o.toString();
 232  
         }
 233  
     }
 234  
 
 235  
     public static String headersToString(MuleMessage m)
 236  
     {
 237  0
         if (m == null)
 238  
         {
 239  0
             return null;
 240  
         }
 241  0
         StringBuilder buf = new StringBuilder();
 242  0
         buf.append(SystemUtils.LINE_SEPARATOR).append("Message properties:").append(SystemUtils.LINE_SEPARATOR);
 243  
 
 244  0
         for (int i = 0; i < PropertyScope.ALL_SCOPES.length; i++)
 245  
         {
 246  0
             PropertyScope scope = PropertyScope.ALL_SCOPES[i];
 247  
             try
 248  
             {
 249  0
                 Set names = new TreeSet(m.getPropertyNames(scope));
 250  0
                 buf.append("  ").append(scope.getScopeName().toUpperCase()).append(" scoped properties:").append(SystemUtils.LINE_SEPARATOR);
 251  
 
 252  0
                 for (Object name : names)
 253  
                 {
 254  0
                     Object value = m.getProperty(name.toString(), scope);
 255  0
                     if (name.equals("password") || name.toString().contains("secret") || name.equals("pass"))
 256  
                     {
 257  0
                         value = "****";
 258  
                     }
 259  0
                     buf.append("    ").append(name).append("=").append(value).append(SystemUtils.LINE_SEPARATOR);
 260  0
                 }
 261  
             }
 262  0
             catch (IllegalArgumentException e)
 263  
             {
 264  
                 // ignored
 265  0
             }
 266  
         }
 267  0
         return buf.toString();
 268  
     }
 269  
 }