001    /*
002     * CDDL HEADER START
003     *
004     * The contents of this file are subject to the terms of the
005     * Common Development and Distribution License, Version 1.0 only
006     * (the "License").  You may not use this file except in compliance
007     * with the License.
008     *
009     * You can obtain a copy of the license at
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2006-2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.loggers;
028    import org.opends.messages.Message;
029    
030    import org.opends.server.types.DN;
031    
032    import static org.opends.messages.LoggerMessages.*;
033    import static org.opends.server.util.StaticUtils.*;
034    
035    import java.io.File;
036    
037    /**
038     * A LogPublisherErrorHandler is used for notification of exceptions which
039     * occur during the publishing of a record.
040     *
041     * The advantage of using a handler is that we can handle exceptions
042     * asynchronously (useful when dealing with an AsynchronousPublisher).
043     */
044    public class LogPublisherErrorHandler
045    {
046      private DN publisherConfigDN;
047      private boolean writeErroroccurred = false;
048    
049      /**
050       * Construct a new log publisher error handler for a log publisher
051       * with the provided configuration DN.
052       *
053       * @param publisherConfigDN The DN of the managed object for the
054       * log publisher.
055       */
056      public LogPublisherErrorHandler(DN publisherConfigDN)
057      {
058        this.publisherConfigDN = publisherConfigDN;
059      }
060    
061      /**
062       * Handle an exception which occurred during the publishing
063       * of a log record.
064       * @param record - the record which was being published.
065       * @param ex - the exception occurred.
066       */
067      public void handleWriteError(String record, Throwable ex)
068      {
069        if(!writeErroroccurred)
070        {
071          Message msg = ERR_LOGGER_ERROR_WRITING_RECORD.get(
072                  publisherConfigDN.toString(),
073                  stackTraceToSingleLineString(ex));
074          System.err.println(msg);
075          writeErroroccurred = true;
076        }
077      }
078    
079      /**
080       * Handle an exception which occurred while trying to open a log
081       * file.
082       * @param file - the file which was being opened.
083       * @param ex - the exception occurred.
084       */
085      public void handleOpenError(File file, Throwable ex)
086      {
087        Message msg = ERR_LOGGER_ERROR_OPENING_FILE.get(file.toString(),
088                                publisherConfigDN.toString(),
089                                stackTraceToSingleLineString(ex));
090        System.err.println(msg);
091      }
092    
093      /**
094       * Handle an exception which occurred while trying to close a log
095       * file.
096       * @param ex - the exception occurred.
097       */
098      public void handleCloseError(Throwable ex)
099      {
100        Message msg = ERR_LOGGER_ERROR_CLOSING_FILE.get(
101                publisherConfigDN.toString(),
102                stackTraceToSingleLineString(ex));
103        System.err.println(msg);
104      }
105    
106      /**
107       * Handle an exception which occurred while trying to flush the
108       * writer buffer.
109       * @param ex - the exception occurred.
110       */
111      public void handleFlushError(Throwable ex)
112      {
113        Message msg = ERR_LOGGER_ERROR_FLUSHING_BUFFER.get(
114                publisherConfigDN.toString(),
115                stackTraceToSingleLineString(ex));
116        System.err.println(msg);
117      }
118    
119      /**
120       * Handle an exception which occured while trying to list log files
121       * in a directory.
122       * @param retentionPolicy - the retention policy being enforced when
123       *                          the exception occured.
124       * @param ex - the exception occurred.
125       */
126      public void handleDeleteError(RetentionPolicy retentionPolicy, Throwable ex)
127      {
128        Message msg = ERR_LOGGER_ERROR_ENFORCING_RETENTION_POLICY.get(
129                retentionPolicy.toString(), publisherConfigDN.toString(),
130                stackTraceToSingleLineString(ex));
131        System.err.println(msg);
132      }
133    }