1package org.apache.archiva.policies;
23/*4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */2122import java.text.MessageFormat;
23import java.util.Locale;
24import java.util.ResourceBundle;
25import java.util.stream.Collectors;
2627/**28 * Abstract policy class that handles the name and description loading with message bundles.29 *30 * The prefix for the keys is normally:31 * <ul>32 * <li>Policies: POLICY-ID.policy.</li>33 * <li>Options: POLICY-ID.option.</li>34 * </ul>35 *36 * This prefix can be changed by subclasses.37 *38 * For each policy and each option there must exist a name and description entry in the message bundle.39 *40 */41publicabstractclassAbstractPolicyimplementsPolicy {
4243private String policyPrefix;
44private String optionPrefix;
4546publicAbstractPolicy() {
47 policyPrefix = getId() + ".policy.";
48 optionPrefix = getId() + ".option.";
49 }
5051protected String getPolicyPrefix() {
52return policyPrefix;
53 }
5455protected String getOptionPrefix() {
56return optionPrefix;
57 }
5859protectedvoid setPolicyPrefix(String policyPrefix) {
60this.policyPrefix = policyPrefix;
61 }
6263publicvoid setOptionPrefix(String optionPrefix) {
64this.optionPrefix = optionPrefix;
65 }
6667privatestaticfinal ResourceBundle getBundle(Locale locale) {
68return ResourceBundle.getBundle(RESOURCE_BUNDLE, locale);
69 }
707172 @Override
73public String getName() {
74return getName(Locale.getDefault());
75 }
7677 @Override
78public String getName(Locale locale) {
79return getBundle(locale).getString(getPolicyPrefix() + "name");
80 }
8182 @Override
83public String getDescription(Locale locale) {
84return MessageFormat.format(getBundle(locale).getString(getPolicyPrefix() + "description")
85 , getOptions().stream().map(o -> o.getId()).collect(Collectors.joining(",")));
86 }
8788 @Override
89public String getOptionDescription(Locale locale, PolicyOption option) {
90return getBundle(locale).getString(getOptionPrefix()+option.getId()+".description");
91 }
9293 @Override
94public String getOptionName(Locale locale, PolicyOption option) {
95return getBundle(locale).getString(getOptionPrefix()+option.getId()+".name");
96 }
9798 }