Search in sources :

Example 11 with NestedAnnotationAttributeValue

use of org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue in project spring-roo by spring-projects.

the class SecurityOperationsImpl method generateFilterAnnotations.

public void generateFilterAnnotations(JavaType klass, String methodName, String roles, String usernames, String when) {
    // Get methods to annotate.
    // With the last parameter to false, we avoid that push in action occurs.
    List<Object> pushedElements = getPushInOperations().pushIn(klass.getPackage(), klass, methodName, false);
    List<AnnotationAttributeValue<?>> rooSecurityFiltersToAdd = new ArrayList<AnnotationAttributeValue<?>>();
    for (Object pushedElement : pushedElements) {
        if (pushedElement instanceof DefaultMethodMetadata) {
            DefaultMethodMetadata method = (DefaultMethodMetadata) pushedElement;
            // Get parameters
            List<AnnotationAttributeValue<?>> lstParamTypes = new ArrayList<AnnotationAttributeValue<?>>();
            List<AnnotatedJavaType> parameterTypes = method.getParameterTypes();
            Iterator<AnnotatedJavaType> iterParamTypes = parameterTypes.iterator();
            while (iterParamTypes.hasNext()) {
                ClassAttributeValue parameterAttributeValue = new ClassAttributeValue(new JavaSymbolName("value"), iterParamTypes.next().getJavaType());
                lstParamTypes.add(parameterAttributeValue);
            }
            // Generate new annotations @RooSecurityFilter
            NestedAnnotationAttributeValue newFilter = new NestedAnnotationAttributeValue(new JavaSymbolName("value"), getRooSecurityFilterAnnotation(method.getMethodName().getSymbolName(), lstParamTypes, roles, usernames, when).build());
            rooSecurityFiltersToAdd.add(newFilter);
        }
    }
    // Get actual values of @RooSecurityFilters
    ClassOrInterfaceTypeDetails serviceDetails = getTypeLocationService().getTypeDetails(klass);
    ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(serviceDetails);
    // Check annotation @RooSecurityFilters to delete defined annotations
    // that will be redefined
    AnnotationMetadata annotationFilters = serviceDetails.getAnnotation(RooJavaType.ROO_SECURITY_FILTERS);
    AnnotationMetadataBuilder annotationFiltersMetadataBuilder;
    if (annotationFilters != null) {
        // Getting filters from annotation
        AnnotationAttributeValue<?> attributeFilters = annotationFilters.getAttribute("filters");
        List<?> values = (List<?>) attributeFilters.getValue();
        if (values != null && !values.isEmpty()) {
            Iterator<?> valuesIt = values.iterator();
            while (valuesIt.hasNext()) {
                NestedAnnotationAttributeValue filterAnnotation = (NestedAnnotationAttributeValue) valuesIt.next();
                if (checkRooSecurityFilterMaintainAnnotation(rooSecurityFiltersToAdd, filterAnnotation)) {
                    // Maintain annotation if 'method', 'parameters' or 'when' are different
                    rooSecurityFiltersToAdd.add(filterAnnotation);
                }
            }
        }
        annotationFiltersMetadataBuilder = new AnnotationMetadataBuilder(annotationFilters);
        // remove annotation
        cidBuilder.removeAnnotation(RooJavaType.ROO_SECURITY_FILTERS);
    } else {
        // Doesn't exist @RooSecurityFilters, create it
        annotationFiltersMetadataBuilder = new AnnotationMetadataBuilder(RooJavaType.ROO_SECURITY_FILTERS);
    }
    // Add filters attribute
    ArrayAttributeValue<AnnotationAttributeValue<?>> newFilters = new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("filters"), rooSecurityFiltersToAdd);
    annotationFiltersMetadataBuilder.addAttribute(newFilters);
    // Include new @RooSecurityFilters annotation
    cidBuilder.addAnnotation(annotationFiltersMetadataBuilder);
    // Write on disk
    getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
    // Add Spring Security dependency
    getProjectOperations().addDependency(klass.getModule(), SPRING_SECURITY_CORE, false);
}
Also used : ArrayAttributeValue(org.springframework.roo.classpath.details.annotations.ArrayAttributeValue) AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) ClassAttributeValue(org.springframework.roo.classpath.details.annotations.ClassAttributeValue) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) DefaultMethodMetadata(org.springframework.roo.classpath.details.DefaultMethodMetadata) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ArrayList(java.util.ArrayList) List(java.util.List) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 12 with NestedAnnotationAttributeValue

