Skip to content

Commit dd521b7

Browse files
committed
Merge pull request #41 from simonbasle/configuration
basic configuration (xml parsers and @configuration)
2 parents c2efe5d + 9b824c8 commit dd521b7

File tree

10 files changed

+978
-0
lines changed

10 files changed

+978
-0
lines changed
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
/*
2+
* Copyright 2012-2015 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+
17+
package org.springframework.data.couchbase.config;
18+
19+
import java.util.HashSet;
20+
import java.util.List;
21+
import java.util.Set;
22+
23+
import com.couchbase.client.java.Bucket;
24+
import com.couchbase.client.java.Cluster;
25+
import com.couchbase.client.java.CouchbaseCluster;
26+
import com.couchbase.client.java.env.CouchbaseEnvironment;
27+
import com.couchbase.client.java.env.DefaultCouchbaseEnvironment;
28+
29+
import org.springframework.beans.factory.config.BeanDefinition;
30+
import org.springframework.context.annotation.Bean;
31+
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
32+
import org.springframework.context.annotation.Conditional;
33+
import org.springframework.context.annotation.Configuration;
34+
import org.springframework.context.annotation.ConfigurationCondition;
35+
import org.springframework.core.type.filter.AnnotationTypeFilter;
36+
import org.springframework.data.annotation.Persistent;
37+
import org.springframework.data.couchbase.core.CouchbaseTemplate;
38+
import org.springframework.data.couchbase.core.mapping.Document;
39+
import org.springframework.data.mapping.model.CamelCaseAbbreviatingFieldNamingStrategy;
40+
import org.springframework.data.mapping.model.FieldNamingStrategy;
41+
import org.springframework.data.mapping.model.PropertyNameFieldNamingStrategy;
42+
import org.springframework.util.ClassUtils;
43+
import org.springframework.util.StringUtils;
44+
45+
/**
46+
* Base class for Spring Data Couchbase configuration using JavaConfig.
47+
*
48+
* @author Michael Nitschinger
49+
* @author Simon Baslé
50+
*/
51+
@Configuration
52+
public abstract class AbstractCouchbaseConfiguration {
53+
54+
/**
55+
* The list of hostnames (or IP addresses) to bootstrap from.
56+
*
57+
* @return the list of bootstrap hosts.
58+
*/
59+
protected abstract List<String> getBootstrapHosts();
60+
61+
/**
62+
* The name of the bucket to connect to.
63+
*
64+
* @return the name of the bucket.
65+
*/
66+
protected abstract String getBucketName();
67+
68+
/**
69+
* The password of the bucket (can be an empty string).
70+
*
71+
* @return the password of the bucket.
72+
*/
73+
protected abstract String getBucketPassword();
74+
75+
/**
76+
* Is the {@link #getEnvironment()} to be destroyed by Spring?
77+
*
78+
* @return true if Spring should destroy the environment with the context, false otherwise.
79+
*/
80+
protected boolean isEnvironmentManagedBySpring() {
81+
return true;
82+
}
83+
84+
/**
85+
* Override this method if you want a customized {@link CouchbaseEnvironment}.
86+
* This environment will be managed by Spring, which will call its shutdown()
87+
* method upon bean destruction, unless you override {@link #isEnvironmentManagedBySpring()}
88+
* as well to return false.
89+
*
90+
* @return a customized environment, defaults to a {@link DefaultCouchbaseEnvironment}.
91+
*/
92+
protected CouchbaseEnvironment getEnvironment() {
93+
return DefaultCouchbaseEnvironment.create();
94+
}
95+
96+
@Bean(destroyMethod = "shutdown", name = BeanNames.COUCHBASE_ENV)
97+
public CouchbaseEnvironment couchbaseEnvironment() {
98+
CouchbaseEnvironment env = getEnvironment();
99+
if (isEnvironmentManagedBySpring()) {
100+
return env;
101+
}
102+
return new CouchbaseEnvironmentNoShutdownProxy(env);
103+
}
104+
105+
/**
106+
* Returns the {@link Cluster} instance to connect to.
107+
*
108+
* @throws Exception on Bean construction failure.
109+
*/
110+
@Bean(destroyMethod = "disconnect", name = BeanNames.COUCHBASE_CLUSTER)
111+
public Cluster couchbaseCluster() throws Exception {
112+
return CouchbaseCluster.create(couchbaseEnvironment(), getBootstrapHosts());
113+
}
114+
115+
/**
116+
* Return the {@link Bucket} instance to connect to.
117+
*
118+
* @throws Exception on Bean construction failure.
119+
*/
120+
@Bean(destroyMethod = "close", name = BeanNames.COUCHBASE_BUCKET)
121+
public Bucket couchbaseClient() throws Exception {
122+
//@Bean method can use another @Bean method in the same @Configuration by directly invoking it
123+
return couchbaseCluster().openBucket(getBucketName(), getBucketPassword());
124+
}
125+
126+
/**
127+
* Creates a {@link CouchbaseTemplate}.
128+
*
129+
* @throws Exception on Bean construction failure.
130+
*/
131+
@Bean(name = BeanNames.COUCHBASE_TEMPLATE)
132+
public CouchbaseTemplate couchbaseTemplate() throws Exception {
133+
//TODO use mappingCouchbaseConverter and translationService when implemented
134+
return new CouchbaseTemplate(couchbaseClient());
135+
}
136+
137+
//TODO create beans for mappingCouchbaseConverter, translationService, couchbaseMappingContext when implemented
138+
//TODO for mappingCouchbaseConverter, allow registering of customConversions
139+
140+
/**
141+
* Scans the mapping base package for classes annotated with {@link Document}.
142+
*
143+
* @throws ClassNotFoundException if initial entity sets could not be loaded.
144+
*/
145+
protected Set<Class<?>> getInitialEntitySet() throws ClassNotFoundException {
146+
String basePackage = getMappingBasePackage();
147+
Set<Class<?>> initialEntitySet = new HashSet<Class<?>>();
148+
149+
if (StringUtils.hasText(basePackage)) {
150+
ClassPathScanningCandidateComponentProvider componentProvider = new ClassPathScanningCandidateComponentProvider(false);
151+
componentProvider.addIncludeFilter(new AnnotationTypeFilter(Document.class));
152+
componentProvider.addIncludeFilter(new AnnotationTypeFilter(Persistent.class));
153+
for (BeanDefinition candidate : componentProvider.findCandidateComponents(basePackage)) {
154+
initialEntitySet.add(ClassUtils.forName(candidate.getBeanClassName(), AbstractCouchbaseConfiguration.class.getClassLoader()));
155+
}
156+
}
157+
158+
return initialEntitySet;
159+
}
160+
161+
/**
162+
* Return the base package to scan for mapped {@link Document}s. Will return the package name of the configuration
163+
* class (the concrete class, not this one here) by default.
164+
* <p/>
165+
* <p>So if you have a {@code com.acme.AppConfig} extending {@link AbstractCouchbaseConfiguration} the base package
166+
* will be considered {@code com.acme} unless the method is overridden to implement alternate behavior.</p>
167+
*
168+
* @return the base package to scan for mapped {@link Document} classes or {@literal null} to not enable scanning for
169+
* entities.
170+
*/
171+
protected String getMappingBasePackage() {
172+
return getClass().getPackage().getName();
173+
}
174+
175+
/**
176+
* Set to true if field names should be abbreviated with the {@link CamelCaseAbbreviatingFieldNamingStrategy}.
177+
*
178+
* @return true if field names should be abbreviated, default is false.
179+
*/
180+
protected boolean abbreviateFieldNames() {
181+
return false;
182+
}
183+
184+
/**
185+
* Configures a {@link FieldNamingStrategy} on the CouchbaseMappingContext instance created.
186+
*
187+
* @return the naming strategy.
188+
*/
189+
protected FieldNamingStrategy fieldNamingStrategy() {
190+
//TODO implement a CouchbaseMappingContext, use this method (update link in javadoc)
191+
return abbreviateFieldNames() ? new CamelCaseAbbreviatingFieldNamingStrategy() : PropertyNameFieldNamingStrategy.INSTANCE;
192+
}
193+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2012-2015 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+
17+
package org.springframework.data.couchbase.config;
18+
19+
/**
20+
* Contains default bean names that will be used when no "id" is supplied to the beans.
21+
*
22+
* @author Michael Nitschinger
23+
* @author Simon Baslé
24+
*/
25+
public class BeanNames {
26+
27+
/**
28+
* Refers to the <couchbase:env /> bean.
29+
*/
30+
static final String COUCHBASE_ENV = "couchbaseEnv";
31+
/**
32+
* Refers to the "<couchbase:cluster />" bean.
33+
*/
34+
static final String COUCHBASE_CLUSTER = "couchbaseCluster";
35+
36+
/**
37+
* Refers to the "<couchbase:bucket />" bean.
38+
*/
39+
static final String COUCHBASE_BUCKET = "couchbaseBucket";
40+
41+
/**
42+
* Refers to the "<couchbase:template />" bean.
43+
*/
44+
static final String COUCHBASE_TEMPLATE = "couchbaseTemplate";
45+
46+
/**
47+
* Refers to the "<couchbase:translation-service />" bean
48+
*/
49+
static final String TRANSLATION_SERVICE = "couchbaseTranslationService";
50+
51+
52+
}

0 commit comments

Comments
 (0)