View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.gaffer;
15  
16  import ch.qos.logback.classic.ClassicConstants;
17  import ch.qos.logback.classic.LoggerContext;
18  import ch.qos.logback.core.status.ErrorStatus;
19  import ch.qos.logback.core.status.StatusManager;
20  
21  import java.io.File;
22  import java.lang.reflect.Constructor;
23  import java.lang.reflect.InvocationTargetException;
24  import java.net.URL;
25  
26  /**
27   * @author Ceki Gücü
28   */
29  public class GafferUtil {
30  
31    private static String ERROR_MSG = "Failed to instantiate " + ClassicConstants.GAFFER_CONFIGURATOR_FQCN;
32  
33    public static void runGafferConfiguratorOn(LoggerContext loggerContext, Object origin, File configFile) {
34      GafferConfigurator gafferConfigurator = GafferUtil.newGafferConfiguratorInstance(loggerContext, origin);
35      if (gafferConfigurator != null) {
36        gafferConfigurator.run(configFile);
37      }
38    }
39  
40    public static void runGafferConfiguratorOn(LoggerContext loggerContext, Object origin, URL configFile) {
41      GafferConfigurator gafferConfigurator = GafferUtil.newGafferConfiguratorInstance(loggerContext, origin);
42      if (gafferConfigurator != null) {
43        gafferConfigurator.run(configFile);
44      }
45    }
46  
47    private static GafferConfigurator newGafferConfiguratorInstance(LoggerContext loggerContext, Object origin) {
48  
49      try {
50        Class gcClass = Class.forName(ClassicConstants.GAFFER_CONFIGURATOR_FQCN);
51        Constructor c = gcClass.getConstructor(LoggerContext.class);
52        return (GafferConfigurator) c.newInstance(loggerContext);
53      } catch (ClassNotFoundException e) {
54        addError(loggerContext, origin, ERROR_MSG, e);
55      } catch (NoSuchMethodException e) {
56        addError(loggerContext, origin, ERROR_MSG, e);
57      } catch (InvocationTargetException e) {
58        addError(loggerContext, origin, ERROR_MSG, e);
59      } catch (InstantiationException e) {
60        addError(loggerContext, origin, ERROR_MSG, e);
61      } catch (IllegalAccessException e) {
62        addError(loggerContext, origin, ERROR_MSG, e);
63      }
64      return null;
65    }
66  
67    private static void addError(LoggerContext context, Object origin, String msg) {
68      addError(context, origin, msg, null);
69    }
70  
71    private static void addError(LoggerContext context, Object origin, String msg, Throwable t) {
72      StatusManager sm = context.getStatusManager();
73      if (sm == null) {
74        return;
75      }
76      sm.add(new ErrorStatus(msg, origin, t));
77    }
78  
79  
80  }