use of org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue in project spring-roo by spring-projects.

the class ExceptionsMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(String metadataIdentificationString, JavaType aspectName, PhysicalTypeMetadata governorPhysicalTypeMetadata, String itdFilename) {
    AnnotationMetadata handlersAnnotation = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails().getAnnotation(ROO_EXCEPTION_HANDLERS);
    // Prepare list to register @RooExceptionHandler annotations data
    List<ExceptionHandlerAnnotationValues> exceptionHandlerValues = new ArrayList<ExceptionHandlerAnnotationValues>();
    // Get nested annotations
    AnnotationAttributeValue<Object> handlers = handlersAnnotation.getAttribute("value");
    List<?> values = (List<?>) handlers.getValue();
    Iterator<?> valuesIt = values.iterator();
    // Iterate over nested annotations
    while (valuesIt.hasNext()) {
        NestedAnnotationAttributeValue handlerAnnotation = (NestedAnnotationAttributeValue) valuesIt.next();
        if (handlerAnnotation.getValue() != null) {
            // Get attribute values
            JavaType exception = getNestedAttributeValue(handlerAnnotation, "exception");
            ClassOrInterfaceTypeDetails exceptionDetails = getTypeLocationService().getTypeDetails(exception);
            Validate.notNull(exception, "'exception' attribute in @RooExceptionHandler must not be null");
            String errorView = getNestedAttributeValue(handlerAnnotation, "errorView");
            // Register attribute values
            exceptionHandlerValues.add(new ExceptionHandlerAnnotationValues(exceptionDetails, errorView));
        }
    }
    // Check if type is a controller
    AnnotationMetadata rooControllerAnnotation = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails().getAnnotation(RooJavaType.ROO_CONTROLLER);
    boolean isController = rooControllerAnnotation != null;
    List<FieldMetadata> fieldsMetadata = getMemberDetailsScanner().getMemberDetails(this.getClass().getName(), governorPhysicalTypeMetadata.getMemberHoldingTypeDetails()).getFields();
    return new ExceptionsMetadata(metadataIdentificationString, exceptionHandlerValues, aspectName, governorPhysicalTypeMetadata, fieldsMetadata, isController);
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ArrayList(java.util.ArrayList) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) ArrayList(java.util.ArrayList) List(java.util.List) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)

Example 13 with NestedAnnotationAttributeValue

use of org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue in project spring-roo by spring-projects.

the class JavaParserAnnotationMetadataBuilder method convert.

