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      public void debug( String message )
44      {
45          try
46          {
47              logger.getClass()
48                      .getMethod( "debug", String.class )
49                      .invoke( logger, message );
50          }
51          catch ( Exception e )
52          {
53              throw new IllegalStateException( e.getLocalizedMessage(), e );
54          }
55      }
56  
57      public void info( String message )
58      {
59          try
60          {
61              logger.getClass()
62                      .getMethod( "info", String.class )
63                      .invoke( logger, message );
64          }
65          catch ( Exception e )
66          {
67              throw new IllegalStateException( e.getLocalizedMessage(), e );
68          }
69      }
70  
71      public void warning( String message )
72      {
73          try
74          {
75              logger.getClass()
76                      .getMethod( "warning", String.class )
77                      .invoke( logger, message );
78          }
79          catch ( Exception e )
80          {
81              throw new IllegalStateException( e.getLocalizedMessage(), e );
82          }
83      }
84  
85      public void error( String message )
86      {
87          try
88          {
89              logger.getClass()
90                      .getMethod( "error", String.class )
91                      .invoke( logger, message );
92          }
93          catch ( Exception e )
94          {
95              throw new IllegalStateException( e.getLocalizedMessage(), e );
96          }
97      }
98  
99      public void error( String message, Throwable t )
100     {
101         try
102         {
103             logger.getClass()
104                     .getMethod( "error", String.class, Throwable.class )
105                     .invoke( logger, message, t );
106         }
107         catch ( Exception e )
108         {
109             throw new IllegalStateException( e.getLocalizedMessage(), e );
110         }
111     }
112 
113     public void error( Throwable t )
114     {
115         try
116         {
117             logger.getClass()
118                     .getMethod( "error", Throwable.class )
119                     .invoke( logger, t );
120         }
121         catch ( Exception e )
122         {
123             throw new IllegalStateException( e.getLocalizedMessage(), e );
124         }
125     }
126 }