Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License"). You may not
   * use this file except in compliance with the License. A copy of the License is
   * located at
   * 
   * http://aws.amazon.com/apache2.0
   * 
  * or in the "license" file accompanying this file. This file 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 com.amazonaws.services.simpleworkflow.flow.pojo;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 
 
     private DataConverter converter = new JsonDataConverter();
 
 
 
     private final Collection<Class<?>> workflowImplementationTypes = new ArrayList<Class<?>>();
 
     public DataConverter getDataConverter() {
         return ;
     }
 
     public void setDataConverter(DataConverter converter) {
         this. = converter;
     }
 
     @Override
         return .get(workflowType);
     }
 
     @Override
         return ;
     }
 
     public void addWorkflowImplementationType(Class<?> workflowImplementationType)
             throws InstantiationExceptionIllegalAccessException {
         addWorkflowImplementationType(workflowImplementationTypenull);
     }
 
     public void addWorkflowImplementationType(Class<?> workflowImplementationTypeDataConverter converterOverride)
             throws InstantiationExceptionIllegalAccessException {
         if (workflowImplementationType.isInterface()) {
             throw new IllegalArgumentException(workflowImplementationType + " has to be a instantiatable class");
         }
         Set<Class<?>> implementedInterfaces = new HashSet<Class<?>>();
         getImplementedInterfacesAnnotatedWithWorkflow(workflowImplementationTypeimplementedInterfaces);
         if (implementedInterfaces.size() == 0) {
             throw new IllegalArgumentException("Workflow definition does not implement any @Workflow interface. "
                     + workflowImplementationType);
         }
         for (Class<?> interfaze : implementedInterfaces) {
             addWorkflowType(interfazeworkflowImplementationTypeconverterOverride);
         }
     }
 
     public void setWorkflowImplementationTypes(Collection<Class<?>> workflowImplementationTypes)
             throws InstantiationExceptionIllegalAccessException {
         for (Class<?> type : workflowImplementationTypes) {
             addWorkflowImplementationType(type);
         }
     }
 
        return ;
    }
    private void addWorkflowType(Class<?> interfazeClass<?> workflowImplementationTypeDataConverter converterOverride)
            throws InstantiationExceptionIllegalAccessException {
        Workflow workflowAnnotation = interfaze.getAnnotation(Workflow.class);
        String interfaceName = interfaze.getSimpleName();
        MethodConverterPair workflowImplementationMethod = null;
        MethodConverterPair getStateMethod = null;
        WorkflowType workflowType = null;
        WorkflowTypeRegistrationOptions registrationOptions = null;
        Map<StringMethodConverterPairsignals = new HashMap<StringMethodConverterPair>();
        for (Method method : interfaze.getMethods()) {
            if (method.getDeclaringClass().getAnnotation(Workflow.class) == null) {
                continue;
            }
            Execute executeAnnotation = method.getAnnotation(Execute.class);
            Signal signalAnnotation = method.getAnnotation(Signal.class);
            GetState getStateAnnotation = method.getAnnotation(GetState.class);
            checkAnnotationUniqueness(methodexecuteAnnotationsignalAnnotationgetStateAnnotation);
            if (executeAnnotation != null) {
                if (workflowImplementationMethod != null) {
                    throw new IllegalArgumentException(
                            "Interface annotated with @Workflow is allowed to have only one method annotated with @Execute. Found "
                                    + getMethodFullName(workflowImplementationMethod.getMethod()) + " and "
                                    + getMethodFullName(method));
                }
                if (!method.getReturnType().equals(void.class) && !(Promise.class.isAssignableFrom(method.getReturnType()))) {
                    throw new IllegalArgumentException(
                            "Workflow implementation method annotated with @Execute can return only Promise or void: "
                                    + getMethodFullName(method));
                }
                if (!method.getDeclaringClass().equals(interfaze)) {
                    throw new IllegalArgumentException("Interface " + interfaze.getName()
                            + " cannot inherit workflow implementation method annotated with @Execute: "
                            + getMethodFullName(method));
                }
                DataConverter converter = createConverter(workflowAnnotation.dataConverter(), converterOverride);
                workflowImplementationMethod = new MethodConverterPair(methodconverter);
                workflowType = getWorkflowType(interfaceNamemethodexecuteAnnotation);
                
                WorkflowRegistrationOptions registrationOptionsAnnotation = interfaze.getAnnotation(WorkflowRegistrationOptions.class);
                SkipTypeRegistration skipRegistrationAnnotation = interfaze.getAnnotation(SkipTypeRegistration.class);
                if (skipRegistrationAnnotation == null) {
                    if (registrationOptionsAnnotation == null) {
                        throw new IllegalArgumentException(
                                "@WorkflowRegistrationOptions is required for the interface that contains method annotated with @Execute");
                    }
                    registrationOptions = createRegistrationOptions(registrationOptionsAnnotation);
                }
                else {
                    if (registrationOptionsAnnotation != null) {
                        throw new IllegalArgumentException(
                                "@WorkflowRegistrationOptions is not allowed for the interface annotated with @SkipTypeRegistration.");
                    }
                }
            }
            if (signalAnnotation != null) {
                String signalName = signalAnnotation.name();
                if (signalName == null || signalName.isEmpty()) {
                    signalName = method.getName();
                }
                DataConverter signalConverter = createConverter(workflowAnnotation.dataConverter(), converterOverride);
                signals.put(signalNamenew MethodConverterPair(methodsignalConverter));
            }
            if (getStateAnnotation != null) {
                if (getStateMethod != null) {
                    throw new IllegalArgumentException(
                            "Interface annotated with @Workflow is allowed to have only one method annotated with @GetState. Found "
                                    + getMethodFullName(getStateMethod.getMethod()) + " and " + getMethodFullName(method));
                }
                if (method.getReturnType().equals(void.class) || (Promise.class.isAssignableFrom(method.getReturnType()))) {
                    throw new IllegalArgumentException(
                            "Workflow method annotated with @GetState cannot have void or Promise return type: "
                                    + getMethodFullName(method));
                }
                DataConverter converter = createConverter(workflowAnnotation.dataConverter(), converterOverride);
                getStateMethod = new MethodConverterPair(methodconverter);
            }
        }
        if (workflowImplementationMethod == null) {
            throw new IllegalArgumentException("Workflow definition does not implement any method annotated with @Execute. "
                    + workflowImplementationType);
        }
        POJOWorkflowImplementationFactory implementationFactory = getImplementationFactory(workflowImplementationTypeinterfaze,
                workflowType);
        WorkflowDefinitionFactory factory = new POJOWorkflowDefinitionFactory(implementationFactoryworkflowType,
                registrationOptionsworkflowImplementationMethodsignalsgetStateMethod);
        .put(workflowTypefactory);
        .add(workflowImplementationType);
        if (factory.getWorkflowRegistrationOptions() != null) {
            .add(workflowType);
        }
    }
    private void checkAnnotationUniqueness(Method methodObject... annotations) {
        List<ObjectnotNullOnes = new ArrayList<Object>();
        for (Object annotation : annotations) {
            if (annotation != null) {
                notNullOnes.add(annotation);
            }
        }
        if (notNullOnes.size() > 1) {
            throw new IllegalArgumentException("Method " + method.getName() + " is annotated with both " + notNullOnes);
        }
    }

    
