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

gbellato
25.35.2007 5349c97806bb0a00038d8dc3dee4254fad187649
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
 * add the following below this CDDL HEADER, with the fields enclosed
 * by brackets "[]" replaced with your own identifying information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Portions Copyright 2006-2007 Sun Microsystems, Inc.
 */
package org.opends.server.messages;
 
 
 
import java.util.concurrent.ConcurrentHashMap;
import java.util.IllegalFormatException;
 
 
/**
 * This class defines the set of structures and methods that should be used when
 * retrieving text strings for use in a localized and/or internationalized
 * environment.  Each message will have a unique message number, and each
 * message will have a default English representation that can be overridden
 * from another list.
 * <BR><BR>
 * Note that there should be a pattern used for the message ID list so that each
 * message ID is unique, and so that the message ID itself may be keyed upon for
 * certain purposes.  The pattern that will be used is based on the hexadecimal
 * representation of the 32-bit value.  The first 12 bits will be used for the
 * category (a maximum of 4096 values), the next four bits will be used for the
 * severity (a maximum of 16 values), and the last 16 bits will be used for the
 * unique ID (a maximum of 65536 values).  The last 16 bits may be further
 * broken up within each category if desired.
 * <BR><BR>
 * The categories that have been defined include:
 * <UL>
 *   <LI>000 -- Core server processing</LI>
 *   <LI>001 -- Server extensions</LI>
 *   <LI>002 -- Connection and protocol handling</LI>
 *   <LI>003 -- Configuration handling</LI>
 *   <LI>004 -- The server loggers</LI>
 *   <LI>005 -- General server utilities</LI>
 *   <LI>006 -- Schema elements</LI>
 *   <LI>007 -- Plugin processing</LI>
 *   <LI>008 -- JE backend processing</LI>
 *   <LI>009 -- Generic backend processing</LI>
 *   <LI>00A -- Directory Server tools</LI>
 *   <LI>00B -- Task processing</LI>
 *   <LI>00C -- Access Control</LI>
 *   <LI>00D -- Administration framework</LI>
 *   <LI>00E -- Synchronization</LI>
 *   <LI>800 through FFE -- Reserved for third-party modules</LI>
 *   <LI>FFF -- User-defined processing</LI>
 * </UL>
 * <BR><BR>
 * The severity levels that have been defined include:
 * <UL>
 *   <LI>0 -- Informational</LI>
 *   <LI>1 -- Mild warning</LI>
 *   <LI>2 -- Severe warning</LI>
 *   <LI>3 -- Mild error</LI>
 *   <LI>4 -- Severe error</LI>
 *   <LI>5 -- Fatal error</LI>
 *   <LI>6 -- Debug</LI>
 *   <LI>7 -- Notice</LI>
 * </UL>
 */
public class MessageHandler
{
  /**
   * The category bitmask that will be used for messages associated with the
   * core server.
   */
  public static final int CATEGORY_MASK_CORE = 0x00000000;
 
 
 
  /**
   * The category bitmask that will be used for messages associated with server
   * extensions (e.g., extended operations, SASL mechanisms, password storage
   * schemes, password validators, etc.).
   */
  public static final int CATEGORY_MASK_EXTENSIONS = 0x00100000;
 
 
 
  /**
   * The category bitmask that will be used for messages associated with
   * connection and protocol handling (e.g., ASN.1 and LDAP).
   */
  public static final int CATEGORY_MASK_PROTOCOL = 0x00200000;
 
 
 
  /**
   * The category bitmask that will be used for messages associated with
   * configuration handling.
   */
  public static final int CATEGORY_MASK_CONFIG = 0x00300000;
 
 
 
  /**
   * The category bitmask that will be used for messages associated with the
   * server loggers.
   */
  public static final int CATEGORY_MASK_LOG = 0x00400000;
 
 
 
  /**
   * The category bitmask that will be used for messages associated with the
   * general server utilities.
   */
  public static final int CATEGORY_MASK_UTIL = 0x00500000;
 
 
 
  /**
   * The category bitmask that will be used for messages associated with the
   * server schema elements.
   */
  public static final int CATEGORY_MASK_SCHEMA = 0x00600000;
 
 
 
  /**
   * The category bitmask that will be used for messages associated with plugin
   * processing.
   */
  public static final int CATEGORY_MASK_PLUGIN = 0x00700000;
 
 
 
  /**
   * The category bitmask used for messages associated with the JE backend.
   */
  public static final int CATEGORY_MASK_JEB = 0x00800000;
 
 
 
  /**
   * The category bitmask used for messages associated with generic backends.
   */
  public static final int CATEGORY_MASK_BACKEND = 0x00900000;
 
 
 
  /**
   * The category bitmask used for messages associated with tools.
   */
  public static final int CATEGORY_MASK_TOOLS = 0x00A00000;
 
 
 
  /**
   * The category bitmask used for messages associated with tasks.
   */
  public static final int CATEGORY_MASK_TASK = 0x00B00000;
 
 
  /**
   * The category bitmask used for messages associated with Access Control.
   */
  public static final int CATEGORY_MASK_ACCESS_CONTROL = 0x00C00000;
 
 
  /**
   * The category bitmask used for messages associated with the
   * administration framework.
   */
  public static final int CATEGORY_MASK_ADMIN = 0x00D00000;
 
 
  /**
   * The category bitmask used for messages associated with the Synchronization.
   */
  public static final int CATEGORY_MASK_SYNC = 0x0E000000;
 
 
 
