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 static class ParentInterfaceOptions {
 
         private String version;
 
         private String prefix;
 
 
         private boolean skipRegistration;
 
         public String getVersion() {
             return ;
         }
 
         public void setVersion(String version) {
             this. = version;
         }
 
         public String getPrefix() {
             return ;
         }
 
         public void setPrefix(String prefix) {
             this. = prefix;
         }
 
             return ;
         }
 
         public void setRegistrationOptions(ActivityRegistrationOptions options) {
             if (options != null) {
                 setSkipRegistration(false);
             }
             this. = options;
         }
 
         public boolean isSkipRegistration() {
             return ;
         }
 
         public void setSkipRegistration(boolean skipRegistration) {
             if (skipRegistration) {
                  = null;
             }
             this. = skipRegistration;
         }
 
     }
 
 
 
     private DataConverter dataConverter = new JsonDataConverter();
 
         super();
    }
    public POJOActivityImplementationFactory(Iterable<ObjectactivityImplementationObjects)
        this();
        addActivitiesImplementations(activityImplementationObjectsnull);
    }
    public POJOActivityImplementationFactory(Iterable<ObjectactivityImplementationObjectsDataConverter dataConverter)
        this();
        addActivitiesImplementations(activityImplementationObjectsdataConverter);
    }
    public DataConverter getDataConverter() {
        return ;
    }
    public void setDataConverter(DataConverter dataConverter) {
        if (dataConverter == null) {
            throw new IllegalArgumentException("null dataConverter");
        }
        this. = dataConverter;
    }
    public void setActivitiesImplementations(Iterable<ObjectactivitiesImplementations)
        addActivitiesImplementations(activitiesImplementationsnull);
    }
        List<Objectresult = new ArrayList<Object>();
        for (POJOActivityImplementation impl : .values()) {
            result.add(impl.getActivitiesImplementation());
        }
        return result;
    }
    public List<ActivityTypeaddActivitiesImplementations(Iterable<ObjectactivitiesImplementations)
        return addActivitiesImplementations(activitiesImplementationsnull);
    }
    public List<ActivityTypeaddActivitiesImplementations(Iterable<ObjectactivitiesImplementationsDataConverter dataConverter)
        List<ActivityTyperesult = new ArrayList<ActivityType>();
        for (Object activityImplementationObject : activitiesImplementations) {
            result.addAll(addActivitiesImplementation(activityImplementationObjectdataConverter));
        }
        return result;
    }
    public List<ActivityTypeaddActivitiesImplementation(Object activitiesImplementation)
        return addActivitiesImplementation(activitiesImplementationnull);
    }
    public List<ActivityTypeaddActivitiesImplementation(Object activitiesImplementationDataConverter converter)
        if (activitiesImplementation == null) {
            throw new IllegalArgumentException("activitiesImplementation is null.");
        }
        Set<ActivityTyperesult = new HashSet<ActivityType>();
        Set<Class<?>> activitiesInterfaces = new HashSet<Class<?>>();
        getImplementedInterfacesAnnotatedWithActivities(activitiesImplementation.getClass(), activitiesInterfaces);
        if (activitiesInterfaces.size() == 0) {
            throw new IllegalArgumentException(
                    "Activity implementation object does not implement any interface annotated with @Activities: "
                            + activitiesImplementation.getClass());
        }
        for (Class<?> interfaze : activitiesInterfaces) {
            Map<StringMethodmethods = new HashMap<StringMethod>();
            ParentInterfaceOptions parentOptions = new ParentInterfaceOptions();
            addActivities(activitiesImplementationinterfazemethodsparentOptionsconverterresult);
        }
        return new ArrayList<ActivityType>(result);
    }
    private void addActivities(Object implementationClass<?> interfazeMap<StringMethodmethods,
            ParentInterfaceOptions parentOptionsDataConverter converterSet<ActivityTypeaddedTypes)
        Activities activitiesAnnotation = interfaze.getAnnotation(Activities.class);
        for (Class<?> parent : interfaze.getInterfaces()) {
            addActivities(implementationparentmethodsparentOptionsconverteraddedTypes);
        }
        if (activitiesAnnotation != null) {
            String interfaceName = interfaze.getSimpleName();
            if (!nullOrEmpty(activitiesAnnotation.activityNamePrefix())) {
                parentOptions.setPrefix(activitiesAnnotation.activityNamePrefix());
            }
            if (!nullOrEmpty(activitiesAnnotation.version())) {
                parentOptions.setVersion(activitiesAnnotation.version());
            }
            converter = (converter == null) ? createConverter(activitiesAnnotation.dataConverter()) : converter;
            ActivityRegistrationOptions interfaceRegistrationOptionsAnnotation = interfaze.getAnnotation(ActivityRegistrationOptions.class);
            SkipTypeRegistration interfaceSkipTypeRegistrationAnnotation = interfaze.getAnnotation(SkipTypeRegistration.class);
            if (interfaceRegistrationOptionsAnnotation != null) {
                if (interfaceSkipTypeRegistrationAnnotation != null) {
                    throw new IllegalArgumentException(
                            "@ActivityRegistrationOptions is not allowed for the interface annotated with @SkipTypeRegistration.");
                }
                parentOptions.setRegistrationOptions(interfaceRegistrationOptionsAnnotation);
            }
            else if (interfaceSkipTypeRegistrationAnnotation != null) {
                parentOptions.setSkipRegistration(true);
            }
            for (Method method : interfaze.getMethods()) {
                if (!method.getDeclaringClass().equals(interfaze)) {
                    continue;
                }
                Activity activityAnnotation = method.getAnnotation(Activity.class);
                ActivityType activityType = getActivityType(interfaceNamemethodactivityAnnotationparentOptions);
                ActivityRegistrationOptions registrationOptionsAnnotation = method.getAnnotation(ActivityRegistrationOptions.class);
                SkipTypeRegistration skipTypeRegistrationAnnotation = interfaze.getAnnotation(SkipTypeRegistration.class);
                ActivityTypeRegistrationOptions registrationOptions = null;
                if (skipTypeRegistrationAnnotation != null) {
                    if (registrationOptionsAnnotation != null) {
                        throw new IllegalArgumentException(
                                "@ActivityRegistrationOptions is not allowed for the method annotated with @SkipTypeRegistration: "
                                        + method);
                    }
                }
                else {
                    if (registrationOptionsAnnotation != null || parentOptions.getRegistrationOptions() != null) {
                        POJOActivityImplementation existingImplementation = .get(activityType);
                        if (existingImplementation != null && !addedTypes.contains(activityType)) {
                            String message = "Duplicate declaration for activity type=" + activityType.getName() + ", version="
                                    + activityType.getVersion() + ": " + existingImplementation.getMethod() + " and " + method;
                            throw new IllegalArgumentException(message);
                        }
                        registrationOptions = createRegistrationOptions(registrationOptionsAnnotation,
                                parentOptions.getRegistrationOptions());
                    }
                    else if (!parentOptions.isSkipRegistration()) {
                        throw new IllegalArgumentException(
                                "No @ActivityRegistationOptions found either on interface or method for " + method);
                    }
                }
                //TODO: support methods defined in parents as well as overrides
                if (!addedTypes.contains(activityType)) {
                    Method activityImplementationMethod = implementation.getClass().getMethod(method.getName(),
                            method.getParameterTypes());
                    ActivityTypeExecutionOptions executionOptions = createExecutionOptions(activityTypeactivityImplementationMethod);
                    
                    POJOActivityImplementation activityImplementation = new POJOActivityImplementation(implementationmethod,
                            registrationOptionsexecutionOptionsconverter);
                    .add(activityType);
                    addedTypes.add(activityType);
                    .put(activityTypeactivityImplementation);
                }
            }
        }
    }
    @Override
        return ;
    }
    @Override
        return .get(activityType);
    }
    private DataConverter createConverter(Class<? extends DataConverterconverterType)
            throws InstantiationExceptionIllegalAccessException {
        if (converterType == null || converterType.equals(NullDataConverter.class)) {
            return  == null ? new JsonDataConverter() : ;
        }
        return converterType.newInstance();
    }
    private static ActivityType getActivityType(String interfaceNameMethod activityActivity activityAnnotation,
            ParentInterfaceOptions parentOptions) {
        ActivityType activityType = new ActivityType();
        String activityName = null;
        String activityVersion = null;
        if (activityAnnotation != null) {
            if (!nullOrEmpty(activityAnnotation.name())) {
                activityName = activityAnnotation.name();
            }
            if (!nullOrEmpty(activityAnnotation.version())) {
                activityVersion = activityAnnotation.version();
            }
        }
        if (activityName == null) {
            if (!nullOrEmpty(parentOptions.getPrefix())) {
                activityName = parentOptions.getPrefix() + activity.getName();
            }
            else {
                activityName = interfaceName + "." + activity.getName();
            }
        }
        if (activityVersion == null) {
            if (!nullOrEmpty(parentOptions.getVersion())) {
                activityVersion = parentOptions.getVersion();
            }
            else {
                throw new IllegalArgumentException("No version found for activity defined by " + activity);
            }
        }
        activityType.setName(activityName);
        activityType.setVersion(activityVersion);
        return activityType;
    }
    private static boolean nullOrEmpty(String nameFromAnnotation) {
        return nameFromAnnotation == null || nameFromAnnotation.isEmpty();
    }

    
