You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractPolicy.java 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package org.apache.archiva.policies;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import java.text.MessageFormat;
  21. import java.util.Locale;
  22. import java.util.ResourceBundle;
  23. import java.util.stream.Collectors;
  24. /**
  25. * Abstract policy class that handles the name and description loading with message bundles.
  26. *
  27. * The prefix for the keys is normally:
  28. * <ul>
  29. * <li>Policies: POLICY-ID.policy.</li>
  30. * <li>Options: POLICY-ID.option.</li>
  31. * </ul>
  32. *
  33. * This prefix can be changed by subclasses.
  34. *
  35. * For each policy and each option there must exist a name and description entry in the message bundle.
  36. *
  37. */
  38. public abstract class AbstractPolicy implements Policy {
  39. private String policyPrefix;
  40. private String optionPrefix;
  41. public AbstractPolicy() {
  42. policyPrefix = getId() + ".policy.";
  43. optionPrefix = getId() + ".option.";
  44. }
  45. protected String getPolicyPrefix() {
  46. return policyPrefix;
  47. }
  48. protected String getOptionPrefix() {
  49. return optionPrefix;
  50. }
  51. protected void setPolicyPrefix(String policyPrefix) {
  52. this.policyPrefix = policyPrefix;
  53. }
  54. public void setOptionPrefix(String optionPrefix) {
  55. this.optionPrefix = optionPrefix;
  56. }
  57. private static final ResourceBundle getBundle(Locale locale) {
  58. return ResourceBundle.getBundle(RESOURCE_BUNDLE, locale);
  59. }
  60. @Override
  61. public String getName() {
  62. return getName(Locale.getDefault());
  63. }
  64. @Override
  65. public String getName(Locale locale) {
  66. return getBundle(locale).getString(getPolicyPrefix() + "name");
  67. }
  68. @Override
  69. public String getDescription(Locale locale) {
  70. return MessageFormat.format(getBundle(locale).getString(getPolicyPrefix() + "description")
  71. , getOptions().stream().map(o -> o.getId()).collect(Collectors.joining(",")));
  72. }
  73. @Override
  74. public String getOptionDescription(Locale locale, PolicyOption option) {
  75. return getBundle(locale).getString(getOptionPrefix()+option.getId()+".description");
  76. }
  77. @Override
  78. public String getOptionName(Locale locale, PolicyOption option) {
  79. return getBundle(locale).getString(getOptionPrefix()+option.getId()+".name");
  80. }
  81. }