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; |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 20 | import java.util.ArrayList; |
crazyboblee | e039bac | 2007-02-02 21:42:09 +0000 | [diff] [blame] | 21 | import java.util.Collection; |
| 22 | import java.util.Collections; |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 23 | import java.util.Comparator; |
kevinb9n | 6a565c7 | 2007-02-11 01:58:33 +0000 | [diff] [blame] | 24 | import java.util.Formatter; |
crazyboblee | 0789b19 | 2007-02-13 02:43:28 +0000 | [diff] [blame] | 25 | import java.util.List; |
crazyboblee | e039bac | 2007-02-02 21:42:09 +0000 | [diff] [blame] | 26 | |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 27 | /** |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 28 | * Thrown when errors occur while creating a {@link Injector}. Includes a list |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 29 | * of encountered errors. Typically, a client should catch this exception, log |
| 30 | * it, and stop execution. |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 31 | * |
| 32 | * @author crazybob@google.com (Bob Lee) |
| 33 | */ |
crazyboblee | c1d0c64 | 2007-03-07 17:20:22 +0000 | [diff] [blame] | 34 | public class CreationException extends RuntimeException { |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 35 | |
crazyboblee | d9d16a0 | 2007-09-09 21:18:48 +0000 | [diff] [blame^] | 36 | final List<? extends Message> errorMessages; |
crazyboblee | e039bac | 2007-02-02 21:42:09 +0000 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Constructs a new exception for the given errors. |
| 40 | */ |
crazyboblee | d9d16a0 | 2007-09-09 21:18:48 +0000 | [diff] [blame^] | 41 | public CreationException(Collection<? extends Message> errorMessages) { |
crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 42 | super(); |
crazyboblee | 0789b19 | 2007-02-13 02:43:28 +0000 | [diff] [blame] | 43 | |
| 44 | // Sort the messages by source. |
| 45 | this.errorMessages = new ArrayList<Message>(errorMessages); |
| 46 | Collections.sort(this.errorMessages, new Comparator<Message>() { |
| 47 | public int compare(Message a, Message b) { |
| 48 | return a.getSourceString().compareTo(b.getSourceString()); |
| 49 | } |
| 50 | }); |
crazyboblee | e039bac | 2007-02-02 21:42:09 +0000 | [diff] [blame] | 51 | } |
| 52 | |
crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 53 | public String getMessage() { |
| 54 | return createErrorMessage(errorMessages); |
| 55 | } |
| 56 | |
crazyboblee | d9d16a0 | 2007-09-09 21:18:48 +0000 | [diff] [blame^] | 57 | private static String createErrorMessage( |
| 58 | Collection<? extends Message> errorMessages) { |
kevinb9n | 6a565c7 | 2007-02-11 01:58:33 +0000 | [diff] [blame] | 59 | Formatter fmt = new Formatter().format("Guice configuration errors:%n%n"); |
crazyboblee | e039bac | 2007-02-02 21:42:09 +0000 | [diff] [blame] | 60 | int index = 1; |
| 61 | for (Message errorMessage : errorMessages) { |
crazyboblee | 0789b19 | 2007-02-13 02:43:28 +0000 | [diff] [blame] | 62 | fmt.format("%s) Error at %s:%n", index++, errorMessage.getSourceString()) |
kevinb9n | 6a565c7 | 2007-02-11 01:58:33 +0000 | [diff] [blame] | 63 | .format(" %s%n%n", errorMessage.getMessage()); |
crazyboblee | e039bac | 2007-02-02 21:42:09 +0000 | [diff] [blame] | 64 | } |
crazyboblee | 0bfdbc6 | 2007-02-20 21:47:19 +0000 | [diff] [blame] | 65 | return fmt.format("%s error[s]", errorMessages.size()).toString(); |
crazyboblee | e039bac | 2007-02-02 21:42:09 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Gets the error messages which resulted in this exception. |
| 70 | */ |
| 71 | public Collection<Message> getErrorMessages() { |
| 72 | return Collections.unmodifiableCollection(errorMessages); |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 73 | } |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 74 | } |