Override to control how implementation is instantiated.

Parameters:
workflowImplementationType type that was registered with the factory
workflowInteface interface that defines external workflow contract
workflowType type of the workflow that implementation implements
Returns:
factory that creates new instances of the POJO that implements workflow
    protected POJOWorkflowImplementationFactory getImplementationFactory(final Class<?> workflowImplementationType,
            Class<?> workflowIntefaceWorkflowType workflowType) {
        return new POJOWorkflowImplementationFactory() {
            @Override
            public Object newInstance(DecisionContext decisionContextthrows Exception {
                return workflowImplementationType.newInstance();
            }
            @Override
            public void deleteInstance(Object instance) {
            }
        };
    }

    
Recursively find all interfaces annotated with

Workflow:
that given class implements. Don not include interfaces that @Workflow annotated interface extends.
    private void getImplementedInterfacesAnnotatedWithWorkflow(Class<?> workflowImplementationType,
            Set<Class<?>> implementedInterfaces) {
        Class<?> superClass = workflowImplementationType.getSuperclass();
        if (superClass != null) {
            getImplementedInterfacesAnnotatedWithWorkflow(superClassimplementedInterfaces);
        }
        
        Class<?>[] interfaces = workflowImplementationType.getInterfaces();
        for (Class<?> i : interfaces) {
            if (i.getAnnotation(Workflow.class) != null && !implementedInterfaces.contains(i)) {
                boolean skipAdd = removeSuperInterfaces(iimplementedInterfaces);
                if (!skipAdd) {
                    implementedInterfaces.add(i);
                }
            }
            else {
                getImplementedInterfacesAnnotatedWithWorkflow(iimplementedInterfaces);
            }
        }
    }
    
    private boolean removeSuperInterfaces(Class<?> interfaceToAddSet<Class<?>> implementedInterfaces) {
        boolean skipAdd = false;
        List<Class<?>> interfacesToRemove = new ArrayList<Class<?>>();
        for (Class<?> addedInterface : implementedInterfaces) {
            if (addedInterface.isAssignableFrom(interfaceToAdd)) {
                interfacesToRemove.add(addedInterface);
            }
            if (interfaceToAdd.isAssignableFrom(addedInterface)) {
                skipAdd = true;
            }
        }
        
        for (Class<?> interfaceToRemove : interfacesToRemove) {
            implementedInterfaces.remove(interfaceToRemove);
        }
        
        return skipAdd;
    }
    private static String getMethodFullName(Method m) {
        return m.getDeclaringClass().getName() + "." + m.getName();
    }
    private DataConverter createConverter(Class<? extends DataConverterconverterTypeFromAnnotation,
            DataConverter converterOverridethrows InstantiationExceptionIllegalAccessException {
        if (converterOverride != null) {
            return converterOverride;
        }
        if (converterTypeFromAnnotation == null || converterTypeFromAnnotation.equals(NullDataConverter.class)) {
            return ;
        }
        return converterTypeFromAnnotation.newInstance();
    }
    protected WorkflowType getWorkflowType(String interfaceNameMethod methodExecute executeAnnotation) {
        assert (method != null);
        assert (executeAnnotation != null);
        WorkflowType workflowType = new WorkflowType();
        String workflowName = null;
        if (executeAnnotation.name() != null && !executeAnnotation.name().isEmpty()) {
            workflowName = executeAnnotation.name();
        }
        else {
            workflowName = interfaceName + "." + method.getName();
        }
        if (executeAnnotation.version().isEmpty()) {
            throw new IllegalArgumentException(
                    "Empty value of the required \"version\" parameter of the @Execute annotation found on "
                            + getMethodFullName(method));
        }
        workflowType.setName(workflowName);
        workflowType.setVersion(executeAnnotation.version());
        return workflowType;
    }
        result.setDescription(emptyStringToNull(registrationOptionsAnnotation.description()));
        result.setDefaultTaskStartToCloseTimeoutSeconds(registrationOptionsAnnotation.defaultTaskStartToCloseTimeoutSeconds());
        String taskList = registrationOptionsAnnotation.defaultTaskList();
        if (!taskList.equals(.)) {
            result.setDefaultTaskList(taskList);
        }
        result.setDefaultChildPolicy(registrationOptionsAnnotation.defaultChildPolicy());
        return result;
    }
    private static String emptyStringToNull(String value) {
        if (value.length() == 0) {
            return null;
        }
        return value;
    }
New to GrepCode? Check out our FAQ X