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   * @version $Id$
30   */
31  public class ComponentLookupException
32      extends Exception
33  {
34      private String LS = System.getProperty( "line.separator" );
35  
36      private String role;
37  
38      private String roleHint;
39  
40      private ClassRealm realm;
41  
42      public ComponentLookupException( String message, String role, String roleHint )
43      {
44          super( message );
45  
46          this.role = role;
47  
48          this.roleHint = roleHint;
49      }
50  
51      public ComponentLookupException( String message, String role, String roleHint, Throwable cause )
52      {
53          super( message, cause );
54  
55          this.role = role;
56  
57          this.roleHint = roleHint;
58      }
59  
60      public ComponentLookupException( String message, String role, String roleHint, ClassRealm realm )
61      {
62          super( message );
63  
64          this.role = role;
65  
66          this.roleHint = roleHint;
67  
68          this.realm = realm;
69      }
70  
71      public ComponentLookupException( String message, String role, String roleHint, ClassRealm realm, Throwable cause )
72      {
73          super( message, cause );
74  
75          this.role = role;
76  
77          this.roleHint = roleHint;
78  
79          this.realm = realm;
80      }
81  
82      public String getMessage()
83      {
84          StringBuilder sb = new StringBuilder()
85              .append( super.getMessage() ).append( LS )
86              .append( "      role: ").append( role ).append( LS )
87              .append( "  roleHint: ").append( roleHint ).append( LS )
88              .append("classRealm: ");
89  
90          if ( realm != null )
91          {
92              sb.append( realm.getId() ).append( LS );
93              ByteArrayOutputStream os = new ByteArrayOutputStream( 1024 );
94              PrintStream ps = new PrintStream( os );
95              realm.display( ps );
96              sb.append( os.toString() );
97          }
98          else
99          {
100             sb.append( "none specified" );
101         }
102 
103         return sb.toString();
104     }
105 }