Search in sources :

Example 1 with CreateFile

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

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

the class DefaultFileManager method createFile.

public MutableFile createFile(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' already exists", fileIdentifier);
    try {
        fileMonitorService.notifyCreated(actual.getCanonicalPath());
        final File parentDirectory = new File(actual.getParent());
        if (!parentDirectory.exists()) {
            createDirectory(parentDirectory.getCanonicalPath());
        }
    } catch (final IOException ignored) {
    }
    new CreateFile(undoManager, filenameResolver, actual);
    final ManagedMessageRenderer renderer = new ManagedMessageRenderer(filenameResolver, actual, true);
    renderer.setIncludeHashCode(processManager.isDevelopmentMode());
    return new DefaultMutableFile(actual, null, renderer);
}
Also used : CreateFile(org.springframework.roo.file.undo.CreateFile) 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)

Aggregations

File (java.io.File)2 IOException (java.io.IOException)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 OutputStream (java.io.OutputStream)1