001package org.apache.archiva.policies; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.text.MessageFormat; 023import java.util.Locale; 024import java.util.ResourceBundle; 025import java.util.stream.Collectors; 026 027/** 028 * Abstract policy class that handles the name and description loading with message bundles. 029 * 030 * The prefix for the keys is normally: 031 * <ul> 032 * <li>Policies: POLICY-ID.policy.</li> 033 * <li>Options: POLICY-ID.option.</li> 034 * </ul> 035 * 036 * This prefix can be changed by subclasses. 037 * 038 * For each policy and each option there must exist a name and description entry in the message bundle. 039 * 040 */ 041public abstract class AbstractPolicy implements Policy { 042 043 private String policyPrefix; 044 private String optionPrefix; 045 046 public AbstractPolicy() { 047 policyPrefix = getId() + ".policy."; 048 optionPrefix = getId() + ".option."; 049 } 050 051 protected String getPolicyPrefix() { 052 return policyPrefix; 053 } 054 055 protected String getOptionPrefix() { 056 return optionPrefix; 057 } 058 059 protected void setPolicyPrefix(String policyPrefix) { 060 this.policyPrefix = policyPrefix; 061 } 062 063 public void setOptionPrefix(String optionPrefix) { 064 this.optionPrefix = optionPrefix; 065 } 066 067 private static final ResourceBundle getBundle(Locale locale) { 068 return ResourceBundle.getBundle(RESOURCE_BUNDLE, locale); 069 } 070 071 072 @Override 073 public String getName() { 074 return getName(Locale.getDefault()); 075 } 076 077 @Override 078 public String getName(Locale locale) { 079 return getBundle(locale).getString(getPolicyPrefix() + "name"); 080 } 081 082 @Override 083 public String getDescription(Locale locale) { 084 return MessageFormat.format(getBundle(locale).getString(getPolicyPrefix() + "description") 085 , getOptions().stream().map(o -> o.getId()).collect(Collectors.joining(","))); 086 } 087 088 @Override 089 public String getOptionDescription(Locale locale, PolicyOption option) { 090 return getBundle(locale).getString(getOptionPrefix()+option.getId()+".description"); 091 } 092 093 @Override 094 public String getOptionName(Locale locale, PolicyOption option) { 095 return getBundle(locale).getString(getOptionPrefix()+option.getId()+".name"); 096 } 097 098}