  /**
   * The category bitmask that will be used for messages associated with
   * third-party (including user-defined) modules.
   */
  public static final int CATEGORY_MASK_THIRD_PARTY = 0x80000000;
 
 
 
  /**
   * The category bitmask that will be used for messages associated with
   * user-defined modules.
   */
  public static final int CATEGORY_MASK_USER_DEFINED = 0xFFF00000;
 
 
 
  /**
   * The severity bitmask that will be used for informational messages.
   */
  public static final int SEVERITY_MASK_INFORMATIONAL = 0x00000000;
 
 
 
  /**
   * The severity bitmask that will be used for mild warning messages.
   */
  public static final int SEVERITY_MASK_MILD_WARNING = 0x00010000;
 
 
 
  /**
   * The severity bitmask that will be used for severe warning messages.
   */
  public static final int SEVERITY_MASK_SEVERE_WARNING = 0x00020000;
 
 
 
  /**
   * The severity bitmask that will be used for mild error messages.
   */
  public static final int SEVERITY_MASK_MILD_ERROR = 0x00030000;
 
 
 
  /**
   * The severity bitmask that will be used for severe error messages.
   */
  public static final int SEVERITY_MASK_SEVERE_ERROR = 0x00040000;
 
 
 
  /**
   * The severity bitmask that will be used for fatal error messages.
   */
  public static final int SEVERITY_MASK_FATAL_ERROR = 0x00050000;
 
 
 
  /**
   * The severity bitmask that will be used for debug messages.
   */
  public static final int SEVERITY_MASK_DEBUG = 0x00060000;
 
 
 
  /**
   * The severity bitmask that will be used for important informational
   * messages.
   */
  public static final int SEVERITY_MASK_NOTICE = 0x00070000;
 
 
 
  // The set of messages that have been registered with this message handler.
  private static ConcurrentHashMap<Integer,String> messageMap =
                      new ConcurrentHashMap<Integer,String>();
 
 
 
  // Performs the appropriate initialization for this message handler.  Whenever
  // a new message file is defined, then it must be placed here to ensure that
  // the messages get populated properly.
  static
  {
    CoreMessages.registerMessages();
    ExtensionsMessages.registerMessages();
    ProtocolMessages.registerMessages();
    ConfigMessages.registerMessages();
    LoggerMessages.registerMessages();
    UtilityMessages.registerMessages();
    SchemaMessages.registerMessages();
    PluginMessages.registerMessages();
    JebMessages.registerMessages();
    BackendMessages.registerMessages();
    ToolMessages.registerMessages();
    TaskMessages.registerMessages();
    AdminMessages.registerMessages();
    AciMessages.registerMessages();
    ReplicationMessages.registerMessages();
  }
 
 
 
  /**
   * Retrieves the message string associated with the provided message ID.  No
   * formatting or replacements will be made within the message string.  If no
   * message exists with the specified message ID, then a generic message will
   * be returned.
   *
   * @param  messageID  The unique ID assigned to the message to retrieve.
   *
   * @return  The message string associated with the provided message ID.
   */
  public static String getMessage(int messageID)
  {
    String message = messageMap.get(messageID);
    if (message == null)
    {
      message = "Unknown message for message ID " + messageID;
    }
 
    return message;
  }
 
 
 
  /**
   * Retrieves the message string associated with the provided message ID,
   * treating it as a format string and replacing any tokens with information
   * from the provided argument list.  If no message exists with the specified
   * message ID, then a generic message will be returned.
   *
   * @param  messageID  The unique ID assigned to the message to retrieve.
   * @param  arguments  The set of arguments to use to replace tokens in the
   *                    format string before it is returned.
   *
   * @return  The message string associated with the provided message ID.
   */
  public static String getMessage(int messageID, Object... arguments)
  {
    String formatString = messageMap.get(messageID);
    if (formatString == null)
    {
      StringBuilder buffer = new StringBuilder();
      buffer.append("Unknown message for message ID ");
      buffer.append(messageID);
      buffer.append(" -- provided arguments were:  ");
      if ((arguments != null) && (arguments.length > 0))
      {
        buffer.append(String.valueOf(arguments[0]));
 
        for (int i=1; i < arguments.length; i++)
        {
          buffer.append(", ");
          buffer.append(String.valueOf(arguments[i]));
        }
      }
 
      return buffer.toString();
    }
 
    try
    {
      return String.format(formatString, arguments);
    }
    catch (IllegalFormatException e)
    {
      // Make a more useful message than a stack trace.
      StringBuilder buffer = new StringBuilder();
      buffer.append(formatString);
      buffer.append(" -- mismatched arguments were:  ");
      if ((arguments != null) && (arguments.length > 0))
      {
        buffer.append(String.valueOf(arguments[0]));
 
        for (int i=1; i < arguments.length; i++)
        {
          buffer.append(", ");
          buffer.append(String.valueOf(arguments[i]));
        }
      }
 
      return buffer.toString();
    }
  }
 
 
 
  /**
   * Registers the provided message with this message handler.
   *
   * @param  messageID     The unique identifier assigned to this message.
   * @param  formatString  The format string to use for this message.
   */
  static void registerMessage(int messageID, String formatString)
  {
    messageMap.put(messageID, formatString);
  }
 
 
 
  /**
   * Retrieves the entire set of messages defined in the server, mapped between
   * their message ID and the format string for that message.  The resulting
   * mapping must not be modified.
   *
   * @return  The entire set of messages defined in the server.
   */
  public static ConcurrentHashMap<Integer,String> getMessages()
  {
    return messageMap;
  }
}