use of org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder in project spring-roo by spring-projects.
the class JmsOperationsImpl method addJmsSender.
@Override
public void addJmsSender(String destinationName, JavaType classSelected, String jndiConnectionFactory, String profile, boolean force) {
// Check that module included in destionationName is an application module
String module = "";
String destination = "";
if (getProjectOperations().isMultimoduleProject()) {
Collection<String> moduleNames = getTypeLocationService().getModuleNames(ModuleFeatureName.APPLICATION);
// if only have one module, select this, else check the parameter
if (moduleNames.size() > 1) {
// user select a module
if (destinationName.contains(":")) {
int charSeparation = destinationName.indexOf(":");
if (charSeparation > 0 && destinationName.length() > charSeparation) {
module = destinationName.substring(0, charSeparation);
destination = destinationName.substring(charSeparation + 1, destinationName.length());
if (!moduleNames.contains(module)) {
// error, is necessary select an application module
throw new IllegalArgumentException(String.format("Module '%s' must be of application type. Select one in --destinationName parameter", module));
}
} else {
// error, is necessary select an application module and destination
throw new IllegalArgumentException(String.format("--destinationName parameter must be composed by [application type module]:[destination] or focus module must be application type module and only write the destination name"));
}
} else {
// module not selected, check if focus module is application type
Pom focusedModule = getProjectOperations().getFocusedModule();
if (getTypeLocationService().hasModuleFeature(focusedModule, ModuleFeatureName.APPLICATION)) {
module = focusedModule.getModuleName();
destination = destinationName;
} else {
throw new IllegalArgumentException(String.format("--destinationName parameter must be composed by [application type module]:[destination] or focus module must be application type module and only write the destination name"));
}
}
} else {
if (moduleNames.isEmpty()) {
// error, is necessary select an application module
throw new IllegalArgumentException(String.format("Is necessary to have at least an application type module."));
} else {
module = moduleNames.iterator().next();
destination = destinationName;
}
}
} else {
destination = destinationName;
}
// Add jms springlets dependecies
getProjectOperations().addDependency(classSelected.getModule(), DEPENDENCY_SPRINGLETS_JMS);
// Include springlets-boot-starter-mail in module
getProjectOperations().addDependency(module, DEPENDENCY_SPRINGLETS_STARTER_JMS);
// Include property version
getProjectOperations().addProperty("", PROPERTY_SPRINGLETS_VERSION);
// Add instance of springlets service to service defined by parameter
// Add JavaMailSender to service
final ClassOrInterfaceTypeDetails serviceTypeDetails = getTypeLocationService().getTypeDetails(classSelected);
Validate.isTrue(serviceTypeDetails != null, "Cannot locate source for '%s'", classSelected.getFullyQualifiedTypeName());
final String declaredByMetadataId = serviceTypeDetails.getDeclaredByMetadataId();
final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(serviceTypeDetails);
// Create service field
cidBuilder.addField(new FieldMetadataBuilder(declaredByMetadataId, PRIVATE, Arrays.asList(new AnnotationMetadataBuilder(AUTOWIRED)), new JavaSymbolName("jmsSendingService"), SpringletsJavaType.SPRINGLETS_JMS_SENDING_SERVICE));
// Set destionation property name
StringBuffer destinationNamePropertyName = new StringBuffer(JMS_PROPERTY_DESTINATION_NAME_PREFIX);
destinationNamePropertyName.append(destination.replaceAll("/", "."));
destinationNamePropertyName.append(JMS_PROPERTY_DESTINATION_NAME_SUFIX);
StringBuffer destionationNameVar = new StringBuffer(JMS_VAR_DESTINATION_NAME_PREFIX);
if (destination.contains("/")) {
// Delete char '/' and each word
String[] destinationNameArray = destination.split("/");
for (String destinationFragment : destinationNameArray) {
destionationNameVar.append(StringUtils.capitalize(destinationFragment));
}
} else {
destionationNameVar.append(StringUtils.capitalize(destination));
}
destionationNameVar.append(JMS_VAR_DESTINATION_NAME_SUFIX);
// Adding @Value annotation
AnnotationMetadataBuilder valueAnnotation = new AnnotationMetadataBuilder(SpringJavaType.VALUE);
valueAnnotation.addStringAttribute("value", "${".concat(destinationNamePropertyName.toString()).concat("}"));
// Add instance of destination name
cidBuilder.addField(new FieldMetadataBuilder(declaredByMetadataId, PRIVATE, Arrays.asList(valueAnnotation), new JavaSymbolName(destionationNameVar.toString()), JavaType.STRING));
// Write both, springlets service and destination instance
getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
// Set properties
setProperties(destination, destinationNamePropertyName.toString(), jndiConnectionFactory, module, profile, force);
}
use of org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder in project spring-roo by spring-projects.
the class SecurityOperationsImpl method generateAuthorizeAnnotations.
@Override
public void generateAuthorizeAnnotations(JavaType klass, String methodName, String roles, String usernames) {
Validate.notNull(klass, "ERROR: klass parameter is mandatory on 'generateAuthorizeAnnotations' method");
Validate.notNull(methodName, "ERROR: method parameter is mandatory on 'generateAuthorizeAnnotations' method");
// 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<?>> rooSecurityAuthorizationsToAdd = 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 @RooSecurityAuthorization
NestedAnnotationAttributeValue newFilter = new NestedAnnotationAttributeValue(new JavaSymbolName("value"), getRooSecurityAuthorizationsAnnotation(method.getMethodName().getSymbolName(), lstParamTypes, roles, usernames).build());
rooSecurityAuthorizationsToAdd.add(newFilter);
}
}
// Get actual values of @RooSecurityAuthorizations
ClassOrInterfaceTypeDetails serviceDetails = getTypeLocationService().getTypeDetails(klass);
ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(serviceDetails);
// Check annotation @RooSecurityAuthorizations to delete defined annotations
// that will be redefined
AnnotationMetadata annotationAuthorizations = serviceDetails.getAnnotation(RooJavaType.ROO_SECURITY_AUTHORIZATIONS);
AnnotationMetadataBuilder annotationAuthorizationsMetadataBuilder;
if (annotationAuthorizations != null) {
// Getting authorizations from annotation
AnnotationAttributeValue<?> attributeAuthorizations = annotationAuthorizations.getAttribute("authorizations");
List<?> values = (List<?>) attributeAuthorizations.getValue();
if (values != null && !values.isEmpty()) {
Iterator<?> valuesIt = values.iterator();
while (valuesIt.hasNext()) {
NestedAnnotationAttributeValue authorizationAnnotation = (NestedAnnotationAttributeValue) valuesIt.next();
if (checkRooSecurityAuthorizationMaintainAnnotation(rooSecurityAuthorizationsToAdd, authorizationAnnotation)) {
// Maintain annotation if 'method' or 'parameters' are different
rooSecurityAuthorizationsToAdd.add(authorizationAnnotation);
}
}
}
annotationAuthorizationsMetadataBuilder = new AnnotationMetadataBuilder(annotationAuthorizations);
// remove annotation
cidBuilder.removeAnnotation(RooJavaType.ROO_SECURITY_AUTHORIZATIONS);
} else {
// Doesn't exist @RooSecurityAuthorizations, create it
annotationAuthorizationsMetadataBuilder = new AnnotationMetadataBuilder(RooJavaType.ROO_SECURITY_AUTHORIZATIONS);
}
// Add authorizations attribute
ArrayAttributeValue<AnnotationAttributeValue<?>> newAuthorizations = new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("authorizations"), rooSecurityAuthorizationsToAdd);
annotationAuthorizationsMetadataBuilder.addAttribute(newAuthorizations);
// Include new @RooSecurityAuthorizations annotation
cidBuilder.addAnnotation(annotationAuthorizationsMetadataBuilder);
// Write on disk
getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
// Add Spring Security dependency
getProjectOperations().addDependency(klass.getModule(), SPRING_SECURITY_CORE, false);
}
use of org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder 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);
}
use of org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder in project spring-roo by spring-projects.
the class ExceptionsOperationsImpl method createControllerAdviceIfRequired.
/**
* Creates a new class annotated with @ControllerAdvice if not exists. Returns true if success
* or class already exists.
*
* Returns false if class already exists but it's not annotated with @ControllerAdvice.
*
* @param controllerAdviceClass
*/
private boolean createControllerAdviceIfRequired(JavaType controllerAdviceClass) {
// Checks if new service interface already exists.
final String controllerAdviceClassIdentifier = getPathResolver().getCanonicalPath(controllerAdviceClass.getModule(), Path.SRC_MAIN_JAVA, controllerAdviceClass);
if (!getFileManager().exists(controllerAdviceClassIdentifier)) {
// Creating class builder
final String mid = PhysicalTypeIdentifier.createIdentifier(controllerAdviceClass, getPathResolver().getPath(controllerAdviceClassIdentifier));
final ClassOrInterfaceTypeDetailsBuilder typeBuilder = new ClassOrInterfaceTypeDetailsBuilder(mid, PUBLIC, controllerAdviceClass, PhysicalTypeCategory.CLASS);
typeBuilder.addAnnotation(new AnnotationMetadataBuilder(SpringJavaType.CONTROLLER_ADVICE).build());
// Write new class disk
getTypeManagementService().createOrUpdateTypeOnDisk(typeBuilder.build());
} else {
// Check if class is annotated with @ControllerAdvice
ClassOrInterfaceTypeDetails typeDetails = typeLocationService.getTypeDetails(controllerAdviceClass);
AnnotationMetadata annotation = typeDetails.getAnnotation(SpringJavaType.CONTROLLER_ADVICE);
if (annotation == null) {
LOGGER.warning("Class must be annotated with @ControllerAdvice");
return false;
}
}
return true;
}
use of org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder in project spring-roo by spring-projects.
the class JSONMVCResponseService method annotate.
/**
* This operation annotates a controller with the JSON annotation
*
* @param controller JavaType with the controller to be annotated.
*/
@Override
public void annotate(JavaType controller) {
Validate.notNull(controller, "ERROR: You must provide a valid controller");
ClassOrInterfaceTypeDetails controllerDetails = getTypeLocationService().getTypeDetails(controller);
// Check if provided controller exists on current project
Validate.notNull(controllerDetails, "ERROR: You must provide an existing controller");
// Check if provided controller has been annotated with @RooController
Validate.notNull(controllerDetails.getAnnotation(RooJavaType.ROO_CONTROLLER), "ERROR: You must provide a controller annotated with @RooController");
// Add JSON annotation
ClassOrInterfaceTypeDetailsBuilder typeBuilder = new ClassOrInterfaceTypeDetailsBuilder(controllerDetails);
typeBuilder.addAnnotation(new AnnotationMetadataBuilder(getAnnotation()));
// Write changes on provided controller
getTypeManagementService().createOrUpdateTypeOnDisk(typeBuilder.build());
}
Aggregations