This project has retired. For details please refer to its Attic page.
AbstractPolicy xref
View Javadoc
1   package org.apache.archiva.policies;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.text.MessageFormat;
23  import java.util.Locale;
24  import java.util.ResourceBundle;
25  import java.util.stream.Collectors;
26  
27  /**
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   */
41  public abstract class AbstractPolicy implements Policy {
42  
43      private String policyPrefix;
44      private String optionPrefix;
45  
46      public AbstractPolicy() {
47          policyPrefix = getId() + ".policy.";
48          optionPrefix = getId() + ".option.";
49      }
50  
51      protected String getPolicyPrefix() {
52          return policyPrefix;
53      }
54  
55      protected String getOptionPrefix() {
56          return optionPrefix;
57      }
58  
59      protected void setPolicyPrefix(String policyPrefix) {
60          this.policyPrefix = policyPrefix;
61      }
62  
63      public void setOptionPrefix(String optionPrefix) {
64          this.optionPrefix = optionPrefix;
65      }
66  
67      private static final ResourceBundle getBundle(Locale locale) {
68          return ResourceBundle.getBundle(RESOURCE_BUNDLE, locale);
69      }
70  
71  
72      @Override
73      public String getName() {
74          return getName(Locale.getDefault());
75      }
76  
77      @Override
78      public String getName(Locale locale) {
79          return getBundle(locale).getString(getPolicyPrefix() + "name");
80      }
81  
82      @Override
83      public String getDescription(Locale locale) {
84          return MessageFormat.format(getBundle(locale).getString(getPolicyPrefix() + "description")
85                  , getOptions().stream().map(o -> o.getId()).collect(Collectors.joining(",")));
86      }
87  
88      @Override
89      public String getOptionDescription(Locale locale, PolicyOption option) {
90          return getBundle(locale).getString(getOptionPrefix()+option.getId()+".description");
91      }
92  
93      @Override
94      public String getOptionName(Locale locale, PolicyOption option) {
95          return getBundle(locale).getString(getOptionPrefix()+option.getId()+".name");
96      }
97  
98  }