View Javadoc
1   /*
2    * Copyright 2013–2016 Michael Osipov
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.michaelo.tomcat.extras.listeners;
17  
18  import net.sf.michaelo.tomcat.extras.utils.ContextUtils;
19  
20  import org.apache.catalina.Context;
21  import org.apache.catalina.Lifecycle;
22  import org.apache.catalina.LifecycleEvent;
23  import org.apache.catalina.LifecycleListener;
24  import org.apache.catalina.deploy.ContextEnvironment;
25  import org.apache.commons.lang3.StringUtils;
26  import org.apache.juli.logging.Log;
27  import org.apache.juli.logging.LogFactory;
28  
29  /**
30   * A listener which creates an environment entry with the name of the current context to populate
31   * Logback's context name fully dynamic in a webapp's {@code logback.xml} file.
32   * <p>
33   * The default entry name is {@code logback/contextName}.
34   * </p>
35   *
36   * @version $Id: LogbackContextNameListener.java 60 2016-01-11 09:24:43Z michael-o $
37   */
38  public class LogbackContextNameListener implements LifecycleListener {
39  
40  	private static final Log logger = LogFactory.getLog(LogbackContextNameListener.class);
41  	private Context context;
42  
43  	private String name = "logback/contextName";
44  
45  	@Override
46  	public void lifecycleEvent(LifecycleEvent le) {
47  
48  		if (le.getLifecycle() instanceof Context)
49  			context = (Context) le.getLifecycle();
50  		else
51  			return;
52  
53  		String contextName = ContextUtils.getName(context);
54  		if (le.getType().equals(Lifecycle.START_EVENT)) {
55  			// Check whether env entry is already set and remove it as advised
56  			// by Konstantin Kolinko
57  			ContextEnvironment ce = context.getNamingResources().findEnvironment(name);
58  			if (ce != null)
59  				context.getNamingResources().removeEnvironment(name);
60  
61  			ce = new ContextEnvironment();
62  			ce.setName(name);
63  			ce.setOverride(false);
64  			ce.setType("java.lang.String");
65  			ce.setValue(contextName);
66  			if (logger.isDebugEnabled())
67  				logger.debug(String.format(
68  						"Adding Logback context name env entry '%s' to context '%s'", name,
69  						contextName));
70  			context.getNamingResources().addEnvironment(ce);
71  		}
72  
73  	}
74  
75  	public void setName(String name) {
76  		if (StringUtils.isEmpty(name))
77  			throw new IllegalArgumentException("Parameter 'name' cannot be empty");
78  
79  		this.name = name;
80  	}
81  
82  }