Skip to content
This repository has been archived by the owner on Apr 23, 2024. It is now read-only.

Commit

Permalink
Added initial Spring Framework extension (enables Logback easily for …
Browse files Browse the repository at this point in the history
…Spring applications)
  • Loading branch information
lhazlewood committed Mar 10, 2012
1 parent bf0d6be commit cbacc70
Show file tree
Hide file tree
Showing 10 changed files with 985 additions and 0 deletions.
36 changes: 36 additions & 0 deletions pom.xml
Expand Up @@ -72,6 +72,7 @@
<modules>
<module>json</module>
<module>jackson</module>
<module>spring</module>
</modules>

<properties>
Expand All @@ -83,6 +84,7 @@
<!-- Compile Dependencies: -->
<logback.version>1.0.1</logback.version>
<jackson.version>1.9.5</jackson.version>
<spring.version>3.1.1.RELEASE</spring.version>

<!-- Test Dependencies: -->
<easymock.version>3.1</easymock.version>
Expand Down Expand Up @@ -153,13 +155,47 @@
</dependency>

<!-- 3rd party dependencies: -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>${jackson.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
<scope>provided</scope>
<optional>true</optional>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
<scope>provided</scope>
<optional>true</optional>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

</dependencies>

</dependencyManagement>
Expand Down
48 changes: 48 additions & 0 deletions spring/pom.xml
@@ -0,0 +1,48 @@
<!--
~ Copyright 2012 Ceki Gulcu, Les Hazlewood, et. al.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<parent>
<groupId>ch.qos.logback.extensions</groupId>
<artifactId>logback-ext-parent</artifactId>
<version>0.1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>logback-ext-spring</artifactId>
<name>Logback Extensions :: Spring</name>
<packaging>jar</packaging>

<dependencies>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</dependency>

</dependencies>

</project>
@@ -0,0 +1,106 @@
/*
* Copyright 2012 Ceki Gulcu, Les Hazlewood, et. al.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.qos.logback.ext.spring;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

/**
* A special bean which may be defined in the Spring {@code ApplicationContext} to make the context available statically
* to objects which, for whatever reason, cannot be wired up in Spring (for example, logging appenders which must be
* defined in XML or properties files used to initialize the logging system).
* <p/>
* To use this holder, <i>exactly one</i> bean should be declared as follows:
* <pre>
* &lt;bean class="ch.qos.logback.ext.spring.ApplicationContextHolder"/&gt;
* </pre>
* Note that no ID is necessary because this holder should always be used via its static accessors, rather than being
* injected. Any Spring bean which wishes to access the {@code ApplicationContext} should not rely on this holder; it
* should simply implement {@code ApplicationContextAware}.
* <p/>
* <b>WARNING: This object uses static memory to retain the ApplicationContext.</b> This means this bean (and the
* related configuration strategy) is only usable when no other Logback-enabled Spring applications exist in the same
* JVM.
*
* @author Bryan Turner
* @author Les Hazlewood
* @since 0.1
*/
public class ApplicationContextHolder implements ApplicationContextAware, ApplicationListener<ContextRefreshedEvent> {

private static ApplicationContext applicationContext;
private static volatile boolean refreshed;

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
refreshed = true;
}

/**
* Ensures that the {@code ApplicationContext} has been set <i>and</i> that it has been refreshed. The refresh
* event is sent when the context has completely finished starting up, meaning all beans have been created and
* initialized successfully.
* <p/>
* This method has a loosely defined relationship with {@link #getApplicationContext()}. When this method returns
* {@code true}, calling {@link #getApplicationContext()} is guaranteed to return a non-{@code null} context which
* has been completely initialized. When this method returns {@code false}, {@link #getApplicationContext()} may
* return {@code null}, or it may return a non-{@code null} context which is not yet completely initialized.
*
* @return {@code true} if the context has been set and refreshed; otherwise, {@code false}
*/
public static boolean hasApplicationContext() {
return (refreshed && applicationContext != null);
}

/**
* Retrieves the {@code ApplicationContext} set when Spring created and initialized the holder bean. If the
* holder has not been created (see the class documentation for details on how to wire up the holder), or if
* the holder has not been initialized, this accessor may return {@code null}.
* <p/>
* As a general usage pattern, callers should wrap this method in a check for {@link #hasApplicationContext()}.
* That ensures both that the context is set and also that it has fully initialized. Using a context which has
* not been fully initialized can result in unexpected initialization behaviors for some beans. The most common
* example of this behavior is receiving unproxied references to some beans, such as beans which were supposed
* to have transactional semantics applied by AOP. By waiting for the context refresh event, the likelihood of
* encountering such behavior is greatly reduced.
*
* @return the set context, or {@code null} if the holder bean has not been initialized
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}

@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
applicationContext = context;
}

/**
* Returns a flag indicating whether the {@code ApplicationContext} has been refreshed. Theoretically, it is
* possible for this method to return {@code true} when {@link #hasApplicationContext()} returns {@code false},
* but in practice that is very unlikely since the bean for the holder should have been created and initialized
* before the refresh event was raised.
*
* @return {@code true} if the context refresh event has been received; otherwise, {@code false}
*/
public static boolean isRefreshed() {
return refreshed;
}
}

0 comments on commit cbacc70

Please sign in to comment.