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 2013 ForgeRock AS.
016 */
017package org.forgerock.opendj.server.core;
018
019import java.util.Collections;
020import java.util.EnumSet;
021import java.util.Set;
022
023import org.forgerock.i18n.LocalizableMessage;
024import org.forgerock.util.Reject;
025
026/**
027 * An object that provides information about the source of a data provider
028 * related event. {@code DataProviderEvent} objects are generated when a data
029 * provider experiences an operational error or a state change resulting from
030 * configuration updates or administrative actions.
031 * <p>
032 * TODO: what else should this contain?
033 */
034public final class DataProviderEvent {
035
036    /**
037     * Indicates the type of event that has occurred in the data provider.
038     */
039    public static enum Type {
040        /**
041         * The data provider's access mode has changed.
042         */
043        ACCESS_MODE,
044
045        /**
046         * The data provider's set of base DNs has changed.
047         */
048        BASE_DNS,
049
050        /**
051         * The data provider's set of supported controls has changed.
052         */
053        SUPPORTED_CONTROLS,
054
055        /**
056         * The data provider's set of supported features has changed.
057         */
058        SUPPORTED_FEATURES;
059    }
060
061    /** A message describing this event. */
062    private final LocalizableMessage reason;
063
064    /** The types of event that have occurred in the data provider. */
065    private final Set<Type> types;
066
067    /**
068     * Creates a new data provider event.
069     *
070     * @param reason
071     *            A message describing this event.
072     * @param types
073     *            The types of event that have occurred in the data provider.
074     */
075    public DataProviderEvent(final LocalizableMessage reason, final Set<Type> types) {
076        Reject.ifNull(reason, types);
077        Reject.ifTrue(types.isEmpty());
078
079        this.reason = reason;
080
081        final EnumSet<Type> tmp = EnumSet.noneOf(Type.class);
082        tmp.addAll(types);
083        this.types = Collections.unmodifiableSet(tmp);
084    }
085
086    /**
087     * Returns an unmodifiable set containing the types of event that have
088     * occurred in the data provider.
089     *
090     * @return The unmodifiable set containing the types of event that have
091     *         occurred in the data provider.
092     */
093    public Set<Type> getEventTypes() {
094        return types;
095    }
096
097    /**
098     * Returns a message describing this event.
099     *
100     * @return A message describing this event.
101     */
102    public LocalizableMessage getReason() {
103        return reason;
104    }
105
106    /**
107     * Returns a string describing this event.
108     *
109     * @return A string describing this event.
110     */
111    @Override
112    public String toString() {
113        return reason.toString();
114    }
115
116}