Search in sources :

Example 1 with MutableFile

use of org.springframework.roo.process.manager.MutableFile in project spring-roo by spring-projects.

the class PropFilesManagerServiceImpl method removePropertiesByPrefix.

@Override
public void removePropertiesByPrefix(LogicalPath propertyFilePath, String propertyFilename, String prefix) {
    Validate.notBlank(prefix, "Prefix required");
    Validate.notNull(propertyFilePath, "Property file path required");
    Validate.notBlank(propertyFilename, "Property filename required");
    final String filePath = getProjectOperations().getPathResolver().getIdentifier(propertyFilePath, propertyFilename);
    MutableFile mutableFile = null;
    final Properties props = new Properties();
    if (getFileManager().exists(filePath)) {
        mutableFile = getFileManager().updateFile(filePath);
        loadProperties(props, mutableFile.getInputStream());
    } else {
        throw new IllegalStateException(String.format("ERROR: '%s' properties file doesn't exists.", filePath));
    }
    for (Entry property : props.entrySet()) {
        String key = (String) property.getKey();
        if (key != null && key.startsWith(prefix)) {
            props.remove(key);
        }
    }
    storeProps(props, mutableFile.getOutputStream(), "Updated at " + new Date());
}
Also used : Entry(java.util.Map.Entry) Properties(java.util.Properties) Date(java.util.Date) MutableFile(org.springframework.roo.process.manager.MutableFile)

Example 2 with MutableFile

use of org.springframework.roo.process.manager.MutableFile in project spring-roo by spring-projects.

the class PropFilesManagerServiceImpl method getProperty.

@Override
public String getProperty(LogicalPath propertyFilePath, String propertyFilename, String prefix, String key) {
    Validate.notNull(prefix, "Prefix could be blank but not null");
    Validate.notNull(propertyFilePath, "Property file path required");
    Validate.notBlank(propertyFilename, "Property filename required");
    Validate.notBlank(key, "Key required");
    final String filePath = getProjectOperations().getPathResolver().getIdentifier(propertyFilePath, propertyFilename);
    MutableFile mutableFile = null;
    final Properties props = new Properties();
    if (getFileManager().exists(filePath)) {
        mutableFile = getFileManager().updateFile(filePath);
        loadProperties(props, mutableFile.getInputStream());
    } else {
        return null;
    }
    // Including prefix if needed
    if (StringUtils.isNotBlank(prefix)) {
        key = prefix.concat(".").concat(key);
    }
    return props.getProperty(key);
}
Also used : Properties(java.util.Properties) MutableFile(org.springframework.roo.process.manager.MutableFile)

Example 3 with MutableFile

use of org.springframework.roo.process.manager.MutableFile in project spring-roo by spring-projects.

the class MavenOperationsImpl method addApplicationDevPropertiesFile.

/**
 * Copy file application-dev.properties
 *
 * @param Pom
 *            module where application-dev.properties should be generated
 * @param topLevelPackage
 *         JavaPackage that represents the top level package
 */
