use of org.springframework.roo.classpath.scanner.MemberDetails in project spring-roo by spring-projects.
the class SecurityFiltersMetadataProviderImpl method getMetadata.
@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
// Getting the annotated entity type
JavaType annotatedService = governorPhysicalTypeMetadata.getType();
// Getting the service details
MemberDetails serviceDetails = getMemberDetailsScanner().getMemberDetails(getClass().getName(), getTypeLocationService().getTypeDetails(annotatedService));
Map<MethodMetadata, String> prefilterMethods = new LinkedHashMap<MethodMetadata, String>();
Map<MethodMetadata, String> postfilterMethods = new LinkedHashMap<MethodMetadata, String>();
// Get methods defined in each annotation @RooSecurityFilter
AnnotationMetadata annotationFilters = serviceDetails.getAnnotation(RooJavaType.ROO_SECURITY_FILTERS);
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();
// Get attributes from the annotation @RooSecurityFilter
String methodName = (String) filterAnnotation.getValue().getAttribute("method").getValue();
String when = (String) filterAnnotation.getValue().getAttribute("when").getValue();
List<?> methodParameters = (List<?>) filterAnnotation.getValue().getAttribute("parameters").getValue();
List<MethodMetadata> methods = serviceDetails.getMethods(new JavaSymbolName(methodName));
for (MethodMetadata method : methods) {
// check the parameters to get corresponding method
if (checkParameters(method, methodParameters)) {
String roles = null;
if (filterAnnotation.getValue().getAttribute("roles") != null) {
roles = (String) filterAnnotation.getValue().getAttribute("roles").getValue();
}
String usernames = null;
if (filterAnnotation.getValue().getAttribute("usernames") != null) {
usernames = (String) filterAnnotation.getValue().getAttribute("usernames").getValue();
}
// Create the content based on defined roles and
// usernames to include it in the security annotation
String secAnnotationValue = getSecurityOperations().getSpringSecurityAnnotationValue(roles, usernames);
if (PRE_FILTER.equals(when)) {
prefilterMethods.put(method, secAnnotationValue);
} else {
if (POST_FILTER.equals(when)) {
postfilterMethods.put(method, secAnnotationValue);
}
}
break;
}
}
}
}
return new SecurityFiltersMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, prefilterMethods, postfilterMethods);
}
use of org.springframework.roo.classpath.scanner.MemberDetails in project spring-roo by spring-projects.
the class SeiMetadataProviderImpl method getMetadata.
@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
// Getting annotated class details
ClassOrInterfaceTypeDetails cid = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails();
AnnotationMetadata seiAnnotation = cid.getAnnotation(ROO_SEI);
AnnotationAttributeValue<?> serviceAttr = seiAnnotation.getAttribute(new JavaSymbolName("service"));
Validate.notNull(serviceAttr, "ERROR: You must provide a valid service to be able to generate a SEI.");
// Getting service from annotation
JavaType serviceType = (JavaType) serviceAttr.getValue();
// Getting service details
ClassOrInterfaceTypeDetails serviceTypeDetails = getTypeLocationService().getTypeDetails(serviceType);
// Obtaining all defined methods in the service interface
MemberDetails serviceDetails = getMemberDetailsScanner().getMemberDetails(getClass().getName(), serviceTypeDetails);
// Registering dependency between ServiceMetadata and this one, to be able to
// update Endpoint if service .aj file changes
final String serviceMetadataKey = ServiceMetadata.createIdentifier(serviceTypeDetails);
registerDependency(serviceMetadataKey, metadataIdentificationString);
// Register dependency between Service PhysicalTypeMetadata and this one,
// to be able to update Endpoint if service .java file changes
final String servicePhysicalTypeKey = PhysicalTypeIdentifier.createIdentifier(serviceTypeDetails);
registerDependency(servicePhysicalTypeKey, metadataIdentificationString);
return new SeiMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, getProjectOperations().getTopLevelPackage(""), cid, serviceType, serviceDetails.getMethods());
}
use of org.springframework.roo.classpath.scanner.MemberDetails 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();
}
use of org.springframework.roo.classpath.scanner.MemberDetails in project spring-roo by spring-projects.
the class JavaBeanMetadata method checkIfInterfaceMethodWasImplemented.
/**
* To check if current method was implemented on all Java classes or ITds
* associated to this entity class.
* If method was implemented, is not necessary to add again.
*
* @param methodBuilder
* @return
*/
private boolean checkIfInterfaceMethodWasImplemented(MethodMetadataBuilder methodBuilder) {
// ROO-3584: Obtain current declared methods
List<MethodMetadataBuilder> declaredMethods = builder.getDeclaredMethods();
for (MethodMetadataBuilder method : declaredMethods) {
// If current method equals to interface method, return false
if (method.getMethodName().equals(methodBuilder.getMethodName())) {
return true;
}
}
// ROO-3587: Obtain ALL declared methods from Java classes and ITDs.
MemberDetails memberDetails = memberDetailsScanner.getMemberDetails(getClass().getName(), governorTypeDetails);
List<MethodMetadata> allMethods = memberDetails.getMethods();
for (MethodMetadata method : allMethods) {
// If current method equals to interface method, return false
if (method.getMethodName().equals(methodBuilder.getMethodName())) {
return true;
}
}
return false;
}
use of org.springframework.roo.classpath.scanner.MemberDetails in project spring-roo by spring-projects.
the class DtoCommands method getEntityFieldList.
/**
* Gets a list of fields from an entity. Static and transient fields are excluded
* as well as collection fields which type is an entity.
*
* @param entity the JavaType from which to obtain the field list.
* @return a List<FieldMetadata> with info of the entity fields.
*/
private List<FieldMetadata> getEntityFieldList(JavaType entity) {
List<FieldMetadata> fieldList = new ArrayList<FieldMetadata>();
ClassOrInterfaceTypeDetails typeDetails = typeLocationService.getTypeDetails(entity);
Validate.notNull(typeDetails, String.format("Cannot find details for class %s. Please be sure that the class exists.", entity.getFullyQualifiedTypeName()));
MemberDetails entityMemberDetails = memberDetailsScanner.getMemberDetails(this.getClass().getName(), typeDetails);
Validate.notNull(entityMemberDetails.getAnnotation(JpaJavaType.ENTITY), String.format("%s must be an entity to obtain it's fields info.", entity.getFullyQualifiedTypeName()));
// Get fields and check for other fields from relations
List<FieldMetadata> entityFields = entityMemberDetails.getFields();
for (FieldMetadata field : entityFields) {
// Exclude static fields
if (Modifier.isStatic(field.getModifier())) {
continue;
}
// Exclude transient fields
if (field.getAnnotation(JpaJavaType.TRANSIENT) != null) {
continue;
}
// Exclude entity collection fields
JavaType fieldType = field.getFieldType();
if (fieldType.isCommonCollectionType()) {
boolean isEntityCollectionField = false;
List<JavaType> parameters = fieldType.getParameters();
for (JavaType parameter : parameters) {
if (typeIsEntity(parameter)) {
isEntityCollectionField = true;
break;
}
}
if (isEntityCollectionField) {
continue;
}
}
fieldList.add(field);
}
return fieldList;
}
Aggregations