@SuppressWarnings("unchecked")
private static MemberValuePair convert(final AnnotationAttributeValue<?> value, CompilationUnitServices compilationUnitServices) {
    if (value instanceof NestedAnnotationAttributeValue) {
        final NestedAnnotationAttributeValue castValue = (NestedAnnotationAttributeValue) value;
        AnnotationExpr annotationExpr;
        final AnnotationMetadata nestedAnnotation = castValue.getValue();
        if (castValue.getValue().getAttributeNames().size() == 0) {
            annotationExpr = new MarkerAnnotationExpr(JavaParserUtils.getNameExpr(nestedAnnotation.getAnnotationType().getSimpleTypeName()));
        } else if (castValue.getValue().getAttributeNames().size() == 1 && (castValue.getValue().getAttributeNames().get(0) == null || "value".equals(castValue.getValue().getAttributeNames().get(0).getSymbolName()))) {
            annotationExpr = new SingleMemberAnnotationExpr(JavaParserUtils.getNameExpr(nestedAnnotation.getAnnotationType().getSimpleTypeName()), convert(nestedAnnotation.getAttribute(nestedAnnotation.getAttributeNames().get(0)), compilationUnitServices).getValue());
        } else {
            final List<MemberValuePair> memberValuePairs = new ArrayList<MemberValuePair>();
            for (final JavaSymbolName attributeName : nestedAnnotation.getAttributeNames()) {
                memberValuePairs.add(convert(nestedAnnotation.getAttribute(attributeName), compilationUnitServices));
            }
            annotationExpr = new NormalAnnotationExpr(JavaParserUtils.getNameExpr(nestedAnnotation.getAnnotationType().getSimpleTypeName()), memberValuePairs);
        }
        // Add imports for nested annotation types
        JavaParserUtils.importTypeIfRequired(compilationUnitServices.getEnclosingTypeName(), compilationUnitServices.getImports(), nestedAnnotation.getAnnotationType());
        // Rely on the nested instance to know its member value pairs
        return new MemberValuePair(value.getName().getSymbolName(), annotationExpr);
    }
    if (value instanceof BooleanAttributeValue) {
        final boolean castValue = ((BooleanAttributeValue) value).getValue();
        final BooleanLiteralExpr convertedValue = new BooleanLiteralExpr(castValue);
        return new MemberValuePair(value.getName().getSymbolName(), convertedValue);
    }
    if (value instanceof CharAttributeValue) {
        final char castValue = ((CharAttributeValue) value).getValue();
        final CharLiteralExpr convertedValue = new CharLiteralExpr(new String(new char[] { castValue }));
        return new MemberValuePair(value.getName().getSymbolName(), convertedValue);
    }
    if (value instanceof LongAttributeValue) {
        final Long castValue = ((LongAttributeValue) value).getValue();
        final LongLiteralExpr convertedValue = new LongLiteralExpr(castValue.toString() + "L");
        return new MemberValuePair(value.getName().getSymbolName(), convertedValue);
    }
    if (value instanceof IntegerAttributeValue) {
        final Integer castValue = ((IntegerAttributeValue) value).getValue();
        final IntegerLiteralExpr convertedValue = new IntegerLiteralExpr(castValue.toString());
        return new MemberValuePair(value.getName().getSymbolName(), convertedValue);
    }
    if (value instanceof DoubleAttributeValue) {
        final DoubleAttributeValue doubleAttributeValue = (DoubleAttributeValue) value;
        final Double castValue = doubleAttributeValue.getValue();
        DoubleLiteralExpr convertedValue;
        if (doubleAttributeValue.isFloatingPrecisionOnly()) {
            convertedValue = new DoubleLiteralExpr(castValue.toString() + "F");
        } else {
            convertedValue = new DoubleLiteralExpr(castValue.toString() + "D");
        }
        return new MemberValuePair(value.getName().getSymbolName(), convertedValue);
    }
    if (value instanceof StringAttributeValue) {
        final String castValue = ((StringAttributeValue) value).getValue();
        final StringLiteralExpr convertedValue = new StringLiteralExpr(castValue.toString());
        return new MemberValuePair(value.getName().getSymbolName(), convertedValue);
    }
    if (value instanceof EnumAttributeValue) {
        final EnumDetails castValue = ((EnumAttributeValue) value).getValue();
        // This isn't as elegant as it could be (ie loss of type
        // parameters), but it will do for now
        final FieldAccessExpr convertedValue = new FieldAccessExpr(JavaParserUtils.getNameExpr(castValue.getType().getFullyQualifiedTypeName()), castValue.getField().getSymbolName());
        return new MemberValuePair(value.getName().getSymbolName(), convertedValue);
    }
    if (value instanceof ClassAttributeValue) {
        final JavaType castValue = ((ClassAttributeValue) value).getValue();
        // This doesn't preserve type parameters
        final NameExpr nameExpr = JavaParserUtils.importTypeIfRequired(compilationUnitServices.getEnclosingTypeName(), compilationUnitServices.getImports(), castValue);
        final ClassExpr convertedValue = new ClassExpr(JavaParserUtils.getReferenceType(nameExpr));
        return new MemberValuePair(value.getName().getSymbolName(), convertedValue);
    }
    if (value instanceof ArrayAttributeValue) {
        final ArrayAttributeValue<AnnotationAttributeValue<?>> castValue = (ArrayAttributeValue<AnnotationAttributeValue<?>>) value;
        final List<Expression> arrayElements = new ArrayList<Expression>();
        for (final AnnotationAttributeValue<?> v : castValue.getValue()) {
            final MemberValuePair converted = convert(v, compilationUnitServices);
            if (converted != null) {
                arrayElements.add(converted.getValue());
            }
        }
        return new MemberValuePair(value.getName().getSymbolName(), new ArrayInitializerExpr(arrayElements));
    }
    throw new UnsupportedOperationException("Unsupported attribute value '" + value.getName() + "' of type '" + value.getClass().getName() + "'");
}
Also used : IntegerLiteralExpr(com.github.antlrjavaparser.api.expr.IntegerLiteralExpr) ClassAttributeValue(org.springframework.roo.classpath.details.annotations.ClassAttributeValue) AnnotationExpr(com.github.antlrjavaparser.api.expr.AnnotationExpr) MarkerAnnotationExpr(com.github.antlrjavaparser.api.expr.MarkerAnnotationExpr) SingleMemberAnnotationExpr(com.github.antlrjavaparser.api.expr.SingleMemberAnnotationExpr) NormalAnnotationExpr(com.github.antlrjavaparser.api.expr.NormalAnnotationExpr) DoubleAttributeValue(org.springframework.roo.classpath.details.annotations.DoubleAttributeValue) StringLiteralExpr(com.github.antlrjavaparser.api.expr.StringLiteralExpr) NameExpr(com.github.antlrjavaparser.api.expr.NameExpr) ArrayList(java.util.ArrayList) CharAttributeValue(org.springframework.roo.classpath.details.annotations.CharAttributeValue) EnumAttributeValue(org.springframework.roo.classpath.details.annotations.EnumAttributeValue) EnumDetails(org.springframework.roo.model.EnumDetails) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) SingleMemberAnnotationExpr(com.github.antlrjavaparser.api.expr.SingleMemberAnnotationExpr) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MemberValuePair(com.github.antlrjavaparser.api.expr.MemberValuePair) ArrayInitializerExpr(com.github.antlrjavaparser.api.expr.ArrayInitializerExpr) BooleanLiteralExpr(com.github.antlrjavaparser.api.expr.BooleanLiteralExpr) LongLiteralExpr(com.github.antlrjavaparser.api.expr.LongLiteralExpr) ArrayList(java.util.ArrayList) List(java.util.List) FieldAccessExpr(com.github.antlrjavaparser.api.expr.FieldAccessExpr) NormalAnnotationExpr(com.github.antlrjavaparser.api.expr.NormalAnnotationExpr) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) ArrayAttributeValue(org.springframework.roo.classpath.details.annotations.ArrayAttributeValue) AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) IntegerAttributeValue(org.springframework.roo.classpath.details.annotations.IntegerAttributeValue) CharLiteralExpr(com.github.antlrjavaparser.api.expr.CharLiteralExpr) MarkerAnnotationExpr(com.github.antlrjavaparser.api.expr.MarkerAnnotationExpr) BooleanAttributeValue(org.springframework.roo.classpath.details.annotations.BooleanAttributeValue) JavaType(org.springframework.roo.model.JavaType) LongAttributeValue(org.springframework.roo.classpath.details.annotations.LongAttributeValue) DoubleLiteralExpr(com.github.antlrjavaparser.api.expr.DoubleLiteralExpr) Expression(com.github.antlrjavaparser.api.expr.Expression) ClassExpr(com.github.antlrjavaparser.api.expr.ClassExpr)

