|
| 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.mongodb; |
| 17 | + |
| 18 | +import java.lang.reflect.Method; |
| 19 | +import java.net.InetAddress; |
| 20 | +import java.net.InetSocketAddress; |
| 21 | + |
| 22 | +import com.mongodb.ServerAddress; |
| 23 | +import org.apache.commons.logging.Log; |
| 24 | +import org.apache.commons.logging.LogFactory; |
| 25 | +import org.springframework.data.mongodb.core.MongoClientSettingsFactoryBean; |
| 26 | +import org.springframework.data.mongodb.util.MongoClientVersion; |
| 27 | +import org.springframework.util.ClassUtils; |
| 28 | +import org.springframework.util.ReflectionUtils; |
| 29 | + |
| 30 | +import com.mongodb.MongoClientSettings; |
| 31 | +import com.mongodb.MongoClientSettings.Builder; |
| 32 | +import com.mongodb.client.MapReduceIterable; |
| 33 | +import com.mongodb.client.model.IndexOptions; |
| 34 | +import com.mongodb.reactivestreams.client.MapReducePublisher; |
| 35 | + |
| 36 | +/** |
| 37 | + * @author Christoph Strobl |
| 38 | + * @since 2023/12 |
| 39 | + */ |
| 40 | +public class MongoCompatibilityAdapter { |
| 41 | + |
| 42 | + private static final String NO_LONGER_SUPPORTED = "%s is no longer supported on Mongo Client 5+"; |
| 43 | + |
| 44 | + public static ClientSettingsBuilderAdapter clientSettingsBuilderAdapter(MongoClientSettings.Builder builder) { |
| 45 | + return new MongoStreamFactoryFactorySettingsConfigurer(builder)::setStreamFactory; |
| 46 | + } |
| 47 | + |
| 48 | + public static ClientSettingsAdapter clientSettingsAdapter(MongoClientSettings clientSettings) { |
| 49 | + return new ClientSettingsAdapter() { |
| 50 | + @Override |
| 51 | + public <T> T getStreamFactoryFactory() { |
| 52 | + if (MongoClientVersion.is5PlusClient()) { |
| 53 | + return null; |
| 54 | + } |
| 55 | + |
| 56 | + Method getStreamFactoryFactory = ReflectionUtils.findMethod(MongoClientSettings.class, |
| 57 | + "getStreamFactoryFactory"); |
| 58 | + return getStreamFactoryFactory != null |
| 59 | + ? (T) ReflectionUtils.invokeMethod(getStreamFactoryFactory, clientSettings) |
| 60 | + : null; |
| 61 | + } |
| 62 | + }; |
| 63 | + } |
| 64 | + |
| 65 | + public static IndexOptionsAdapter indexOptionsAdapter(IndexOptions options) { |
| 66 | + return new IndexOptionsAdapter() { |
| 67 | + @Override |
| 68 | + public void setBucketSize(Double bucketSize) { |
| 69 | + |
| 70 | + if (MongoClientVersion.is5PlusClient()) { |
| 71 | + throw new UnsupportedOperationException(NO_LONGER_SUPPORTED.formatted("IndexOptions.bucketSize")); |
| 72 | + } |
| 73 | + |
| 74 | + Method setBucketSize = ReflectionUtils.findMethod(IndexOptions.class, "bucketSize", Double.class); |
| 75 | + ReflectionUtils.invokeMethod(setBucketSize, options, bucketSize); |
| 76 | + } |
| 77 | + }; |
| 78 | + } |
| 79 | + |
| 80 | + @SuppressWarnings({ "deprecation" }) |
| 81 | + public static MapReduceIterableAdapter mapReduceIterableAdapter(MapReduceIterable<?> iterable) { |
| 82 | + return sharded -> { |
| 83 | + if (MongoClientVersion.is5PlusClient()) { |
| 84 | + throw new UnsupportedOperationException(NO_LONGER_SUPPORTED.formatted("sharded")); |
| 85 | + } |
| 86 | + |
| 87 | + Method shardedMethod = ReflectionUtils.findMethod(iterable.getClass(), "MapReduceIterable.sharded", |
| 88 | + boolean.class); |
| 89 | + ReflectionUtils.invokeMethod(shardedMethod, iterable, shardedMethod); |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + public static MapReducePublisherAdapter mapReducePublisherAdapter(MapReducePublisher<?> publisher) { |
| 94 | + return sharded -> { |
| 95 | + if (MongoClientVersion.is5PlusClient()) { |
| 96 | + throw new UnsupportedOperationException(NO_LONGER_SUPPORTED.formatted("sharded")); |
| 97 | + } |
| 98 | + |
| 99 | + Method shardedMethod = ReflectionUtils.findMethod(publisher.getClass(), "MapReduceIterable.sharded", |
| 100 | + boolean.class); |
| 101 | + ReflectionUtils.invokeMethod(shardedMethod, publisher, shardedMethod); |
| 102 | + }; |
| 103 | + } |
| 104 | + |
| 105 | + public static ServerAddressAdapter serverAddressAdapter(ServerAddress serverAddress) { |
| 106 | + return new ServerAddressAdapter() { |
| 107 | + @Override |
| 108 | + public InetSocketAddress getSocketAddress() { |
| 109 | + |
| 110 | + if(MongoClientVersion.is5PlusClient()) { |
| 111 | + return null; |
| 112 | + } |
| 113 | + |
| 114 | + Method serverAddressMethod = ReflectionUtils.findMethod(serverAddress.getClass(), "getSocketAddress"); |
| 115 | + Object value = ReflectionUtils.invokeMethod(serverAddressMethod, serverAddress); |
| 116 | + return value != null ? InetSocketAddress.class.cast(value) : null; |
| 117 | + } |
| 118 | + }; |
| 119 | + } |
| 120 | + |
| 121 | + public interface IndexOptionsAdapter { |
| 122 | + void setBucketSize(Double bucketSize); |
| 123 | + } |
| 124 | + |
| 125 | + public interface ClientSettingsAdapter { |
| 126 | + <T> T getStreamFactoryFactory(); |
| 127 | + } |
| 128 | + |
| 129 | + public interface ClientSettingsBuilderAdapter { |
| 130 | + <T> void setStreamFactoryFactory(T streamFactory); |
| 131 | + } |
| 132 | + |
| 133 | + public interface MapReduceIterableAdapter { |
| 134 | + void sharded(boolean sharded); |
| 135 | + } |
| 136 | + |
| 137 | + public interface MapReducePublisherAdapter { |
| 138 | + void sharded(boolean sharded); |
| 139 | + } |
| 140 | + |
| 141 | + public interface ServerAddressAdapter { |
| 142 | + InetSocketAddress getSocketAddress(); |
| 143 | + } |
| 144 | + |
| 145 | + static class MongoStreamFactoryFactorySettingsConfigurer { |
| 146 | + |
| 147 | + private static final Log logger = LogFactory.getLog(MongoClientSettingsFactoryBean.class); |
| 148 | + |
| 149 | + private static final String STREAM_FACTORY_NAME = "com.mongodb.connection.StreamFactoryFactory"; |
| 150 | + private static final boolean STREAM_FACTORY_PRESENT = ClassUtils.isPresent(STREAM_FACTORY_NAME, |
| 151 | + MongoCompatibilityAdapter.class.getClassLoader()); |
| 152 | + private final MongoClientSettings.Builder settingsBuilder; |
| 153 | + |
| 154 | + static boolean isStreamFactoryPresent() { |
| 155 | + return STREAM_FACTORY_PRESENT; |
| 156 | + } |
| 157 | + |
| 158 | + public MongoStreamFactoryFactorySettingsConfigurer(Builder settingsBuilder) { |
| 159 | + this.settingsBuilder = settingsBuilder; |
| 160 | + } |
| 161 | + |
| 162 | + void setStreamFactory(Object streamFactory) { |
| 163 | + |
| 164 | + if (MongoClientVersion.is5PlusClient()) { |
| 165 | + logger.warn("StreamFactoryFactory is no longer available. Use TransportSettings instead."); |
| 166 | + } |
| 167 | + |
| 168 | + if (isStreamFactoryPresent()) { // |
| 169 | + try { |
| 170 | + Class<?> streamFactoryType = ClassUtils.forName(STREAM_FACTORY_NAME, |
| 171 | + streamFactory.getClass().getClassLoader()); |
| 172 | + if (!ClassUtils.isAssignable(streamFactoryType, streamFactory.getClass())) { |
| 173 | + throw new IllegalArgumentException("Expected %s but found %s".formatted(streamFactoryType, streamFactory)); |
| 174 | + } |
| 175 | + |
| 176 | + Method setter = ReflectionUtils.findMethod(settingsBuilder.getClass(), "streamFactoryFactory", |
| 177 | + streamFactoryType); |
| 178 | + if (setter != null) { |
| 179 | + ReflectionUtils.invokeMethod(setter, settingsBuilder, streamFactoryType.cast(streamFactory)); |
| 180 | + } |
| 181 | + } catch (ClassNotFoundException e) { |
| 182 | + throw new IllegalArgumentException("Cannot set StreamFactoryFactory for %s".formatted(settingsBuilder), e); |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | +} |
0 commit comments