use of org.springframework.roo.process.manager.MutableFile in project spring-roo by spring-projects.
the class PropFilesManagerServiceImpl method removePropertiesByPrefix.
@Override
public void removePropertiesByPrefix(LogicalPath propertyFilePath, String propertyFilename, String prefix) {
Validate.notBlank(prefix, "Prefix required");
Validate.notNull(propertyFilePath, "Property file path required");
Validate.notBlank(propertyFilename, "Property filename 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));
}
for (Entry property : props.entrySet()) {
String key = (String) property.getKey();
if (key != null && key.startsWith(prefix)) {
props.remove(key);
}
}
storeProps(props, mutableFile.getOutputStream(), "Updated at " + new Date());
}
use of org.springframework.roo.process.manager.MutableFile in project spring-roo by spring-projects.
the class PropFilesManagerServiceImpl method getProperty.
@Override
public String getProperty(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 {
return null;
}
// Including prefix if needed
if (StringUtils.isNotBlank(prefix)) {
key = prefix.concat(".").concat(key);
}
return props.getProperty(key);
}
use of org.springframework.roo.process.manager.MutableFile in project spring-roo by spring-projects.
the class MavenOperationsImpl method addApplicationDevPropertiesFile.
/**
* Copy file application-dev.properties
*
* @param Pom
* module where application-dev.properties should be generated
* @param topLevelPackage
* JavaPackage that represents the top level package
*/
private void addApplicationDevPropertiesFile(Pom module, JavaPackage topLevelPackage) {
LogicalPath resourcesPath = LogicalPath.getInstance(Path.SRC_MAIN_RESOURCES, module.getModuleName());
String filePath = getPathResolver().getIdentifier(resourcesPath, "application-dev.properties");
MutableFile appDevMutableFile = null;
final Properties props = new Properties();
InputStream inputStream = null;
try {
if (fileManager.exists(filePath)) {
appDevMutableFile = fileManager.updateFile(filePath);
inputStream = appDevMutableFile.getInputStream();
props.load(inputStream);
} else {
appDevMutableFile = fileManager.createFile(filePath);
}
} catch (final IOException ioe) {
throw new IllegalStateException(ioe);
} finally {
IOUtils.closeQuietly(inputStream);
}
OutputStream outputStream = null;
try {
outputStream = appDevMutableFile.getOutputStream();
props.put("spring.messages.cache-seconds", "0");
props.put("logging.file", "");
props.put("logging.level.".concat(topLevelPackage.getFullyQualifiedPackageName()), "DEBUG");
props.store(outputStream, "Updated at " + new Date());
} catch (final IOException ioe) {
throw new IllegalStateException(ioe);
} finally {
IOUtils.closeQuietly(outputStream);
}
}
use of org.springframework.roo.process.manager.MutableFile 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.process.manager.MutableFile in project spring-roo by spring-projects.
the class TilesOperationsImpl method writeToDiskIfNecessary.
/**
* @param viewsDefinitionFile the canonical path of the file to update
* @param body the element whose parent document is to be written
* @return
*/
private boolean writeToDiskIfNecessary(final String tilesDefinitionFile, final Element body) {
// Build a string representation of the Tiles config file
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
final Transformer transformer = XmlUtils.createIndentingTransformer();
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "http://tiles.apache.org/dtds/tiles-config_2_1.dtd");
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN");
XmlUtils.writeXml(transformer, byteArrayOutputStream, body.getOwnerDocument());
final String viewContent = byteArrayOutputStream.toString();
// If mutableFile becomes non-null, it means we need to use it to write
// out the contents of jspContent to the file
MutableFile mutableFile = null;
if (fileManager.exists(tilesDefinitionFile)) {
// First verify if the file has even changed
final File file = new File(tilesDefinitionFile);
String existing = null;
try {
existing = org.apache.commons.io.FileUtils.readFileToString(file);
} catch (final IOException ignored) {
}
if (!viewContent.equals(existing)) {
mutableFile = fileManager.updateFile(tilesDefinitionFile);
}
} else {
mutableFile = fileManager.createFile(tilesDefinitionFile);
Validate.notNull(mutableFile, "Could not create tiles view definition '%s'", tilesDefinitionFile);
}
if (mutableFile != null) {
OutputStream outputStream = null;
try {
// We need to write the file out (it's a new file, or the
// existing file has different contents)
outputStream = mutableFile.getOutputStream();
IOUtils.write(viewContent, outputStream);
// Return and indicate we wrote out the file
return true;
} catch (final IOException ioe) {
throw new IllegalStateException("Could not output '" + mutableFile.getCanonicalPath() + "'", ioe);
} finally {
IOUtils.closeQuietly(outputStream);
}
}
// A file existed, but it contained the same content, so we return false
return false;
}
Aggregations