Skip to content

Commit e0ddb5d

Browse files
committed
Support the mechanism for initializing a cache after set all properties
fix gh-816
1 parent 53ce5fa commit e0ddb5d

File tree

3 files changed

+142
-1
lines changed

3 files changed

+142
-1
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright 2009-2016 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.apache.ibatis.builder;
17+
18+
/**
19+
* Interface that indicate to provide a initialization method.
20+
*
21+
* @since 3.4.2
22+
* @author Kazuki Shimizu
23+
*/
24+
public interface InitializingObject {
25+
26+
/**
27+
* Initialize a instance.
28+
* <p>
29+
* This method will be invoked after it has set all properties.
30+
* </p>
31+
* @throws Exception in the event of misconfiguration (such as failure to set an essential property) or if initialization fails
32+
*/
33+
void initialize() throws Exception;
34+
35+
}

src/main/java/org/apache/ibatis/mapping/CacheBuilder.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2015 the original author or authors.
2+
* Copyright 2009-2016 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.
@@ -23,6 +23,7 @@
2323

2424
import org.apache.ibatis.cache.Cache;
2525
import org.apache.ibatis.cache.CacheException;
26+
import org.apache.ibatis.builder.InitializingObject;
2627
import org.apache.ibatis.cache.decorators.BlockingCache;
2728
import org.apache.ibatis.cache.decorators.LoggingCache;
2829
import org.apache.ibatis.cache.decorators.LruCache;
@@ -175,6 +176,14 @@ private void setCacheProperties(Cache cache) {
175176
}
176177
}
177178
}
179+
if (InitializingObject.class.isAssignableFrom(cache.getClass())){
180+
try {
181+
((InitializingObject) cache).initialize();
182+
} catch (Exception e) {
183+
throw new CacheException("Failed cache initialization for '" +
184+
cache.getId() + "' on '" + cache.getClass().getName() + "'", e);
185+
}
186+
}
178187
}
179188

180189
private Cache newBaseCacheInstance(Class<? extends Cache> cacheClass, String id) {
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* Copyright 2009-2016 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.apache.ibatis.mapping;
17+
18+
import org.apache.ibatis.builder.InitializingObject;
19+
import org.apache.ibatis.cache.Cache;
20+
import org.apache.ibatis.cache.CacheException;
21+
import org.apache.ibatis.cache.impl.PerpetualCache;
22+
import org.hamcrest.core.Is;
23+
import org.junit.Assert;
24+
import org.junit.Rule;
25+
import org.junit.Test;
26+
import org.junit.rules.ExpectedException;
27+
28+
import java.lang.reflect.Field;
29+
30+
public class CacheBuilderTest {
31+
32+
@Rule
33+
public ExpectedException expectedException = ExpectedException.none();
34+
35+
@Test
36+
public void testInitializing() throws Exception {
37+
InitializingCache cache = unwrap(new CacheBuilder("test").implementation(InitializingCache.class).build());
38+
39+
Assert.assertThat(cache.initialized, Is.is(true));
40+
}
41+
42+
@Test
43+
public void testInitializingFailure() throws Exception {
44+
expectedException.expect(CacheException.class);
45+
expectedException.expectMessage("Failed cache initialization for 'test' on 'org.apache.ibatis.mapping.CacheBuilderTest$InitializingFailureCache'");
46+
47+
new CacheBuilder("test").implementation(InitializingFailureCache.class).build();
48+
}
49+
50+
@SuppressWarnings("unchecked")
51+
private <T> T unwrap(Cache cache){
52+
Field field;
53+
try {
54+
field = cache.getClass().getDeclaredField("delegate");
55+
} catch (NoSuchFieldException e) {
56+
throw new IllegalStateException(e);
57+
}
58+
try {
59+
field.setAccessible(true);
60+
return (T)field.get(cache);
61+
} catch (IllegalAccessException e) {
62+
throw new IllegalStateException(e);
63+
} finally {
64+
field.setAccessible(false);
65+
}
66+
}
67+
68+
69+
private static class InitializingCache extends PerpetualCache implements InitializingObject {
70+
71+
private boolean initialized;
72+
73+
public InitializingCache(String id) {
74+
super(id);
75+
}
76+
77+
@Override
78+
public void initialize() {
79+
this.initialized = true;
80+
}
81+
82+
}
83+
84+
private static class InitializingFailureCache extends PerpetualCache implements InitializingObject {
85+
86+
public InitializingFailureCache(String id) {
87+
super(id);
88+
}
89+
90+
@Override
91+
public void initialize() throws Exception {
92+
throw new IllegalStateException("error");
93+
}
94+
95+
}
96+
97+
}

0 commit comments

Comments
 (0)