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

com.sap.it.commons.logging.MessageTools Maven / Gradle / Ivy

The newest version!
package com.sap.it.commons.logging;

import java.text.MessageFormat;
import java.util.Collection;

import com.sap.it.commons.lang.Checks;
import com.sap.it.commons.lang.Equals;
import com.sap.it.commons.lang.Hash;
import com.sap.it.commons.layering.SubsystemPart;

public class MessageTools {

    private MessageTools() {

    }

    public static String assembleMessage(Message message) {
        return assembleMessage(message, (Object[]) null);
    }

    private static String assembleMessageInternal(Message message, boolean withCategory, Object... messageParameters) {
        // 1.step: assemble categories...
        Checks.nonNull(message, "message must not be null");

        StringBuilder lassembledMessage = new StringBuilder(128);

        if (withCategory) {
            MessageCategory mc = peekForMessageCategory(message);
            if (mc != null) {
                lassembledMessage.append("{");
                lassembledMessage.append(mc);
                lassembledMessage.append("}");
            }
        }

        appendMessageIdentifiers(lassembledMessage, message);
        // final double point
        lassembledMessage.append(":");

        String messageText = message.getMessageText();

        if (messageParameters != null) {
            lassembledMessage.append(MessageFormat.format(messageText, messageParameters));
        } else {
            lassembledMessage.append(messageText);
        }

        return lassembledMessage.toString();
    }

    public static String assembleMessage(Message message, Object... messageParameters) {
        return assembleMessageInternal(message, false, messageParameters);
    }

    private static int getMessageTextSize(Message message) {
        return 3 * 2 + //
                message.getSubsystemPart().getSubsystem().getName().length() + //
                message.getSubsystemPart().getName().length() + //
                message.getMessageId().length();
    }

    public static void appendMessageIdentifiers(StringBuilder sb, Message message) {
        appendMessageIdentifiers(sb, message.getSubsystemPart().getSubsystem().getName(), message.getSubsystemPart().getName(),
                message.getMessageId());
    }

    public static void appendMessageIdentifiers(StringBuilder sb, String subsystem, String subsystemPart, String messageId) {
        // Subsystem
        sb.append("[");
        sb.append(subsystem);
        sb.append("]");

        // SubsystemPart
        sb.append("[");
        sb.append(subsystemPart);
        sb.append("]");

        // MessageId
        sb.append("[");
        sb.append(messageId);
        sb.append("]");
    }

    public static String assembleMessageIdentifiers(Message message) {
        StringBuilder lassembledString = new StringBuilder(getMessageTextSize(message));
        appendMessageIdentifiers(lassembledString, message);
        return lassembledString.toString();
    }

    //
    // MessageInstance
    //
    public static MessageInstance createMessageInstance(Collection children, Message message, Object... messageParameters) {
        return new MessageInstance(children, message, messageParameters);
    }

    public static MessageInstance createMessageInstance(Message message, Object... messageParameters) {
        return new MessageInstance(message, messageParameters);
    }

    public static MessageInstance createMessageInstance(Message message) {
        return new MessageInstance(message);
    }

    /*
     * Typo in name. Use createMessageInstance
     */
    @Deprecated
    public static MessageInstance createMessageinstance(Message message) {
        return new MessageInstance(message);
    }

    public static String assembleMessage(MessageInstance messageinstance) {
        return assembleMessageInternal(messageinstance.getMessage(), false, messageinstance.getParameters());
    }

    public static String assembleMessageWithCategory(MessageInstance messageinstance) {
        return assembleMessageInternal(messageinstance.getMessage(), true, messageinstance.getParameters());
    }

    public static boolean isEnumBound(Message message) {
        return message != null && Enum.class.isAssignableFrom(message.getClass());
    }

    public static boolean isEnumBound(MessageInstance messageinstance) {
        return messageinstance != null && isEnumBound(messageinstance.getMessage());
    }

