net.sf.eBusx.monitor.MonitorUpdate 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.client.EObject;
import net.sf.eBus.messages.ENotificationMessage;
import net.sf.eBus.util.Validator;
/**
* Reports the {@link Monitor#register(EObject) registration},
* {@link Monitor#deregister(EObject) deregistration}, an
* eBus object's change in on-going status, or an eBus object's
* transient event. See each field's documentation for more
* information.
*
* In order to receive this notification message, subscribe to:
* {@code net.sf.eBusx.monitor.MonitorUpdate}:{@code /eBus/monitor/update//}
* where <host> is {@link Monitor#hostName()} and
* <app> is {@link Monitor#applicationName()}.
*
* @see ApplicationInfo
* @see Monitor
*
* @author Charles Rapp
*/
public final class MonitorUpdate
extends ENotificationMessage
implements Serializable
{
//---------------------------------------------------------------
// Member enums.
//
/**
* {@code MonitorUpdate} has four types: object registration,
* on-going status update, transient status update, and
* object de-registration.
*/
public enum UpdateType
{
/**
* eBus object registered with {@code Monitor}.
*/
REGISTER,
/**
* eBus object's on-going status update.
*/
ON_GOING_UPDATE,
/**
* eBus object's transient status update.
*/
TRANSIENT_UPDATE,
/**
* eBus object de-registered from {@code Monitor}.
*/
DEREGISTER
} // end of enum UpdateType
//---------------------------------------------------------------
// 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;
/**
* Registered eBus object's unique identifier.
*/
public final MonitorId instance;
/**
* Specifies update is for either object registration,
* on-going status update, transient status update, and
* de-registration.
*/
public final UpdateType updateType;
/**
* Action level associated with update type.
*/
public final ActionLevel actionLevel;
/**
* Action name associated with update type. Will not be
* {@code null} or empty.
*/
public final String actionName;
/**
* Optional human-readable text associated with action
* name providing further explanation.
*/
public final @Nullable String actionMessage;
//---------------------------------------------------------------
// Member methods.
//
//-----------------------------------------------------------
// Constructors.
//
private MonitorUpdate(final Builder builder)
{
super (builder);
this.hostName = builder.mHost;
this.appName = builder.mAppName;
this.instance = builder.mInstance;
this.updateType = builder.mUpdateType;
this.actionLevel = builder.mActionLevel;
this.actionName = builder.mActionName;
this.actionMessage = builder.mActionMsg;
} // end of MonitorUpdate(Builder)
//
// end of Constructors.
//-----------------------------------------------------------
//-----------------------------------------------------------
// Object Method Overrides.
//
@Override
public String toString()
{
final StringBuilder retval = new StringBuilder();
retval.append(super.toString())
.append("\n host: ")
.append(hostName)
.append("\n app name: ")
.append(appName)
.append("\n instance: ")
.append(instance)
.append("\n update type: ")
.append(updateType)
.append("\n action name: ")
.append(actionName);
if (!Strings.isNullOrEmpty(actionMessage))
{
retval.append("\n action message: ")
.append(actionMessage);
}
return (retval.toString());
} // end of toString()
//
// end of Object Method Overrides.
//-----------------------------------------------------------
public static Builder builder()
{
return (new Builder());
} // end of builder()
//---------------------------------------------------------------
// Inner classes.
//
/**
* Builder for creating {@link MonitorUpdate} notification.
*/
public static final class Builder
extends ENotificationMessage.Builder
{
//-----------------------------------------------------------
// Member data.
//
//-------------------------------------------------------
// Locals.
//
private String mHost;
private String mAppName;
private MonitorId mInstance;
private UpdateType mUpdateType;
private ActionLevel mActionLevel;
private String mActionName;
private String mActionMsg;
//-----------------------------------------------------------
// Member methods.
//
//-------------------------------------------------------
// Constructors.
//
private Builder()
{
super (MonitorUpdate.class);
} // end of Builder()
//
// end of Constructors.
//-------------------------------------------------------
//-------------------------------------------------------
// Builder Method Overrides.
//
@Override
protected MonitorUpdate buildImpl()
{
return (new MonitorUpdate(this));
} // end of buildImpl()
@Override
protected Validator validate(final Validator problems)
{
return (super.validate(problems)
.requireNotNull(mHost, "hostName")
.requireNotNull(mAppName, "appName")
.requireNotNull(mInstance, "instance")
.requireNotNull(mUpdateType, "updateType")
.requireNotNull(mActionLevel, "actionLevel")
.requireNotNull(mActionName, "actionName"));
} // 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(
"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)
public Builder instance(final MonitorId id)
{
if (id == null)
{
throw (new NullPointerException("id is null"));
}
mInstance = id;
return (this);
} // end of instance(MonitorId)
public Builder updateType(final UpdateType type)
{
mUpdateType =
Objects.requireNonNull(type, "type is null");
return (this);
} // end of updateType(UpdateType)
public Builder actionLevel(final ActionLevel level)
{
mActionLevel =
Objects.requireNonNull(level, "level is null");
return (this);
} // end of actionLevel(ActionLevel)
public Builder actionName(final String actionName)
{
if (Strings.isNullOrEmpty(actionName))
{
throw (
new IllegalArgumentException(
"actionName is null or empty"));
}
mActionName = actionName;
return (this);
} // end of actionName(String)
public Builder actionMessage(final @Nullable String message)
{
mActionMsg = message;
return (this);
} // end of actionMessage(String)
//
// end of Set Methods.
//-------------------------------------------------------
} // end of class Builder
} // end of class MonitorUpdate