anchela commented on code in PR #45: URL: https://github.com/apache/sling-org-apache-sling-feature-analyser/pull/45#discussion_r1627144277
########## src/main/java/org/apache/sling/feature/analyser/task/impl/CheckServiceUserMapping.java: ########## @@ -0,0 +1,161 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +package org.apache.sling.feature.analyser.task.impl; + +import org.apache.sling.feature.Configuration; +import org.apache.sling.feature.Configurations; +import org.apache.sling.feature.analyser.task.AnalyserTask; +import org.apache.sling.feature.analyser.task.AnalyserTaskContext; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.osgi.util.converter.Converters; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Set; + +public class CheckServiceUserMapping implements AnalyserTask { + + static final String SERVICE_USER_MAPPING_PID = "org.apache.sling.serviceusermapping.impl.ServiceUserMapperImpl"; + + static final String FACTORY_PID = "org.apache.sling.serviceusermapping.impl.ServiceUserMapperImpl.amended"; + + static final String USER_MAPPING = "user.mapping"; + + static final String CFG_WARN_ONLY_FOR_DEPRECATED_MAPPINGS = "warnOnlyForDeprecatedMappings"; + static final String CFG_WARN_ONLY_FOR_DEPRECATED_MAPPINGS_DEFAULT = Boolean.FALSE.toString(); + + private static final Logger log = LoggerFactory.getLogger(CheckServiceUserMapping.class); + + @Override + public String getName() { + return "Service User Mapping Check"; + } + + @Override + public String getId() { + return SERVICE_USER_MAPPING_PID; + } + + @Override + public void execute(final AnalyserTaskContext ctx) { + final boolean warnOnlyForDeprecation = Boolean.parseBoolean(ctx.getConfiguration().getOrDefault(CFG_WARN_ONLY_FOR_DEPRECATED_MAPPINGS, CFG_WARN_ONLY_FOR_DEPRECATED_MAPPINGS_DEFAULT)); + + // configuration + Configurations configurations = ctx.getFeature().getConfigurations(); + final Configuration cfg = configurations.getConfiguration(SERVICE_USER_MAPPING_PID); + if (cfg != null) { + check(ctx, cfg, warnOnlyForDeprecation); + } + for (final Configuration c : configurations.getFactoryConfigurations(FACTORY_PID)) { + check(ctx, c, warnOnlyForDeprecation); + } + } + + private static void check(final AnalyserTaskContext ctx, final Configuration cfg, final boolean warnOnlyForDeprecation) { + final Object val = cfg.getConfigurationProperties().get(USER_MAPPING); + if (val != null) { + final String[] mappings = Converters.standardConverter().convert(val).to(String[].class); + for (final String spec : mappings) { + check(ctx, cfg, spec, warnOnlyForDeprecation); + } + } + } + + private static void check(final @NotNull AnalyserTaskContext ctx, final @NotNull Configuration cfg, final @Nullable String spec, final boolean warnOnlyForDeprecation) { + final String id = cfg.getPid(); + if (spec == null || spec.trim().isEmpty()) { + log.warn("Ignoring empty mapping in {}", id); Review Comment: good point! thanks. i replaced all usages of the logger with configuration warning. so for the invalid mappings there will be a warning elaborating why the mapping is invalid followed by an error that the mapping statement in configuration x for pid y is invalid -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
