Search in sources :

Example 1 with UpdateFile

use of org.springframework.roo.file.undo.UpdateFile 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 2 with UpdateFile

use of org.springframework.roo.file.undo.UpdateFile in project spring-roo by spring-projects.

the class DefaultFileManager method updateFile.

public MutableFile updateFile(final String fileIdentifier) {
    if (fileMonitorService == null) {
        fileMonitorService = getFileMonitorService();
    }
    if (processManager == null) {
        processManager = getProcessManager();
    }
    if (filenameResolver == null) {
        filenameResolver = getFileNameResolver();
    }
    if (undoManager == null) {
        undoManager = getUndoManager();
    }
    Validate.notNull(fileIdentifier, "File identifier required");
    Validate.notNull(fileMonitorService, "FileMonitorService required");
    Validate.notNull(processManager, "ProcessManager required");
    Validate.notNull(filenameResolver, "FilenameResolver required");
    Validate.notNull(undoManager, "UndoManager required");
    final File actual = new File(fileIdentifier);
    Validate.isTrue(actual.exists(), "File '%s' does not exist", fileIdentifier);
    new UpdateFile(undoManager, filenameResolver, actual);
    final ManagedMessageRenderer renderer = new ManagedMessageRenderer(filenameResolver, actual, false);
    renderer.setIncludeHashCode(processManager.isDevelopmentMode());
    return new DefaultMutableFile(actual, fileMonitorService, renderer);
}
Also used : UpdateFile(org.springframework.roo.file.undo.UpdateFile) 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)

Aggregations

File (java.io.File)2 CreateFile (org.springframework.roo.file.undo.CreateFile)2 DeleteFile (org.springframework.roo.file.undo.DeleteFile)2 UpdateFile (org.springframework.roo.file.undo.UpdateFile)2 MutableFile (org.springframework.roo.process.manager.MutableFile)2 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1