View Javadoc

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