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