Browse Source

DATAMONGO-641 - Fixed potential NullPointerException in MongoLog4jAppender.

Log4j appender now only closes Mongo instance if available.
pull/32/head
Oliver Gierke 13 years ago
parent
commit
133975fb44
  1. 29
      spring-data-mongodb-log4j/src/main/java/org/springframework/data/mongodb/log4j/MongoLog4jAppender.java
  2. 34
      spring-data-mongodb-log4j/src/test/java/org/springframework/data/mongodb/log4j/MongoLog4jAppenderUnitTests.java

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

@ -1,11 +1,11 @@
/* /*
* Copyright (c) 2011 by the original author(s). * Copyright 2011-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.mongodb.log4j; package org.springframework.data.mongodb.log4j;
import java.net.UnknownHostException; import java.net.UnknownHostException;
@ -34,7 +33,10 @@ import com.mongodb.Mongo;
import com.mongodb.WriteConcern; import com.mongodb.WriteConcern;
/** /**
* @author Jon Brisbin <jbrisbin@vmware.com> * Log4j appender writing log entries into a MongoDB instance.
*
* @author Jon Brisbin
* @author Oliver Gierke
*/ */
public class MongoLog4jAppender extends AppenderSkeleton { public class MongoLog4jAppender extends AppenderSkeleton {
@ -130,8 +132,12 @@ public class MongoLog4jAppender extends AppenderSkeleton {
this.db = mongo.getDB(database); this.db = mongo.getDB(database);
} }
@SuppressWarnings({ "unchecked" }) /*
* (non-Javadoc)
* @see org.apache.log4j.AppenderSkeleton#append(org.apache.log4j.spi.LoggingEvent)
*/
@Override @Override
@SuppressWarnings({ "unchecked" })
protected void append(final LoggingEvent event) { protected void append(final LoggingEvent event) {
if (null == db) { if (null == db) {
try { try {
@ -199,10 +205,21 @@ public class MongoLog4jAppender extends AppenderSkeleton {
db.getCollection(coll).insert(dbo, wc); db.getCollection(coll).insert(dbo, wc);
} }
/*
* (non-Javadoc)
* @see org.apache.log4j.AppenderSkeleton#close()
*/
public void close() { public void close() {
mongo.close();
if (mongo != null) {
mongo.close();
}
} }
/*
* (non-Javadoc)
* @see org.apache.log4j.AppenderSkeleton#requiresLayout()
*/
public boolean requiresLayout() { public boolean requiresLayout() {
return true; return true;
} }

34
spring-data-mongodb-log4j/src/test/java/org/springframework/data/mongodb/log4j/MongoLog4jAppenderUnitTests.java

@ -0,0 +1,34 @@
/*
* Copyright 2013 the original author or authors.
*
* 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 org.springframework.data.mongodb.log4j;
import org.junit.Test;
/**
* Unit tests for {@link MongoLog4jAppender}.
*
* @author Oliver Gierke
*/
public class MongoLog4jAppenderUnitTests {
/**
* @see DATAMONGO-641
*/
@Test
public void closesWithoutMongoInstancePresent() {
new MongoLog4jAppender().close();
}
}
Loading…
Cancel
Save