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