throw new IllegalStateException("Unable to create temporary directory");
}
if (!tempFile.delete() || !tempFile.mkdir()) {
throw new IllegalStateException("Unable to create temporary directory");
}
ArchiveManipulator am = new ArchiveManipulator();
am.extract(source, dir);
Properties properties;
try {
if (propertyFile == null) {
propertyFile = source.substring(0, source.lastIndexOf(".")) +
PROPERTIES_FILE_EXTENSION;
}
properties = loadProperties(propertyFile);
if (properties.size() > 0) {
for (String str : am.check(source)) {
try {
String filePath = dir + File.separator + str;
File file = new File(filePath);
if (file.isFile()) {
List<String> archiveTypes = Arrays.asList("jar", "aar", "war",
"dar", "mar", "gar", "zip");
List<String> textTypes = Arrays.asList("xml", "dbs", "xslt",
"properties", "service", "js", "jsp", "css", "txt", "wsdl",
"bpel");
if (FilenameUtils.isExtension(filePath, archiveTypes)) {
File modified = new File(generateAppArchiveForTenant(filePath,
session, propertyFile));
FileUtils.copyFile(modified, file);
FileUtils.deleteDirectory(modified.getParentFile());
} else if (FilenameUtils.isExtension(filePath, textTypes)) {
String line;
StringBuffer oldText = new StringBuffer();
BufferedReader reader = new BufferedReader(
new FileReader(file));
FileWriter writer = null;
try {
while ((line = reader.readLine()) != null) {
oldText.append(line).append("\n");
}
String newText = oldText.toString();
for (String key : properties.stringPropertyNames()) {
String value = properties.getProperty(key)
.replace(TENANT_ID_KEY,
Integer.toString(tenantId))
.replace(TENANT_DOMAIN_KEY, tenantDomain)
.replace(USER_NAME, username);
newText = newText.replace(key, value);
}
writer = new FileWriter(file);
writer.write(newText);
} finally {
try {
reader.close();
} finally {
if (writer != null) {
writer.close();
}
}
}
}
}
} catch (IOException e) {
log.warn("An error occurred while making replacements in a file in: " +
source, e);
}
}
}
} catch (IOException e) {
log.warn("An error occurred while reading properties file for: " + source, e);
}
am.archiveDir(destination, dir);
} finally {
if (tempDir != null) {
FileUtils.deleteDirectory(tempDir);
}
}