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