Skip to content

Commit

Permalink
DATACOUCH-14 - Enable SLF4J as default logging.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
daschl committed Jan 23, 2014
1 parent 46b33e9 commit 7b6a3a9
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 22 deletions.
Expand Up @@ -23,6 +23,7 @@
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;
Expand All @@ -32,8 +33,9 @@
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.
Expand All @@ -43,13 +45,62 @@
@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}.
Expand Down Expand Up @@ -130,4 +181,19 @@ protected String getMappingBasePackage() {
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;
}

}
Expand Up @@ -30,6 +30,7 @@
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
* Parser for "<couchbase:couchbase />" bean definitions.
Expand Down Expand Up @@ -68,6 +69,14 @@ protected void doParse(final Element element, final BeanDefinitionBuilder bean)
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);
}

/**
Expand Down
Expand Up @@ -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}.
*/
Expand Down
Expand Up @@ -42,7 +42,7 @@ public void afterPropertiesSet() throws Exception {

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 {
Expand Down
Expand Up @@ -26,6 +26,7 @@

import java.net.URI;
import java.util.Arrays;
import java.util.List;

/**
* @author Michael Nitschinger
Expand All @@ -37,40 +38,40 @@ public class TestApplicationConfig extends AbstractCouchbaseConfiguration {
private Environment env;

@Bean
public String couchbaseHost() {
return env.getProperty("couchbase.host", "http://127.0.0.1:8091/pools");
public String couchbaseAdminUser() {
return env.getProperty("couchbase.adminUser", "Administrator");
}

@Bean
public String couchbaseBucket() {
return env.getProperty("couchbase.bucket", "default");
public String couchbaseAdminPassword() {
return env.getProperty("couchbase.adminUser", "password");
}

@Bean
public String couchbasePassword() {
return env.getProperty("couchbase.password", "");
@Override
protected List<String> bootstrapHosts() {
return Arrays.asList(env.getProperty("couchbase.host", "127.0.0.1"));
}

@Bean
public String couchbaseAdminUser() {
return env.getProperty("couchbase.adminUser", "Administrator");
@Override
protected String getBucketName() {
return env.getProperty("couchbase.bucket", "default");
}

@Bean
public String couchbaseAdminPassword() {
return env.getProperty("couchbase.adminUser", "password");
@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();
}


Expand Down
Expand Up @@ -27,6 +27,8 @@
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;

Expand Down Expand Up @@ -55,6 +57,21 @@ public void usesConfigClassPackageAsBaseMappingPackage() throws Exception {

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 {
Expand Down
Expand Up @@ -295,9 +295,10 @@ public void writesAndReadsListAndNestedList() {
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
Expand Down

0 comments on commit 7b6a3a9

Please sign in to comment.