001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2008 Sun Microsystems, Inc.
015 * Portions Copyright 2016 ForgeRock AS.
016 */
017package org.forgerock.opendj.config;
018
019import java.util.Locale;
020import java.util.MissingResourceException;
021
022import org.forgerock.i18n.LocalizableMessage;
023
024/**
025 * Defines an optional action which administators must perform after they have
026 * modified a property. By default modifications to properties are assumed to
027 * take effect immediately and require no additional administrative action.
028 * Developers should be aware that, where feasible, they should implement
029 * components such that property modifications require no additional
030 * administrative action. This is required in order to minimize server downtime
031 * during administration and provide a more user-friendly experience.
032 */
033public final class AdministratorAction {
034
035    /**
036     * Specifies the type of administrator action which must be performed in
037     * order for pending changes to take effect.
038     */
039    public static enum Type {
040        /**
041         * Used when modifications to a property require a component restart in
042         * order to take effect (usually by disabling and re-enabling the
043         * component). May have a description describing any additional
044         * administrator action that is required when the component is
045         * restarted.
046         */
047        COMPONENT_RESTART("component-restart"),
048
049        /**
050         * Used when modifications to a property take effect immediately, and no
051         * additional administrator action is required. May have a description
052         * describing how changes to the modified property will take effect.
053         */
054        NONE("none"),
055
056        /**
057         * Used when modifications to a property require an additional
058         * administrative action in order to take effect. This should be used
059         * when neither a server restart nor a component restart are applicable.
060         * Always has a description which describes the additional administrator
061         * action which is required when the property is modified.
062         */
063        OTHER("other"),
064
065        /**
066         * Used when modifications to a property require a server restart in
067         * order to take effect. May have a description describing any
068         * additional administrator action that is required when the component
069         * is restarted.
070         */
071        SERVER_RESTART("server-restart");
072
073        /** The user-friendly name of the type. */
074        private final String name;
075
076        /** Private constructor. */
077        private Type(String name) {
078            this.name = name;
079        }
080
081        @Override
082        public String toString() {
083            return name;
084        }
085
086    }
087
088    /** The managed object definition associated with this administrator action. */
089    private final AbstractManagedObjectDefinition<?, ?> definition;
090
091    /** The name of the property definition associated with this administrator action. */
092    private final String propertyName;
093
094    /** The type of administration action. */
095    private final Type type;
096
097    /**
098     * Create a new administrator action.
099     *
100     * @param type
101     *            The type of this administration action.
102     * @param d
103     *            The managed object definition associated with this
104     *            administrator action.
105     * @param propertyName
106     *            The name of the property definition associated with this
107     *            administrator action.
108     */
109    public AdministratorAction(Type type, AbstractManagedObjectDefinition<?, ?> d, String propertyName) {
110        this.type = type;
111        this.definition = d;
112        this.propertyName = propertyName;
113    }
114
115    /**
116     * Gets the synopsis of this administrator action in the default locale.
117     *
118     * @return Returns the synopsis of this administrator action in the default
119     *         locale, or <code>null</code> if there is no synopsis defined.
120     */
121    public final LocalizableMessage getSynopsis() {
122        return getSynopsis(Locale.getDefault());
123    }
124
125    /**
126     * Gets the synopsis of this administrator action in the specified locale.
127     *
128     * @param locale
129     *            The locale.
130     * @return Returns the synopsis of this administrator action in the
131     *         specified locale, or <code>null</code> if there is no synopsis
132     *         defined.
133     */
134    public final LocalizableMessage getSynopsis(Locale locale) {
135        ManagedObjectDefinitionI18NResource resource = ManagedObjectDefinitionI18NResource.getInstance();
136        String property = "property." + propertyName + ".requires-admin-action.synopsis";
137        try {
138            return resource.getMessage(definition, property, locale);
139        } catch (MissingResourceException e) {
140            return null;
141        }
142    }
143
144    /**
145     * Gets the type of this administrator action.
146     *
147     * @return Returns the type of this administrator action.
148     */
149    public final Type getType() {
150        return type;
151    }
152}