Skip to content

Commit

Permalink
Merge pull request #441 from wttech/fix/rework-custom-definitions
Browse files Browse the repository at this point in the history
added custom definitions configuration service
  • Loading branch information
dprzybyl authored Mar 24, 2024
2 parents 5ab35a8 + cf10eba commit 45ce7f7
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.stream.Collectors;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.jackrabbit.api.JackrabbitSession;
Expand Down Expand Up @@ -193,9 +195,13 @@ private void updateScriptProperties(Script script, ExecutionMode mode, boolean s

@Override
public Map<String, String> getPredefinedDefinitions() {
Map<String, String> predefinedDefinitions = new HashMap<>();
definitionsProviders.forEach(provider -> predefinedDefinitions.putAll(provider.getPredefinedDefinitions()));
return predefinedDefinitions;
return definitionsProviders.stream()
.flatMap(provider -> provider.getPredefinedDefinitions().entrySet().stream())
.collect(Collectors.toMap(
Map.Entry::getKey, Map.Entry::getValue,
(first, second) -> first,
() -> new TreeMap<>(String.CASE_INSENSITIVE_ORDER)
));
}

private ActionExecutor createExecutor(ExecutionMode mode, ResourceResolver resolver) throws RepositoryException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*-
* ========================LICENSE_START=================================
* AEM Permission Management
* %%
* Copyright (C) 2013 Wunderman Thompson Technology
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =========================LICENSE_END==================================
*/
package com.cognifide.apm.core.services;

import com.cognifide.apm.api.services.DefinitionsProvider;
import com.cognifide.apm.core.Property;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.metatype.annotations.AttributeDefinition;
import org.osgi.service.metatype.annotations.Designate;
import org.osgi.service.metatype.annotations.ObjectClassDefinition;

@Component(
immediate = true,
property = {
Property.DESCRIPTION + "APM Custom Definitions Provider",
Property.VENDOR
}
)
@Designate(ocd = CustomDefinitionsProvider.Configuration.class, factory = true)
public class CustomDefinitionsProvider implements DefinitionsProvider {

private Map<String, String> predefinedDefinitions;

@Activate
public void activate(Configuration config) {
predefinedDefinitions = Arrays.stream(config.definitions())
.map(definition -> definition.split("="))
.map(StringUtils::stripAll)
.filter(StringUtils::isNoneEmpty)
.filter(parts -> parts.length == 2)
.collect(Collectors.toMap(parts -> parts[0], parts -> parts[1], (first, second) -> first));
}

@Override
public Map<String, String> getPredefinedDefinitions() {
return predefinedDefinitions;
}

@ObjectClassDefinition(name = "AEM Permission Management - Custom Definitions Configuration")
public @interface Configuration {

@AttributeDefinition(name = "Definitions", description = "format: key=value")
String[] definitions();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* limitations under the License.
* =========================LICENSE_END==================================
*/
package com.cognifide.apm.core.scripts;
package com.cognifide.apm.core.services;

import com.cognifide.apm.api.services.DefinitionsProvider;
import java.util.HashMap;
Expand Down

0 comments on commit 45ce7f7

Please sign in to comment.