View Javadoc
1   package org.apache.maven.plugin.surefire.log.api;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  /**
23   * Decorator around {@link ConsoleLogger}.
24   * This class is loaded in the isolated ClassLoader and the child logger in the in-plugin ClassLoader.
25   *
26   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
27   * @since 2.20
28   */
29  public final class ConsoleLoggerDecorator
30          implements ConsoleLogger
31  {
32      private final Object logger;
33  
34      public ConsoleLoggerDecorator( Object logger )
35      {
36          if ( logger == null )
37          {
38              throw new NullPointerException( "logger argument is null in " + ConsoleLoggerDecorator.class );
39          }
40          this.logger = logger;
41      }
42  
43      @Override
44      public boolean isDebugEnabled()
45      {
46          return invokeReturnedBoolean( "isDebugEnabled" );
47      }
48  
49      @Override
50      public void debug( String message )
51      {
52          invokeWithMessage( message, "debug" );
53      }
54  
55      @Override
56      public boolean isInfoEnabled()
57      {
58          return invokeReturnedBoolean( "isInfoEnabled" );
59      }
60  
61      @Override
62      public void info( String message )
63      {
64          invokeWithMessage( message, "info" );
65      }
66  
67      @Override
68      public boolean isWarnEnabled()
69      {
70          return invokeReturnedBoolean( "isWarnEnabled" );
71      }
72  
73      @Override
74      public void warning( String message )
75      {
76          invokeWithMessage( message, "warning" );
77      }
78  
79      @Override
80      public boolean isErrorEnabled()
81      {
82          return invokeReturnedBoolean( "isErrorEnabled" );
83      }
84  
85      @Override
86      public void error( String message )
87      {
88          invokeWithMessage( message, "error" );
89      }
90  
91      @Override
92      public void error( String message, Throwable t )
93      {
94          try
95          {
96              logger.getClass()
97                      .getMethod( "error", String.class, Throwable.class )
98                      .invoke( logger, message, t );
99          }
100         catch ( Exception e )
101         {
102             throw new IllegalStateException( e.getLocalizedMessage(), e );
103         }
104     }
105 
106     @Override
107     public void error( Throwable t )
108     {
109         try
110         {
111             logger.getClass()
112                     .getMethod( "error", Throwable.class )
113                     .invoke( logger, t );
114         }
115         catch ( Exception e )
116         {
117             throw new IllegalStateException( e.getLocalizedMessage(), e );
118         }
119     }
120 
121     private boolean invokeReturnedBoolean( String isErrorEnabled )
122     {
123         try
124         {
125             return (Boolean) logger.getClass()
126                     .getMethod( isErrorEnabled )
127                     .invoke( logger );
128         }
129         catch ( Exception e )
130         {
131             throw new IllegalStateException( e.getLocalizedMessage(), e );
132         }
133     }
134 
135     private void invokeWithMessage( String message, String error )
136     {
137         try
138         {
139             logger.getClass()
140                     .getMethod( error, String.class )
141                     .invoke( logger, message );
142         }
143         catch ( Exception e )
144         {
145             throw new IllegalStateException( e.getLocalizedMessage(), e );
146         }
147     }
148 }