Skip to content

Commit dc2e93f

Browse files
Add native-image support for Session JDBC
Closes gh-2103
1 parent 00d248b commit dc2e93f

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2014-2022 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+
* https://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.session.jdbc.aot.hint;
18+
19+
import org.springframework.aot.hint.MemberCategory;
20+
import org.springframework.aot.hint.RuntimeHints;
21+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
22+
import org.springframework.aot.hint.TypeReference;
23+
24+
/**
25+
* A {@link RuntimeHintsRegistrar} for JDBC Session hints.
26+
*
27+
* @author Marcus Da Coregio
28+
*/
29+
public class SessionJdbcSecurityHints implements RuntimeHintsRegistrar {
30+
31+
@Override
32+
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
33+
hints.reflection().registerType(TypeReference.of("javax.sql.DataSource"),
34+
(hint) -> hint.withMembers(MemberCategory.INVOKE_DECLARED_METHODS));
35+
hints.resources().registerPattern("org/springframework/session/jdbc/*.sql");
36+
}
37+
38+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
org.springframework.aot.hint.RuntimeHintsRegistrar=\
2+
org.springframework.session.jdbc.aot.hint.SessionJdbcSecurityHints
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2014-2022 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+
* https://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.session.jdbc.aot.hint;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.aot.hint.RuntimeHints;
22+
import org.springframework.aot.hint.RuntimeHintsPredicates;
23+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
24+
import org.springframework.aot.hint.TypeReference;
25+
import org.springframework.core.io.support.SpringFactoriesLoader;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
/**
30+
* Tests for {@link SessionJdbcSecurityHints}
31+
*
32+
* @author Marcus Da Coregio
33+
*/
34+
class SessionJdbcSecurityHintsTests {
35+
36+
private final RuntimeHints hints = new RuntimeHints();
37+
38+
private final SessionJdbcSecurityHints sessionJdbcSecurityHints = new SessionJdbcSecurityHints();
39+
40+
@Test
41+
void aotFactoriesContainsRegistrar() {
42+
boolean match = SpringFactoriesLoader.forResourceLocation("META-INF/spring/aot.factories")
43+
.load(RuntimeHintsRegistrar.class).stream()
44+
.anyMatch((registrar) -> registrar instanceof SessionJdbcSecurityHints);
45+
assertThat(match).isTrue();
46+
}
47+
48+
@Test
49+
void jdbcSchemasHasHints() {
50+
this.sessionJdbcSecurityHints.registerHints(this.hints, getClass().getClassLoader());
51+
assertThat(RuntimeHintsPredicates.resource().forResource("org/springframework/session/jdbc/schema.sql"))
52+
.accepts(this.hints);
53+
}
54+
55+
@Test
56+
void dataSourceHasHints() {
57+
this.sessionJdbcSecurityHints.registerHints(this.hints, getClass().getClassLoader());
58+
assertThat(RuntimeHintsPredicates.reflection().onType(TypeReference.of("javax.sql.DataSource")))
59+
.accepts(this.hints);
60+
}
61+
62+
}

0 commit comments

Comments
 (0)