crazyboblee | abc4dd0 | 2007-02-01 01:44:36 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2006 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 16 | |
| 17 | package com.google.inject; |
| 18 | |
crazyboblee | e039bac | 2007-02-02 21:42:09 +0000 | [diff] [blame] | 19 | import com.google.inject.spi.Message; |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame] | 20 | |
| 21 | import java.util.*; |
crazyboblee | e039bac | 2007-02-02 21:42:09 +0000 | [diff] [blame] | 22 | |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 23 | /** |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 24 | * Thrown when errors occur while creating a {@link Injector}. Includes a list |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 25 | * of encountered errors. Typically, a client should catch this exception, log |
| 26 | * it, and stop execution. |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 27 | * |
| 28 | * @author crazybob@google.com (Bob Lee) |
| 29 | */ |
crazyboblee | c1d0c64 | 2007-03-07 17:20:22 +0000 | [diff] [blame] | 30 | public class CreationException extends RuntimeException { |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 31 | |
crazyboblee | d9d16a0 | 2007-09-09 21:18:48 +0000 | [diff] [blame] | 32 | final List<? extends Message> errorMessages; |
crazyboblee | e039bac | 2007-02-02 21:42:09 +0000 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Constructs a new exception for the given errors. |
| 36 | */ |
crazyboblee | d9d16a0 | 2007-09-09 21:18:48 +0000 | [diff] [blame] | 37 | public CreationException(Collection<? extends Message> errorMessages) { |
crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 38 | super(); |
crazyboblee | 0789b19 | 2007-02-13 02:43:28 +0000 | [diff] [blame] | 39 | |
| 40 | // Sort the messages by source. |
| 41 | this.errorMessages = new ArrayList<Message>(errorMessages); |
| 42 | Collections.sort(this.errorMessages, new Comparator<Message>() { |
| 43 | public int compare(Message a, Message b) { |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame] | 44 | return a.getSource().compareTo(b.getSource()); |
crazyboblee | 0789b19 | 2007-02-13 02:43:28 +0000 | [diff] [blame] | 45 | } |
| 46 | }); |
crazyboblee | e039bac | 2007-02-02 21:42:09 +0000 | [diff] [blame] | 47 | } |
| 48 | |
crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 49 | public String getMessage() { |
| 50 | return createErrorMessage(errorMessages); |
| 51 | } |
| 52 | |
crazyboblee | d9d16a0 | 2007-09-09 21:18:48 +0000 | [diff] [blame] | 53 | private static String createErrorMessage( |
| 54 | Collection<? extends Message> errorMessages) { |
kevinb9n | 6a565c7 | 2007-02-11 01:58:33 +0000 | [diff] [blame] | 55 | Formatter fmt = new Formatter().format("Guice configuration errors:%n%n"); |
crazyboblee | e039bac | 2007-02-02 21:42:09 +0000 | [diff] [blame] | 56 | int index = 1; |
| 57 | for (Message errorMessage : errorMessages) { |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame] | 58 | fmt.format("%s) Error at %s:%n", index++, errorMessage.getSource()) |
kevinb9n | 6a565c7 | 2007-02-11 01:58:33 +0000 | [diff] [blame] | 59 | .format(" %s%n%n", errorMessage.getMessage()); |
crazyboblee | e039bac | 2007-02-02 21:42:09 +0000 | [diff] [blame] | 60 | } |
crazyboblee | 0bfdbc6 | 2007-02-20 21:47:19 +0000 | [diff] [blame] | 61 | return fmt.format("%s error[s]", errorMessages.size()).toString(); |
crazyboblee | e039bac | 2007-02-02 21:42:09 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Gets the error messages which resulted in this exception. |
| 66 | */ |
| 67 | public Collection<Message> getErrorMessages() { |
| 68 | return Collections.unmodifiableCollection(errorMessages); |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 69 | } |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 70 | } |