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

net.sf.eBusx.monitor.MonitoredObjectReply 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.Arrays;
import java.util.Objects;
import net.sf.eBus.messages.EReplyMessage;
import net.sf.eBus.util.Validator;

/**
 * Returns persistent status for all currently registered
 * monitored objects within the eBus virtual machine, specified
 * as an array of {@link PersistentStatusMessage}s. This message
 * is sent in reply to a {@link MonitoredObjectRequest} with the
 * subject {@link Monitor#ONGOING_REQUEST_SUBJECT}.
 *
 * @see MonitoredObjectRequest
 *
 * @author Charles Rapp
 */

public final class MonitoredObjectReply
    extends EReplyMessage
    implements Serializable
{
//---------------------------------------------------------------
// Member data.
//

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

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

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

    /**
     * Application is running on this named host. Note: this
     * name does not necessarily need to be a network
     * host name but should be a name meaningful to both the
     * monitored system and those monitoring it.
     */
    public final String hostName;

    /**
     * The application name.
     */
    public final String appName;

    /**
     * Persistent status for all currently registered monitor
     * objects.
     */
    public final PersistentStatusMessage[] statuses;

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

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

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

        this.hostName = builder.mHost;
        this.appName = builder.mAppName;
        this.statuses = builder.mStatuses;
    } // end of MonitoredObjectReply(Builder)

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

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

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

    /**
     * Builder class used to create a
     * {@code MonitoredObjectReply} instance. This class
     * guarantees that reply's message fields are correctly and
     * completely configured before instantiating the reply
     * instance
     */
    public static final class Builder
        extends EReplyMessage.Builder
    {
    //-----------------------------------------------------------
    // Member data.
    //

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

        private String mHost;
        private String mAppName;
        private PersistentStatusMessage[] mStatuses;

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

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

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

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

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

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

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

        //
        // 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(
                        "host name 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 persistent status array to given value. Note that
         * given array is copied.
         * @param statuses persistent status array.
         * @return {@code this Builder} instance.
         * @throws NullPointerException
         * if {@code statuses} is {@code null}.
         */
        public Builder statuses(final PersistentStatusMessage[] statuses)
        {
            Objects.requireNonNull(
                statuses, "status array is null");

            mStatuses = Arrays.copyOf(statuses, statuses.length);

            return (this);
        } // end of statuses(PersistentStatusMessage[])

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy