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
021/**
022 * An event in the Nuxeo Runtime life cycle.
023 * <p>
024 * The following event types are defined:
025 * <ul>
026 * <li> <code>RUNTIME_ABOUT_TO_START</code> Sent before starting the runtime
027 * <li> <code>RUNTIME_STARTED</code> Sent after the runtime was started
028 * <li> <code>RUNTIME_ABOUT_TO_STOP</code> Sent before stopping the runtime
029 * <li> <code>RUNTIME_STOPPED</code> Sent after the runtime stopped
030 * </ul>
031 * Note: these events are not supposed to leave the runtime, hence are not declared serializable.
032 *
033 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
034 */
035public class RuntimeServiceEvent {
036
037    // the event IDs
038    public static final int RUNTIME_ABOUT_TO_START = 0;
039
040    public static final int RUNTIME_STARTED = 1;
041
042    public static final int RUNTIME_ABOUT_TO_STOP = 2;
043
044    public static final int RUNTIME_STOPPED = 3;
045
046    /** The event id. */
047    public final int id;
048
049    public final RuntimeService runtime;
050
051    public RuntimeServiceEvent(int id, RuntimeService runtime) {
052        this.id = id;
053        this.runtime = runtime;
054    }
055
056    /**
057     * Gets the event name as a string.
058     *
059     * @return the event name
060     */
061    public final String getEventName() {
062        switch (id) {
063        case RUNTIME_STARTED:
064            return "RUNTIME_STARTED";
065        case RUNTIME_STOPPED:
066            return "RUNTIME_STOPPED";
067        case RUNTIME_ABOUT_TO_STOP:
068            return "RUNTIME_ABOUT_TO_STOP";
069        case RUNTIME_ABOUT_TO_START:
070            return "RUNTIME_ABOUT_TO_START";
071        }
072        return "UNKNOWN";
073    }
074
075    @Override
076    public String toString() {
077        return getEventName();
078    }
079
080    @Override
081    public int hashCode() {
082        final int prime = 31;
083        int result = 1;
084        result = prime * result + id;
085        result = prime * result + runtime.hashCode();
086        return result;
087    }
088
089    @Override
090    public boolean equals(Object obj) {
091        if (this == obj) {
092            return true;
093        }
094        if (obj == null) {
095            return false;
096        }
097        if (!(obj instanceof RuntimeServiceEvent)) {
098            return false;
099        }
100        RuntimeServiceEvent other = (RuntimeServiceEvent) obj;
101        if (id != other.id) {
102            return false;
103        }
104        if (!runtime.equals(other.runtime)) {
105            return false;
106        }
107        return true;
108    }
109
110}