in src/main/java/org/apache/sling/installer/provider/file/impl/FileInstaller.java [168:231]
private UpdateResult handleUpdate(final String resourceType,
final String id,
final String url,
final InputStream is,
final Dictionary<String, Object> dict,
final Map<String, Object> attributes) {
if ( !this.writeBack ) {
return null;
}
// we only handle add/update of configs for now
if ( !resourceType.equals(InstallableResource.TYPE_CONFIG) ) {
return null;
}
try {
final String path;
final String prefix;
if ( url != null ) {
// update
final int pos = url.indexOf(':');
final String oldPath = url.substring(pos + 1);
prefix = url.substring(0, pos);
// ensure extension '.cfg.json'
if ( !oldPath.endsWith(CONFIG_FILE_EXTENSION) ) {
final File file = new File(oldPath);
if ( file.exists() ) {
file.delete();
}
final int lastDot = oldPath.lastIndexOf('.');
final int lastSlash = oldPath.lastIndexOf('/');
if ( lastDot <= lastSlash ) {
path = oldPath + CONFIG_FILE_EXTENSION;
} else {
path = oldPath.substring(0, lastDot) + CONFIG_FILE_EXTENSION;
}
} else {
path = oldPath;
}
logger.debug("Update of {} at {}", resourceType, path);
} else {
// add
final FileMonitor first = this.monitors.get(0);
path = first.getRoot().getAbsolutePath() + '/' + id + CONFIG_FILE_EXTENSION;
prefix = first.getListener().getScheme();
logger.debug("Add of {} at {}", resourceType, path);
}
final File file = new File(path);
file.getParentFile().mkdirs();
try (OutputStream fos = new BufferedOutputStream(new FileOutputStream(file))) {
// comments starting with "//" allowed according to https://osgi.org/specification/osgi.cmpn/7.0.0/service.configurator.html#d0e131566
fos.write("// Configuration created by Apache Sling File Installer\n".getBytes(StandardCharsets.UTF_8));
ConfigurationSerializer serializer = ConfigurationSerializerFactory.create(ConfigurationSerializerFactory.Format.JSON);
serializer.serialize(dict, fos);
}
final UpdateResult result = new UpdateResult(prefix + ':' + path);
result.setResourceIsMoved(true);
return result;
} catch (final IOException e) {
logger.error("Unable to add/update resource " + resourceType + ':' + id, e);
return null;
}
}