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 */
016package org.forgerock.opendj.config;
017
018import java.util.Locale;
019import java.util.MissingResourceException;
020
021import org.forgerock.i18n.LocalizableMessage;
022
023/**
024 * Defines an optional action which administators must perform after they have
025 * modified a property. By default modifications to properties are assumed to
026 * take effect immediately and require no additional administrative action.
027 * Developers should be aware that, where feasible, they should implement
028 * components such that property modifications require no additional
029 * administrative action. This is required in order to minimize server downtime
030 * during administration and provide a more user-friendly experience.
031 */
032public final class AdministratorAction {
033
034    /**
035     * Specifies the type of administrator action which must be performed in
036     * order for pending changes to take effect.
037     */
038    public static enum Type {
039        /**
040         * Used when modifications to a property require a component restart in
041         * order to take effect (usually by disabling and re-enabling the
042         * component). May have a description describing any additional
043         * administrator action that is required when the component is
044         * restarted.
045         */
046        COMPONENT_RESTART("component-restart"),
047
048        /**
049         * Used when modifications to a property take effect immediately, and no
050         * additional administrator action is required. May have a description
051         * describing how changes to the modified property will take effect.
052         */
053        NONE("none"),
054
055        /**
056         * Used when modifications to a property require an additional
057         * administrative action in order to take effect. This should be used
058         * when neither a server restart nor a component restart are applicable.
059         * Always has a description which describes the additional administrator
060         * action which is required when the property is modified.
061         */
062        OTHER("other"),
063
064        /**
065         * Used when modifications to a property require a server restart in
066         * order to take effect. May have a description describing any
067         * additional administrator action that is required when the component
068         * is restarted.
069         */
070        SERVER_RESTART("server-restart");
071
072        /** The user-friendly name of the type. */
073        private final String name;
074
075        /** Private constructor. */
076        private Type(String name) {
077            this.name = name;
078        }
079
080        /** {@inheritDoc} */
081        @Override
082        public String toString() {
083            return name;
084        }
085
086    }
087
088    /**
089     * The managed object definition associated with this administrator
090     * action.
091     */
092    private final AbstractManagedObjectDefinition<?, ?> definition;
093
094    /**
095     * The name of the property definition associated with this
096     * administrator action.
097     */
098    private final String propertyName;
099
100    /** The type of administration action. */
101    private final Type type;
102
103    /**
104     * Create a new administrator action.
105     *
106     * @param type
107     *            The type of this administration action.
108     * @param d
109     *            The managed object definition associated with this
110     *            administrator action.
111     * @param propertyName
112     *            The name of the property definition associated with this
113     *            administrator action.
114     */
115    public AdministratorAction(Type type, AbstractManagedObjectDefinition<?, ?> d, String propertyName) {
116        this.type = type;
117        this.definition = d;
118        this.propertyName = propertyName;
119    }
120
121    /**
122     * Gets the synopsis of this administrator action in the default locale.
123     *
124     * @return Returns the synopsis of this administrator action in the default
125     *         locale, or <code>null</code> if there is no synopsis defined.
126     */
127    public final LocalizableMessage getSynopsis() {
128        return getSynopsis(Locale.getDefault());
129    }
130
131    /**
132     * Gets the synopsis of this administrator action in the specified locale.
133     *
134     * @param locale
135     *            The locale.
136     * @return Returns the synopsis of this administrator action in the
137     *         specified locale, or <code>null</code> if there is no synopsis
138     *         defined.
139     */
140    public final LocalizableMessage getSynopsis(Locale locale) {
141        ManagedObjectDefinitionI18NResource resource = ManagedObjectDefinitionI18NResource.getInstance();
142        String property = "property." + propertyName + ".requires-admin-action.synopsis";
143        try {
144            return resource.getMessage(definition, property, locale);
145        } catch (MissingResourceException e) {
146            return null;
147        }
148    }
149
150    /**
151     * Gets the type of this administrator action.
152     *
153     * @return Returns the type of this administrator action.
154     */
155    public final Type getType() {
156        return type;
157    }
158}