public Database getDatabase(final boolean evictCache) { if (!evictCache && cachedIntrospections.contains(lastDatabase)) { for (final Database database : cachedIntrospections) { if (database.equals(lastDatabase)) { return lastDatabase; } } } if (evictCache && cachedIntrospections.contains(lastDatabase)) { cachedIntrospections.remove(lastDatabase); } final String dbreXmlPath = getDbreXmlPath(); if (StringUtils.isBlank(dbreXmlPath) || !fileManager.exists(dbreXmlPath)) { return null; } Database database = null; InputStream inputStream = null; try { inputStream = fileManager.getInputStream(dbreXmlPath); database = DatabaseXmlUtils.readDatabase(inputStream); cacheDatabase(database); return database; } catch (final Exception e) { throw new IllegalStateException(e); } finally { IOUtils.closeQuietly(inputStream); } }
private String getJndiDataSourceName() { final String contextPath = projectOperations .getPathResolver() .getFocusedIdentifier(Path.SPRING_CONFIG_ROOT, "applicationContext.xml"); final Document appCtx = XmlUtils.readXml(fileManager.getInputStream(contextPath)); final Element root = appCtx.getDocumentElement(); final Element dataSourceJndi = XmlUtils.findFirstElement("/beans/jndi-lookup[@id = 'dataSource']", root); return dataSourceJndi != null ? dataSourceJndi.getAttribute("jndi-name") : null; }
public void configureTemplateMessage(final String from, final String subject) { final String contextPath = getApplicationContextPath(); final Document document = XmlUtils.readXml(fileManager.getInputStream(contextPath)); final Element root = document.getDocumentElement(); final Map<String, String> props = new HashMap<String, String>(); if (StringUtils.hasText(from) || StringUtils.hasText(subject)) { Element smmBean = getSimpleMailMessageBean(root); if (smmBean == null) { smmBean = document.createElement("bean"); smmBean.setAttribute("class", "org.springframework.mail.SimpleMailMessage"); smmBean.setAttribute("id", "templateMessage"); } if (StringUtils.hasText(from)) { Element smmProperty = XmlUtils.findFirstElement("//property[@name='from']", smmBean); if (smmProperty != null) { smmBean.removeChild(smmProperty); } smmProperty = document.createElement("property"); smmProperty.setAttribute("value", "${email.from}"); smmProperty.setAttribute("name", "from"); smmBean.appendChild(smmProperty); props.put("email.from", from); } if (StringUtils.hasText(subject)) { Element smmProperty = XmlUtils.findFirstElement("//property[@name='subject']", smmBean); if (smmProperty != null) { smmBean.removeChild(smmProperty); } smmProperty = document.createElement("property"); smmProperty.setAttribute("value", "${email.subject}"); smmProperty.setAttribute("name", "subject"); smmBean.appendChild(smmProperty); props.put("email.subject", subject); } root.appendChild(smmBean); DomUtils.removeTextNodes(root); fileManager.createOrUpdateTextFileIfRequired( contextPath, XmlUtils.nodeToString(document), false); } if (props.size() > 0) { propFileOperations.addProperties( Path.SPRING_CONFIG_ROOT, "email.properties", props, true, true); } }
@Override public void setup() { if (!fileManager.exists( projectOperations .getPathResolver() .getFocusedIdentifier(SRC_MAIN_WEBAPP, "WEB-INF/web.xml"))) { webMvcOperations.installAllWebMvcArtifacts(); } final Element configuration = XmlUtils.getConfiguration(getClass()); for (Element propertyElement : XmlUtils.findElements("/configuration/batch/properties/*", configuration)) { projectOperations.addProperty( projectOperations.getFocusedModuleName(), new Property(propertyElement)); } final List<Dependency> dependencies = new ArrayList<Dependency>(); for (final Element dependencyElement : XmlUtils.findElements("/configuration/batch/dependencies/*", configuration)) { dependencies.add(new Dependency(dependencyElement)); } projectOperations.removeDependencies(projectOperations.getFocusedModuleName(), dependencies); metadataService.evict( ProjectMetadata.getProjectIdentifier(projectOperations.getFocusedModuleName())); projectOperations.addDependencies(projectOperations.getFocusedModuleName(), dependencies); final String webConfigFile = pathResolver.getFocusedIdentifier(Path.SRC_MAIN_WEBAPP, WEBMVC_CONFIG_XML); Validate.isTrue(fileManager.exists(webConfigFile), "Aborting: Unable to find " + webConfigFile); InputStream webMvcConfigInputStream = null; try { webMvcConfigInputStream = fileManager.getInputStream(webConfigFile); Validate.notNull( webMvcConfigInputStream, "Aborting: Unable to acquire webmvc-config.xml file"); final Document webMvcConfig = XmlUtils.readXml(webMvcConfigInputStream); final Element root = webMvcConfig.getDocumentElement(); if (XmlUtils.findFirstElement("/beans/bean[@class='" + REST_MVC_CONFIG + "']", root) == null) { final Element config = webMvcConfig.createElement("bean"); config.setAttribute("class", REST_MVC_CONFIG); root.appendChild(config); fileManager.createOrUpdateTextFileIfRequired( webConfigFile, XmlUtils.nodeToString(webMvcConfig), true); } } finally { IOUtils.closeQuietly(webMvcConfigInputStream); } }
@Override public void resourceString( final JavaType type, final String name, final JavaSymbolName fieldName, final String value) { final ClassOrInterfaceTypeDetails typeDetails = typeLocationService.getTypeDetails(type); Validate.notNull(typeDetails, "The type specified, '" + type + "' doesn't exist"); final DocumentBuilder builder = newDocumentBuilder(); final String valuesPath = pathResolver.getFocusedIdentifier(Path.ROOT, VALUES_PATH + SEP + STRINGS + XML_EXTENSION); InputStream inputStream = null; Document document = null; try { inputStream = fileManager.getInputStream(valuesPath); document = builder.parse(inputStream); } catch (final Exception e) { LOGGER.severe("Error reading resource XML: " + e.getMessage()); } finally { IOUtils.closeQuietly(inputStream); } if (document != null) { final Element root = document.getDocumentElement(); final Element stringElem = XmlUtils.createTextElement(document, "string", value); final String id = StringUtils.isEmpty(name) ? fieldName.getSymbolName() : name; stringElem.setAttribute("name", id); root.appendChild(stringElem); fileManager.createOrUpdateTextFileIfRequired( valuesPath, XmlUtils.nodeToString(document), true); } final String physicalTypeIdentifier = typeDetails.getDeclaredByMetadataId(); final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>(); final AnnotationMetadataBuilder annotationBuilder = new AnnotationMetadataBuilder(ROO_STRING); if (!StringUtils.isEmpty(name)) { annotationBuilder.addStringAttribute("value", name); } annotations.add(annotationBuilder); final FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(physicalTypeIdentifier, 0, annotations, fieldName, STRING); typeManagementService.addField(fieldBuilder.build()); }
@Override public boolean isInstalledInModule(String moduleName) { final String webConfigFile = pathResolver.getFocusedIdentifier(Path.SRC_MAIN_WEBAPP, WEBMVC_CONFIG_XML); if (!fileManager.exists(webConfigFile)) { return false; } InputStream webMvcConfigInputStream = null; try { webMvcConfigInputStream = fileManager.getInputStream(webConfigFile); if (webMvcConfigInputStream == null) { return false; } final Document webMvcConfig = XmlUtils.readXml(webMvcConfigInputStream); final Element root = webMvcConfig.getDocumentElement(); return XmlUtils.findFirstElement("/beans/bean[@class='" + REST_MVC_CONFIG + "']", root) != null; } finally { IOUtils.closeQuietly(webMvcConfigInputStream); } }
public void installEmail( final String hostServer, final MailProtocol protocol, final String port, final String encoding, final String username, final String password) { Assert.hasText(hostServer, "Host server name required"); final String contextPath = getApplicationContextPath(); final Document document = XmlUtils.readXml(fileManager.getInputStream(contextPath)); final Element root = document.getDocumentElement(); boolean installDependencies = true; final Map<String, String> props = new HashMap<String, String>(); Element mailBean = XmlUtils.findFirstElement( "/beans/bean[@class = 'org.springframework.mail.javamail.JavaMailSenderImpl']", root); if (mailBean != null) { root.removeChild(mailBean); installDependencies = false; } mailBean = document.createElement("bean"); mailBean.setAttribute("class", "org.springframework.mail.javamail.JavaMailSenderImpl"); mailBean.setAttribute("id", "mailSender"); final Element property = document.createElement("property"); property.setAttribute("name", "host"); property.setAttribute("value", "${email.host}"); mailBean.appendChild(property); root.appendChild(mailBean); props.put("email.host", hostServer); if (protocol != null) { final Element pElement = document.createElement("property"); pElement.setAttribute("value", "${email.protocol}"); pElement.setAttribute("name", "protocol"); mailBean.appendChild(pElement); props.put("email.protocol", protocol.getProtocol()); } if (StringUtils.hasText(port)) { final Element pElement = document.createElement("property"); pElement.setAttribute("name", "port"); pElement.setAttribute("value", "${email.port}"); mailBean.appendChild(pElement); props.put("email.port", port); } if (StringUtils.hasText(encoding)) { final Element pElement = document.createElement("property"); pElement.setAttribute("name", "defaultEncoding"); pElement.setAttribute("value", "${email.encoding}"); mailBean.appendChild(pElement); props.put("email.encoding", encoding); } if (StringUtils.hasText(username)) { final Element pElement = document.createElement("property"); pElement.setAttribute("name", "username"); pElement.setAttribute("value", "${email.username}"); mailBean.appendChild(pElement); props.put("email.username", username); } if (StringUtils.hasText(password)) { final Element pElement = document.createElement("property"); pElement.setAttribute("name", "password"); pElement.setAttribute("value", "${email.password}"); mailBean.appendChild(pElement); props.put("email.password", password); if (SMTP.equals(protocol)) { final Element javaMailProperties = document.createElement("property"); javaMailProperties.setAttribute("name", "javaMailProperties"); final Element securityProps = document.createElement("props"); javaMailProperties.appendChild(securityProps); final Element prop = document.createElement("prop"); prop.setAttribute("key", "mail.smtp.auth"); prop.setTextContent("true"); securityProps.appendChild(prop); final Element prop2 = document.createElement("prop"); prop2.setAttribute("key", "mail.smtp.starttls.enable"); prop2.setTextContent("true"); securityProps.appendChild(prop2); mailBean.appendChild(javaMailProperties); } } DomUtils.removeTextNodes(root); fileManager.createOrUpdateTextFileIfRequired( contextPath, XmlUtils.nodeToString(document), false); if (installDependencies) { updateConfiguration(); } propFileOperations.addProperties( Path.SPRING_CONFIG_ROOT, "email.properties", props, true, true); }
/** * Generates the "send email" method to be added to the domain type * * @param mailSenderName the name of the MailSender field (required) * @param async whether to send the email asynchronously * @param targetClassMID the MID of the class to receive the method * @param mutableTypeDetails the type to which the method is being added (required) * @return a non-<code>null</code> method */ private MethodMetadata getSendMethod( final JavaSymbolName mailSenderName, final boolean async, final String targetClassMID, final ClassOrInterfaceTypeDetailsBuilder classOrInterfaceTypeDetailsBuilder) { final String contextPath = getApplicationContextPath(); final Document document = XmlUtils.readXml(fileManager.getInputStream(contextPath)); final Element root = document.getDocumentElement(); // Make a builder for the created method's body final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder(); // Collect the types and names of the created method's parameters final PairList<AnnotatedJavaType, JavaSymbolName> parameters = new PairList<AnnotatedJavaType, JavaSymbolName>(); if (getSimpleMailMessageBean(root) == null) { // There's no SimpleMailMessage bean; use a local variable bodyBuilder.appendFormalLine( "org.springframework.mail.SimpleMailMessage " + LOCAL_MESSAGE_VARIABLE + " = new org.springframework.mail.SimpleMailMessage();"); // Set the from address parameters.add(STRING, new JavaSymbolName("mailFrom")); bodyBuilder.appendFormalLine(LOCAL_MESSAGE_VARIABLE + ".setFrom(mailFrom);"); // Set the subject parameters.add(STRING, new JavaSymbolName("subject")); bodyBuilder.appendFormalLine(LOCAL_MESSAGE_VARIABLE + ".setSubject(subject);"); } else { // A SimpleMailMessage bean exists; auto-wire it into the entity and use it as a template final List<AnnotationMetadataBuilder> smmAnnotations = Arrays.asList(new AnnotationMetadataBuilder(AUTOWIRED)); final FieldMetadataBuilder smmFieldBuilder = new FieldMetadataBuilder( targetClassMID, PRIVATE_TRANSIENT, smmAnnotations, new JavaSymbolName(TEMPLATE_MESSAGE_FIELD), SIMPLE_MAIL_MESSAGE); classOrInterfaceTypeDetailsBuilder.addField(smmFieldBuilder.build()); // Use the injected bean as a template (for thread safety) bodyBuilder.appendFormalLine( "org.springframework.mail.SimpleMailMessage " + LOCAL_MESSAGE_VARIABLE + " = new org.springframework.mail.SimpleMailMessage(" + TEMPLATE_MESSAGE_FIELD + ");"); } // Set the to address parameters.add(STRING, new JavaSymbolName("mailTo")); bodyBuilder.appendFormalLine(LOCAL_MESSAGE_VARIABLE + ".setTo(mailTo);"); // Set the message body parameters.add(STRING, new JavaSymbolName("message")); bodyBuilder.appendFormalLine(LOCAL_MESSAGE_VARIABLE + ".setText(message);"); bodyBuilder.newLine(); bodyBuilder.appendFormalLine(mailSenderName + ".send(" + LOCAL_MESSAGE_VARIABLE + ");"); final MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder( targetClassMID, Modifier.PUBLIC, new JavaSymbolName("sendMessage"), JavaType.VOID_PRIMITIVE, parameters.getKeys(), parameters.getValues(), bodyBuilder); if (async) { if (DomUtils.findFirstElementByName("task:annotation-driven", root) == null) { // Add asynchronous email support to the application if (!StringUtils.hasText(root.getAttribute("xmlns:task"))) { // Add the "task" namespace to the Spring config file root.setAttribute("xmlns:task", SPRING_TASK_NS); root.setAttribute( "xsi:schemaLocation", root.getAttribute("xsi:schemaLocation") + " " + SPRING_TASK_NS + " " + SPRING_TASK_XSD); } root.appendChild( new XmlElementBuilder("task:annotation-driven", document) .addAttribute("executor", "asyncExecutor") .addAttribute("mode", "aspectj") .build()); root.appendChild( new XmlElementBuilder("task:executor", document) .addAttribute("id", "asyncExecutor") .addAttribute("pool-size", "${executor.poolSize}") .build()); // Write out the new Spring config file fileManager.createOrUpdateTextFileIfRequired( contextPath, XmlUtils.nodeToString(document), false); // Update the email properties file propFileOperations.addPropertyIfNotExists( Path.SPRING_CONFIG_ROOT, "email.properties", "executor.poolSize", "10", true); } methodBuilder.addAnnotation(new AnnotationMetadataBuilder(ASYNC)); } return methodBuilder.build(); }
@Override public void view( final JavaType type, final String viewName, final String identifier, final JavaSymbolName fieldName, final Dimension height, final Dimension width) { final ClassOrInterfaceTypeDetails typeDetails = typeLocationService.getTypeDetails(type); Validate.notNull(typeDetails, "The type specified, '" + type + "'doesn't exist"); final JavaType viewType = new JavaType(viewName.contains(".") ? viewName : WIDGET_PACKAGE + "." + viewName); final String layout = RequestFactoryUtils.getStringAnnotationValue(typeDetails, ROO_ACTIVITY, "value", ""); if (!StringUtils.isEmpty(layout)) { final DocumentBuilder builder = newDocumentBuilder(); final String layoutPath = pathResolver.getFocusedIdentifier(Path.ROOT, LAYOUT_PATH + SEP + layout + XML_EXTENSION); InputStream inputStream = null; Document document = null; try { inputStream = fileManager.getInputStream(layoutPath); document = builder.parse(inputStream); } catch (final Exception e) { LOGGER.severe("Error reading layout XML: " + e.getMessage()); } finally { IOUtils.closeQuietly(inputStream); } if (document != null) { final Element root = document.getDocumentElement(); final Element viewElem = document.createElement(viewType.getSimpleTypeName()); final String id = StringUtils.isEmpty(identifier) ? fieldName.getSymbolName() : identifier; viewElem.setAttribute("android:id", ID_PREFIX + id); viewElem.setAttribute("android:layout_height", height.value()); viewElem.setAttribute("android:layout_width", width.value()); root.appendChild(viewElem); fileManager.createOrUpdateTextFileIfRequired( layoutPath, XmlUtils.nodeToString(document), true); } } final String physicalTypeIdentifier = typeDetails.getDeclaredByMetadataId(); final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>(); final AnnotationMetadataBuilder annotationBuilder = new AnnotationMetadataBuilder(ROO_VIEW); if (!StringUtils.isEmpty(identifier)) { annotationBuilder.addStringAttribute("value", identifier); } annotations.add(annotationBuilder); final FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(physicalTypeIdentifier, 0, annotations, fieldName, viewType); typeManagementService.addField(fieldBuilder.build()); }