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.core;
028    import org.opends.messages.Message;
029    
030    
031    
032    import java.util.*;
033    
034    import org.opends.server.config.ConfigException;
035    import org.opends.server.types.*;
036    
037    import org.opends.server.loggers.debug.DebugLogger;
038    import org.opends.server.loggers.ErrorLogger;
039    import org.opends.server.loggers.AccessLogger;
040    import static org.opends.messages.ConfigMessages.*;
041    import static org.opends.server.loggers.ErrorLogger.*;
042    
043    import org.opends.server.admin.std.server.*;
044    import org.opends.server.admin.server.ConfigurationAddListener;
045    import org.opends.server.admin.server.ConfigurationDeleteListener;
046    import org.opends.server.admin.server.ServerManagementContext;
047    
048    
049    /**
050     * This class defines a utility that will be used to manage the set of loggers
051     * used in the Directory Server.  It will perform the logger initialization when
052     * the server is starting, and then will manage any additions, removals, or
053     * modifications of any loggers while the server is running.
054     */
055    public class LoggerConfigManager implements
056        ConfigurationAddListener<LogPublisherCfg>,
057        ConfigurationDeleteListener<LogPublisherCfg>
058    {
059    
060      /**
061       * Initializes all the log publishers.
062       *
063       * @throws ConfigException
064       *           If an unrecoverable problem arises in the process of
065       *           performing the initialization as a result of the server
066       *           configuration.
067       * @throws InitializationException
068       *           If a problem occurs during initialization that is not
069       *           related to the server configuration.
070       */
071      public void initializeLoggerConfig()
072          throws ConfigException, InitializationException
073      {
074        // Create an internal server management context and retrieve
075        // the root configuration which has the log publisher relation.
076        ServerManagementContext context = ServerManagementContext.getInstance();
077        RootCfg root = context.getRootConfiguration();
078    
079        root.addLogPublisherAddListener(this);
080        root.addLogPublisherDeleteListener(this);
081    
082        List<DebugLogPublisherCfg> debugPublisherCfgs =
083            new ArrayList<DebugLogPublisherCfg>();
084    
085        List<AccessLogPublisherCfg> accessPublisherCfgs =
086            new ArrayList<AccessLogPublisherCfg>();
087    
088        List<ErrorLogPublisherCfg> errorPublisherCfgs =
089            new ArrayList<ErrorLogPublisherCfg>();
090    
091        for (String name : root.listLogPublishers())
092        {
093          LogPublisherCfg config = root.getLogPublisher(name);
094    
095          if(config instanceof DebugLogPublisherCfg)
096          {
097            debugPublisherCfgs.add((DebugLogPublisherCfg)config);
098          }
099          else if(config instanceof AccessLogPublisherCfg)
100          {
101            accessPublisherCfgs.add((AccessLogPublisherCfg)config);
102          }
103          else if(config instanceof ErrorLogPublisherCfg)
104          {
105            errorPublisherCfgs.add((ErrorLogPublisherCfg)config);
106          }
107          else
108          {
109            Message message = ERR_CONFIG_LOGGER_INVALID_OBJECTCLASS.get(
110                String.valueOf(config.dn()));
111            throw new ConfigException(message);
112          }
113        }
114    
115        // See if there are active loggers in all categories.  If not, then log a
116        // message.
117        if (accessPublisherCfgs.isEmpty())
118        {
119          logError(WARN_CONFIG_LOGGER_NO_ACTIVE_ACCESS_LOGGERS.get());
120        }
121        if (errorPublisherCfgs.isEmpty())
122        {
123          logError(WARN_CONFIG_LOGGER_NO_ACTIVE_ERROR_LOGGERS.get());
124        }
125    
126        DebugLogger.getInstance().initializeDebugLogger(debugPublisherCfgs);
127        AccessLogger.getInstance().initializeAccessLogger(accessPublisherCfgs);
128        ErrorLogger.getInstance().initializeErrorLogger(errorPublisherCfgs);
129      }
130    
131      /**
132       * {@inheritDoc}
133       */
134      public boolean isConfigurationAddAcceptable(LogPublisherCfg config,
135                                                  List<Message> unacceptableReasons)
136      {
137        if(config instanceof DebugLogPublisherCfg)
138        {
139          return DebugLogger.getInstance().isConfigurationAddAcceptable(
140              (DebugLogPublisherCfg)config, unacceptableReasons);
141        }
142       else if(config instanceof AccessLogPublisherCfg)
143       {
144         return AccessLogger.getInstance().isConfigurationAddAcceptable(
145             (AccessLogPublisherCfg)config, unacceptableReasons);
146       }
147       else if(config instanceof ErrorLogPublisherCfg)
148       {
149         return ErrorLogger.getInstance().isConfigurationAddAcceptable(
150             (ErrorLogPublisherCfg)config, unacceptableReasons);
151       }
152        else
153        {
154    
155          unacceptableReasons.add(ERR_CONFIG_LOGGER_INVALID_OBJECTCLASS.get(
156                  String.valueOf(config.dn())));
157          return false;
158        }
159      }
160    
161      /**
162       * {@inheritDoc}
163       */
164      public ConfigChangeResult applyConfigurationAdd(LogPublisherCfg config)
165      {
166        if(config instanceof DebugLogPublisherCfg)
167        {
168          return DebugLogger.getInstance().applyConfigurationAdd(
169              (DebugLogPublisherCfg)config);
170        }
171       else if(config instanceof AccessLogPublisherCfg)
172       {
173         return AccessLogger.getInstance().applyConfigurationAdd(
174             (AccessLogPublisherCfg)config);
175       }
176       else if(config instanceof ErrorLogPublisherCfg)
177       {
178         return ErrorLogger.getInstance().applyConfigurationAdd(
179             (ErrorLogPublisherCfg)config);
180       }
181        else
182        {
183          ArrayList<Message> messages            = new ArrayList<Message>();
184          messages.add(ERR_CONFIG_LOGGER_INVALID_OBJECTCLASS.
185                  get(String.valueOf(config.dn())));
186          boolean           adminActionRequired = false;
187          ResultCode resultCode = ResultCode.UNWILLING_TO_PERFORM;
188          return new ConfigChangeResult(resultCode, adminActionRequired, messages);
189        }
190      }
191    
192      /**
193       * {@inheritDoc}
194       */
195      public boolean isConfigurationDeleteAcceptable(LogPublisherCfg config,
196                                                  List<Message> unacceptableReasons)
197      {
198        if(config instanceof DebugLogPublisherCfg)
199        {
200          return DebugLogger.getInstance().isConfigurationDeleteAcceptable(
201              (DebugLogPublisherCfg)config, unacceptableReasons);
202        }
203       else if(config instanceof AccessLogPublisherCfg)
204       {
205         return AccessLogger.getInstance().isConfigurationDeleteAcceptable(
206             (AccessLogPublisherCfg)config, unacceptableReasons);
207       }
208       else if(config instanceof ErrorLogPublisherCfg)
209       {
210         return ErrorLogger.getInstance().isConfigurationDeleteAcceptable(
211             (ErrorLogPublisherCfg)config, unacceptableReasons);
212       }
213        else
214        {
215          unacceptableReasons.add(ERR_CONFIG_LOGGER_INVALID_OBJECTCLASS.get(
216                  String.valueOf(config.dn())));
217          return false;
218        }
219      }
220    
221      /**
222       * {@inheritDoc}
223       */
224      public ConfigChangeResult applyConfigurationDelete(LogPublisherCfg config)
225      {
226        if(config instanceof DebugLogPublisherCfg)
227        {
228          return DebugLogger.getInstance().applyConfigurationDelete(
229              (DebugLogPublisherCfg)config);
230        }
231       else if(config instanceof AccessLogPublisherCfg)
232       {
233         return AccessLogger.getInstance().applyConfigurationDelete(
234             (AccessLogPublisherCfg)config);
235       }
236       else if(config instanceof ErrorLogPublisherCfg)
237       {
238         return ErrorLogger.getInstance().applyConfigurationDelete(
239             (ErrorLogPublisherCfg)config);
240       }
241        else
242        {
243          ArrayList<Message> messages            = new ArrayList<Message>();
244          messages.add(ERR_CONFIG_LOGGER_INVALID_OBJECTCLASS.get(
245                  String.valueOf(config.dn())));
246          boolean           adminActionRequired = false;
247          ResultCode resultCode = ResultCode.UNWILLING_TO_PERFORM;
248          return new ConfigChangeResult(resultCode, adminActionRequired, messages);
249        }
250      }
251    }
252