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

Violette Roche-Montane
27.17.2014 6f5e10b0dc596c8e7625de7ddf523b6d5bc220c7
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
 * 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 legal-notices/CDDLv1_0.txt
 * or http://forgerock.org/license/CDDLv1.0.html.
 * 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 legal-notices/CDDLv1_0.txt.
 * 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
 *
 *
 *      Copyright 2006-2008 Sun Microsystems, Inc.
 *      Portions Copyright 2014 ForgeRock AS
 */
package org.opends.server.extensions;
 
 
import org.forgerock.i18n.slf4j.LocalizedLogger;
import static org.opends.server.util.StaticUtils.*;
 
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.SortedSet;
 
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.i18n.LocalizableMessageDescriptor;
import org.opends.server.types.Attribute;
import org.opends.server.types.Attributes;
import org.opends.server.types.DN;
import org.opends.server.types.DirectoryException;
import org.forgerock.opendj.ldap.ResultCode;
import org.opends.server.types.SearchFilter;
 
 
 
/**
 * This class provides some common tools to all entry cache implementations.
 */
public class EntryCacheCommon
{
 
  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
 
  /**
   * Configuration phases. Each value identifies a configuration step:
   * - PHASE_INIT       when invoking method initializeEntryCache()
   * - PHASE_ACCEPTABLE when invoking method isConfigurationChangeAcceptable()
   * - PHASE_APPLY      when invoking method applyConfigurationChange()
   */
  public static enum ConfigPhase
  {
    /**
     * Indicates that entry cache is in initialization check phase.
     */
    PHASE_INIT,
 
    /**
     * Indicates that entry cache is in configuration check phase.
     */
    PHASE_ACCEPTABLE,
 
    /**
     * Indicates that entry cache is applying its configuration.
     */
    PHASE_APPLY
  }
 
  /**
   * Error handler used by local methods to report configuration error.
   * The error handler simplifies the code of initializeEntryCache(),
   * isConfigurationChangeAcceptable() and applyConfigurationChanges() methods.
   */
  public class ConfigErrorHandler
  {
    // Configuration phase.
    private EntryCacheCommon.ConfigPhase _configPhase;
 
    // Unacceptable reasons. Used when _configPhase is PHASE_ACCEPTABLE.
    private List<LocalizableMessage> _unacceptableReasons;
 
    // Error messages. Used when _configPhase is PHASE_APPLY.
    private ArrayList<LocalizableMessage> _errorMessages;
 
    // Result code. Used when _configPhase is PHASE_APPLY.
    private ResultCode _resultCode;
 
    // Acceptable Configuration ? Used when _configPhase is PHASE_ACCEPTABLE
    // or PHASE_APPLY.
    private boolean _isAcceptable;
 
    // Indicates whether administrative action is required or not. Used when
    // _configPhase is PHASE_APPLY.
    private boolean _isAdminActionRequired;
 
    /**
     * Create an error handler.
     *
     * @param configPhase          the configuration phase for which the
     *                             error handler is used
     * @param unacceptableReasons  the reasons why the configuration cannot
     *                             be applied (during PHASE_ACCEPTABLE phase)
     * @param errorMessages        the errors found when applying a new
     *                             configuration (during PHASE_APPLY phase)
     */
    public ConfigErrorHandler (
        EntryCacheCommon.ConfigPhase configPhase,
        List<LocalizableMessage> unacceptableReasons,
        ArrayList<LocalizableMessage>            errorMessages
        )
    {
      _configPhase           = configPhase;
      _unacceptableReasons   = unacceptableReasons;
      _errorMessages         = errorMessages;
      _resultCode            = ResultCode.SUCCESS;
      _isAcceptable          = true;
      _isAdminActionRequired = false;
    }
 
