01: /**
02: * Copyright (C) 2006 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: */package com.bm.ejb3guice.inject;
16:
17: import com.bm.ejb3guice.spi.Message;
18:
19: import java.util.ArrayList;
20: import java.util.Collection;
21: import java.util.Collections;
22: import java.util.Comparator;
23: import java.util.Formatter;
24: import java.util.List;
25:
26: /**
27: * Thrown when errors occur while creating a {@link Injector}. Includes a list
28: * of encountered errors. Typically, a client should catch this exception, log
29: * it, and stop execution.
30: *
31: * @author crazybob@google.com (Bob Lee)
32: */
33: public class CreationException extends RuntimeException {
34:
35: final List<Message> errorMessages;
36:
37: /**
38: * Constructs a new exception for the given errors.
39: */
40: public CreationException(Collection<Message> errorMessages) {
41: super ();
42:
43: // Sort the messages by source.
44: this .errorMessages = new ArrayList<Message>(errorMessages);
45: Collections.sort(this .errorMessages, new Comparator<Message>() {
46: public int compare(Message a, Message b) {
47: return a.getSourceString().compareTo(
48: b.getSourceString());
49: }
50: });
51: }
52:
53: public String getMessage() {
54: return createErrorMessage(errorMessages);
55: }
56:
57: private static String createErrorMessage(
58: Collection<Message> errorMessages) {
59: Formatter fmt = new Formatter()
60: .format("Guice configuration errors:%n%n");
61: int index = 1;
62: for (Message errorMessage : errorMessages) {
63: fmt.format("%s) Error at %s:%n", index++,
64: errorMessage.getSourceString()).format(" %s%n%n",
65: errorMessage.getMessage());
66: }
67: return fmt.format("%s error[s]", errorMessages.size())
68: .toString();
69: }
70:
71: /**
72: * Gets the error messages which resulted in this exception.
73: */
74: public Collection<Message> getErrorMessages() {
75: return Collections.unmodifiableCollection(errorMessages);
76: }
77: }
|