use of org.springframework.roo.process.manager.MutableFile in project spring-roo by spring-projects.
the class CreatorOperationsImpl method writePomFile.
/**
* Writes the given Maven POM to disk
*
* @param pom the POM to write (required)
*/
private void writePomFile(final Document pom, String folder) {
MutableFile pomFile;
if (folder != null) {
LogicalPath rootPath = LogicalPath.getInstance(Path.ROOT, "");
pomFile = fileManager.createFile(pathResolver.getIdentifier(rootPath, folder + "/" + POM_XML));
} else {
LogicalPath rootPath = LogicalPath.getInstance(Path.ROOT, "");
pomFile = fileManager.createFile(pathResolver.getIdentifier(rootPath, POM_XML));
}
XmlUtils.writeXml(pomFile.getOutputStream(), pom);
}
use of org.springframework.roo.process.manager.MutableFile in project spring-roo by spring-projects.
the class BackupOperationsImpl method backup.
public String backup() {
Validate.isTrue(isBackupPossible(), "Project metadata unavailable");
// For Windows, make a date format that can legally form part of a
// filename (ROO-277)
final String pattern = File.separatorChar == '\\' ? "yyyy-MM-dd_HH.mm.ss" : "yyyy-MM-dd_HH:mm:ss";
final DateFormat df = new SimpleDateFormat(pattern);
final long start = System.nanoTime();
ZipOutputStream zos = null;
try {
final File projectDirectory = new File(getProjectOperations().getPathResolver().getFocusedIdentifier(Path.ROOT, "."));
final MutableFile file = getFileManager().createFile(FileUtils.getCanonicalPath(new File(projectDirectory, getProjectOperations().getFocusedProjectName() + "_" + df.format(new Date()) + ".zip")));
zos = new ZipOutputStream(file.getOutputStream());
zip(projectDirectory, projectDirectory, zos);
} catch (final FileNotFoundException e) {
LOGGER.fine("Could not determine project directory");
} catch (final IOException e) {
LOGGER.fine("Could not create backup archive");
} finally {
IOUtils.closeQuietly(zos);
}
final long milliseconds = (System.nanoTime() - start) / 1000000;
return "Backup completed in " + milliseconds + " ms";
}
use of org.springframework.roo.process.manager.MutableFile 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);
}
use of org.springframework.roo.process.manager.MutableFile 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);
}
use of org.springframework.roo.process.manager.MutableFile in project spring-roo by spring-projects.
the class PropFilesManagerServiceImpl method manageProperty.
private void manageProperty(final LogicalPath propertyFilePath, final String propertyFilename, final String prefix, final Map<String, String> properties, final boolean sorted, final boolean force, boolean preventChangesIfAlreadyExists) {
Validate.notNull(prefix, "Prefix could be blank but not null");
Validate.notNull(propertyFilePath, "Property file path required");
Validate.notBlank(propertyFilename, "Property filename required");
Validate.notNull(properties, "Property map required");
final String filePath = getProjectOperations().getPathResolver().getIdentifier(propertyFilePath, propertyFilename);
MutableFile mutableFile = null;
Properties props;
if (sorted) {
props = new Properties() {
private static final long serialVersionUID = 1L;
// Override the keys() method to order the keys alphabetically
@SuppressWarnings("all")
public synchronized Enumeration keys() {
final Object[] keys = keySet().toArray();
Arrays.sort(keys);
return new Enumeration() {
int i = 0;
public boolean hasMoreElements() {
return i < keys.length;
}
public Object nextElement() {
return keys[i++];
}
};
}
};
} else {
props = new Properties();
}
if (getFileManager().exists(filePath)) {
mutableFile = getFileManager().updateFile(filePath);
loadProperties(props, mutableFile.getInputStream());
} else {
// Unable to find the file, so let's create it
mutableFile = getFileManager().createFile(filePath);
}
boolean saveNeeded = false;
boolean needForce = false;
Map<String, String> overwriteProperties = new HashMap<String, String>();
for (final Entry<String, String> entry : properties.entrySet()) {
String key = entry.getKey();
// Adding prefix if needed
if (StringUtils.isNotBlank(prefix)) {
key = prefix.concat(".").concat(key);
}
final String newValue = entry.getValue();
final String existingValue = props.getProperty(key);
if (existingValue != null && !newValue.equals(existingValue) && preventChangesIfAlreadyExists) {
// Ignore this label if already exists
continue;
} else if (existingValue == null || !existingValue.equals(newValue) && force) {
props.setProperty(key, newValue);
saveNeeded = true;
} else if (!existingValue.equals(newValue) && !force) {
// ROO-3702: Show error when tries to update some properties that
// already exists and --force global param is false.
needForce = true;
overwriteProperties.put(key, existingValue);
}
}
// already exists and --force global param is false.
if (needForce) {
String propertyCount = overwriteProperties.size() > 1 ? "Properties" : "Property";
String propertyLists = "";
for (Entry<String, String> property : overwriteProperties.entrySet()) {
String key = property.getKey();
String value = property.getValue();
propertyLists = propertyLists.concat("'").concat(key).concat(" = ").concat(value).concat("', ");
}
String msg = String.format("WARNING: %s %s already exists. " + "Use --force parameter to overwrite it.", propertyCount, propertyLists.substring(0, propertyLists.length() - 2));
throw new RuntimeException(msg);
}
if (saveNeeded) {
storeProps(props, mutableFile.getOutputStream(), "Updated at " + new Date());
String propertyCount = props.size() > 1 ? "Properties" : "Property";
String haveCount = props.size() > 1 ? "have" : "has";
String propertyLists = "";
for (Entry<Object, Object> property : props.entrySet()) {
String key = (String) property.getKey();
String value = (String) property.getValue();
propertyLists = propertyLists.concat("'").concat(key).concat(" = ").concat(value).concat("', ");
}
}
}
Aggregations