    /**
     * Report an error.
     *
     * @param error        the error to report
     * @param isAcceptable <code>true</code> if the configuration is acceptable
     * @param resultCode   the change result for the current configuration
     */
    public void reportError(
            LocalizableMessage error,
            boolean isAcceptable,
            ResultCode resultCode
    )
    {
      switch (_configPhase)
      {
      case PHASE_INIT:
        {
        _errorMessages.add (error);
        _isAcceptable = isAcceptable;
        break;
        }
      case PHASE_ACCEPTABLE:
        {
        _unacceptableReasons.add (error);
        _isAcceptable = isAcceptable;
        break;
        }
      case PHASE_APPLY:
        {
        _errorMessages.add (error);
        _isAcceptable = isAcceptable;
        if (_resultCode == ResultCode.SUCCESS)
        {
          _resultCode = resultCode;
        }
        break;
        }
      }
    }
 
    /**
     * Report an error.
     *
     * @param error        the error to report
     * @param isAcceptable <code>true</code> if the configuration is acceptable
     * @param resultCode   the change result for the current configuration
     * @param isAdminActionRequired <code>true</code> if administrative action
     *                              is required or <code>false</code> otherwise
     */
    public void reportError(
            LocalizableMessage error,
            boolean isAcceptable,
            ResultCode resultCode,
            boolean isAdminActionRequired
    )
    {
      switch (_configPhase)
      {
      case PHASE_INIT:
        {
        logger.error(error);
        break;
        }
      case PHASE_ACCEPTABLE:
        {
        _unacceptableReasons.add (error);
        _isAcceptable = isAcceptable;
        break;
        }
      case PHASE_APPLY:
        {
        _errorMessages.add (error);
        _isAcceptable = isAcceptable;
        if (_resultCode == ResultCode.SUCCESS)
        {
          _resultCode = resultCode;
        }
        _isAdminActionRequired = isAdminActionRequired;
        break;
        }
      }
    }
 
    /**
     * Get the current result code that was elaborated right after a
     * configuration has been applied.
     *
     * @return the current result code
     */
    public ResultCode getResultCode()
    {
      return _resultCode;
    }
 
    /**
     * Get the current isAcceptable flag. The isAcceptable flag is elaborated
     * right after the configuration was checked.
     *
     * @return the isAcceptable flag
     */
    public boolean getIsAcceptable()
    {
      return _isAcceptable;
    }
 
    /**
     * Get the current unacceptable reasons. The unacceptable reasons are
     * elaborated when the configuration is checked.
     *
     * @return the list of unacceptable reasons
     */
    public List<LocalizableMessage> getUnacceptableReasons()
    {
      return _unacceptableReasons;
    }
 
    /**
     * Get the current error messages. The error messages are elaborated
     * when the configuration is applied.
     *
     * @return the list of error messages
     */
    public ArrayList<LocalizableMessage> getErrorMessages()
    {
      return _errorMessages;
    }
 
    /**
     * Get the current configuration phase. The configuration phase indicates
     * whether the entry cache is in initialization step, or in configuration
     * checking step or in configuration being applied step.
     *
     * @return the current configuration phase.
     */
    public ConfigPhase getConfigPhase()
    {
      return _configPhase;
    }
 
