DATACOUCH-14 - Enable SLF4J as default logging.
If the whole system is constructed through the AbstractCouchbaseConfiguration or the custom <couchbase> xml config, the logger is specified automatically. Please note that this also changes the java config a little bit, but it should also make it easier to construct it.
This commit is contained in:
@@ -23,6 +23,7 @@ import org.springframework.context.annotation.ClassPathScanningCandidateComponen
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.type.filter.AnnotationTypeFilter;
|
||||
import org.springframework.data.annotation.Persistent;
|
||||
import org.springframework.data.couchbase.core.CouchbaseFactoryBean;
|
||||
import org.springframework.data.couchbase.core.CouchbaseTemplate;
|
||||
import org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter;
|
||||
import org.springframework.data.couchbase.core.convert.translation.JacksonTranslationService;
|
||||
@@ -32,8 +33,9 @@ import org.springframework.data.couchbase.core.mapping.Document;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Base class for Spring Data Couchbase configuration using JavaConfig.
|
||||
@@ -43,13 +45,62 @@ import java.util.Set;
|
||||
@Configuration
|
||||
public abstract class AbstractCouchbaseConfiguration {
|
||||
|
||||
/**
|
||||
* The list of hostnames (or IP addresses to bootstrap from).
|
||||
*
|
||||
* @return the list of bootstrap hosts.
|
||||
*/
|
||||
protected abstract List<String> bootstrapHosts();
|
||||
|
||||
/**
|
||||
* The name of the bucket to connect to.
|
||||
*
|
||||
* @return the name of the bucket.
|
||||
*/
|
||||
protected abstract String getBucketName();
|
||||
|
||||
/**
|
||||
* The password of the bucket (can be an empty string).
|
||||
*
|
||||
* @return the password of the bucket.
|
||||
*/
|
||||
protected abstract String getBucketPassword();
|
||||
|
||||
/**
|
||||
* Return the {@link CouchbaseClient} instance to connect to.
|
||||
*
|
||||
* @throws Exception on Bean construction failure.
|
||||
*/
|
||||
@Bean(destroyMethod = "shutdown")
|
||||
public abstract CouchbaseClient couchbaseClient() throws Exception;
|
||||
public CouchbaseClient couchbaseClient() throws Exception {
|
||||
setLoggerProperty(couchbaseLogger());
|
||||
|
||||
return new CouchbaseClient(
|
||||
bootstrapUris(bootstrapHosts()),
|
||||
getBucketName(),
|
||||
getBucketPassword()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Specifies the logger to use (defaults to SLF4J).
|
||||
*
|
||||
* @return the logger property string.
|
||||
*/
|
||||
protected String couchbaseLogger() {
|
||||
return CouchbaseFactoryBean.DEFAULT_LOGGER_PROPERTY;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the logging property before initializing couchbase.
|
||||
*
|
||||
* @param logger
|
||||
*/
|
||||
private void setLoggerProperty(String logger) {
|
||||
Properties systemProperties = System.getProperties();
|
||||
systemProperties.put("net.spy.log.LoggerImpl", logger);
|
||||
System.setProperties(systemProperties);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@link CouchbaseTemplate}.
|
||||
@@ -130,4 +181,19 @@ public abstract class AbstractCouchbaseConfiguration {
|
||||
return getClass().getPackage().getName();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Converts the given list of hostnames into parsable URIs.
|
||||
*
|
||||
* @param hosts the list of hosts to convert.
|
||||
* @return the converted URIs.
|
||||
*/
|
||||
private List<URI> bootstrapUris(List<String> hosts) throws URISyntaxException {
|
||||
List<URI> uris = new ArrayList<URI>();
|
||||
for (String host : hosts) {
|
||||
uris.add(new URI("http://" + host + ":8091/pools"));
|
||||
}
|
||||
return uris;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* Parser for "<couchbase:couchbase />" bean definitions.
|
||||
@@ -68,6 +69,14 @@ public class CouchbaseParser extends AbstractSingleBeanDefinitionParser {
|
||||
bean.addConstructorArgValue(StringUtils.hasText(password) ? password : CouchbaseFactoryBean.DEFAULT_PASSWORD);
|
||||
|
||||
bean.setDestroyMethodName(CouchbaseFactoryBean.DEFAULT_DESTROY_METHOD);
|
||||
|
||||
setLogger();
|
||||
}
|
||||
|
||||
private void setLogger() {
|
||||
Properties systemProperties = System.getProperties();
|
||||
systemProperties.put("net.spy.log.LoggerImpl", CouchbaseFactoryBean.DEFAULT_LOGGER_PROPERTY);
|
||||
System.setProperties(systemProperties);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -64,6 +64,11 @@ public class CouchbaseFactoryBean implements FactoryBean<CouchbaseClient>, Initi
|
||||
*/
|
||||
public static final String DEFAULT_DESTROY_METHOD = "shutdown";
|
||||
|
||||
/**
|
||||
* Use SLF4J as the default logger if not instructed otherwise.
|
||||
*/
|
||||
public static final String DEFAULT_LOGGER_PROPERTY = "net.spy.memcached.compat.log.SLF4JLogger";
|
||||
|
||||
/**
|
||||
* Holds the enclosed {@link CouchbaseClient}.
|
||||
*/
|
||||
|
||||
@@ -42,7 +42,7 @@ public class BucketCreator implements InitializingBean {
|
||||
|
||||
RestTemplate template = new RestTemplate(rf);
|
||||
|
||||
String fullUri = hostUri + "/default/buckets/default";
|
||||
String fullUri = "http://" + hostUri + ":8091/pools/default/buckets/default";
|
||||
|
||||
ResponseEntity<String> entity = null;
|
||||
try {
|
||||
|
||||
@@ -26,6 +26,7 @@ import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Michael Nitschinger
|
||||
@@ -36,21 +37,6 @@ public class TestApplicationConfig extends AbstractCouchbaseConfiguration {
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
@Bean
|
||||
public String couchbaseHost() {
|
||||
return env.getProperty("couchbase.host", "http://127.0.0.1:8091/pools");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public String couchbaseBucket() {
|
||||
return env.getProperty("couchbase.bucket", "default");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public String couchbasePassword() {
|
||||
return env.getProperty("couchbase.password", "");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public String couchbaseAdminUser() {
|
||||
return env.getProperty("couchbase.adminUser", "Administrator");
|
||||
@@ -61,16 +47,31 @@ public class TestApplicationConfig extends AbstractCouchbaseConfiguration {
|
||||
return env.getProperty("couchbase.adminUser", "password");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<String> bootstrapHosts() {
|
||||
return Arrays.asList(env.getProperty("couchbase.host", "127.0.0.1"));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getBucketName() {
|
||||
return env.getProperty("couchbase.bucket", "default");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getBucketPassword() {
|
||||
return env.getProperty("couchbase.password", "");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public BucketCreator bucketCreator() throws Exception {
|
||||
return new BucketCreator(couchbaseHost(), couchbaseAdminUser(), couchbaseAdminPassword());
|
||||
return new BucketCreator(bootstrapHosts().get(0), couchbaseAdminUser(), couchbaseAdminPassword());
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Override
|
||||
@DependsOn("bucketCreator")
|
||||
public CouchbaseClient couchbaseClient() throws Exception {
|
||||
return new CouchbaseClient(Arrays.asList(new URI(couchbaseHost())), couchbaseBucket(), couchbasePassword());
|
||||
return super.couchbaseClient();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -27,6 +27,8 @@ import org.springframework.data.couchbase.core.mapping.Document;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@@ -55,6 +57,21 @@ public class AbstractCouchbaseConfigurationTests {
|
||||
|
||||
class SampleCouchbaseConfiguration extends AbstractCouchbaseConfiguration {
|
||||
|
||||
@Override
|
||||
protected List<String> bootstrapHosts() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getBucketName() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getBucketPassword() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Override
|
||||
public CouchbaseClient couchbaseClient() throws Exception {
|
||||
|
||||
@@ -295,9 +295,10 @@ public class MappingCouchbaseConverterTests {
|
||||
source.put("attr2", cattr2);
|
||||
|
||||
ListEntity readConverted = converter.read(ListEntity.class, source);
|
||||
System.out.println(readConverted.attr0);
|
||||
System.out.println(readConverted.attr1);
|
||||
System.out.println(readConverted.attr2);
|
||||
assertEquals(2, readConverted.attr0.size());
|
||||
assertEquals(0, readConverted.attr1.size());
|
||||
assertEquals(1, readConverted.attr2.size());
|
||||
assertEquals(2, readConverted.attr2.get(0).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user