private void addApplicationDevPropertiesFile(Pom module, JavaPackage topLevelPackage) {
    LogicalPath resourcesPath = LogicalPath.getInstance(Path.SRC_MAIN_RESOURCES, module.getModuleName());
    String filePath = getPathResolver().getIdentifier(resourcesPath, "application-dev.properties");
    MutableFile appDevMutableFile = null;
    final Properties props = new Properties();
    InputStream inputStream = null;
    try {
        if (fileManager.exists(filePath)) {
            appDevMutableFile = fileManager.updateFile(filePath);
            inputStream = appDevMutableFile.getInputStream();
            props.load(inputStream);
        } else {
            appDevMutableFile = fileManager.createFile(filePath);
        }
    } catch (final IOException ioe) {
        throw new IllegalStateException(ioe);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
    OutputStream outputStream = null;
    try {
        outputStream = appDevMutableFile.getOutputStream();
        props.put("spring.messages.cache-seconds", "0");
        props.put("logging.file", "");
        props.put("logging.level.".concat(topLevelPackage.getFullyQualifiedPackageName()), "DEBUG");
        props.store(outputStream, "Updated at " + new Date());
    } catch (final IOException ioe) {
        throw new IllegalStateException(ioe);
    } finally {
        IOUtils.closeQuietly(outputStream);
    }
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Properties(java.util.Properties) Date(java.util.Date) MutableFile(org.springframework.roo.process.manager.MutableFile)

Example 4 with MutableFile

use of org.springframework.roo.process.manager.MutableFile in project spring-roo by spring-projects.

the class DefaultFileManager method createOrUpdateTextFileIfRequired.

private void createOrUpdateTextFileIfRequired(final String fileIdentifier, final String newContents, final String descriptionOfChange) {
    MutableFile mutableFile = null;
    if (exists(fileIdentifier)) {
        // First verify if the file has even changed
        final File file = new File(fileIdentifier);
        String existing = null;
        try {
            existing = FileUtils.readFileToString(file);
        } catch (final IOException ignored) {
        }
        if (!newContents.equals(existing)) {
            mutableFile = updateFile(fileIdentifier);
        }
    } else {
        mutableFile = createFile(fileIdentifier);
        Validate.notNull(mutableFile, "Could not create file '%s'", fileIdentifier);
    }
    if (mutableFile != null) {
        OutputStream outputStream = null;
        try {
            if (StringUtils.isNotBlank(descriptionOfChange)) {
                mutableFile.setDescriptionOfChange(descriptionOfChange);
            }
            outputStream = mutableFile.getOutputStream();
            IOUtils.write(newContents, outputStream);
        } catch (final IOException e) {
            throw new IllegalStateException("Could not output '" + mutableFile.getCanonicalPath() + "'", e);
        } finally {
            IOUtils.closeQuietly(outputStream);
        }
    }
}
Also used : OutputStream(java.io.OutputStream) IOException(java.io.IOException) MutableFile(org.springframework.roo.process.manager.MutableFile) CreateFile(org.springframework.roo.file.undo.CreateFile) File(java.io.File) DeleteFile(org.springframework.roo.file.undo.DeleteFile) UpdateFile(org.springframework.roo.file.undo.UpdateFile) MutableFile(org.springframework.roo.process.manager.MutableFile)

Example 5 with MutableFile

use of org.springframework.roo.process.manager.MutableFile in project spring-roo by spring-projects.

the class TilesOperationsImpl method writeToDiskIfNecessary.

/**
 * @param viewsDefinitionFile the canonical path of the file to update
 * @param body the element whose parent document is to be written
 * @return
 */
private boolean writeToDiskIfNecessary(final String tilesDefinitionFile, final Element body) {
    // Build a string representation of the Tiles config file
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    final Transformer transformer = XmlUtils.createIndentingTransformer();
    transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "http://tiles.apache.org/dtds/tiles-config_2_1.dtd");
    transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN");
    XmlUtils.writeXml(transformer, byteArrayOutputStream, body.getOwnerDocument());
    final String viewContent = byteArrayOutputStream.toString();
    // If mutableFile becomes non-null, it means we need to use it to write
    // out the contents of jspContent to the file
    MutableFile mutableFile = null;
    if (fileManager.exists(tilesDefinitionFile)) {
        // First verify if the file has even changed
        final File file = new File(tilesDefinitionFile);
        String existing = null;
        try {
            existing = org.apache.commons.io.FileUtils.readFileToString(file);
        } catch (final IOException ignored) {
        }
        if (!viewContent.equals(existing)) {
            mutableFile = fileManager.updateFile(tilesDefinitionFile);
        }
    } else {
        mutableFile = fileManager.createFile(tilesDefinitionFile);
        Validate.notNull(mutableFile, "Could not create tiles view definition '%s'", tilesDefinitionFile);
    }
    if (mutableFile != null) {
        OutputStream outputStream = null;
        try {
            // We need to write the file out (it's a new file, or the
            // existing file has different contents)
            outputStream = mutableFile.getOutputStream();
            IOUtils.write(viewContent, outputStream);
            // Return and indicate we wrote out the file
            return true;
        } catch (final IOException ioe) {
            throw new IllegalStateException("Could not output '" + mutableFile.getCanonicalPath() + "'", ioe);
        } finally {
            IOUtils.closeQuietly(outputStream);
        }
    }
    // A file existed, but it contained the same content, so we return false
    return false;
}
Also used : Transformer(javax.xml.transform.Transformer) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) MutableFile(org.springframework.roo.process.manager.MutableFile) File(java.io.File) MutableFile(org.springframework.roo.process.manager.MutableFile)

Aggregations

MutableFile (org.springframework.roo.process.manager.MutableFile)17 IOException (java.io.IOException)10 OutputStream (java.io.OutputStream)8 Date (java.util.Date)6 Properties (java.util.Properties)6 File (java.io.File)5 InputStream (java.io.InputStream)5 BufferedInputStream (java.io.BufferedInputStream)4 ZipInputStream (java.util.zip.ZipInputStream)4 LogicalPath (org.springframework.roo.project.LogicalPath)4 CreateFile (org.springframework.roo.file.undo.CreateFile)3 DeleteFile (org.springframework.roo.file.undo.DeleteFile)3 UpdateFile (org.springframework.roo.file.undo.UpdateFile)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 URL (java.net.URL)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Enumeration (java.util.Enumeration)1 HashMap (java.util.HashMap)1