Example 14 with NestedAnnotationAttributeValue

use of org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue in project spring-roo by spring-projects.

the class WsClientsMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    RooWsClientsAnnotationValues annotationValues = new RooWsClientsAnnotationValues(governorPhysicalTypeMetadata);
    // Getting @RooWsClients annotation
    ClassOrInterfaceTypeDetails cid = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails();
    AnnotationMetadata wsClientsAnnotation = cid.getAnnotation(ROO_WS_CLIENTS);
    // Getting profile from annotation
    String profile = annotationValues.getProfile();
    // Getting endpoints from annotation
    List<WsClientEndpoint> endpoints = new ArrayList<WsClientEndpoint>();
    AnnotationAttributeValue<?> currentEndpoints = wsClientsAnnotation.getAttribute("endpoints");
    if (currentEndpoints != null) {
        List<?> values = (List<?>) currentEndpoints.getValue();
        Iterator<?> valuesIt = values.iterator();
        while (valuesIt.hasNext()) {
            NestedAnnotationAttributeValue wsClientAnnotation = (NestedAnnotationAttributeValue) valuesIt.next();
            if (wsClientAnnotation.getValue() != null && wsClientAnnotation.getValue().getAttribute("endpoint") != null) {
                // Get endpoint name
                String endpointName = null;
                if (wsClientAnnotation.getValue().getAttribute("endpoint").getValue() instanceof String) {
                    endpointName = (String) wsClientAnnotation.getValue().getAttribute("endpoint").getValue();
                }
                Validate.notNull(endpointName, "'endpoint' attribute in @RooWsClient must be a String");
                // Get endpoint nameSpace
                String endpointNameSpace = null;
                if (wsClientAnnotation.getValue().getAttribute("targetNamespace").getValue() instanceof String) {
                    endpointNameSpace = (String) wsClientAnnotation.getValue().getAttribute("targetNamespace").getValue();
                }
                Validate.notNull(endpointName, "'targetNamespace' attribute in @RooWsClient must be a String");
                // Get endpoint binding type
                EnumDetails endpointBindingType = null;
                if (wsClientAnnotation.getValue().getAttribute("binding").getValue() instanceof EnumDetails) {
                    endpointBindingType = (EnumDetails) wsClientAnnotation.getValue().getAttribute("binding").getValue();
                }
                Validate.notNull(endpointBindingType, "'binding' attribute in @RooWsClient must be a SoapBindingType");
                endpoints.add(new WsClientEndpoint(endpointName, endpointNameSpace, endpointBindingType));
            }
        }
    }
    return new WsClientsMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, endpoints, profile);
}
Also used : ArrayList(java.util.ArrayList) EnumDetails(org.springframework.roo.model.EnumDetails) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) ArrayList(java.util.ArrayList) List(java.util.List) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)

