View Javadoc
1   package org.codehaus.plexus.component.repository.exception;
2   
3   import java.io.ByteArrayOutputStream;
4   import java.io.PrintStream;
5   
6   import org.codehaus.plexus.classworlds.realm.ClassRealm;
7   
8   /*
9    * Copyright 2001-2006 Codehaus Foundation.
10   *
11   * Licensed under the Apache License, Version 2.0 (the "License");
12   * you may not use this file except in compliance with the License.
13   * You may obtain a copy of the License at
14   *
15   *      http://www.apache.org/licenses/LICENSE-2.0
16   *
17   * Unless required by applicable law or agreed to in writing, software
18   * distributed under the License is distributed on an "AS IS" BASIS,
19   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   * See the License for the specific language governing permissions and
21   * limitations under the License.
22   */
23  
24  /**
25   * The exception which is thrown by a component repository when
26   * the requested component cannot be found.
27   *
28   * @author Jason van Zyl
29   */
30  public class ComponentLookupException extends Exception {
31      private String LS = System.getProperty("line.separator");
32  
33      private String role;
34  
35      private String roleHint;
36  
37      private ClassRealm realm;
38  
39      public ComponentLookupException(String message, String role, String roleHint) {
40          super(message);
41  
42          this.role = role;
43  
44          this.roleHint = roleHint;
45      }
46  
47      public ComponentLookupException(String message, String role, String roleHint, Throwable cause) {
48          super(message, cause);
49  
50          this.role = role;
51  
52          this.roleHint = roleHint;
53      }
54  
55      public ComponentLookupException(String message, String role, String roleHint, ClassRealm realm) {
56          super(message);
57  
58          this.role = role;
59  
60          this.roleHint = roleHint;
61  
62          this.realm = realm;
63      }
64  
65      public ComponentLookupException(String message, String role, String roleHint, ClassRealm realm, Throwable cause) {
66          super(message, cause);
67  
68          this.role = role;
69  
70          this.roleHint = roleHint;
71  
72          this.realm = realm;
73      }
74  
75      public String getMessage() {
76          StringBuilder sb = new StringBuilder()
77                  .append(super.getMessage())
78                  .append(LS)
79                  .append("      role: ")
80                  .append(role)
81                  .append(LS)
82                  .append("  roleHint: ")
83                  .append(roleHint)
84                  .append(LS)
85                  .append("classRealm: ");
86  
87          if (realm != null) {
88              sb.append(realm.getId()).append(LS);
89              ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
90              PrintStream ps = new PrintStream(os);
91              realm.display(ps);
92              sb.append(os.toString());
93          } else {
94              sb.append("none specified");
95          }
96  
97          return sb.toString();
98      }
99  }