|
18 | 18 | import java.time.Duration;
|
19 | 19 |
|
20 | 20 | import org.springframework.beans.factory.BeanClassLoaderAware;
|
| 21 | +import org.springframework.beans.factory.ObjectProvider; |
21 | 22 | import org.springframework.beans.factory.annotation.Autowired;
|
22 | 23 | import org.springframework.context.EmbeddedValueResolverAware;
|
23 | 24 | import org.springframework.context.annotation.Bean;
|
|
45 | 46 | */
|
46 | 47 | @Configuration
|
47 | 48 | public class MongoHttpSessionConfiguration extends SpringHttpSessionConfiguration
|
48 |
| - implements BeanClassLoaderAware, EmbeddedValueResolverAware, ImportAware { |
49 |
| - |
50 |
| - private AbstractMongoSessionConverter mongoSessionConverter; |
51 |
| - private Integer maxInactiveIntervalInSeconds; |
52 |
| - private String collectionName; |
53 |
| - private StringValueResolver embeddedValueResolver; |
54 |
| - private ClassLoader classLoader; |
55 |
| - |
56 |
| - @Bean |
57 |
| - public MongoOperationsSessionRepository mongoSessionRepository(MongoOperations mongoOperations) { |
58 |
| - |
59 |
| - MongoOperationsSessionRepository repository = new MongoOperationsSessionRepository(mongoOperations); |
60 |
| - repository.setMaxInactiveIntervalInSeconds(this.maxInactiveIntervalInSeconds); |
61 |
| - |
62 |
| - if (this.mongoSessionConverter != null) { |
63 |
| - repository.setMongoSessionConverter(this.mongoSessionConverter); |
64 |
| - } else { |
65 |
| - JdkMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(new SerializingConverter(), |
66 |
| - new DeserializingConverter(this.classLoader), |
67 |
| - Duration.ofSeconds(MongoOperationsSessionRepository.DEFAULT_INACTIVE_INTERVAL)); |
68 |
| - repository.setMongoSessionConverter(mongoSessionConverter); |
69 |
| - } |
70 |
| - |
71 |
| - if (StringUtils.hasText(this.collectionName)) { |
72 |
| - repository.setCollectionName(this.collectionName); |
73 |
| - } |
74 |
| - |
75 |
| - return repository; |
76 |
| - } |
77 |
| - |
78 |
| - public void setCollectionName(String collectionName) { |
79 |
| - this.collectionName = collectionName; |
80 |
| - } |
81 |
| - |
82 |
| - public void setMaxInactiveIntervalInSeconds(Integer maxInactiveIntervalInSeconds) { |
83 |
| - this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds; |
84 |
| - } |
85 |
| - |
86 |
| - public void setImportMetadata(AnnotationMetadata importMetadata) { |
87 |
| - |
88 |
| - AnnotationAttributes attributes = AnnotationAttributes |
89 |
| - .fromMap(importMetadata.getAnnotationAttributes(EnableMongoHttpSession.class.getName())); |
90 |
| - |
91 |
| - if (attributes != null) { |
92 |
| - this.maxInactiveIntervalInSeconds = attributes.getNumber("maxInactiveIntervalInSeconds"); |
93 |
| - } else { |
94 |
| - this.maxInactiveIntervalInSeconds = MongoOperationsSessionRepository.DEFAULT_INACTIVE_INTERVAL; |
95 |
| - } |
96 |
| - |
97 |
| - String collectionNameValue = attributes != null ? attributes.getString("collectionName") : ""; |
98 |
| - if (StringUtils.hasText(collectionNameValue)) { |
99 |
| - this.collectionName = this.embeddedValueResolver.resolveStringValue(collectionNameValue); |
100 |
| - } |
101 |
| - } |
102 |
| - |
103 |
| - @Autowired(required = false) |
104 |
| - public void setMongoSessionConverter(AbstractMongoSessionConverter mongoSessionConverter) { |
105 |
| - this.mongoSessionConverter = mongoSessionConverter; |
106 |
| - } |
107 |
| - |
108 |
| - @Override |
109 |
| - public void setBeanClassLoader(ClassLoader classLoader) { |
110 |
| - this.classLoader = classLoader; |
111 |
| - } |
112 |
| - |
113 |
| - @Override |
114 |
| - public void setEmbeddedValueResolver(StringValueResolver resolver) { |
115 |
| - this.embeddedValueResolver = resolver; |
116 |
| - } |
| 49 | + implements BeanClassLoaderAware, EmbeddedValueResolverAware, ImportAware { |
| 50 | + |
| 51 | + private AbstractMongoSessionConverter mongoSessionConverter; |
| 52 | + private Integer maxInactiveIntervalInSeconds; |
| 53 | + private String collectionName; |
| 54 | + private StringValueResolver embeddedValueResolver; |
| 55 | + private ClassLoader classLoader; |
| 56 | + private MongoOperations mongoOperations; |
| 57 | + |
| 58 | + @Autowired |
| 59 | + public void setMongoOperations( |
| 60 | + @SpringSessionMongoOperations ObjectProvider<MongoOperations> springSessionMongoOperations, |
| 61 | + ObjectProvider<MongoOperations> mongoOperations) { |
| 62 | + MongoOperations mongoOperationsToUse = springSessionMongoOperations.getIfAvailable(); |
| 63 | + if (mongoOperationsToUse == null) { |
| 64 | + mongoOperationsToUse = mongoOperations.getObject(); |
| 65 | + } |
| 66 | + this.mongoOperations = mongoOperationsToUse; |
| 67 | + } |
| 68 | + |
| 69 | + @Bean |
| 70 | + public MongoOperationsSessionRepository mongoSessionRepository() { |
| 71 | + |
| 72 | + MongoOperationsSessionRepository repository = new MongoOperationsSessionRepository(mongoOperations); |
| 73 | + repository.setMaxInactiveIntervalInSeconds(this.maxInactiveIntervalInSeconds); |
| 74 | + |
| 75 | + if (this.mongoSessionConverter != null) { |
| 76 | + repository.setMongoSessionConverter(this.mongoSessionConverter); |
| 77 | + } else { |
| 78 | + JdkMongoSessionConverter mongoSessionConverter = new JdkMongoSessionConverter(new SerializingConverter(), |
| 79 | + new DeserializingConverter(this.classLoader), |
| 80 | + Duration.ofSeconds(MongoOperationsSessionRepository.DEFAULT_INACTIVE_INTERVAL)); |
| 81 | + repository.setMongoSessionConverter(mongoSessionConverter); |
| 82 | + } |
| 83 | + |
| 84 | + if (StringUtils.hasText(this.collectionName)) { |
| 85 | + repository.setCollectionName(this.collectionName); |
| 86 | + } |
| 87 | + |
| 88 | + return repository; |
| 89 | + } |
| 90 | + |
| 91 | + public void setCollectionName(String collectionName) { |
| 92 | + this.collectionName = collectionName; |
| 93 | + } |
| 94 | + |
| 95 | + public void setMaxInactiveIntervalInSeconds(Integer maxInactiveIntervalInSeconds) { |
| 96 | + this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds; |
| 97 | + } |
| 98 | + |
| 99 | + public void setImportMetadata(AnnotationMetadata importMetadata) { |
| 100 | + |
| 101 | + AnnotationAttributes attributes = AnnotationAttributes |
| 102 | + .fromMap(importMetadata.getAnnotationAttributes(EnableMongoHttpSession.class.getName())); |
| 103 | + |
| 104 | + if (attributes != null) { |
| 105 | + this.maxInactiveIntervalInSeconds = attributes.getNumber("maxInactiveIntervalInSeconds"); |
| 106 | + } else { |
| 107 | + this.maxInactiveIntervalInSeconds = MongoOperationsSessionRepository.DEFAULT_INACTIVE_INTERVAL; |
| 108 | + } |
| 109 | + |
| 110 | + String collectionNameValue = attributes != null ? attributes.getString("collectionName") : ""; |
| 111 | + if (StringUtils.hasText(collectionNameValue)) { |
| 112 | + this.collectionName = this.embeddedValueResolver.resolveStringValue(collectionNameValue); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + @Autowired(required = false) |
| 117 | + public void setMongoSessionConverter(AbstractMongoSessionConverter mongoSessionConverter) { |
| 118 | + this.mongoSessionConverter = mongoSessionConverter; |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public void setBeanClassLoader(ClassLoader classLoader) { |
| 123 | + this.classLoader = classLoader; |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public void setEmbeddedValueResolver(StringValueResolver resolver) { |
| 128 | + this.embeddedValueResolver = resolver; |
| 129 | + } |
117 | 130 |
|
118 | 131 | }
|
0 commit comments