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