    public static int hashCode(Message a) {
        return a == null ? 0 : //
                Hash.hash(a.getSubsystemPart().getSubsystem().getName(), a.getSubsystemPart().getName(), a.getMessageId(),
                        a.getMessageText());
    }

    public static boolean equals(Message a, Message b) {
        if (a == b) {
            return true;
        }

        if (a == null || b == null) {
            return false;
        }

        SubsystemPart aS = a.getSubsystemPart();
        SubsystemPart bS = b.getSubsystemPart();

        return Equals.equals(aS.getSubsystem().getName(), bS.getSubsystem().getName()) && //
                Equals.equals(aS.getName(), bS.getName()) && //
                Equals.equals(a.getMessageId(), b.getMessageId()) && //
                Equals.equals(a.getMessageText(), b.getMessageText());
    }

    public static Message getMessage(MessageInstanceHolder messageInstanceHolder) {
        if (messageInstanceHolder == null) {
            return null;
        }

        MessageInstance mi = messageInstanceHolder.getMessageInstance();
        if (mi == null) {
            return null;
        }

        return mi.getMessage();
    }

    public static MessageInstance peekForMessageInstance(Object potentialHolder) {
        return potentialHolder instanceof MessageInstanceHolder ? ((MessageInstanceHolder) potentialHolder).getMessageInstance() : null;
    }

    public static MessageCategory peekForMessageCategory(Message message) {
        return message instanceof CategorizedMessage ? ((CategorizedMessage) message).getMessageCategory() : null;
    }

    public static Message asMessageByText(Message message) {
        if (message == null) {
            return null;
        }

        if (message instanceof MessageByText) {
            return message;
        }

        return new MessageByText(message);
    }

    private static class NullLogger implements Logger {

        @Override
        public void log(Message message, Object... messageParameter) {
        }

        @Override
        public void log(Message message) {
        }

        @Override
        public void log(MessageInstance messageInstance) {
        }

        @Override
        public void log(CategorizedMessage message, Object... messageParameter) {
        }

        @Override
        public void log(CategorizedMessage message) {
        }

        @Override
        public void log(String subsystem, String subsystemPart, String messageId, String messageText) {
        }

        @Override
        public void log(String subsystem, String subsystemPart, String messageId, String messageText, Object... messageParameter) {
        }

        @Override
        public void log(MessageCategory category, String subsystem, String subsystemPart, String messageId, String messageText) {
        }

        @Override
        public void log(MessageCategory category, String subsystem, String subsystemPart, String messageId, String messageText,
                Object... messageParameter) {
        }
    }

    public static final Logger NULL_LOGGER = new NullLogger();

    public static Message createMessage(String subsystem, String subsystemPart, String messageId, String messageText) {
        MessageByText message = new MessageByText(subsystem, subsystemPart, messageId, messageText);

        return message;
    }

    public static CategorizedMessage createCategorizedMessage(MessageCategory category, String subsystem, String subsystemPart,
            String messageId, String messageText) {
        MessageByText message = new MessageByText(category.name(), subsystem, subsystemPart, messageId, messageText);

        return message;
    }

    public static String assembleMessage(String subsystem, String subsystemPart, String messageId, String messageText,
            Object[] messageParameters) {
        // 1.step: assemble categories...
        Checks.nonNull(subsystem, "subSystem must not be null");
        Checks.nonNull(subsystemPart, "subSystemPart must not be null");
        Checks.nonNull(messageId, "messageId must not be null");

        StringBuilder lassembledMessage = new StringBuilder(128);

        appendMessageIdentifiers(lassembledMessage, subsystem, subsystemPart, messageId);
        // final double point
        lassembledMessage.append(":");

        if (messageParameters != null) {
            lassembledMessage.append(MessageFormat.format(messageText, messageParameters));
        } else {
            lassembledMessage.append(messageText);
        }

        return lassembledMessage.toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy