Search in sources :

Example 1 with MessageProcessor

use of com.nokia.dempsy.annotations.MessageProcessor in project Dempsy by Dempsy.

the class ClusterDefinition method validate.

public void validate() throws DempsyException {
    if (parent == null)
        throw new DempsyException("The parent ApplicationDefinition isn't set for the Cluster " + SafeString.valueOf(clusterName) + ". You need to initialize the parent ApplicationDefinition prior to validating");
    if (clusterName == null)
        throw new DempsyException("You must set the 'clusterName' when configuring a dempsy cluster for the application.");
    if (messageProcessorPrototype == null && adaptor == null)
        throw new DempsyException("A dempsy cluster must contain either an 'adaptor' or a message processor prototype. " + clusterId + " doesn't appear to be configure with either.");
    if (messageProcessorPrototype != null && adaptor != null)
        throw new DempsyException("A dempsy cluster must contain either an 'adaptor' or a message processor prototype but not both. " + clusterId + " appears to be configured with both.");
    if (messageProcessorPrototype != null) {
        if (!messageProcessorPrototype.getClass().isAnnotationPresent(MessageProcessor.class))
            throw new DempsyException("Attempting to set an instance of \"" + SafeString.valueOfClass(messageProcessorPrototype) + "\" within the " + ClusterDefinition.class.getSimpleName() + " for \"" + SafeString.valueOf(clusterId) + "\" but it isn't identified as a MessageProcessor. Please annotate the class.");
        Method[] methods = messageProcessorPrototype.getClass().getMethods();
        boolean foundAtLeastOneMethod = false;
        for (Method method : methods) {
            if (method.isAnnotationPresent(MessageHandler.class)) {
                foundAtLeastOneMethod = true;
                break;
            }
        }
        if (!foundAtLeastOneMethod)
            throw new DempsyException("No method on the message processor of type \"" + SafeString.valueOfClass(messageProcessorPrototype) + "\" is identified as a MessageHandler. Please annotate the appropriate method using @MessageHandler.");
        int startMethods = 0;
        for (Method method : methods) {
            if (method.isAnnotationPresent(Start.class)) {
                startMethods++;
            }
        }
        if (startMethods > 1)
            throw new DempsyException("Multiple methods on the message processor of type\"" + SafeString.valueOf(messageProcessorPrototype) + "\" is identified as a Start method. Please annotate at most one method using @Start.");
        Method[] evictableMethods = messageProcessorPrototype.getClass().getMethods();
        boolean foundEvictableMethod = false;
        Method evictableMethod = null;
        for (Method method : evictableMethods) {
            if (method.isAnnotationPresent(Evictable.class)) {
                if (foundEvictableMethod) {
                    throw new DempsyException("More than one method on the message processor of type \"" + SafeString.valueOfClass(messageProcessorPrototype) + "\" is identified as a Evictable. Please annotate the appropriate method using @Evictable.");
                }
                foundEvictableMethod = true;
                evictableMethod = method;
            }
        }
        if (evictableMethod != null) {
            if (evictableMethod.getReturnType() == null || !evictableMethod.getReturnType().isAssignableFrom(boolean.class))
                throw new DempsyException("Evictable method \"" + SafeString.valueOf(evictableMethod) + "\" on the message processor of type \"" + SafeString.valueOfClass(messageProcessorPrototype) + "\" should return boolean value. Please annotate the appropriate method using @Evictable.");
        }
    }
    if (adaptor != null && keySource != null) {
        throw new DempsyException("A dempsy cluster can not pre-instantation an adaptor.");
    }
}
Also used : MessageProcessor(com.nokia.dempsy.annotations.MessageProcessor) DempsyException(com.nokia.dempsy.DempsyException) Method(java.lang.reflect.Method)

Aggregations

DempsyException (com.nokia.dempsy.DempsyException)1 MessageProcessor (com.nokia.dempsy.annotations.MessageProcessor)1 Method (java.lang.reflect.Method)1