Skip to content

Commit 44a5c75

Browse files
authored
AOT/Native support.
Original Pull Request spring-projects#2423 Closes spring-projects#2419
1 parent 28489ff commit 44a5c75

File tree

9 files changed

+213
-1
lines changed

9 files changed

+213
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2023 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+
package org.springframework.data.elasticsearch.aot;
17+
18+
import java.util.function.Predicate;
19+
20+
import org.springframework.data.util.ReactiveWrappers;
21+
22+
/**
23+
* @author Peter-Josef Meisch
24+
* @since 5.1
25+
*/
26+
public class ElasticsearchAotPredicates {
27+
28+
public static final Predicate<ReactiveWrappers.ReactiveLibrary> IS_REACTIVE_LIBARARY_AVAILABLE = (
29+
lib) -> ReactiveWrappers.isAvailable(lib);
30+
31+
public static boolean isReactorPresent() {
32+
return IS_REACTIVE_LIBARARY_AVAILABLE.test(ReactiveWrappers.ReactiveLibrary.PROJECT_REACTOR);
33+
}
34+
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2023 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+
package org.springframework.data.elasticsearch.aot;
17+
18+
import static org.springframework.data.elasticsearch.aot.ElasticsearchAotPredicates.*;
19+
20+
import java.util.Arrays;
21+
22+
import org.springframework.aot.hint.MemberCategory;
23+
import org.springframework.aot.hint.RuntimeHints;
24+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
25+
import org.springframework.aot.hint.TypeReference;
26+
import org.springframework.data.elasticsearch.client.elc.EntityAsMap;
27+
import org.springframework.data.elasticsearch.core.event.AfterConvertCallback;
28+
import org.springframework.data.elasticsearch.core.event.AfterLoadCallback;
29+
import org.springframework.data.elasticsearch.core.event.AfterSaveCallback;
30+
import org.springframework.data.elasticsearch.core.event.AuditingEntityCallback;
31+
import org.springframework.data.elasticsearch.core.event.BeforeConvertCallback;
32+
import org.springframework.data.elasticsearch.core.event.ReactiveAfterConvertCallback;
33+
import org.springframework.data.elasticsearch.core.event.ReactiveAfterLoadCallback;
34+
import org.springframework.data.elasticsearch.core.event.ReactiveAfterSaveCallback;
35+
import org.springframework.data.elasticsearch.core.event.ReactiveAuditingEntityCallback;
36+
import org.springframework.data.elasticsearch.core.event.ReactiveBeforeConvertCallback;
37+
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
38+
import org.springframework.lang.Nullable;
39+
40+
/**
41+
* @author Peter-Josef Meisch
42+
* @since 5.1
43+
*/
44+
public class ElasticsearchRuntimeHints implements RuntimeHintsRegistrar {
45+
46+
@Override
47+
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
48+
hints.reflection().registerTypes( //
49+
Arrays.asList( //
50+
TypeReference.of(AfterConvertCallback.class), //
51+
TypeReference.of(AfterLoadCallback.class), //
52+
TypeReference.of(AfterSaveCallback.class), //
53+
TypeReference.of(BeforeConvertCallback.class), //
54+
TypeReference.of(EntityAsMap.class) //
55+
), //
56+
builder -> builder.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
57+
MemberCategory.INVOKE_PUBLIC_METHODS));
58+
59+
if (isReactorPresent()) {
60+
hints.reflection().registerTypes( //
61+
Arrays.asList( //
62+
TypeReference.of(ReactiveAfterConvertCallback.class), //
63+
TypeReference.of(ReactiveAfterLoadCallback.class), //
64+
TypeReference.of(ReactiveAfterSaveCallback.class), //
65+
TypeReference.of(ReactiveBeforeConvertCallback.class) //
66+
), //
67+
builder -> builder.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
68+
MemberCategory.INVOKE_PUBLIC_METHODS));
69+
}
70+
71+
// properties needed to log the different versions
72+
hints.resources().registerPattern("versions.properties");
73+
hints.resources().registerPattern("co/elastic/clients/version.properties");
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@org.springframework.lang.NonNullApi
2+
@org.springframework.lang.NonNullFields
3+
package org.springframework.data.elasticsearch.aot;

src/main/java/org/springframework/data/elasticsearch/client/elc/ElasticsearchTemplate.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ public String getVendor() {
298298

299299
@Override
300300
public String getRuntimeLibraryVersion() {
301-
return Version.VERSION.toString();
301+
return Version.VERSION != null ? Version.VERSION.toString() : "0.0.0.?";
302302
}
303303

304304
// region search operations
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2023 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+
package org.springframework.data.elasticsearch.client.elc.aot;
17+
18+
import java.util.Arrays;
19+
20+
import org.springframework.aot.hint.MemberCategory;
21+
import org.springframework.aot.hint.RuntimeHints;
22+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
23+
import org.springframework.aot.hint.TypeReference;
24+
import org.springframework.lang.Nullable;
25+
26+
/**
27+
* @author Peter-Josef Meisch
28+
* @since 5.1
29+
*/
30+
public class ElasticsearchClientRuntimeHints implements RuntimeHintsRegistrar {
31+
32+
@Override
33+
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
34+
// needed for the http client used by the Elasticsearch client
35+
hints.serialization().registerType(org.apache.http.impl.auth.BasicScheme.class);
36+
hints.serialization().registerType(org.apache.http.impl.auth.RFC2617Scheme.class);
37+
hints.serialization().registerType(java.util.HashMap.class);
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@org.springframework.lang.NonNullApi
2+
@org.springframework.lang.NonNullFields
3+
package org.springframework.data.elasticsearch.client.elc.aot;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2023 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+
package org.springframework.data.elasticsearch.repository.aot;
17+
18+
import static org.springframework.data.elasticsearch.aot.ElasticsearchAotPredicates.*;
19+
20+
import java.util.Arrays;
21+
22+
import org.springframework.aot.hint.MemberCategory;
23+
import org.springframework.aot.hint.RuntimeHints;
24+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
25+
import org.springframework.aot.hint.TypeReference;
26+
import org.springframework.lang.Nullable;
27+
28+
/**
29+
* @author Peter-Josef Meisch
30+
* @since 5.1
31+
*/
32+
public class RepositoryRuntimeHints implements RuntimeHintsRegistrar {
33+
@Override
34+
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
35+
hints.reflection().registerTypes(
36+
Arrays.asList(TypeReference
37+
.of("org.springframework.data.elasticsearch.repository.support.SimpleElasticsearchRepository")),
38+
builder -> builder.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
39+
MemberCategory.INVOKE_PUBLIC_METHODS));
40+
41+
if (isReactorPresent()) {
42+
hints.reflection().registerTypes(
43+
Arrays.asList(TypeReference
44+
.of("org.springframework.data.elasticsearch.repository.support.SimpleReactiveElasticsearchRepository")),
45+
builder -> builder.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
46+
MemberCategory.INVOKE_PUBLIC_METHODS));
47+
48+
}
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@org.springframework.lang.NonNullApi
2+
@org.springframework.lang.NonNullFields
3+
package org.springframework.data.elasticsearch.repository.aot;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
org.springframework.aot.hint.RuntimeHintsRegistrar=\
2+
org.springframework.data.elasticsearch.aot.ElasticsearchRuntimeHints,\
3+
org.springframework.data.elasticsearch.client.elc.aot.ElasticsearchClientRuntimeHints, \
4+
org.springframework.data.elasticsearch.repository.aot.RepositoryRuntimeHints

0 commit comments

Comments
 (0)