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);
}
}
}
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);
}
Aggregations