Search in sources :

Example 16 with MutableFile

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

the class PropFilesManagerServiceImpl method removeProperty.

@Override
public void removeProperty(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 {
        throw new IllegalStateException(String.format("ERROR: '%s' properties file doesn't exists.", filePath));
    }
    // Including prefix if needed
    if (StringUtils.isNotBlank(prefix)) {
        key = prefix.concat(".").concat(key);
    }
    if (props.containsKey(key)) {
        props.remove(key);
        storeProps(props, mutableFile.getOutputStream(), "Updated at " + new Date());
    }
}
Also used : Properties(java.util.Properties) Date(java.util.Date) MutableFile(org.springframework.roo.process.manager.MutableFile)

Example 17 with MutableFile

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

the class LoggingOperationsImpl method setupProperties.

private void setupProperties(final LogLevel logLevel, final LoggerPackage loggerPackage) {
    final String filePath = pathResolver.getFocusedIdentifier(Path.SRC_MAIN_RESOURCES, "log4j.properties");
    MutableFile log4jMutableFile = null;
    final Properties props = new Properties();
    InputStream inputStream = null;
    try {
        if (fileManager.exists(filePath)) {
            log4jMutableFile = fileManager.updateFile(filePath);
            inputStream = log4jMutableFile.getInputStream();
            props.load(inputStream);
        } else {
            log4jMutableFile = fileManager.createFile(filePath);
            inputStream = FileUtils.getInputStream(getClass(), "log4j-template.properties");
            Validate.notNull(inputStream, "Could not acquire log4j configuration template");
            props.load(inputStream);
        }
    } catch (final IOException ioe) {
        throw new IllegalStateException(ioe);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
    final JavaPackage topLevelPackage = projectOperations.getTopLevelPackage(projectOperations.getFocusedModuleName());
    final String logStr = "log4j.logger.";
    switch(loggerPackage) {
        case ROOT:
            props.remove("log4j.rootLogger");
            props.setProperty("log4j.rootLogger", logLevel.name() + ", stdout");
            break;
        case PROJECT:
            props.remove(logStr + topLevelPackage.getFullyQualifiedPackageName());
            props.setProperty(logStr + topLevelPackage.getFullyQualifiedPackageName(), logLevel.name());
            break;
        default:
            for (final String packageName : loggerPackage.getPackageNames()) {
                props.remove(logStr + packageName);
                props.setProperty(logStr + packageName, logLevel.name());
            }
            break;
    }
    OutputStream outputStream = null;
    try {
        outputStream = log4jMutableFile.getOutputStream();
        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) JavaPackage(org.springframework.roo.model.JavaPackage) Date(java.util.Date) 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