View Javadoc

1   /*
2   Karma core - Core of the Karma application
3   Copyright (C) 2004  Toolforge <www.toolforge.nl>
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19  package nl.toolforge.karma.core;
20  
21  
22  import nl.toolforge.karma.core.location.LocationException;
23  import nl.toolforge.karma.core.manifest.ManifestException;
24  
25  import java.text.MessageFormat;
26  
27  /***
28   *
29   *
30   * @author D.A. Smedes
31   * @version $Id: KarmaException.java,v 1.34 2004/10/04 11:43:22 asmedes Exp $
32   */
33  public class KarmaException extends Exception {
34  
35    // TODO : Work trough all errorcodes and come up with a nice setup of codes.
36    //
37  
38    public static final String EXCEPTION_PREFIX = "KAR-";
39  
40    /***
41     * Can be used to identify something that is not implemented
42     */
43    public static final ErrorCode NOT_IMPLEMENTED = new ErrorCode(EXCEPTION_PREFIX + "00000");
44    /***
45     * Default configuration has been created.
46     */
47    public static final ErrorCode DEFAULT_CONFIGURATION_CREATED = new ErrorCode(EXCEPTION_PREFIX + "00002");
48    /***
49     * Vital configuration is missing
50     */
51    public static final ErrorCode MISSING_CONFIGURATION = new ErrorCode(EXCEPTION_PREFIX + "00001");
52  
53    public static final ErrorCode WORKING_CONTEXT_NOT_FOUND = new ErrorCode(EXCEPTION_PREFIX + "00002");;
54  
55    /***
56     * When the manifest store directory could not be found.
57     */
58    public static final ErrorCode MANIFEST_STORE_NOT_FOUND = new ErrorCode(ManifestException.EXCEPTION_PREFIX + "00010");
59    /***
60     * When the location store directory could not be found.
61     */
62    public static final ErrorCode LOCATION_STORE_NOT_FOUND = new ErrorCode(LocationException.EXCEPTION_PREFIX + "00011");
63    /***
64     * No development home directory could be referenced to. This is panic, because without it, nothing will work.
65     */
66    public static final ErrorCode DEVELOPMENT_HOME_NOT_FOUND = new ErrorCode(EXCEPTION_PREFIX + "00012");
67    /***
68     * When updating the manifest store failed.
69     */
70    public static final ErrorCode MANIFEST_STORE_UPDATE_FAILED = new ErrorCode(EXCEPTION_PREFIX + "00013");;
71    /***
72     * When updating the location store failed.
73     */
74    public static final ErrorCode LOCATION_STORE_UPDATE_FAILED = new ErrorCode(EXCEPTION_PREFIX + "00014");;
75  
76    /***
77     * The build of a module failed.
78     */
79    // todo NO_MAVEN_PROJECT_XML should be moved to CommandException ?
80    public static final ErrorCode NO_MAVEN_PROJECT_XML = new ErrorCode(EXCEPTION_PREFIX + "00100");
81  
82    protected ErrorCode errorCode = null;
83  
84    protected Object[] messageArguments = new Object[]{};
85  
86    /***
87     * Create a new KarmaException, with the specific errorCode.
88     *
89     * @param errorCode The errorCode that identifies the specific error that has occurred.
90     */
91    public KarmaException(ErrorCode errorCode) {
92      this.errorCode = errorCode;
93    }
94  
95    /***
96     * Create a new KarmaException, with the specific errorCode and messageArguments.
97     *
98     * @param errorCode        The errorCode that identifies the specific error that has occurred.
99     * @param messageArguments These arguments are filled in into the error codes' message.
100    */
101   public KarmaException(ErrorCode errorCode, Object[] messageArguments) {
102     this.errorCode = errorCode;
103     this.messageArguments = messageArguments;
104   }
105 
106   /***
107    * Create a new KarmaException, with the specific errorCode and Throwable that caused the exception.
108    *
109    * @param errorCode The errorCode that identifies the specific error that has occurred.
110    * @param t         The Throwable that caused this specific exception.
111    */
112   public KarmaException(ErrorCode errorCode, Throwable t) {
113     super(t);
114     this.errorCode = errorCode;
115   }
116 
117   /***
118    * Create a new KarmaException, with the specific errorCode and Throwable that caused the exception.
119    *
120    * @param errorCode        The errorCode that identifies the specific error that has occurred.
121    * @param messageArguments These arguments are filled in into the error codes' message.
122    * @param t                The Throwable that caused this specific exception.
123    */
124   public KarmaException(ErrorCode errorCode, Object[] messageArguments, Throwable t) {
125     super(t);
126     this.errorCode = errorCode;
127     this.messageArguments = messageArguments;
128   }
129 
130   /***
131    * Gets this instance' {@link nl.toolforge.karma.core.ErrorCode}.
132    *
133    * @return This instance' {@link nl.toolforge.karma.core.ErrorCode} or <code>null</code> if this exception was not initialized with an
134    *         <code>ErrorCode</code>.
135    */
136   public ErrorCode getErrorCode() {
137     return errorCode;
138   }
139 
140   /***
141    * A <code>KarmaException</code> can be constructed with a structured error code {@link nl.toolforge.karma.core.ErrorCode}. When this is
142    * done, the error message will return {@link nl.toolforge.karma.core.ErrorCode#getErrorMessage} for this exception. If no
143    * <code>ErrorCode</code> was used for initialization, the exceptions' {@link #getMessage} is returned, so there is
144    * always something to tell the developer or user.
145    *
146    * @return Return's the <code>ErrorCode</code>s' error message, if the <code>ErrorCode</code> was set, otherwise it
147    *         will return <code>Throwable.getMessage()</code>.
148    */
149   public String getErrorMessage() {
150 //    if (messageArguments != null && messageArguments.length > 0) {
151 //      errorCode.setMessageArguments(messageArguments);
152 //    }
153 //    return errorCode.getErrorMessage();
154     if (getMessageArguments() != null && getMessageArguments().length != 0) {
155       MessageFormat messageFormat = new MessageFormat(getErrorCode().getErrorMessage());
156       return messageFormat.format(getMessageArguments());
157     } else {
158       return getErrorCode().getErrorMessage();
159     }
160   }
161 
162   /***
163    * @return The arguments that are to be filled in into the error codes' message.
164    */
165   public final Object[] getMessageArguments() {
166     return messageArguments;
167   }
168 
169   public String getMessage() {
170     return getErrorMessage();
171   }
172 }