
net.sf.eBus.client.sysmessages.AbstractKeyMessage Maven / Gradle / Ivy
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later
// version.
//
// This library is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the GNU Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General
// Public License along with this library; if not, write to the
//
// Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330,
// Boston, MA
// 02111-1307 USA
//
// The Initial Developer of the Original Code is Charles W. Rapp.
// Portions created by Charles W. Rapp are
// Copyright 2015, 2016. Charles W. Rapp
// All Rights Reserved.
//
package net.sf.eBus.client.sysmessages;
import java.io.Serializable;
import net.sf.eBus.messages.EFieldInfo;
import net.sf.eBus.messages.EMessage;
import net.sf.eBus.messages.EMessageKey;
import net.sf.eBus.messages.ESystemMessage;
import net.sf.eBus.messages.UnknownMessageException;
/**
* Base class for all system messages which contain a message
* class name and message subject. The reason for sending a
* message key as a separate message class name and subject is
* due to the receiving eBus application failing to de-serialize
* the message key because {@code Class.forName} throwing
* {@code ClassNotFoundException}. It is entirely reasonable for
* connected eBus applications not to use exactly the same
* message set. Therefore, if an eBus application receives a
* message class name for a class it doesn't know about, that
* inbound message will successfully deserialize but is ignored.
*
* @author Charles W. Rapp
*/
@EFieldInfo(fields = {"messageClass", "messageSubject"})
public abstract class AbstractKeyMessage
extends ESystemMessage
implements Serializable
{
//---------------------------------------------------------------
// Member methods.
//
//-----------------------------------------------------------
// Constructors.
//
/**
* Creates an outbound key message based on a message key.
* @param key message key.
* @throws IllegalArgumentException
* if {@code key} is {@code null}.
*
* @see #AbstractKeyMessage(Class, String)
* @see #AbstractKeyMessage(String, String)
* @see #AbstractKeyMessage(String, long, String, String)
*/
protected AbstractKeyMessage(final EMessageKey key)
throws IllegalArgumentException
{
super ();
if (key == null)
{
throw (new IllegalArgumentException("key is null"));
}
messageClass = (key.messageClass()).getName();
messageSubject = key.subject();
} // end of AbstractKeyMessage(EMessageKey)
/**
* Creates a new outbound key message for the given class and
* subject.
* @param mc message key class.
* @param msgSubject message key subject.
* @throws IllegalArgumentException
* if {@code mc} is {@code null} or {@code msgSubject} is
* {@code null} or empty.
*
* @see #AbstractKeyMessage(EMessageKey)
* @see #AbstractKeyMessage(String, String)
* @see #AbstractKeyMessage(String, long, String, String)
*/
protected AbstractKeyMessage(final Class extends EMessage> mc,
final String msgSubject)
throws IllegalArgumentException
{
super ();
if (mc == null)
{
throw (new IllegalArgumentException("mc is null"));
}
else if (msgSubject == null ||
msgSubject.isEmpty() == true)
{
throw (
new IllegalArgumentException(
"msgSubject is null or empty "));
}
this.messageClass = mc.getName();
this.messageSubject = msgSubject;
} // end of AbstractKeyMessage(Class, String)
/**
* Creates a new outbound key message for the given class
* name and subject.
* @param msgClass the message key class.
* @param msgSubject the message key subject.
* @throws IllegalArgumentException
* if {@code msgClass} or {@code msgSubject} is either
* {@code null} or empty.
*
* @see #AbstractKeyMessage(EMessageKey)
* @see #AbstractKeyMessage(Class, String)
* @see #AbstractKeyMessage(String, long, String, String)
*/
protected AbstractKeyMessage(final String msgClass,
final String msgSubject)
throws IllegalArgumentException
{
super ();
if (msgClass == null || msgClass.isEmpty() == true)
{
throw (
new IllegalArgumentException(
"msgClass is null or empty"));
}
else if (msgSubject == null ||
msgSubject.isEmpty() == true)
{
throw (
new IllegalArgumentException(
"msgSubject is null or empty "));
}
this.messageClass = msgClass;
this.messageSubject = msgSubject;
} // end of AbstractKeyMessage(String, String)
/**
* Creates a new message key instance based on the
* de-serialized information.
* @param subject {@link ESystemMessage#SYSTEM_SUBJECT}.
* @param timestamp message timestamp.
* @param msgClass the message class.
* @param msgSubject the message subject.
* @throws IllegalArgumentException
* if {@code subject}, {@code msgClass}, or
* {@code msgSubject} is either {@code null} or empty.
*
* @see #AbstractKeyMessage(EMessageKey)
* @see #AbstractKeyMessage(Class, String)
* @see #AbstractKeyMessage(String, String)
*/
protected AbstractKeyMessage(final String subject,
final long timestamp,
final String msgClass,
final String msgSubject)
throws IllegalArgumentException
{
super (subject, timestamp);
if (msgClass == null || msgClass.isEmpty() == true)
{
throw (
new IllegalArgumentException(
"msgClass is null or empty"));
}
else if (msgSubject == null ||
msgSubject.isEmpty() == true)
{
throw (
new IllegalArgumentException(
"msgSubject is null or empty "));
}
this.messageClass = msgClass;
this.messageSubject = msgSubject;
} // end of AbstractKeyMessage(String, long, String, String)
//
// end of Constructors.
//-----------------------------------------------------------
//-----------------------------------------------------------
// Object Method Overrides.
//
/**
* Returns {@code true} if {@code o} is a
* non-{@code null AdMessage} instance with ad status,
* message class, and message type equal to
* {@code this AdMessage} instance and {@code false}
* otherwise.
* @param o comparison object.
* @return {@code true} if the message fields are equal
* and {@code false} otherwise.
*/
@Override
public boolean equals(final Object o)
{
boolean retcode = (this == o);
if (retcode == false && o instanceof AbstractKeyMessage)
{
final AbstractKeyMessage keyMsg =
(AbstractKeyMessage) o;
retcode =
(super.equals(o) == true &&
messageClass.equals(
keyMsg.messageClass) == true &&
messageSubject.equals(
keyMsg.messageSubject) == true);
}
return (retcode);
} // end of equals(Object)
/**
* Returns the advertisement message hash code.
* @return the advertisement message hash code.
*/
@Override
public int hashCode()
{
return (
(((super.hashCode() * 37) +
messageClass.hashCode()) * 37) +
messageSubject.hashCode());
} // end of hashCode()
/**
* Returns a human-readable text version of this message.
* @return text version of this message.
*/
@Override
public String toString()
{
return (
String.format(
"%s%n message key: %s/%s",
super.toString(),
messageClass,
messageSubject));
} // end of toString()
//
// end of Object Method Overrides.
//-----------------------------------------------------------
//-----------------------------------------------------------
// Get Methods.
//
/**
* Returns the message key based on the message class name
* and subject.
* @return the message key.
* @throws UnknownMessageException
* if {@link #messageClass} is an unknown class name.
*/
@SuppressWarnings ("unchecked")
public final EMessageKey messageKey()
throws UnknownMessageException
{
final Class extends EMessage> mc;
try
{
mc =
(Class extends EMessage>)
Class.forName(messageClass);
}
catch (ClassNotFoundException classex)
{
throw (new UnknownMessageException(messageClass));
}
return (new EMessageKey(mc, messageSubject));
} // end of messageKey()
//
// end of Get Methods.
//-----------------------------------------------------------
//---------------------------------------------------------------
// Member data.
//
/**
* The message key class name. The class name is transmitted
* rather than the {@code Class} instance because the far-end
* deserialization will fail if the {@code Class} is
* unknown.
*/
public final String messageClass;
/**
* The message key subject.
*/
public final String messageSubject;
//-----------------------------------------------------------
// Constants.
//
/**
* Serialization version identifier.
*/
private static final long serialVersionUID = 0x060100L;
} // end of class AbstractKeyMessage
© 2015 - 2025 Weber Informatics LLC | Privacy Policy