Recursively find all interfaces annotated with

Activities:
that given class implements. Do not include interfaces that @Activities annotated interface extends.
    private void getImplementedInterfacesAnnotatedWithActivities(Class<?> implementationTypeSet<Class<?>> implementedInterfaces) {
        Class<?> superClass = implementationType.getSuperclass();
        if (superClass != null) {
            getImplementedInterfacesAnnotatedWithActivities(superClassimplementedInterfaces);
        }
        
        Class<?>[] interfaces = implementationType.getInterfaces();
        for (Class<?> i : interfaces) {
            if (i.getAnnotation(Activities.class) != null && !implementedInterfaces.contains(i)) {
                boolean skipAdd = removeSuperInterfaces(iimplementedInterfaces);
                if (!skipAdd) {
                    implementedInterfaces.add(i);
                }
            }
            else {
                getImplementedInterfacesAnnotatedWithActivities(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;
    }
            ActivityRegistrationOptions parentRegistrationOptions) {
        ActivityRegistrationOptions registrationOptionsAnnotation = registrationOptions != null ? registrationOptions
                : parentRegistrationOptions;
        result.setDescription(emptyStringToNull(registrationOptionsAnnotation.description()));
        long taskHeartbeatTimeoutSeconds = registrationOptionsAnnotation.defaultTaskHeartbeatTimeoutSeconds();
        if (taskHeartbeatTimeoutSeconds > .) {
            result.setDefaultTaskHeartbeatTimeoutSeconds(taskHeartbeatTimeoutSeconds);
        }
        long taskScheduleToCloseTimeoutSeconds = registrationOptionsAnnotation.defaultTaskScheduleToCloseTimeoutSeconds();
        if (taskScheduleToCloseTimeoutSeconds > .) {
            result.setDefaultTaskScheduleToCloseTimeoutSeconds(taskScheduleToCloseTimeoutSeconds);
        }
        long taskScheduleToStartTimeoutSeconds = registrationOptionsAnnotation.defaultTaskScheduleToStartTimeoutSeconds();
        if (taskScheduleToStartTimeoutSeconds > .) {
            result.setDefaultTaskScheduleToStartTimeoutSeconds(taskScheduleToStartTimeoutSeconds);
        }
        long taskStartToCloseTimeoutSeconds = registrationOptionsAnnotation.defaultTaskStartToCloseTimeoutSeconds();
        if (taskStartToCloseTimeoutSeconds > .) {
            result.setDefaultTaskStartToCloseTimeoutSeconds(taskStartToCloseTimeoutSeconds);
        }
        String taskList = registrationOptionsAnnotation.defaultTaskList();
        if (!taskList.equals(.)) {
            result.setDefaultTaskList(taskList);
        }
        else if (taskList.equals(.)) {
            result.setDefaultTaskList(null);
        }
        return result;
    }
    private static ActivityTypeExecutionOptions createExecutionOptions(ActivityType activityTypeMethod activityImplementation) {
        assert (activityType != null);
        ActivityTypeExecutionOptions executionOptions = new ActivityTypeExecutionOptions();
        if (activityImplementation != null) {
            ManualActivityCompletion manualCompletion = activityImplementation.getAnnotation(ManualActivityCompletion.class);
            executionOptions.setManualActivityCompletion(manualCompletion != null);
        }
        return executionOptions;
    }
    private static String emptyStringToNull(String value) {
        if (value.length() == 0) {
            return null;
        }
        return value;
    }
New to GrepCode? Check out our FAQ X