in src/main/java/org/apache/sling/serviceuser/webconsole/impl/ServiceUserWebConsolePlugin.java [135:192]
private boolean createOrUpdateMapping(HttpServletRequest request, ResourceResolver resolver) {
String appPath = getParameter(request, PN_APP_PATH, "");
Iterator<Resource> configs = resolver.findResources(
"SELECT * FROM [sling:OsgiConfig] WHERE ISDESCENDANTNODE([" + appPath + "]) AND NAME() LIKE '"
+ COMPONENT_NAME + "%'",
Query.JCR_SQL2);
try {
boolean dirty = false;
Resource config = null;
if (configs.hasNext()) {
config = configs.next();
log.debug("Using existing configuration {}", config);
} else {
String path =
appPath + "/config/" + COMPONENT_NAME + "-" + appPath.substring(appPath.lastIndexOf('/') + 1);
log.debug("Creating new configuration {}", path);
config = ResourceUtil.getOrCreateResource(
resolver,
path,
Collections.singletonMap(JcrConstants.JCR_PRIMARYTYPE, (Object) "sling:OsgiConfig"),
NodeType.NT_FOLDER,
false);
dirty = true;
}
String bundle = getParameter(request, PN_BUNDLE, "");
String subService = getParameter(request, PN_SUB_SERVICE, "");
String name = getParameter(request, PN_NAME, "");
String mapping = bundle + (StringUtils.isNotBlank(subService) ? ":" + subService : "") + "=" + name;
ModifiableValueMap properties = config.adaptTo(ModifiableValueMap.class);
String[] mappings = properties.get("user.mapping", new String[0]);
if (!ArrayUtils.contains(mappings, mapping)) {
log.debug("Adding {} into service user mapping", mapping);
List<String> m = new ArrayList<>();
m.addAll(Arrays.asList(mappings));
m.add(mapping);
properties.put("user.mapping", m.toArray(new String[m.size()]));
dirty = true;
} else {
log.debug("Already found {} in service user mapping", mapping);
}
if (dirty) {
log.debug("Saving changes to osgi config");
resolver.commit();
}
} catch (PersistenceException e) {
log.warn("Exception creating service mapping", e);
return false;
}
return true;
}