001/*
002 * (C) Copyright 2017-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 *     Kevin Leturc <kleturc@nuxeo.com>
018 *     Anahide Tchertchian
019 */
020package org.nuxeo.runtime;
021
022/**
023 * Represents a message to be held by the Runtime framework.
024 * <p>
025 * Allows detecting and displaying errors and warnings at server startup and when hot-reloading.
026 *
027 * @since 11.3
028 */
029public class RuntimeMessage {
030
031    protected final Level level;
032
033    protected final String message;
034
035    protected final Source source;
036
037    protected final String sourceId;
038
039    public RuntimeMessage(Level level, String message, Source source, String sourceId) {
040        this.level = level;
041        this.message = message;
042        this.source = source;
043        this.sourceId = sourceId;
044    }
045
046    public Level getLevel() {
047        return level;
048    }
049
050    public String getMessage() {
051        return message;
052    }
053
054    /**
055     * Returns the type of source that produced the message.
056     */
057    public Source getSource() {
058        if (source == null) {
059            return Source.UNKNOWN;
060        }
061        return source;
062    }
063
064    /**
065     * Returns a string identifier for the source that produce the message.
066     */
067    public String getSourceId() {
068        if (sourceId == null) {
069            return getClass().getName();
070        }
071        return sourceId;
072    }
073
074    @Override
075    public String toString() {
076        return String.format("%s {level=%s, message=%s, source=%s, sourceId=%s}", getClass().getName(), level, message,
077                source, sourceId);
078    }
079
080    public enum Level {
081
082        ERROR,
083
084        WARNING
085
086    }
087
088    /**
089     * The type of source that produced a message.
090     * <p>
091     * Useful to track errors on components, extension, etc...
092     */
093    public enum Source {
094        UNKNOWN, //
095        DEPLOYMENT, //
096        CONFIG, //
097        BUNDLE, //
098        COMPONENT, //
099        EXTENSION, //
100        CODE,
101    }
102
103}