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 legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2006-2008 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027package org.opends.server.core;
028import java.util.List;
029
030import org.forgerock.i18n.LocalizableMessage;
031import org.forgerock.i18n.slf4j.LocalizedLogger;
032import org.opends.server.loggers.RotationPolicy;
033import org.opends.server.admin.server.ConfigurationAddListener;
034import org.opends.server.admin.server.ConfigurationDeleteListener;
035import org.opends.server.admin.server.ServerManagementContext;
036import org.opends.server.admin.server.ConfigurationChangeListener;
037import org.opends.server.admin.std.server.LogRotationPolicyCfg;
038import org.opends.server.admin.std.server.RootCfg;
039import org.opends.server.admin.std.meta.LogRotationPolicyCfgDefn;
040import org.opends.server.admin.ClassPropertyDefinition;
041import org.opends.server.types.InitializationException;
042import org.forgerock.opendj.config.server.ConfigChangeResult;
043import org.forgerock.opendj.config.server.ConfigException;
044
045import static org.opends.messages.ConfigMessages.*;
046import static org.opends.server.util.StaticUtils.*;
047
048/**
049 * This class defines a utility that will be used to manage the set of
050 * log rotation policies used in the Directory Server.  It will perform the
051 * initialization when the server is starting, and then will manage any
052 * additions, and removals of policies while the server is running.
053 */
054public class LogRotationPolicyConfigManager implements
055    ConfigurationAddListener<LogRotationPolicyCfg>,
056    ConfigurationDeleteListener<LogRotationPolicyCfg>,
057    ConfigurationChangeListener<LogRotationPolicyCfg>
058{
059  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
060
061  private final ServerContext serverContext;
062
063  /**
064   * Creates this log rotation policy manager.
065   *
066   * @param serverContext
067   *          The server context.
068   */
069  public LogRotationPolicyConfigManager(ServerContext serverContext)
070  {
071    this.serverContext = serverContext;
072  }
073
074  /**
075   * Initializes all the log rotation policies.
076   *
077   * @throws ConfigException
078   *           If an unrecoverable problem arises in the process of performing
079   *           the initialization as a result of the server configuration.
080   * @throws InitializationException
081   *           If a problem occurs during initialization that is not related to
082   *           the server configuration.
083   */
084  public void initializeLogRotationPolicyConfig() throws ConfigException, InitializationException
085  {
086    ServerManagementContext context = ServerManagementContext.getInstance();
087    RootCfg root = context.getRootConfiguration();
088
089    root.addLogRotationPolicyAddListener(this);
090    root.addLogRotationPolicyDeleteListener(this);
091
092    for(String name : root.listLogRotationPolicies())
093    {
094      LogRotationPolicyCfg config = root.getLogRotationPolicy(name);
095
096      RotationPolicy rotationPolicy = getRotationPolicy(config);
097
098      DirectoryServer.registerRotationPolicy(config.dn(), rotationPolicy);
099    }
100  }
101
102  /** {@inheritDoc} */
103  @Override
104  public boolean isConfigurationAddAcceptable(
105      LogRotationPolicyCfg configuration,
106      List<LocalizableMessage> unacceptableReasons)
107  {
108    return isJavaClassAcceptable(configuration, unacceptableReasons);
109  }
110
111  /** {@inheritDoc} */
112  @Override
113  public boolean isConfigurationDeleteAcceptable(
114      LogRotationPolicyCfg configuration,
115      List<LocalizableMessage> unacceptableReasons)
116  {
117    // TODO: Make sure nothing is using this policy before deleting it.
118    return true;
119  }
120
121  /** {@inheritDoc} */
122  @Override
123  public ConfigChangeResult applyConfigurationAdd(LogRotationPolicyCfg config)
124  {
125    final ConfigChangeResult ccr = new ConfigChangeResult();
126
127    try
128    {
129      RotationPolicy rotationPolicy = getRotationPolicy(config);
130
131      DirectoryServer.registerRotationPolicy(config.dn(), rotationPolicy);
132    }
133    catch (ConfigException e) {
134      logger.traceException(e);
135      ccr.addMessage(e.getMessageObject());
136      ccr.setResultCode(DirectoryServer.getServerErrorResultCode());
137    } catch (Exception e) {
138      logger.traceException(e);
139
140      ccr.addMessage(ERR_CONFIG_ROTATION_POLICY_CANNOT_CREATE_POLICY.get(config.dn(),
141              stackTraceToSingleLineString(e)));
142      ccr.setResultCode(DirectoryServer.getServerErrorResultCode());
143    }
144
145    return ccr;
146  }
147
148  /** {@inheritDoc} */
149  @Override
150  public ConfigChangeResult applyConfigurationDelete(
151      LogRotationPolicyCfg config)
152  {
153    final ConfigChangeResult ccr = new ConfigChangeResult();
154
155    RotationPolicy policy = DirectoryServer.getRotationPolicy(config.dn());
156    if(policy != null)
157    {
158      DirectoryServer.deregisterRotationPolicy(config.dn());
159    }
160    else
161    {
162      // TODO: Add message and check for usage
163      ccr.setResultCode(DirectoryServer.getServerErrorResultCode());
164    }
165
166    return ccr;
167  }
168
169  /** {@inheritDoc} */
170  @Override
171  public boolean isConfigurationChangeAcceptable(
172      LogRotationPolicyCfg configuration,
173      List<LocalizableMessage> unacceptableReasons)
174  {
175    return isJavaClassAcceptable(configuration, unacceptableReasons);
176  }
177
178  /** {@inheritDoc} */
179  @Override
180  public ConfigChangeResult applyConfigurationChange(
181      LogRotationPolicyCfg configuration)
182  {
183    final ConfigChangeResult ccr = new ConfigChangeResult();
184
185    RotationPolicy policy =
186        DirectoryServer.getRotationPolicy(configuration.dn());
187    String className = configuration.getJavaClass();
188    if(!className.equals(policy.getClass().getName()))
189    {
190      ccr.setAdminActionRequired(true);
191    }
192
193    return ccr;
194  }
195
196  private boolean isJavaClassAcceptable(LogRotationPolicyCfg config,
197                                        List<LocalizableMessage> unacceptableReasons)
198  {
199    String className = config.getJavaClass();
200    LogRotationPolicyCfgDefn d = LogRotationPolicyCfgDefn.getInstance();
201    ClassPropertyDefinition pd = d.getJavaClassPropertyDefinition();
202    try {
203      Class<? extends RotationPolicy> theClass =
204          pd.loadClass(className, RotationPolicy.class);
205      // Explicitly cast to check that implementation implements the correct interface.
206      RotationPolicy retentionPolicy = theClass.newInstance();
207      // next line is here to ensure that eclipse does not remove the cast in the line above
208      retentionPolicy.hashCode();
209      return true;
210    } catch (Exception e) {
211      unacceptableReasons.add(
212          ERR_CONFIG_ROTATION_POLICY_INVALID_CLASS.get(className, config.dn(), e));
213      return false;
214    }
215  }
216
217  private RotationPolicy getRotationPolicy(LogRotationPolicyCfg config)
218      throws ConfigException {
219    String className = config.getJavaClass();
220    LogRotationPolicyCfgDefn d = LogRotationPolicyCfgDefn.getInstance();
221    ClassPropertyDefinition pd = d.getJavaClassPropertyDefinition();
222    try {
223      Class<? extends RotationPolicy> theClass =
224          pd.loadClass(className, RotationPolicy.class);
225      RotationPolicy rotationPolicy = theClass.newInstance();
226
227      rotationPolicy.initializeLogRotationPolicy(config);
228
229      return rotationPolicy;
230    } catch (Exception e) {
231      LocalizableMessage message = ERR_CONFIG_ROTATION_POLICY_INVALID_CLASS.get(
232          className, config.dn(), e);
233      throw new ConfigException(message, e);
234    }
235  }
236}