    /**
     * Get the current isAdminActionRequired flag as determined after apply
     * action has been taken on a given configuration.
     *
     * @return the isAdminActionRequired flag
     */
    public boolean getIsAdminActionRequired()
    {
      return _isAdminActionRequired;
    }
  } // ConfigErrorHandler
 
 
  /**
   * Reads a list of string filters and convert it to a list of search
   * filters.
   *
   * @param filters  the list of string filter to convert to search filters
   * @param decodeErrorMsg  the error message ID to use in case of error
   * @param errorHandler  error handler to report filter decoding errors on
   * @param configEntryDN  the entry cache configuration DN
   *
   * @return the set of search filters
   */
  public static HashSet<SearchFilter> getFilters(SortedSet<String> filters,
      LocalizableMessageDescriptor.Arg3<Object, Object, Object> decodeErrorMsg,
      ConfigErrorHandler errorHandler, DN configEntryDN)
  {
    // Returned value
    HashSet<SearchFilter> searchFilters = new HashSet<SearchFilter>();
 
    // Convert the string filters to search filters.
    if ((filters != null) && (! filters.isEmpty()))
    {
      for (String curFilter: filters)
      {
        try
        {
          searchFilters.add (SearchFilter.createFilterFromString (curFilter));
        }
        catch (DirectoryException de)
        {
          // We couldn't decode this filter. Report an error and continue.
          LocalizableMessage message = decodeErrorMsg.get(String.valueOf(configEntryDN),
            curFilter, (de.getMessage() != null ? de.getMessage() :
              stackTraceToSingleLineString(de)));
          errorHandler.reportError(message, false,
            ResultCode.INVALID_ATTRIBUTE_SYNTAX);
        }
      }
    }
 
    // done
    return searchFilters;
  }
 
 
  /**
   * Create a new error handler.
   *
   * @param configPhase          the configuration phase for which the
   *                             error handler is used
   * @param unacceptableReasons  the reasons why the configuration cannot
   *                             be applied (during PHASE_ACCEPTABLE phase)
   * @param errorMessages        the errors found when applying a new
   *                             configuration (during PHASE_APPLY phase)
   *
   * @return a new configuration error handler
   */
  public static ConfigErrorHandler getConfigErrorHandler (
      EntryCacheCommon.ConfigPhase  configPhase,
      List<LocalizableMessage> unacceptableReasons,
      ArrayList<LocalizableMessage>             errorMessages
      )
  {
    ConfigErrorHandler errorHandler = null;
 
    EntryCacheCommon ec = new EntryCacheCommon();
 
    errorHandler = ec.new ConfigErrorHandler (
        configPhase, unacceptableReasons, errorMessages
        );
    return errorHandler;
  }
 
 
  /**
   * Constructs a set of generic attributes containing entry cache
   * monitor data. Note that <code>null</code> can be passed in
   * place of any argument to denote the argument is omitted, such
   * is when no state data of a given kind is available or can be
   * provided.
   *
   * @param cacheHits      number of cache hits.
   * @param cacheMisses    number of cache misses.
   * @param cacheSize      size of the current cache, in bytes.
   * @param maxCacheSize   maximum allowed cache size, in bytes.
   * @param cacheCount     number of entries stored in the cache.
   * @param maxCacheCount  maximum number of cache entries allowed.
   *
   * @return  A set of generic attributes containing monitor data.
   */
  public static ArrayList<Attribute> getGenericMonitorData(
    Long cacheHits,
    Long cacheMisses,
    Long cacheSize,
    Long maxCacheSize,
    Long cacheCount,
    Long maxCacheCount)
  {
    ArrayList<Attribute> attrs = new ArrayList<Attribute>();
 
    if (cacheHits != null)
    {
      attrs
          .add(Attributes.create("entryCacheHits", cacheHits.toString()));
 
      // Cache misses is required to get cache tries and hit ratio.
      if (cacheMisses != null)
      {
        Long cacheTries = cacheHits + cacheMisses;
        attrs.add(Attributes.create("entryCacheTries", cacheTries
            .toString()));
 
        Double hitRatioRaw = cacheTries > 0 ? cacheHits.doubleValue()
            / cacheTries.doubleValue() : cacheHits.doubleValue() / 1;
        Double hitRatio = hitRatioRaw * 100D;
        attrs.add(Attributes.create("entryCacheHitRatio", Long
            .toString(hitRatio.longValue())));
      }
    }
 
    if (cacheSize != null)
    {
      attrs.add(Attributes.create("currentEntryCacheSize", cacheSize
          .toString()));
    }
 
    if (maxCacheSize != null)
    {
      attrs.add(Attributes.create("maxEntryCacheSize", maxCacheSize
          .toString()));
    }
 
    if (cacheCount != null)
    {
      attrs.add(Attributes.create("currentEntryCacheCount", cacheCount
          .toString()));
    }
 
    if (maxCacheCount != null)
    {
      attrs.add(Attributes.create("maxEntryCacheCount", maxCacheCount
          .toString()));
    }
 
    return attrs;
  }
 
}