001/*
002 * (C) Copyright 2006-2017 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 */
019package org.nuxeo.runtime;
020
021import java.io.Serializable;
022
023import org.nuxeo.runtime.model.RegistrationInfo;
024
025/**
026 * A component event.
027 * <p>
028 * The following event types are defined:
029 * <ul>
030 * <li> <code>COMPONENT_REGISTERED</code> Sent when registering a component after the component is created
031 * <li> <code>ACTIVATING_COMPONENT</code> Sent before a component is activated
032 * <li> <code>COMPONENT_ACTIVATED</code> Sent after the component is activated
033 * <li> <code>DEACTIVATING_COMPONENT</code> Sent before a component is deactivated
034 * <li> <code>COMPONENT_DEACTIVATED</code> Sent after a component is deactivated
035 * <li> <code>COMPONENT_RESOLVED</code> Sent when a component was resolved (all dependencies are satisfied)
036 * <li> <code>COMPONENT_UNRESOLVED</code> Sent when a component is unresolved (either it will be unregistered, either one
037 * of its dependencies is no more satosfied)
038 * <li> <code>COMPONENT_UNREGISTERED</code> Sent when unregsitering a component before the component is destroyed
039 * <li> <code>COMPONENT_EVENT</code> May be used by components to end custom events
040 * </ul>
041 *
042 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
043 */
044public class ComponentEvent implements Serializable {
045
046    // the event IDs
047    public static final int COMPONENT_REGISTERED = 1;
048
049    public static final int ACTIVATING_COMPONENT = 2;
050
051    public static final int DEACTIVATING_COMPONENT = 3;
052
053    public static final int COMPONENT_ACTIVATED = 4;
054
055    public static final int COMPONENT_DEACTIVATED = 5;
056
057    public static final int COMPONENT_UNREGISTERED = 6;
058
059    public static final int COMPONENT_RESOLVED = 7;
060
061    public static final int COMPONENT_UNRESOLVED = 8;
062
063    public static final int EXTENSION_REGISTERED = 9;
064
065    public static final int EXTENSION_UNREGISTERED = 10;
066
067    public static final int EXTENSION_PENDING = 11;
068
069    public static final int STARTING_COMPONENT = 12;
070
071    public static final int STOPPING_COMPONENT = 13;
072
073    public static final int COMPONENT_STARTED = 14;
074
075    public static final int COMPONENT_STOPPED = 15;
076
077    public static final int COMPONENT_EVENT = 100;
078
079    private static final long serialVersionUID = 8936615866437064000L;
080
081    /** The event id. */
082    public final int id;
083
084    /** The component this event relates to if any, null otherwise. */
085    public final RegistrationInfo registrationInfo;
086
087    /** Optional event data. */
088    public final Serializable data;
089
090    public ComponentEvent(int id, RegistrationInfo ri) {
091        this(id, ri, null);
092    }
093
094    public ComponentEvent(int id, RegistrationInfo ri, Serializable data) {
095        this.id = id;
096        registrationInfo = ri;
097        this.data = data;
098    }
099
100    /**
101     * Gets the event name as a string.
102     *
103     * @return the event name
104     */
105    public final String getEventName() {
106        switch (id) {
107        case COMPONENT_REGISTERED:
108            return "COMPONENT_REGISTERED";
109        case COMPONENT_RESOLVED:
110            return "COMPONENT_RESOLVED";
111        case COMPONENT_UNRESOLVED:
112            return "COMPONENT_UNRESOLVED";
113        case ACTIVATING_COMPONENT:
114            return "ACTIVATING_COMPONENT";
115        case DEACTIVATING_COMPONENT:
116            return "DEACTIVATING_COMPONENT";
117        case COMPONENT_ACTIVATED:
118            return "COMPONENT_ACTIVATED";
119        case COMPONENT_DEACTIVATED:
120            return "COMPONENT_DEACTIVATED";
121        case COMPONENT_UNREGISTERED:
122            return "COMPONENT_UNREGISTERED";
123        case STARTING_COMPONENT:
124            return "STARTING_COMPONENT";
125        case STOPPING_COMPONENT:
126            return "STOPPING_COMPONENT";
127        case COMPONENT_STARTED:
128            return "COMPONENT_STARTED";
129        case COMPONENT_STOPPED:
130            return "COMPONENT_STOPPED";
131        case COMPONENT_EVENT:
132            return "COMPONENT_EVENT";
133        case EXTENSION_REGISTERED:
134            return "EXTENSION_REGISTERED";
135        case EXTENSION_PENDING:
136            return "EXTENSION_PENDING";
137        }
138        return "UNKNOWN_" + id;
139    }
140
141    @Override
142    public String toString() {
143        return getEventName() + ": " + registrationInfo.getName();
144    }
145
146}