View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.osgi.boot.logback.internal;
20  
21  import java.io.File;
22  import java.util.Map;
23  
24  import org.slf4j.LoggerFactory;
25  
26  import ch.qos.logback.classic.LoggerContext;
27  import ch.qos.logback.classic.joran.JoranConfigurator;
28  import ch.qos.logback.core.joran.JoranConfiguratorBase;
29  import ch.qos.logback.core.joran.spi.JoranException;
30  import ch.qos.logback.core.util.StatusPrinter;
31  
32  /**
33   * Setup logback eventually located in the config file inside jettyhome/resources
34   * All logback related code is done in this separate class for better debug
35   * and isolation when it does not load.
36   */
37  public class LogbackInitializer {
38  
39      /**
40       * @return true when we are currently being run by the pde in development mode.
41       */
42      private static boolean isPDEDevelopment()
43      {
44          String eclipseCommands = System.getProperty("eclipse.commands");
45          // detect if we are being run from the pde: ie during development.
46          return eclipseCommands != null && eclipseCommands.indexOf("-dev") != -1
47                  && (eclipseCommands.indexOf("-dev\n") != -1
48                          || eclipseCommands.indexOf("-dev\r") != -1
49                          || eclipseCommands.indexOf("-dev ") != -1);
50      }
51  
52      
53      /**
54       * Follow the configuration for logback.
55       * unless the system propery was set in which case it
56       * was assume it was already setup.
57       */
58      public static void processFilesInResourcesFolder(File jettyHome, Map<String,File> files)
59      {
60      	String logbackConf = System.getProperty("logback.configurationFile");
61      	if (logbackConf != null)
62      	{
63      		File confFile = new File(logbackConf);
64      		if (confFile.exists())
65      		{
66      			//assume logback was configured by this one?
67      			return;
68      		}
69      	}
70      	
71      	File logConf = isPDEDevelopment() ? files.get("logback-dev.xml") : null;
72  	    if (logConf == null)
73  	    {
74  	        logConf = files.get("logback-test.xml");
75  	    }
76  	    if (logConf == null)
77  	    {
78  	        logConf = files.get("logback.xml");
79  	    }
80  	    if (logConf == null)
81  	    {
82  	        return;
83  	    }
84  	 // assume SLF4J is bound to logback in the current environment
85  	    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
86  	    
87  	    try
88  	    {
89  	      JoranConfiguratorBase configurator = new JoranConfigurator();
90  	      configurator.setContext(lc);
91  	      lc.reset(); 
92  	      configurator.doConfigure(logConf.getAbsoluteFile().getAbsolutePath());
93  	    }
94  	    catch (JoranException je)
95  	    {
96  	       je.printStackTrace();
97  	    }
98  	    StatusPrinter.printIfErrorsOccured(lc);
99  	    
100     }
101 
102 }