Skip to content

Commit 133975f

Browse files
committed
DATAMONGO-641 - Fixed potential NullPointerException in MongoLog4jAppender.
Log4j appender now only closes Mongo instance if available.
1 parent 9a372a5 commit 133975f

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

spring-data-mongodb-log4j/src/main/java/org/springframework/data/mongodb/log4j/MongoLog4jAppender.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
/*
2-
* Copyright (c) 2011 by the original author(s).
2+
* Copyright 2011-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package org.springframework.data.mongodb.log4j;
1817

1918
import java.net.UnknownHostException;
@@ -34,7 +33,10 @@
3433
import com.mongodb.WriteConcern;
3534

3635
/**
37-
* @author Jon Brisbin <jbrisbin@vmware.com>
36+
* Log4j appender writing log entries into a MongoDB instance.
37+
*
38+
* @author Jon Brisbin
39+
* @author Oliver Gierke
3840
*/
3941
public class MongoLog4jAppender extends AppenderSkeleton {
4042

@@ -130,8 +132,12 @@ protected void connectToMongo() throws UnknownHostException {
130132
this.db = mongo.getDB(database);
131133
}
132134

133-
@SuppressWarnings({ "unchecked" })
135+
/*
136+
* (non-Javadoc)
137+
* @see org.apache.log4j.AppenderSkeleton#append(org.apache.log4j.spi.LoggingEvent)
138+
*/
134139
@Override
140+
@SuppressWarnings({ "unchecked" })
135141
protected void append(final LoggingEvent event) {
136142
if (null == db) {
137143
try {
@@ -199,10 +205,21 @@ protected void append(final LoggingEvent event) {
199205
db.getCollection(coll).insert(dbo, wc);
200206
}
201207

208+
/*
209+
* (non-Javadoc)
210+
* @see org.apache.log4j.AppenderSkeleton#close()
211+
*/
202212
public void close() {
203-
mongo.close();
213+
214+
if (mongo != null) {
215+
mongo.close();
216+
}
204217
}
205218

219+
/*
220+
* (non-Javadoc)
221+
* @see org.apache.log4j.AppenderSkeleton#requiresLayout()
222+
*/
206223
public boolean requiresLayout() {
207224
return true;
208225
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
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 org.springframework.data.mongodb.log4j;
17+
18+
import org.junit.Test;
19+
20+
/**
21+
* Unit tests for {@link MongoLog4jAppender}.
22+
*
23+
* @author Oliver Gierke
24+
*/
25+
public class MongoLog4jAppenderUnitTests {
26+
27+
/**
28+
* @see DATAMONGO-641
29+
*/
30+
@Test
31+
public void closesWithoutMongoInstancePresent() {
32+
new MongoLog4jAppender().close();
33+
}
34+
}

0 commit comments

Comments
 (0)