Example 15 with NestedAnnotationAttributeValue

use of org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue in project spring-roo by spring-projects.

the class WsOperationsImpl method addWsClient.

@Override
public void addWsClient(String wsdlLocation, String endPoint, JavaType configClass, SoapBindingType bindingType, String serviceUrl, String profile) {
    Validate.notEmpty(wsdlLocation, "ERROR: Provide a valid wsdlLocation");
    Validate.notEmpty(endPoint, "ERROR: Provide a valid endPoint");
    Validate.notNull(configClass, "ERROR: Provide a valid configClass");
    // Check if the configClass is located in an application module
    if (!isLocatedInApplicationModule(configClass)) {
        LOGGER.log(Level.INFO, "ERROR: The provided config class is not located in an application module.");
        return;
    }
    // Getting module from the .wsdl file
    final Pom wsdlModule = getModuleFromWsdlLocation(wsdlLocation);
    final String wsdlModuleName = wsdlModule.getModuleName();
    // Getting the wsdlName without the module
    final String wsdlName = getWsdlNameWithoutModule(wsdlLocation);
    // Getting wsdl absolute path from the provided wsdlLocation
    final String wsdlPath = getWsdlAbsolutePathFromWsdlName(wsdlLocation);
    // Check if provided .wsdl exists
    Validate.isTrue(getFileManager().exists(wsdlPath), "ERROR: You must provide an existing .wsdl file.");
    // the configClass module and the .wsdl module
    if (wsdlModuleName != configClass.getModule()) {
        getProjectOperations().addDependency(configClass.getModule(), new Dependency(wsdlModule.getGroupId(), wsdlModule.getArtifactId(), null));
    }
    // Check if provided configClass exists or should be generated
    boolean isNewConfigClass = false;
    ClassOrInterfaceTypeDetails configClassDetails = getTypeLocationService().getTypeDetails(configClass);
    if (configClassDetails == null) {
        isNewConfigClass = true;
    }
    // If it have it, it should match with the provided one the provided one
    if (!isNewConfigClass) {
        MemberDetails configClassMemberDetails = getMemberDetailsScanner().getMemberDetails(getClass().getName(), configClassDetails);
        AnnotationMetadata configurationAnnotation = configClassMemberDetails.getAnnotation(SpringJavaType.CONFIGURATION);
        if (configurationAnnotation == null) {
            LOGGER.log(Level.INFO, "ERROR: The provided class is not annotated with @Configuration so is not possible to include Web Service client configuration on it." + "Specify other configuration class that contains @Configuration annotation or specify a not existing class to generate it.");
            return;
        }
        if (StringUtils.isNotEmpty(profile)) {
            AnnotationMetadata profileAnnotation = configClassMemberDetails.getAnnotation(SpringJavaType.PROFILE);
            if (profileAnnotation != null) {
                String profiles = (String) profileAnnotation.getAttribute("value").getValue();
                String[] definedProfiles = profiles.split(",");
                boolean profileExists = false;
                for (String definedProfile : definedProfiles) {
                    if (definedProfile.equals(profile)) {
                        profileExists = true;
                    }
                }
                if (!profileExists) {
                    LOGGER.log(Level.INFO, "ERROR: The provided configuration class doesn't work in the provided profile. " + "Use a different configuration class or use a different profile.");
                    return;
                }
            }
        }
    }
    // Obtain the service URL from the provided .wsdl file if empty
    if (StringUtils.isEmpty(serviceUrl)) {
        serviceUrl = getServiceUrlForEndpointFromWsdlFile(endPoint, wsdlPath);
        Validate.notEmpty(serviceUrl, "ERROR: It has not been possible to obtain the URL of the service from the provided .wsdl file. Indicate some serviceUrl using --serviceUrl parameter");
    }
    // Obtain the binding type from the provided .wsdl file if empty
    if (bindingType == null) {
        bindingType = getBindingTypeFromWsdlFile(wsdlPath);
        Validate.notNull(bindingType, "ERROR: It has not been possible to obtain the BindingType of the service from the provided .wsdl file. Indicate an specific BindingType using --binding parameter");
    }
    // Always is necessary to obtain the targetNameSpace from the provided .wsdl file
    String targetNameSpace = getTargetNameSpaceFromWsdlFile(wsdlPath);
    Validate.notEmpty(targetNameSpace, "ERROR: It has not been possible to obtain the targetNamespace of the service from the provided .wsdl file. Check if your .wsdl file has the correct format.");
    // Include necessary dependencies and plugins
    includeDependenciesAndPluginsForWsClient(wsdlName, wsdlModuleName);
    // Include the necessary properties using the provided profile
    getApplicationConfigService().addProperty(configClass.getModule(), "url/".concat(endPoint), serviceUrl, profile, true);
    // Generating the new configuration class if not exists
    // If provided class already exists, update it
    ClassOrInterfaceTypeDetailsBuilder cidBuilder = null;
    if (!isNewConfigClass) {
        // Obtain builder from the existing class
        cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(configClassDetails);
        // Check if already have @RooWsClients annotation
        AnnotationMetadataBuilder wsClientsAnnotation = cidBuilder.getDeclaredTypeAnnotation(RooJavaType.ROO_WS_CLIENTS);
        if (wsClientsAnnotation != null) {
            // Update the existing one
            AnnotationAttributeValue<?> existingEndPoints = wsClientsAnnotation.build().getAttribute("endpoints");
            List<?> values = (List<?>) existingEndPoints.getValue();
            if (values != null) {
                // Check if the provided endpoint exists yet in this config class
                Iterator<?> it = values.iterator();
                boolean alreadyManaged = false;
                while (it.hasNext()) {
                    NestedAnnotationAttributeValue existingEndPoint = (NestedAnnotationAttributeValue) it.next();
                    String existingEndpointName = (String) existingEndPoint.getValue().getAttribute("endpoint").getValue();
                    if (existingEndpointName.equals(endPoint)) {
                        alreadyManaged = true;
                    }
                }
                // If endpoint already exists, show an error indicating that this endpoint is already managed
                if (alreadyManaged) {
                    LOGGER.log(Level.INFO, "ERROR: The provided endpoint is already defined in the provided configuration class. " + "Specify some different configuration class.");
                    return;
                } else {
                    // Update existing annotation with the new endPoint
                    Iterator<?> iterator = values.iterator();
                    List<AnnotationAttributeValue<?>> endpoints = new ArrayList<AnnotationAttributeValue<?>>();
                    while (iterator.hasNext()) {
                        NestedAnnotationAttributeValue existingEndPoint = (NestedAnnotationAttributeValue) iterator.next();
                        String existingEndpointName = (String) existingEndPoint.getValue().getAttribute("endpoint").getValue();
                        String existingEndpointNameSpace = (String) existingEndPoint.getValue().getAttribute("targetNamespace").getValue();
                        EnumDetails existingType = (EnumDetails) existingEndPoint.getValue().getAttribute("binding").getValue();
                        // Create @RooWsClient annotation
                        NestedAnnotationAttributeValue existingEndpoint = new NestedAnnotationAttributeValue(new JavaSymbolName("value"), getWsClientAnnotation(existingEndpointName, existingEndpointNameSpace, existingType).build());
                        endpoints.add(existingEndpoint);
                    }
                    // Create @RooWsClient annotation
                    NestedAnnotationAttributeValue newEndpoint = new NestedAnnotationAttributeValue(new JavaSymbolName("value"), getWsClientAnnotation(endPoint, targetNameSpace, bindingType).build());
                    endpoints.add(newEndpoint);
                    ArrayAttributeValue<AnnotationAttributeValue<?>> newEndpoints = new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("endpoints"), endpoints);
                    wsClientsAnnotation.addAttribute(newEndpoints);
                }
            }
        } else {
            // If not exists, add it with the new elements
            wsClientsAnnotation = new AnnotationMetadataBuilder(new JavaType(RooWsClients.class));
            // Create @RooWsClient annotation
            List<AnnotationAttributeValue<?>> endpoints = new ArrayList<AnnotationAttributeValue<?>>();
            NestedAnnotationAttributeValue newEndpoint = new NestedAnnotationAttributeValue(new JavaSymbolName("value"), getWsClientAnnotation(endPoint, targetNameSpace, bindingType).build());
            endpoints.add(newEndpoint);
            ArrayAttributeValue<AnnotationAttributeValue<?>> newEndpoints = new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("endpoints"), endpoints);
            wsClientsAnnotation.addAttribute(newEndpoints);
            if (StringUtils.isNotEmpty(profile)) {
                wsClientsAnnotation.addStringAttribute("profile", profile);
            }
            // Include new @RooWsClients annotation
            cidBuilder.addAnnotation(wsClientsAnnotation);
        }
    } else {
        // Create new configuration class
        final String configClassIdentifier = getPathResolver().getCanonicalPath(configClass.getModule(), Path.SRC_MAIN_JAVA, configClass);
        final String mid = PhysicalTypeIdentifier.createIdentifier(configClass, getPathResolver().getPath(configClassIdentifier));
        cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(mid, Modifier.PUBLIC, configClass, PhysicalTypeCategory.CLASS);
        // Create new @RooWsClients annotation
        AnnotationMetadataBuilder wsClientsAnnotation = new AnnotationMetadataBuilder(new JavaType(RooWsClients.class));
        // Create @RooWsClient annotation
        List<AnnotationAttributeValue<?>> endpoints = new ArrayList<AnnotationAttributeValue<?>>();
        NestedAnnotationAttributeValue newEndpoint = new NestedAnnotationAttributeValue(new JavaSymbolName("value"), getWsClientAnnotation(endPoint, targetNameSpace, bindingType).build());
        endpoints.add(newEndpoint);
        ArrayAttributeValue<AnnotationAttributeValue<?>> newEndpoints = new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("endpoints"), endpoints);
        wsClientsAnnotation.addAttribute(newEndpoints);
        if (StringUtils.isNotEmpty(profile)) {
            wsClientsAnnotation.addStringAttribute("profile", profile);
        }
        // Include new @RooWsClients annotation
        cidBuilder.addAnnotation(wsClientsAnnotation);
    }
    getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
    // Compile project to be able to generate necessary resources.
    // Is necessary to create new thread and wat for it.
    Thread generateSourcesThread = new Thread() {

        public void run() {
            try {
                Thread.sleep(1000);
                final StringBuilder sb = new StringBuilder();
                sb.append(LINE_SEPARATOR);
                sb.append(LINE_SEPARATOR);
                sb.append("##########################################################").append(LINE_SEPARATOR);
                sb.append("##########################################################").append(LINE_SEPARATOR);
                sb.append("################# Generating client sources ##############").append(LINE_SEPARATOR);
                sb.append("##########################################################").append(LINE_SEPARATOR);
                sb.append("##########################################################").append(LINE_SEPARATOR);
                sb.append("#").append(LINE_SEPARATOR);
                sb.append("# Please wait...").append(LINE_SEPARATOR);
                sb.append("# Don't execute any command until this operation finishes.").append(LINE_SEPARATOR);
                sb.append("#").append(LINE_SEPARATOR);
                sb.append(LINE_SEPARATOR);
                sb.append(LINE_SEPARATOR);
                LOGGER.log(Level.INFO, sb.toString());
                // Changing focus to the module where the .wsdl file is located
                getProjectOperations().setModule(wsdlModule);
                // executing mvn generate-sources command
                getMavenOperations().executeMvnCommand("generate-sources");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };
    generateSourcesThread.start();
}
Also used : ArrayList(java.util.ArrayList) EnumDetails(org.springframework.roo.model.EnumDetails) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) List(java.util.List) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) ArrayAttributeValue(org.springframework.roo.classpath.details.annotations.ArrayAttributeValue) AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) Dependency(org.springframework.roo.project.Dependency) IOException(java.io.IOException) Pom(org.springframework.roo.project.maven.Pom) RooWsClients(org.springframework.roo.addon.ws.annotations.RooWsClients) RooJavaType(org.springframework.roo.model.RooJavaType) SpringJavaType(org.springframework.roo.model.SpringJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Aggregations

NestedAnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)19 ArrayList (java.util.ArrayList)16 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)15 List (java.util.List)14 AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)14 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)14 JavaType (org.springframework.roo.model.JavaType)10 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)9 ArrayAttributeValue (org.springframework.roo.classpath.details.annotations.ArrayAttributeValue)9 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)8 ClassAttributeValue (org.springframework.roo.classpath.details.annotations.ClassAttributeValue)8 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)5 StringAttributeValue (org.springframework.roo.classpath.details.annotations.StringAttributeValue)5 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)5 EnumDetails (org.springframework.roo.model.EnumDetails)5 RooJavaType (org.springframework.roo.model.RooJavaType)5 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)4 LinkedHashMap (java.util.LinkedHashMap)3 AnnotationExpr (com.github.antlrjavaparser.api.expr.AnnotationExpr)2 ArrayInitializerExpr (com.github.antlrjavaparser.api.expr.ArrayInitializerExpr)2