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    
029    
030    import org.opends.server.types.InitializationException;
031    import org.opends.server.types.DN;
032    import org.opends.server.admin.std.server.ErrorLogPublisherCfg;
033    import org.opends.server.config.ConfigException;
034    import org.opends.server.api.ErrorLogPublisher;
035    import org.opends.server.util.TimeThread;
036    import org.opends.messages.Message;
037    import org.opends.messages.Severity;
038    import org.opends.messages.Category;
039    
040    /**
041     * This class provides an implementation of an error logger where only messages
042     * generated by a specified thread is actually logged.
043     */
044    public class ThreadFilterTextErrorLogPublisher
045        extends ErrorLogPublisher<ErrorLogPublisherCfg>
046    {
047      private Thread thread;
048    
049      private TextWriter writer;
050    
051      /**
052       * Construct a new instance with the provided settings.
053       *
054       * @param thread The thread to log from.
055       * @param writer The writer used to write the messages.
056       */
057      public ThreadFilterTextErrorLogPublisher(Thread thread,
058                                               TextWriter writer)
059      {
060        this.thread = thread;
061        this.writer = writer;
062      }
063    
064      /**
065       * {@inheritDoc}
066       */
067      public void initializeErrorLogPublisher(ErrorLogPublisherCfg config)
068          throws ConfigException, InitializationException
069      {
070        // This class should only be used internally in the server and not be
071        // configurable via the admin framework.
072      }
073    
074      /**
075       * {@inheritDoc}
076       */
077      public void close()
078      {
079        writer.shutdown();
080      }
081    
082      /**
083       * {@inheritDoc}
084       */
085      public void logError(Message message)
086      {
087        if (message != null) {
088          Severity severity = message.getDescriptor().getSeverity();
089          Category category = message.getDescriptor().getCategory();
090          int msgId = message.getDescriptor().getId();
091          Thread currentThread = Thread.currentThread();
092          if(this.thread.equals(currentThread) ||
093              this.thread.getThreadGroup().equals(currentThread.getThreadGroup()))
094          {
095            StringBuilder sb = new StringBuilder();
096            sb.append("[");
097            sb.append(TimeThread.getLocalTime());
098            sb.append("] category=").append(category).
099                append(" severity=").append(severity).
100                append(" msgID=").append(msgId).
101                append(" msg=").append(message);
102    
103            this.writer.writeRecord(sb.toString());
104          }
105        }
106      }
107    
108      /**
109       * {@inheritDoc}
110       */
111      public DN getDN()
112      {
113        // This class should only be used internally in the server and not be
114        // configurable via the admin framework.
115        return null;
116      }
117    }