001/*
002 * (C) Copyright 2006-2020 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *     Anahide Tchertchian
019 */
020package org.nuxeo.runtime;
021
022import java.io.Serializable;
023
024import org.nuxeo.runtime.model.RegistrationInfo;
025
026/**
027 * A component event.
028 * <p>
029 * The following event types are defined:
030 * <ul>
031 * <li><code>COMPONENT_REGISTERED</code> Sent when registering a component after the component is created
032 * <li><code>COMPONENT_UNREGISTERED</code> Sent when unregistering a component before the component is destroyed
033 * <li><code>ACTIVATING_COMPONENT</code> Sent before a component is activated
034 * <li><code>COMPONENT_ACTIVATED</code> Sent after the component is activated
035 * <li><code>DEACTIVATING_COMPONENT</code> Sent before a component is deactivated
036 * <li><code>COMPONENT_DEACTIVATED</code> Sent after a component is deactivated
037 * <li><code>COMPONENT_RESOLVED</code> Sent when a component was resolved (all dependencies are satisfied)
038 * <li><code>COMPONENT_UNRESOLVED</code> Sent when a component is unresolved (either it will be unregistered, either one
039 * of its dependencies is no more satisfied)
040 * <li><code>COMPONENT_STARTED</code> Sent when a component was started (even when component is not a java instance)
041 * <li><code>COMPONENT_STOPPED</code> Sent when a component was stopped (even when component is not a java instance)
042 * <li><code>EXTENSION_REGISTERED</code>Sent when a component contribution to an extension point is registered
043 * <li><code>EXTENSION_UNREGISTERED</code>Sent when a component contribution to an extension point is unregistered
044 * <li><code>EXTENSION_PENDING</code>Sent when a component contribution to an extension point is pending, waiting for
045 * dependencies to be resolved.
046 * <li><code>COMPONENT_EVENT</code> May be used by components to end custom events
047 * </ul>
048 * <p>
049 * Ordering of events for a given component:
050 * <ul>
051 * <li><code>COMPONENT_REGISTERED</code>
052 * <li><code>COMPONENT_RESOLVED</code> (if the component dependencies were satisfied)
053 * </ul>
054 * <ul>
055 * <li><code>ACTIVATING_COMPONENT</code>
056 * <li><code>COMPONENT_ACTIVATED</code>
057 * </ul>
058 * <ul>
059 * <li><code>EXTENSION_PENDING</code> (if an extension is pending)
060 * <li><code>EXTENSION_REGISTERED</code> (if an extension is registered)
061 * </ul>
062 * <ul>
063 * <li><code>STARTING_COMPONENT</code>
064 * <li><code>COMPONENT_STARTED</code>
065 * </ul>
066 * <ul>
067 * <li><code>EXTENSION_UNREGISTERED</code> (if an extension was registered)
068 * </ul>
069 * <ul>
070 * <li><code>STOPPING_COMPONENT</code>
071 * <li><code>COMPONENT_STOPPED</code>
072 * </ul>
073 * <ul>
074 * <li><code>DEACTIVATING_COMPONENT</code>
075 * <li><code>COMPONENT_DEACTIVATED</code>
076 * <li><code>COMPONENT_UNRESOLVED</code>
077 * <li><code>COMPONENT_UNREGISTERED</code>
078 * </ul>
079 *
080 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
081 */
082public class ComponentEvent {
083
084    // the event IDs
085    public static final int COMPONENT_REGISTERED = 1;
086
087    public static final int ACTIVATING_COMPONENT = 2;
088
089    public static final int DEACTIVATING_COMPONENT = 3;
090
091    public static final int COMPONENT_ACTIVATED = 4;
092
093    public static final int COMPONENT_DEACTIVATED = 5;
094
095    public static final int COMPONENT_UNREGISTERED = 6;
096
097    public static final int COMPONENT_RESOLVED = 7;
098
099    public static final int COMPONENT_UNRESOLVED = 8;
100
101    public static final int EXTENSION_REGISTERED = 9;
102
103    public static final int EXTENSION_UNREGISTERED = 10;
104
105    public static final int EXTENSION_PENDING = 11;
106
107    public static final int STARTING_COMPONENT = 12;
108
109    public static final int STOPPING_COMPONENT = 13;
110
111    public static final int COMPONENT_STARTED = 14;
112
113    public static final int COMPONENT_STOPPED = 15;
114
115    public static final int COMPONENT_EVENT = 100;
116
117    /** The event id. */
118    public final int id;
119
120    /** The component this event relates to if any, null otherwise. */
121    public final RegistrationInfo registrationInfo;
122
123    /** Optional event data. */
124    public final Serializable data;
125
126    public ComponentEvent(int id, RegistrationInfo ri) {
127        this(id, ri, null);
128    }
129
130    public ComponentEvent(int id, RegistrationInfo ri, Serializable data) {
131        this.id = id;
132        registrationInfo = ri;
133        this.data = data;
134    }
135
136    /**
137     * Returns the event name given an integer id.
138     *
139     * @since 11.3
140     */
141    public static String getEventName(int id) {
142        switch (id) {
143        case COMPONENT_REGISTERED:
144            return "COMPONENT_REGISTERED";
145        case COMPONENT_RESOLVED:
146            return "COMPONENT_RESOLVED";
147        case COMPONENT_UNRESOLVED:
148            return "COMPONENT_UNRESOLVED";
149        case ACTIVATING_COMPONENT:
150            return "ACTIVATING_COMPONENT";
151        case DEACTIVATING_COMPONENT:
152            return "DEACTIVATING_COMPONENT";
153        case COMPONENT_ACTIVATED:
154            return "COMPONENT_ACTIVATED";
155        case COMPONENT_DEACTIVATED:
156            return "COMPONENT_DEACTIVATED";
157        case COMPONENT_UNREGISTERED:
158            return "COMPONENT_UNREGISTERED";
159        case STARTING_COMPONENT:
160            return "STARTING_COMPONENT";
161        case STOPPING_COMPONENT:
162            return "STOPPING_COMPONENT";
163        case COMPONENT_STARTED:
164            return "COMPONENT_STARTED";
165        case COMPONENT_STOPPED:
166            return "COMPONENT_STOPPED";
167        case COMPONENT_EVENT:
168            return "COMPONENT_EVENT";
169        case EXTENSION_REGISTERED:
170            return "EXTENSION_REGISTERED";
171        case EXTENSION_PENDING:
172            return "EXTENSION_PENDING";
173        }
174        return "UNKNOWN_" + id;
175    }
176
177    /**
178     * Gets the event name as a string.
179     *
180     * @return the event name
181     */
182    public final String getEventName() {
183        return getEventName(id);
184    }
185
186    @Override
187    public String toString() {
188        return getEventName() + ": " + registrationInfo.getName();
189    }
190
191}