/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoServerApiFactoryBean.java

https://github.com/spring-projects/spring-data-mongodb · Java · 92 lines · 45 code · 12 blank · 35 comment · 4 complexity · cdec0b5fcf996f7bffb901d6afd23f82 MD5 · raw file

  1. /*
  2. * Copyright 2021-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. package org.springframework.data.mongodb.core;
  17. import org.springframework.beans.factory.FactoryBean;
  18. import org.springframework.lang.Nullable;
  19. import org.springframework.util.ObjectUtils;
  20. import com.mongodb.ServerApi;
  21. import com.mongodb.ServerApi.Builder;
  22. import com.mongodb.ServerApiVersion;
  23. /**
  24. * {@link FactoryBean} for creating {@link ServerApi} using the {@link ServerApi.Builder}.
  25. *
  26. * @author Christoph Strobl
  27. * @since 3.3
  28. */
  29. public class MongoServerApiFactoryBean implements FactoryBean<ServerApi> {
  30. private String version;
  31. private @Nullable Boolean deprecationErrors;
  32. private @Nullable Boolean strict;
  33. /**
  34. * @param version the version string either as the enum name or the server version value.
  35. * @see ServerApiVersion
  36. */
  37. public void setVersion(String version) {
  38. this.version = version;
  39. }
  40. /**
  41. * @param deprecationErrors
  42. * @see ServerApi.Builder#deprecationErrors(boolean)
  43. */
  44. public void setDeprecationErrors(@Nullable Boolean deprecationErrors) {
  45. this.deprecationErrors = deprecationErrors;
  46. }
  47. /**
  48. * @param strict
  49. * @see ServerApi.Builder#strict(boolean)
  50. */
  51. public void setStrict(@Nullable Boolean strict) {
  52. this.strict = strict;
  53. }
  54. @Nullable
  55. @Override
  56. public ServerApi getObject() throws Exception {
  57. Builder builder = ServerApi.builder().version(version());
  58. if (deprecationErrors != null) {
  59. builder = builder.deprecationErrors(deprecationErrors);
  60. }
  61. if (strict != null) {
  62. builder = builder.strict(strict);
  63. }
  64. return builder.build();
  65. }
  66. @Nullable
  67. @Override
  68. public Class<?> getObjectType() {
  69. return ServerApi.class;
  70. }
  71. private ServerApiVersion version() {
  72. try {
  73. // lookup by name eg. 'V1'
  74. return ObjectUtils.caseInsensitiveValueOf(ServerApiVersion.values(), version);
  75. } catch (IllegalArgumentException e) {
  76. // or just the version number, eg. just '1'
  77. return ServerApiVersion.findByValue(version);
  78. }
  79. }
  80. }