All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.eBusx.monitor.PersistentStatusMessage Maven / Gradle / Ivy

//
// Copyright 2012, 2013, 2016, 2019, 2020 Charles W. Rapp
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package net.sf.eBusx.monitor;

import com.google.common.base.Strings;
import java.io.Serializable;
import java.util.Objects;
import javax.annotation.Nullable;
import net.sf.eBus.messages.ENotificationMessage;
import net.sf.eBus.util.Validator;

/**
 * Reports a monitorable object's on-going status. This status
 * is in effect until further notice. The notification subject
 * is the {@link MonitorId#typeName} and
 * {@link MonitorId#instanceName} in the format
 * "<type name>.<instance name>". This means that
 * in order to subscribe to this notification, you must first
 * request the registered monitored objects using the
 * {@link MonitoredObjectRequest} message.
 *
 * @author Charles Rapp
 */

public final class PersistentStatusMessage
    extends ENotificationMessage
    implements Serializable
{
//---------------------------------------------------------------
// Member data.
//

    //-----------------------------------------------------------
    // Constants.
    //

    /**
     * Serialization version identifier.
     */
    private static final long serialVersionUID = 0x050200L;

    //-----------------------------------------------------------
    // Locals.
    //

    /**
     * Application is running on this host. Note: this name is
     * not not required to be a network host name but
     * should be a name meaningful to the application and those
     * monitoring it.
     */
    public final String hostName;

    /**
     * Application name. This name should be meaningful both to
     * the application and those monitoring it.
     */
    public final String appName;

    /**
     * The monitored object's unique identifier.
     */
    public final MonitorId instance;

    /**
     * The instance on-going action level.
     */
    public final ActionLevel actionLevel;

    /**
     * The on-going action name.
     */
    public final String actionName;

    /**
     * Optional message explaining the action.
     */
    public final String actionMessage;

//---------------------------------------------------------------
// Member methods.
//

    //-----------------------------------------------------------
    // Constructors.
    //

    private PersistentStatusMessage(final Builder builder)
    {
        super (builder);

        this.hostName = builder.mHost;
        this.appName = builder.mAppName;
        this.instance = builder.mInstanceId;
        this.actionLevel = builder.mActionLevel;
        this.actionName = builder.mActionName;
        this.actionMessage = builder.mActionMessage;
    } // end of PerisistentStatusMessage(Builder)

    //
    // end of Constructors.
    //-----------------------------------------------------------

    //-----------------------------------------------------------
    // Object Method Overrides.
    //

    /**
     * Returns the application information message as text.
     * @return textual representation of application information.
     */
    @Override
    public String toString()
    {
        final StringBuilder retval = new StringBuilder();

        return (retval.append(super.toString())
                      .append("\n                   host: ")
                      .append(hostName)
                      .append("\n               app name: ")
                      .append(appName)
                      .append("\n               instance: ")
                      .append(instance)
                      .append("\n           action level: ")
                      .append(actionLevel)
                      .append("\n            action name: ")
                      .append(actionName)
                      .append("\n         action message: ")
                      .append(actionMessage)
                      .toString());
    } // end of toString()

    //
    // end of Object Method Overrides.
    //-----------------------------------------------------------

    /**
     * Returns a new instance of persistent status message
     * builder.
     * @return persistent status message builder.
     */
    public static Builder builder()
    {
        return (new Builder());
    } // end of builder()

//---------------------------------------------------------------
// Inner classes.
//

    /**
     * Builder class used to construct a
     * {@link PersistentStatusMessage persistent status message}.
     * Used to set status message instance identifier, action
     * level, action name, and action message.
     */
    public static final class Builder
        extends ENotificationMessage.Builder
    {
    //-----------------------------------------------------------
    // Member data.
    //

        //-------------------------------------------------------
        // Locals.
        //

        private String mHost;
        private String mAppName;
        private MonitorId mInstanceId;
        private ActionLevel mActionLevel;
        private String mActionName;
        private String mActionMessage;

    //-----------------------------------------------------------
    // Member methods.
    //

        //-------------------------------------------------------
        // Constructors.
        //

        private Builder()
        {
            super (PersistentStatusMessage.class);
        } // end of Builder()

        //
        // end of Constructors.
        //-------------------------------------------------------

        //-------------------------------------------------------
        // Builder Method Overrides.
        //

        @Override
        protected PersistentStatusMessage buildImpl()
        {
            return (new PersistentStatusMessage(this));
        } // end of buildImpl()

        @Override
        protected Validator validate(final Validator problems)
        {
            return (super.validate(problems)
                         .requireNotNull(mHost, "hostName")
                         .requireNotNull(mAppName, "appName")
                         .requireNotNull(mActionLevel,
                                         "actionLevel")
                         .requireNotNull(mActionName,
                                         "actionName"));
        } // end of validate()

        //
        // end of Builder Method Overrides.
        //-------------------------------------------------------

        //-------------------------------------------------------
        // Set Methods.
        //

        /**
         * Sets host name to given value. This name does
         * not need to be a network host name but any
         * name meaningful to the application and those
         * monitoring it.
         * @param name host name.
         * @return {@code this Builder} instance.
         * @throws IllegalArgumentException
         * if {@code name} is either {@code null} or empty.
         */
        public Builder hostName(final String name)
        {
            if (Strings.isNullOrEmpty(name))
            {
                throw (
                    new IllegalArgumentException(
                        "address is either null or empty"));
            }

            mHost = name;

            return (this);
        } // end of hostName(String)

        /**
         * Sets application name to given value.
         * @param name application name.
         * @return {@code this Builder} instance.
         * @throws IllegalArgumentException
         * if {@code name} is either {@code null} or empty.
         */
        public Builder appName(final String name)
        {
            if (Strings.isNullOrEmpty(name))
            {
                throw (
                    new IllegalArgumentException(
                        "name is null or empty"));
            }

            mAppName = name;

            return (this);
        } // end of appName(String)

        /**
         * Sets monitor identifier.
         * @param id unique monitor identifier.
         * @return {@code this Builder} instance.
         */
        public Builder instance(final MonitorId id)
        {
            mInstanceId = id;

            return (this);
        } // end of instance(MonitorId)

        /**
         * Sets action level.
         * @param level action level.
         * @return {@code this Builder} instance.
         * @throws NullPointerException
         * if {@code level} is {@code null}.
         */
        public Builder actionLevel(final ActionLevel level)
        {
            mActionLevel =
                Objects.requireNonNull(level, "level is null");

            return (this);
        } // end of actionLevel(ActionLevel)

        /**
         * Sets action name.
         * @param name action name.
         * @return {@code this Builder} instance.
         * @throws IllegalArgumentException
         * if {@code name} is either {@code null} or an empty
         * string.
         */
        public Builder actionName(final String name)
        {
            if (Strings.isNullOrEmpty(name))
            {
                throw (
                    new IllegalArgumentException(
                        "name is null or empty"));
            }

            mActionName = name;

            return (this);
        } // end of actionName(String)

        /**
         * Sets action message.
         * @param message action message. May be either
         * {@code null} or an empty string.
         * @return {@code this Builder} instance.
         */
        public Builder actionMessage(final @Nullable String message)
        {
            mActionMessage = message;

            return (this);
        } // end of actionMessage(String)

        //
        // end of Set Methods.
        //-------------------------------------------------------
    } // end of class Builder
} // end of class PersistentStatusMessage




© 2015 - 2025 Weber Informatics LLC | Privacy Policy