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          try
47          {
48              return (Boolean) logger.getClass()
49                      .getMethod( "isDebugEnabled" )
50                      .invoke( logger );
51          }
52          catch ( Exception e )
53          {
54              throw new IllegalStateException( e.getLocalizedMessage(), e );
55          }
56      }
57  
58      @Override
59      public void debug( String message )
60      {
61          try
62          {
63              logger.getClass()
64                      .getMethod( "debug", String.class )
65                      .invoke( logger, message );
66          }
67          catch ( Exception e )
68          {
69              throw new IllegalStateException( e.getLocalizedMessage(), e );
70          }
71      }
72  
73      @Override
74      public boolean isInfoEnabled()
75      {
76          try
77          {
78              return (Boolean) logger.getClass()
79                      .getMethod( "isInfoEnabled" )
80                      .invoke( logger );
81          }
82          catch ( Exception e )
83          {
84              throw new IllegalStateException( e.getLocalizedMessage(), e );
85          }
86      }
87  
88      @Override
89      public void info( String message )
90      {
91          try
92          {
93              logger.getClass()
94                      .getMethod( "info", String.class )
95                      .invoke( logger, message );
96          }
97          catch ( Exception e )
98          {
99              throw new IllegalStateException( e.getLocalizedMessage(), e );
100         }
101     }
102 
103     @Override
104     public boolean isWarnEnabled()
105     {
106         try
107         {
108             return (Boolean) logger.getClass()
109                     .getMethod( "isWarnEnabled" )
110                     .invoke( logger );
111         }
112         catch ( Exception e )
113         {
114             throw new IllegalStateException( e.getLocalizedMessage(), e );
115         }
116     }
117 
118     @Override
119     public void warning( String message )
120     {
121         try
122         {
123             logger.getClass()
124                     .getMethod( "warning", String.class )
125                     .invoke( logger, message );
126         }
127         catch ( Exception e )
128         {
129             throw new IllegalStateException( e.getLocalizedMessage(), e );
130         }
131     }
132 
133     @Override
134     public boolean isErrorEnabled()
135     {
136         try
137         {
138             return (Boolean) logger.getClass()
139                     .getMethod( "isErrorEnabled" )
140                     .invoke( logger );
141         }
142         catch ( Exception e )
143         {
144             throw new IllegalStateException( e.getLocalizedMessage(), e );
145         }
146     }
147 
148     @Override
149     public void error( String message )
150     {
151         try
152         {
153             logger.getClass()
154                     .getMethod( "error", String.class )
155                     .invoke( logger, message );
156         }
157         catch ( Exception e )
158         {
159             throw new IllegalStateException( e.getLocalizedMessage(), e );
160         }
161     }
162 
163     @Override
164     public void error( String message, Throwable t )
165     {
166         try
167         {
168             logger.getClass()
169                     .getMethod( "error", String.class, Throwable.class )
170                     .invoke( logger, message, t );
171         }
172         catch ( Exception e )
173         {
174             throw new IllegalStateException( e.getLocalizedMessage(), e );
175         }
176     }
177 
178     @Override
179     public void error( Throwable t )
180     {
181         try
182         {
183             logger.getClass()
184                     .getMethod( "error", Throwable.class )
185                     .invoke( logger, t );
186         }
187         catch ( Exception e )
188         {
189             throw new IllegalStateException( e.getLocalizedMessage(), e );
190         }
191     }
192 }