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.

RepositoryAdminException.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package org.apache.archiva.admin.model;
  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 org.apache.commons.lang3.StringUtils;
  21. import java.text.MessageFormat;
  22. import java.util.Locale;
  23. import java.util.ResourceBundle;
  24. /**
  25. *
  26. * Base exception class for the admin interfaces. Exceptions should set keys that allows identifying and classifying the error.
  27. *
  28. * @author Olivier Lamy
  29. * @since 1.4-M1
  30. */
  31. public class RepositoryAdminException
  32. extends Exception
  33. {
  34. private static final ResourceBundle bundle = ResourceBundle.getBundle( "org.apache.archiva.admin.model.error.AdminErrors", Locale.ROOT );
  35. /**
  36. * can return the field name of bean with issue
  37. * can be <code>null</code>
  38. * @since 1.4-M3
  39. */
  40. private String fieldName;
  41. /**
  42. * A unique identifier of this error
  43. * @since 3.0
  44. */
  45. private String key;
  46. private boolean keyExists = false;
  47. /**
  48. * Message parameters
  49. */
  50. String[] parameters = new String[0];
  51. protected static String getMessage( String key, String[] params )
  52. {
  53. return MessageFormat.format( bundle.getString( key ), params );
  54. }
  55. /**
  56. * Tries to retrieve a message from the bundle for the given key and returns the
  57. * exception.
  58. * @param key the identifier of the error
  59. * @param params parameters for translating the message
  60. * @return the exception
  61. */
  62. public static RepositoryAdminException ofKey(String key, String... params) {
  63. String message = getMessage( key, params );
  64. RepositoryAdminException ex = new RepositoryAdminException( message );
  65. ex.setKey( key );
  66. ex.setParameters( params );
  67. return ex;
  68. }
  69. /**
  70. * Tries to retrieve a message from the bundle for the given key and returns the
  71. * exception.
  72. * @param key the identifier of the error
  73. * @param cause the exception that caused the error
  74. * @param params parameters for translating the message
  75. * @return the exception
  76. */
  77. public static RepositoryAdminException ofKey(String key, Throwable cause, String... params) {
  78. String message = getMessage( key, params );
  79. RepositoryAdminException ex = new RepositoryAdminException( message, cause );
  80. ex.setKey( key );
  81. ex.setParameters( params );
  82. return ex;
  83. }
  84. /**
  85. * Tries to retrieve a message from the bundle for the given key and the given field and returns the
  86. * exception.
  87. * @param key the identifier of the error
  88. * @param fieldName the field this exception is for
  89. * @param params parameters for translating the message
  90. * @return the exception
  91. */
  92. public static RepositoryAdminException ofKeyAndField(String key, String fieldName, String... params) {
  93. String message = getMessage( key, params );
  94. RepositoryAdminException ex = new RepositoryAdminException( message, fieldName );
  95. ex.setKey( key );
  96. ex.setParameters( params );
  97. return ex;
  98. }
  99. /**
  100. * Tries to retrieve a message from the bundle for the given key and the given field and returns the
  101. * exception.
  102. * @param key the identifier of the error
  103. * @param fieldName the field this exception is for
  104. * @param cause the exception that caused this error
  105. * @param params parameters for translating the message
  106. * @return the exception
  107. */
  108. public static RepositoryAdminException ofKeyAndField(String key, Throwable cause, String fieldName, String... params) {
  109. String message = getMessage( key, params );
  110. RepositoryAdminException ex = new RepositoryAdminException( message, cause, fieldName );
  111. ex.setKey( key );
  112. ex.setFieldName( fieldName );
  113. ex.setParameters( params );
  114. return ex;
  115. }
  116. public RepositoryAdminException( String s )
  117. {
  118. super( s );
  119. }
  120. public RepositoryAdminException( String s, String fieldName )
  121. {
  122. this( s );
  123. this.fieldName = fieldName;
  124. }
  125. public RepositoryAdminException( String message, Throwable cause )
  126. {
  127. super( message, cause );
  128. }
  129. public RepositoryAdminException( String message, Throwable cause, String fieldName )
  130. {
  131. super( message, cause );
  132. this.fieldName = fieldName;
  133. }
  134. public String getFieldName()
  135. {
  136. return fieldName;
  137. }
  138. public void setFieldName( String fieldName )
  139. {
  140. this.fieldName = fieldName;
  141. }
  142. public String getKey( )
  143. {
  144. return key;
  145. }
  146. public void setKey( String key )
  147. {
  148. this.keyExists=!StringUtils.isEmpty( key );
  149. this.key = key;
  150. }
  151. public boolean keyExists() {
  152. return this.keyExists;
  153. }
  154. public String[] getParameters( )
  155. {
  156. return parameters;
  157. }
  158. public void setParameters( String[] parameters )
  159. {
  160. if (parameters==null) {
  161. this.parameters = new String[0];
  162. }
  163. this.parameters = parameters;
  164. }
  165. }