diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/AbstractMongoConfiguration.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/AbstractMongoConfiguration.java index d94d93e05..dda80f2c0 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/AbstractMongoConfiguration.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/AbstractMongoConfiguration.java @@ -1,114 +1,114 @@ -/* - * Copyright 2011-2017 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.mongodb.config; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.data.mongodb.MongoDbFactory; -import org.springframework.data.mongodb.core.MongoTemplate; -import org.springframework.data.mongodb.core.SimpleMongoDbFactory; -import org.springframework.data.mongodb.core.convert.DbRefResolver; -import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver; -import org.springframework.data.mongodb.core.convert.MappingMongoConverter; -import org.springframework.data.mongodb.core.mapping.Document; -import org.springframework.lang.Nullable; - -import com.mongodb.MongoClient; - -/** - * Base class for Spring Data MongoDB configuration using JavaConfig. - * - * @author Mark Pollack - * @author Oliver Gierke - * @author Thomas Darimont - * @author Ryan Tenney - * @author Christoph Strobl - * @author Mark Paluch - * @see MongoConfigurationSupport - */ -@Configuration -public abstract class -AbstractMongoConfiguration extends MongoConfigurationSupport { - - /** - * Return the {@link MongoClient} instance to connect to. Annotate with {@link Bean} in case you want to expose a - * {@link MongoClient} instance to the {@link org.springframework.context.ApplicationContext}. - * - * @return - */ - public abstract MongoClient mongoClient(); - - /** - * Creates a {@link MongoTemplate}. - * - * @return - */ - @Bean - public MongoTemplate mongoTemplate() throws Exception { - return new MongoTemplate(mongoDbFactory(), mappingMongoConverter()); - } - - /** - * Creates a {@link SimpleMongoDbFactory} to be used by the {@link MongoTemplate}. Will use the {@link MongoClient} - * instance configured in {@link #mongo()}. - * - * @see #mongoClient() - * @see #mongoTemplate() - * @return - */ - @Bean - public MongoDbFactory mongoDbFactory() { - return new SimpleMongoDbFactory(mongoClient(), getDatabaseName()); - } - - /** - * Return the base package to scan for mapped {@link Document}s. Will return the package name of the configuration - * class' (the concrete class, not this one here) by default. So if you have a {@code com.acme.AppConfig} extending - * {@link AbstractMongoConfiguration} the base package will be considered {@code com.acme} unless the method is - * overridden to implement alternate behavior. - * - * @return the base package to scan for mapped {@link Document} classes or {@literal null} to not enable scanning for - * entities. - * @deprecated use {@link #getMappingBasePackages()} instead. - */ - @Deprecated - @Nullable - protected String getMappingBasePackage() { - - Package mappingBasePackage = getClass().getPackage(); - return mappingBasePackage == null ? null : mappingBasePackage.getName(); - } - - /** - * Creates a {@link MappingMongoConverter} using the configured {@link #mongoDbFactory()} and - * {@link #mongoMappingContext()}. Will get {@link #customConversions()} applied. - * - * @see #customConversions() - * @see #mongoMappingContext() - * @see #mongoDbFactory() - * @return - * @throws Exception - */ - @Bean - public MappingMongoConverter mappingMongoConverter() throws Exception { - - DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory()); - MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, mongoMappingContext()); - converter.setCustomConversions(customConversions()); - - return converter; - } -} +/* + * Copyright 2011-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.mongodb.MongoDbFactory; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.SimpleMongoDbFactory; +import org.springframework.data.mongodb.core.convert.DbRefResolver; +import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver; +import org.springframework.data.mongodb.core.convert.MappingMongoConverter; +import org.springframework.data.mongodb.core.mapping.Document; +import org.springframework.lang.Nullable; + +import com.mongodb.MongoClient; + +/** + * Base class for Spring Data MongoDB configuration using JavaConfig. + * + * @author Mark Pollack + * @author Oliver Gierke + * @author Thomas Darimont + * @author Ryan Tenney + * @author Christoph Strobl + * @author Mark Paluch + * @see MongoConfigurationSupport + */ +@Configuration +public abstract class +AbstractMongoConfiguration extends MongoConfigurationSupport { + + /** + * Return the {@link MongoClient} instance to connect to. Annotate with {@link Bean} in case you want to expose a + * {@link MongoClient} instance to the {@link org.springframework.context.ApplicationContext}. + * + * @return + */ + public abstract MongoClient mongoClient(); + + /** + * Creates a {@link MongoTemplate}. + * + * @return + */ + @Bean + public MongoTemplate mongoTemplate() throws Exception { + return new MongoTemplate(mongoDbFactory(), mappingMongoConverter()); + } + + /** + * Creates a {@link SimpleMongoDbFactory} to be used by the {@link MongoTemplate}. Will use the {@link MongoClient} + * instance configured in {@link #mongo()}. + * + * @see #mongoClient() + * @see #mongoTemplate() + * @return + */ + @Bean + public MongoDbFactory mongoDbFactory() { + return new SimpleMongoDbFactory(mongoClient(), getDatabaseName()); + } + + /** + * Return the base package to scan for mapped {@link Document}s. Will return the package name of the configuration + * class' (the concrete class, not this one here) by default. So if you have a {@code com.acme.AppConfig} extending + * {@link AbstractMongoConfiguration} the base package will be considered {@code com.acme} unless the method is + * overridden to implement alternate behavior. + * + * @return the base package to scan for mapped {@link Document} classes or {@literal null} to not enable scanning for + * entities. + * @deprecated use {@link #getMappingBasePackages()} instead. + */ + @Deprecated + @Nullable + protected String getMappingBasePackage() { + + Package mappingBasePackage = getClass().getPackage(); + return mappingBasePackage == null ? null : mappingBasePackage.getName(); + } + + /** + * Creates a {@link MappingMongoConverter} using the configured {@link #mongoDbFactory()} and + * {@link #mongoMappingContext()}. Will get {@link #customConversions()} applied. + * + * @see #customConversions() + * @see #mongoMappingContext() + * @see #mongoDbFactory() + * @return + * @throws Exception + */ + @Bean + public MappingMongoConverter mappingMongoConverter() throws Exception { + + DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory()); + MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, mongoMappingContext()); + converter.setCustomConversions(customConversions()); + + return converter; + } +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoJmxParser.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoJmxParser.java index 0bfb398d9..800dd448f 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoJmxParser.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoJmxParser.java @@ -1,76 +1,76 @@ -/* - * Copyright 2011-2017 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.mongodb.config; - -import org.springframework.beans.factory.config.BeanDefinition; -import org.springframework.beans.factory.parsing.BeanComponentDefinition; -import org.springframework.beans.factory.parsing.CompositeComponentDefinition; -import org.springframework.beans.factory.support.BeanDefinitionBuilder; -import org.springframework.beans.factory.xml.BeanDefinitionParser; -import org.springframework.beans.factory.xml.ParserContext; -import org.springframework.data.mongodb.core.MongoAdmin; -import org.springframework.data.mongodb.monitor.*; -import org.springframework.util.StringUtils; -import org.w3c.dom.Element; - -/** - * @author Mark Pollack - * @author Thomas Risberg - * @author John Brisbin - * @author Oliver Gierke - * @author Christoph Strobl - */ -public class MongoJmxParser implements BeanDefinitionParser { - - public BeanDefinition parse(Element element, ParserContext parserContext) { - String name = element.getAttribute("mongo-ref"); - if (!StringUtils.hasText(name)) { - name = BeanNames.MONGO_BEAN_NAME; - } - registerJmxComponents(name, element, parserContext); - return null; - } - - protected void registerJmxComponents(String mongoRefName, Element element, ParserContext parserContext) { - Object eleSource = parserContext.extractSource(element); - - CompositeComponentDefinition compositeDef = new CompositeComponentDefinition(element.getTagName(), eleSource); - - createBeanDefEntry(AssertMetrics.class, compositeDef, mongoRefName, eleSource, parserContext); - createBeanDefEntry(BackgroundFlushingMetrics.class, compositeDef, mongoRefName, eleSource, parserContext); - createBeanDefEntry(BtreeIndexCounters.class, compositeDef, mongoRefName, eleSource, parserContext); - createBeanDefEntry(ConnectionMetrics.class, compositeDef, mongoRefName, eleSource, parserContext); - createBeanDefEntry(GlobalLockMetrics.class, compositeDef, mongoRefName, eleSource, parserContext); - createBeanDefEntry(MemoryMetrics.class, compositeDef, mongoRefName, eleSource, parserContext); - createBeanDefEntry(OperationCounters.class, compositeDef, mongoRefName, eleSource, parserContext); - createBeanDefEntry(ServerInfo.class, compositeDef, mongoRefName, eleSource, parserContext); - createBeanDefEntry(MongoAdmin.class, compositeDef, mongoRefName, eleSource, parserContext); - - parserContext.registerComponent(compositeDef); - - } - - protected void createBeanDefEntry(Class clazz, CompositeComponentDefinition compositeDef, String mongoRefName, - Object eleSource, ParserContext parserContext) { - BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(clazz); - builder.getRawBeanDefinition().setSource(eleSource); - builder.addConstructorArgReference(mongoRefName); - BeanDefinition assertDef = builder.getBeanDefinition(); - String assertName = parserContext.getReaderContext().registerWithGeneratedName(assertDef); - compositeDef.addNestedComponent(new BeanComponentDefinition(assertDef, assertName)); - } - -} +/* + * Copyright 2011-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.config; + +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.parsing.BeanComponentDefinition; +import org.springframework.beans.factory.parsing.CompositeComponentDefinition; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.xml.BeanDefinitionParser; +import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.data.mongodb.core.MongoAdmin; +import org.springframework.data.mongodb.monitor.*; +import org.springframework.util.StringUtils; +import org.w3c.dom.Element; + +/** + * @author Mark Pollack + * @author Thomas Risberg + * @author John Brisbin + * @author Oliver Gierke + * @author Christoph Strobl + */ +public class MongoJmxParser implements BeanDefinitionParser { + + public BeanDefinition parse(Element element, ParserContext parserContext) { + String name = element.getAttribute("mongo-ref"); + if (!StringUtils.hasText(name)) { + name = BeanNames.MONGO_BEAN_NAME; + } + registerJmxComponents(name, element, parserContext); + return null; + } + + protected void registerJmxComponents(String mongoRefName, Element element, ParserContext parserContext) { + Object eleSource = parserContext.extractSource(element); + + CompositeComponentDefinition compositeDef = new CompositeComponentDefinition(element.getTagName(), eleSource); + + createBeanDefEntry(AssertMetrics.class, compositeDef, mongoRefName, eleSource, parserContext); + createBeanDefEntry(BackgroundFlushingMetrics.class, compositeDef, mongoRefName, eleSource, parserContext); + createBeanDefEntry(BtreeIndexCounters.class, compositeDef, mongoRefName, eleSource, parserContext); + createBeanDefEntry(ConnectionMetrics.class, compositeDef, mongoRefName, eleSource, parserContext); + createBeanDefEntry(GlobalLockMetrics.class, compositeDef, mongoRefName, eleSource, parserContext); + createBeanDefEntry(MemoryMetrics.class, compositeDef, mongoRefName, eleSource, parserContext); + createBeanDefEntry(OperationCounters.class, compositeDef, mongoRefName, eleSource, parserContext); + createBeanDefEntry(ServerInfo.class, compositeDef, mongoRefName, eleSource, parserContext); + createBeanDefEntry(MongoAdmin.class, compositeDef, mongoRefName, eleSource, parserContext); + + parserContext.registerComponent(compositeDef); + + } + + protected void createBeanDefEntry(Class clazz, CompositeComponentDefinition compositeDef, String mongoRefName, + Object eleSource, ParserContext parserContext) { + BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(clazz); + builder.getRawBeanDefinition().setSource(eleSource); + builder.addConstructorArgReference(mongoRefName); + BeanDefinition assertDef = builder.getBeanDefinition(); + String assertName = parserContext.getReaderContext().registerWithGeneratedName(assertDef); + compositeDef.addNestedComponent(new BeanComponentDefinition(assertDef, assertName)); + } + +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoParsingUtils.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoParsingUtils.java index 1821adad7..7f36decd9 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoParsingUtils.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoParsingUtils.java @@ -1,170 +1,170 @@ -/* - * Copyright 2011-2017 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.mongodb.config; - -import static org.springframework.data.config.ParsingUtils.*; - -import java.util.Map; - -import org.springframework.beans.factory.config.BeanDefinition; -import org.springframework.beans.factory.config.CustomEditorConfigurer; -import org.springframework.beans.factory.support.BeanDefinitionBuilder; -import org.springframework.beans.factory.support.ManagedMap; -import org.springframework.beans.factory.xml.BeanDefinitionParser; -import org.springframework.data.mongodb.core.MongoClientOptionsFactoryBean; -import org.springframework.util.xml.DomUtils; -import org.w3c.dom.Element; - -/** - * Utility methods for {@link BeanDefinitionParser} implementations for MongoDB. - * - * @author Mark Pollack - * @author Oliver Gierke - * @author Thomas Darimont - * @author Christoph Strobl - */ -@SuppressWarnings("deprecation") -abstract class MongoParsingUtils { - - private MongoParsingUtils() {} - - /** - * Parses the mongo replica-set element. - * - * @param parserContext the parser context - * @param element the mongo element - * @param mongoBuilder the bean definition builder to populate - * @return - */ - static void parseReplicaSet(Element element, BeanDefinitionBuilder mongoBuilder) { - setPropertyValue(mongoBuilder, element, "replica-set", "replicaSetSeeds"); - } - - /** - * Parses the {@code mongo:client-options} sub-element. Populates the given attribute factory with the proper - * attributes. - * - * @param element must not be {@literal null}. - * @param mongoClientBuilder must not be {@literal null}. - * @return - * @since 1.7 - */ - public static boolean parseMongoClientOptions(Element element, BeanDefinitionBuilder mongoClientBuilder) { - - Element optionsElement = DomUtils.getChildElementByTagName(element, "client-options"); - - if (optionsElement == null) { - return false; - } - - BeanDefinitionBuilder clientOptionsDefBuilder = BeanDefinitionBuilder - .genericBeanDefinition(MongoClientOptionsFactoryBean.class); - - setPropertyValue(clientOptionsDefBuilder, optionsElement, "description", "description"); - setPropertyValue(clientOptionsDefBuilder, optionsElement, "min-connections-per-host", "minConnectionsPerHost"); - setPropertyValue(clientOptionsDefBuilder, optionsElement, "connections-per-host", "connectionsPerHost"); - setPropertyValue(clientOptionsDefBuilder, optionsElement, "threads-allowed-to-block-for-connection-multiplier", - "threadsAllowedToBlockForConnectionMultiplier"); - setPropertyValue(clientOptionsDefBuilder, optionsElement, "max-wait-time", "maxWaitTime"); - setPropertyValue(clientOptionsDefBuilder, optionsElement, "max-connection-idle-time", "maxConnectionIdleTime"); - setPropertyValue(clientOptionsDefBuilder, optionsElement, "max-connection-life-time", "maxConnectionLifeTime"); - setPropertyValue(clientOptionsDefBuilder, optionsElement, "connect-timeout", "connectTimeout"); - setPropertyValue(clientOptionsDefBuilder, optionsElement, "socket-timeout", "socketTimeout"); - setPropertyValue(clientOptionsDefBuilder, optionsElement, "socket-keep-alive", "socketKeepAlive"); - setPropertyValue(clientOptionsDefBuilder, optionsElement, "read-preference", "readPreference"); - setPropertyValue(clientOptionsDefBuilder, optionsElement, "write-concern", "writeConcern"); - setPropertyValue(clientOptionsDefBuilder, optionsElement, "heartbeat-frequency", "heartbeatFrequency"); - setPropertyValue(clientOptionsDefBuilder, optionsElement, "min-heartbeat-frequency", "minHeartbeatFrequency"); - setPropertyValue(clientOptionsDefBuilder, optionsElement, "heartbeat-connect-timeout", "heartbeatConnectTimeout"); - setPropertyValue(clientOptionsDefBuilder, optionsElement, "heartbeat-socket-timeout", "heartbeatSocketTimeout"); - setPropertyValue(clientOptionsDefBuilder, optionsElement, "ssl", "ssl"); - setPropertyReference(clientOptionsDefBuilder, optionsElement, "ssl-socket-factory-ref", "sslSocketFactory"); - setPropertyValue(clientOptionsDefBuilder, optionsElement, "server-selection-timeout", "serverSelectionTimeout"); - - mongoClientBuilder.addPropertyValue("mongoClientOptions", clientOptionsDefBuilder.getBeanDefinition()); - - return true; - } - - /** - * Returns the {@link BeanDefinitionBuilder} to build a {@link BeanDefinition} for a - * {@link WriteConcernPropertyEditor}. - * - * @return - */ - static BeanDefinitionBuilder getWriteConcernPropertyEditorBuilder() { - - Map> customEditors = new ManagedMap>(); - customEditors.put("com.mongodb.WriteConcern", WriteConcernPropertyEditor.class); - - BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(CustomEditorConfigurer.class); - builder.addPropertyValue("customEditors", customEditors); - - return builder; - } - - /** - * One should only register one bean definition but want to have the convenience of using - * AbstractSingleBeanDefinitionParser but have the side effect of registering a 'default' property editor with the - * container. - */ - static BeanDefinitionBuilder getServerAddressPropertyEditorBuilder() { - - Map customEditors = new ManagedMap(); - customEditors.put("com.mongodb.ServerAddress[]", - "org.springframework.data.mongodb.config.ServerAddressPropertyEditor"); - - BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(CustomEditorConfigurer.class); - builder.addPropertyValue("customEditors", customEditors); - return builder; - } - - /** - * Returns the {@link BeanDefinitionBuilder} to build a {@link BeanDefinition} for a - * {@link ReadPreferencePropertyEditor}. - * - * @return - * @since 1.7 - */ - static BeanDefinitionBuilder getReadPreferencePropertyEditorBuilder() { - - Map> customEditors = new ManagedMap>(); - customEditors.put("com.mongodb.ReadPreference", ReadPreferencePropertyEditor.class); - - BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(CustomEditorConfigurer.class); - builder.addPropertyValue("customEditors", customEditors); - - return builder; - } - - /** - * Returns the {@link BeanDefinitionBuilder} to build a {@link BeanDefinition} for a - * {@link MongoCredentialPropertyEditor}. - * - * @return - * @since 1.7 - */ - static BeanDefinitionBuilder getMongoCredentialPropertyEditor() { - - Map> customEditors = new ManagedMap>(); - customEditors.put("com.mongodb.MongoCredential[]", MongoCredentialPropertyEditor.class); - - BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(CustomEditorConfigurer.class); - builder.addPropertyValue("customEditors", customEditors); - - return builder; - } -} +/* + * Copyright 2011-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.config; + +import static org.springframework.data.config.ParsingUtils.*; + +import java.util.Map; + +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.config.CustomEditorConfigurer; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.support.ManagedMap; +import org.springframework.beans.factory.xml.BeanDefinitionParser; +import org.springframework.data.mongodb.core.MongoClientOptionsFactoryBean; +import org.springframework.util.xml.DomUtils; +import org.w3c.dom.Element; + +/** + * Utility methods for {@link BeanDefinitionParser} implementations for MongoDB. + * + * @author Mark Pollack + * @author Oliver Gierke + * @author Thomas Darimont + * @author Christoph Strobl + */ +@SuppressWarnings("deprecation") +abstract class MongoParsingUtils { + + private MongoParsingUtils() {} + + /** + * Parses the mongo replica-set element. + * + * @param parserContext the parser context + * @param element the mongo element + * @param mongoBuilder the bean definition builder to populate + * @return + */ + static void parseReplicaSet(Element element, BeanDefinitionBuilder mongoBuilder) { + setPropertyValue(mongoBuilder, element, "replica-set", "replicaSetSeeds"); + } + + /** + * Parses the {@code mongo:client-options} sub-element. Populates the given attribute factory with the proper + * attributes. + * + * @param element must not be {@literal null}. + * @param mongoClientBuilder must not be {@literal null}. + * @return + * @since 1.7 + */ + public static boolean parseMongoClientOptions(Element element, BeanDefinitionBuilder mongoClientBuilder) { + + Element optionsElement = DomUtils.getChildElementByTagName(element, "client-options"); + + if (optionsElement == null) { + return false; + } + + BeanDefinitionBuilder clientOptionsDefBuilder = BeanDefinitionBuilder + .genericBeanDefinition(MongoClientOptionsFactoryBean.class); + + setPropertyValue(clientOptionsDefBuilder, optionsElement, "description", "description"); + setPropertyValue(clientOptionsDefBuilder, optionsElement, "min-connections-per-host", "minConnectionsPerHost"); + setPropertyValue(clientOptionsDefBuilder, optionsElement, "connections-per-host", "connectionsPerHost"); + setPropertyValue(clientOptionsDefBuilder, optionsElement, "threads-allowed-to-block-for-connection-multiplier", + "threadsAllowedToBlockForConnectionMultiplier"); + setPropertyValue(clientOptionsDefBuilder, optionsElement, "max-wait-time", "maxWaitTime"); + setPropertyValue(clientOptionsDefBuilder, optionsElement, "max-connection-idle-time", "maxConnectionIdleTime"); + setPropertyValue(clientOptionsDefBuilder, optionsElement, "max-connection-life-time", "maxConnectionLifeTime"); + setPropertyValue(clientOptionsDefBuilder, optionsElement, "connect-timeout", "connectTimeout"); + setPropertyValue(clientOptionsDefBuilder, optionsElement, "socket-timeout", "socketTimeout"); + setPropertyValue(clientOptionsDefBuilder, optionsElement, "socket-keep-alive", "socketKeepAlive"); + setPropertyValue(clientOptionsDefBuilder, optionsElement, "read-preference", "readPreference"); + setPropertyValue(clientOptionsDefBuilder, optionsElement, "write-concern", "writeConcern"); + setPropertyValue(clientOptionsDefBuilder, optionsElement, "heartbeat-frequency", "heartbeatFrequency"); + setPropertyValue(clientOptionsDefBuilder, optionsElement, "min-heartbeat-frequency", "minHeartbeatFrequency"); + setPropertyValue(clientOptionsDefBuilder, optionsElement, "heartbeat-connect-timeout", "heartbeatConnectTimeout"); + setPropertyValue(clientOptionsDefBuilder, optionsElement, "heartbeat-socket-timeout", "heartbeatSocketTimeout"); + setPropertyValue(clientOptionsDefBuilder, optionsElement, "ssl", "ssl"); + setPropertyReference(clientOptionsDefBuilder, optionsElement, "ssl-socket-factory-ref", "sslSocketFactory"); + setPropertyValue(clientOptionsDefBuilder, optionsElement, "server-selection-timeout", "serverSelectionTimeout"); + + mongoClientBuilder.addPropertyValue("mongoClientOptions", clientOptionsDefBuilder.getBeanDefinition()); + + return true; + } + + /** + * Returns the {@link BeanDefinitionBuilder} to build a {@link BeanDefinition} for a + * {@link WriteConcernPropertyEditor}. + * + * @return + */ + static BeanDefinitionBuilder getWriteConcernPropertyEditorBuilder() { + + Map> customEditors = new ManagedMap>(); + customEditors.put("com.mongodb.WriteConcern", WriteConcernPropertyEditor.class); + + BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(CustomEditorConfigurer.class); + builder.addPropertyValue("customEditors", customEditors); + + return builder; + } + + /** + * One should only register one bean definition but want to have the convenience of using + * AbstractSingleBeanDefinitionParser but have the side effect of registering a 'default' property editor with the + * container. + */ + static BeanDefinitionBuilder getServerAddressPropertyEditorBuilder() { + + Map customEditors = new ManagedMap(); + customEditors.put("com.mongodb.ServerAddress[]", + "org.springframework.data.mongodb.config.ServerAddressPropertyEditor"); + + BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(CustomEditorConfigurer.class); + builder.addPropertyValue("customEditors", customEditors); + return builder; + } + + /** + * Returns the {@link BeanDefinitionBuilder} to build a {@link BeanDefinition} for a + * {@link ReadPreferencePropertyEditor}. + * + * @return + * @since 1.7 + */ + static BeanDefinitionBuilder getReadPreferencePropertyEditorBuilder() { + + Map> customEditors = new ManagedMap>(); + customEditors.put("com.mongodb.ReadPreference", ReadPreferencePropertyEditor.class); + + BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(CustomEditorConfigurer.class); + builder.addPropertyValue("customEditors", customEditors); + + return builder; + } + + /** + * Returns the {@link BeanDefinitionBuilder} to build a {@link BeanDefinition} for a + * {@link MongoCredentialPropertyEditor}. + * + * @return + * @since 1.7 + */ + static BeanDefinitionBuilder getMongoCredentialPropertyEditor() { + + Map> customEditors = new ManagedMap>(); + customEditors.put("com.mongodb.MongoCredential[]", MongoCredentialPropertyEditor.class); + + BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(CustomEditorConfigurer.class); + builder.addPropertyValue("customEditors", customEditors); + + return builder; + } +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/CollectionCallback.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/CollectionCallback.java index 444b0a8e8..af1bb9b9a 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/CollectionCallback.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/CollectionCallback.java @@ -1,46 +1,46 @@ -/* - * Copyright 2010-2017 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.mongodb.core; - -import org.bson.Document; -import org.springframework.dao.DataAccessException; -import org.springframework.lang.Nullable; - -import com.mongodb.MongoException; -import com.mongodb.client.MongoCollection; - -/** - * Callback interface for executing actions against a {@link MongoCollection}. - * - * @author Mark Pollak - * @author Grame Rocher - * @author Oliver Gierke - * @author John Brisbin - * @auhtor Christoph Strobl - * @since 1.0 - */ -public interface CollectionCallback { - - /** - * @param collection never {@literal null}. - * @return can be {@literal null}. - * @throws MongoException - * @throws DataAccessException - */ - @Nullable - T doInCollection(MongoCollection collection) throws MongoException, DataAccessException; - -} +/* + * Copyright 2010-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.core; + +import org.bson.Document; +import org.springframework.dao.DataAccessException; +import org.springframework.lang.Nullable; + +import com.mongodb.MongoException; +import com.mongodb.client.MongoCollection; + +/** + * Callback interface for executing actions against a {@link MongoCollection}. + * + * @author Mark Pollak + * @author Grame Rocher + * @author Oliver Gierke + * @author John Brisbin + * @auhtor Christoph Strobl + * @since 1.0 + */ +public interface CollectionCallback { + + /** + * @param collection never {@literal null}. + * @return can be {@literal null}. + * @throws MongoException + * @throws DataAccessException + */ + @Nullable + T doInCollection(MongoCollection collection) throws MongoException, DataAccessException; + +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/CollectionOptions.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/CollectionOptions.java index 7810ac115..f0cb71f8a 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/CollectionOptions.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/CollectionOptions.java @@ -1,166 +1,166 @@ -/* - * Copyright 2010-2017 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.mongodb.core; - -import java.util.Optional; - -import org.springframework.data.mongodb.core.query.Collation; -import org.springframework.lang.Nullable; -import org.springframework.util.Assert; - -/** - * Provides a simple wrapper to encapsulate the variety of settings you can use when creating a collection. - * - * @author Thomas Risberg - * @author Christoph Strobl - * @author Mark Paluch - */ -public class CollectionOptions { - - private @Nullable Long maxDocuments; - private @Nullable Long size; - private @Nullable Boolean capped; - private @Nullable Collation collation; - - /** - * Constructs a new CollectionOptions instance. - * - * @param size the collection size in bytes, this data space is preallocated. Can be {@literal null}. - * @param maxDocuments the maximum number of documents in the collection. Can be {@literal null}. - * @param capped true to created a "capped" collection (fixed size with auto-FIFO behavior based on insertion order), - * false otherwise. Can be {@literal null}. - * @deprecated since 2.0 please use {@link CollectionOptions#empty()} as entry point. - */ - @Deprecated - public CollectionOptions(@Nullable Long size, @Nullable Long maxDocuments, @Nullable Boolean capped) { - this(size, maxDocuments, capped, null); - } - - private CollectionOptions(@Nullable Long size, @Nullable Long maxDocuments, @Nullable Boolean capped, - @Nullable Collation collation) { - - this.maxDocuments = maxDocuments; - this.size = size; - this.capped = capped; - this.collation = collation; - } - - /** - * Create new {@link CollectionOptions} by just providing the {@link Collation} to use. - * - * @param collation must not be {@literal null}. - * @return new {@link CollectionOptions}. - * @since 2.0 - */ - public static CollectionOptions just(Collation collation) { - - Assert.notNull(collation, "Collation must not be null!"); - - return new CollectionOptions(null, null, null, collation); - } - - /** - * Create new empty {@link CollectionOptions}. - * - * @return new {@link CollectionOptions}. - * @since 2.0 - */ - public static CollectionOptions empty() { - return new CollectionOptions(null, null, null, null); - } - - /** - * Create new {@link CollectionOptions} with already given settings and capped set to {@literal true}.
- * NOTE Using capped collections requires defining {@link #size(int)}. - * - * @return new {@link CollectionOptions}. - * @since 2.0 - */ - public CollectionOptions capped() { - return new CollectionOptions(size, maxDocuments, true, collation); - } - - /** - * Create new {@link CollectionOptions} with already given settings and {@code maxDocuments} set to given value. - * - * @param maxDocuments can be {@literal null}. - * @return new {@link CollectionOptions}. - * @since 2.0 - */ - public CollectionOptions maxDocuments(long maxDocuments) { - return new CollectionOptions(size, maxDocuments, capped, collation); - } - - /** - * Create new {@link CollectionOptions} with already given settings and {@code size} set to given value. - * - * @param size can be {@literal null}. - * @return new {@link CollectionOptions}. - * @since 2.0 - */ - public CollectionOptions size(long size) { - return new CollectionOptions(size, maxDocuments, capped, collation); - } - - /** - * Create new {@link CollectionOptions} with already given settings and {@code collation} set to given value. - * - * @param collation can be {@literal null}. - * @return new {@link CollectionOptions}. - * @since 2.0 - */ - public CollectionOptions collation(@Nullable Collation collation) { - return new CollectionOptions(size, maxDocuments, capped, collation); - } - - /** - * Get the max number of documents the collection should be limited to. - * - * @return {@link Optional#empty()} if not set. - */ - public Optional getMaxDocuments() { - return Optional.ofNullable(maxDocuments); - } - - /** - * Get the {@literal size} in bytes the collection should be limited to. - * - * @return {@link Optional#empty()} if not set. - */ - public Optional getSize() { - return Optional.ofNullable(size); - } - - /** - * Get if the collection should be capped. - * - * @return {@link Optional#empty()} if not set. - * @since 2.0 - */ - public Optional getCapped() { - return Optional.ofNullable(capped); - } - - /** - * Get the {@link Collation} settings. - * - * @return {@link Optional#empty()} if not set. - * @since 2.0 - */ - public Optional getCollation() { - return Optional.ofNullable(collation); - } -} +/* + * Copyright 2010-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.core; + +import java.util.Optional; + +import org.springframework.data.mongodb.core.query.Collation; +import org.springframework.lang.Nullable; +import org.springframework.util.Assert; + +/** + * Provides a simple wrapper to encapsulate the variety of settings you can use when creating a collection. + * + * @author Thomas Risberg + * @author Christoph Strobl + * @author Mark Paluch + */ +public class CollectionOptions { + + private @Nullable Long maxDocuments; + private @Nullable Long size; + private @Nullable Boolean capped; + private @Nullable Collation collation; + + /** + * Constructs a new CollectionOptions instance. + * + * @param size the collection size in bytes, this data space is preallocated. Can be {@literal null}. + * @param maxDocuments the maximum number of documents in the collection. Can be {@literal null}. + * @param capped true to created a "capped" collection (fixed size with auto-FIFO behavior based on insertion order), + * false otherwise. Can be {@literal null}. + * @deprecated since 2.0 please use {@link CollectionOptions#empty()} as entry point. + */ + @Deprecated + public CollectionOptions(@Nullable Long size, @Nullable Long maxDocuments, @Nullable Boolean capped) { + this(size, maxDocuments, capped, null); + } + + private CollectionOptions(@Nullable Long size, @Nullable Long maxDocuments, @Nullable Boolean capped, + @Nullable Collation collation) { + + this.maxDocuments = maxDocuments; + this.size = size; + this.capped = capped; + this.collation = collation; + } + + /** + * Create new {@link CollectionOptions} by just providing the {@link Collation} to use. + * + * @param collation must not be {@literal null}. + * @return new {@link CollectionOptions}. + * @since 2.0 + */ + public static CollectionOptions just(Collation collation) { + + Assert.notNull(collation, "Collation must not be null!"); + + return new CollectionOptions(null, null, null, collation); + } + + /** + * Create new empty {@link CollectionOptions}. + * + * @return new {@link CollectionOptions}. + * @since 2.0 + */ + public static CollectionOptions empty() { + return new CollectionOptions(null, null, null, null); + } + + /** + * Create new {@link CollectionOptions} with already given settings and capped set to {@literal true}.
+ * NOTE Using capped collections requires defining {@link #size(int)}. + * + * @return new {@link CollectionOptions}. + * @since 2.0 + */ + public CollectionOptions capped() { + return new CollectionOptions(size, maxDocuments, true, collation); + } + + /** + * Create new {@link CollectionOptions} with already given settings and {@code maxDocuments} set to given value. + * + * @param maxDocuments can be {@literal null}. + * @return new {@link CollectionOptions}. + * @since 2.0 + */ + public CollectionOptions maxDocuments(long maxDocuments) { + return new CollectionOptions(size, maxDocuments, capped, collation); + } + + /** + * Create new {@link CollectionOptions} with already given settings and {@code size} set to given value. + * + * @param size can be {@literal null}. + * @return new {@link CollectionOptions}. + * @since 2.0 + */ + public CollectionOptions size(long size) { + return new CollectionOptions(size, maxDocuments, capped, collation); + } + + /** + * Create new {@link CollectionOptions} with already given settings and {@code collation} set to given value. + * + * @param collation can be {@literal null}. + * @return new {@link CollectionOptions}. + * @since 2.0 + */ + public CollectionOptions collation(@Nullable Collation collation) { + return new CollectionOptions(size, maxDocuments, capped, collation); + } + + /** + * Get the max number of documents the collection should be limited to. + * + * @return {@link Optional#empty()} if not set. + */ + public Optional getMaxDocuments() { + return Optional.ofNullable(maxDocuments); + } + + /** + * Get the {@literal size} in bytes the collection should be limited to. + * + * @return {@link Optional#empty()} if not set. + */ + public Optional getSize() { + return Optional.ofNullable(size); + } + + /** + * Get if the collection should be capped. + * + * @return {@link Optional#empty()} if not set. + * @since 2.0 + */ + public Optional getCapped() { + return Optional.ofNullable(capped); + } + + /** + * Get the {@link Collation} settings. + * + * @return {@link Optional#empty()} if not set. + * @since 2.0 + */ + public Optional getCollation() { + return Optional.ofNullable(collation); + } +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DbCallback.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DbCallback.java index 54e85d9bd..247cb9665 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DbCallback.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DbCallback.java @@ -1,44 +1,44 @@ -/* - * Copyright 2010-2017 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.mongodb.core; - -import org.springframework.dao.DataAccessException; -import org.springframework.lang.Nullable; - -import com.mongodb.MongoException; -import com.mongodb.client.MongoDatabase; - -/** - * Callback interface for executing actions against a {@link MongoDatabase}. - * - * @author Mark Pollak - * @author Graeme Rocher - * @author Thomas Risberg - * @author Oliver Gierke - * @author John Brisbin - * @author Christoph Strobl - */ -public interface DbCallback { - - /** - * @param db must not be {@literal null}. - * @return can be {@literal null}. - * @throws MongoException - * @throws DataAccessException - */ - @Nullable - T doInDB(MongoDatabase db) throws MongoException, DataAccessException; -} +/* + * Copyright 2010-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.core; + +import org.springframework.dao.DataAccessException; +import org.springframework.lang.Nullable; + +import com.mongodb.MongoException; +import com.mongodb.client.MongoDatabase; + +/** + * Callback interface for executing actions against a {@link MongoDatabase}. + * + * @author Mark Pollak + * @author Graeme Rocher + * @author Thomas Risberg + * @author Oliver Gierke + * @author John Brisbin + * @author Christoph Strobl + */ +public interface DbCallback { + + /** + * @param db must not be {@literal null}. + * @return can be {@literal null}. + * @throws MongoException + * @throws DataAccessException + */ + @Nullable + T doInDB(MongoDatabase db) throws MongoException, DataAccessException; +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoAdmin.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoAdmin.java index 497398ca7..632f50f02 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoAdmin.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoAdmin.java @@ -1,78 +1,78 @@ -/* - * Copyright 2011-2017 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.mongodb.core; - -import org.bson.Document; -import org.springframework.jmx.export.annotation.ManagedOperation; -import org.springframework.jmx.export.annotation.ManagedResource; -import org.springframework.util.Assert; - -import com.mongodb.MongoClient; -import com.mongodb.client.MongoDatabase; - -/** - * Mongo server administration exposed via JMX annotations - * - * @author Mark Pollack - * @author Thomas Darimont - * @author Mark Paluch - * @author Christoph Strobl - */ -@ManagedResource(description = "Mongo Admin Operations") -public class MongoAdmin implements MongoAdminOperations { - - private final MongoClient mongoClient; - - public MongoAdmin(MongoClient mongoClient) { - - Assert.notNull(mongoClient, "MongoClient must not be null!"); - this.mongoClient = mongoClient; - } - - /* (non-Javadoc) - * @see org.springframework.data.mongodb.core.core.MongoAdminOperations#dropDatabase(java.lang.String) - */ - @ManagedOperation - public void dropDatabase(String databaseName) { - getDB(databaseName).drop(); - } - - /* (non-Javadoc) - * @see org.springframework.data.mongodb.core.core.MongoAdminOperations#createDatabase(java.lang.String) - */ - @ManagedOperation - public void createDatabase(String databaseName) { - getDB(databaseName); - } - - /* (non-Javadoc) - * @see org.springframework.data.mongodb.core.core.MongoAdminOperations#getDatabaseStats(java.lang.String) - */ - @ManagedOperation - public String getDatabaseStats(String databaseName) { - return getDB(databaseName).runCommand(new Document("dbStats", 1).append("scale", 1024)).toJson(); - } - - @ManagedOperation - public String getServerStatus() { - return getDB("admin").runCommand(new Document("serverStatus", 1).append("rangeDeleter", 1).append("repl", 1)) - .toJson(); - } - - MongoDatabase getDB(String databaseName) { - return mongoClient.getDatabase(databaseName); - } -} +/* + * Copyright 2011-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.core; + +import org.bson.Document; +import org.springframework.jmx.export.annotation.ManagedOperation; +import org.springframework.jmx.export.annotation.ManagedResource; +import org.springframework.util.Assert; + +import com.mongodb.MongoClient; +import com.mongodb.client.MongoDatabase; + +/** + * Mongo server administration exposed via JMX annotations + * + * @author Mark Pollack + * @author Thomas Darimont + * @author Mark Paluch + * @author Christoph Strobl + */ +@ManagedResource(description = "Mongo Admin Operations") +public class MongoAdmin implements MongoAdminOperations { + + private final MongoClient mongoClient; + + public MongoAdmin(MongoClient mongoClient) { + + Assert.notNull(mongoClient, "MongoClient must not be null!"); + this.mongoClient = mongoClient; + } + + /* (non-Javadoc) + * @see org.springframework.data.mongodb.core.core.MongoAdminOperations#dropDatabase(java.lang.String) + */ + @ManagedOperation + public void dropDatabase(String databaseName) { + getDB(databaseName).drop(); + } + + /* (non-Javadoc) + * @see org.springframework.data.mongodb.core.core.MongoAdminOperations#createDatabase(java.lang.String) + */ + @ManagedOperation + public void createDatabase(String databaseName) { + getDB(databaseName); + } + + /* (non-Javadoc) + * @see org.springframework.data.mongodb.core.core.MongoAdminOperations#getDatabaseStats(java.lang.String) + */ + @ManagedOperation + public String getDatabaseStats(String databaseName) { + return getDB(databaseName).runCommand(new Document("dbStats", 1).append("scale", 1024)).toJson(); + } + + @ManagedOperation + public String getServerStatus() { + return getDB("admin").runCommand(new Document("serverStatus", 1).append("rangeDeleter", 1).append("repl", 1)) + .toJson(); + } + + MongoDatabase getDB(String databaseName) { + return mongoClient.getDatabase(databaseName); + } +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoAdminOperations.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoAdminOperations.java index 293ef14c5..cceae298b 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoAdminOperations.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoAdminOperations.java @@ -1,34 +1,34 @@ -/* - * Copyright 2011-2014 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.mongodb.core; - -import org.springframework.jmx.export.annotation.ManagedOperation; - -/** - * @author Mark Pollack - * @author Oliver Gierke - */ -public interface MongoAdminOperations { - - @ManagedOperation - void dropDatabase(String databaseName); - - @ManagedOperation - void createDatabase(String databaseName); - - @ManagedOperation - String getDatabaseStats(String databaseName); -} +/* + * Copyright 2011-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.core; + +import org.springframework.jmx.export.annotation.ManagedOperation; + +/** + * @author Mark Pollack + * @author Oliver Gierke + */ +public interface MongoAdminOperations { + + @ManagedOperation + void dropDatabase(String databaseName); + + @ManagedOperation + void createDatabase(String databaseName); + + @ManagedOperation + String getDatabaseStats(String databaseName); +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveCollectionCallback.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveCollectionCallback.java index edbbc23fc..8d0b76d66 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveCollectionCallback.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveCollectionCallback.java @@ -1,34 +1,34 @@ -/* - * Copyright 2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.mongodb.core; - -import org.springframework.dao.DataAccessException; - -import com.mongodb.MongoException; -import com.mongodb.reactivestreams.client.MongoCollection; -import org.bson.Document; -import org.reactivestreams.Publisher; - -/** - * @author Mark Paluch - * @param - * @since 2.0 - */ -public interface ReactiveCollectionCallback { - - Publisher doInCollection(MongoCollection collection) throws MongoException, DataAccessException; - -} +/* + * Copyright 2016-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.core; + +import org.bson.Document; +import org.reactivestreams.Publisher; +import org.springframework.dao.DataAccessException; + +import com.mongodb.MongoException; +import com.mongodb.reactivestreams.client.MongoCollection; + +/** + * @author Mark Paluch + * @param + * @since 2.0 + */ +public interface ReactiveCollectionCallback { + + Publisher doInCollection(MongoCollection collection) throws MongoException, DataAccessException; + +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/SimpleMongoDbFactory.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/SimpleMongoDbFactory.java index cbef75e4a..224be2270 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/SimpleMongoDbFactory.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/SimpleMongoDbFactory.java @@ -1,150 +1,150 @@ -/* - * Copyright 2011-2017 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.mongodb.core; - -import java.net.UnknownHostException; - -import org.springframework.beans.factory.DisposableBean; -import org.springframework.dao.DataAccessException; -import org.springframework.dao.support.PersistenceExceptionTranslator; -import org.springframework.data.mongodb.MongoDbFactory; -import org.springframework.lang.Nullable; -import org.springframework.util.Assert; - -import com.mongodb.DB; -import com.mongodb.MongoClient; -import com.mongodb.MongoClientURI; -import com.mongodb.WriteConcern; -import com.mongodb.client.MongoDatabase; - -/** - * Factory to create {@link DB} instances from a {@link MongoClient} instance. - * - * @author Mark Pollack - * @author Oliver Gierke - * @author Thomas Darimont - * @author Christoph Strobl - */ -public class SimpleMongoDbFactory implements DisposableBean, MongoDbFactory { - - private final MongoClient mongoClient; - private final String databaseName; - private final boolean mongoInstanceCreated; - private final PersistenceExceptionTranslator exceptionTranslator; - - private @Nullable WriteConcern writeConcern; - - /** - * Creates a new {@link SimpleMongoDbFactory} instance from the given {@link MongoClientURI}. - * - * @param uri must not be {@literal null}. - * @throws UnknownHostException - * @since 1.7 - */ - public SimpleMongoDbFactory(MongoClientURI uri) { - this(new MongoClient(uri), uri.getDatabase(), true); - } - - /** - * Creates a new {@link SimpleMongoDbFactory} instance from the given {@link MongoClient}. - * - * @param mongoClient must not be {@literal null}. - * @param databaseName must not be {@literal null}. - * @since 1.7 - */ - public SimpleMongoDbFactory(MongoClient mongoClient, String databaseName) { - this(mongoClient, databaseName, false); - } - - /** - * @param client - * @param databaseName - * @param mongoInstanceCreated - * @since 1.7 - */ - private SimpleMongoDbFactory(MongoClient mongoClient, String databaseName, boolean mongoInstanceCreated) { - - Assert.notNull(mongoClient, "MongoClient must not be null!"); - Assert.hasText(databaseName, "Database name must not be empty!"); - Assert.isTrue(databaseName.matches("[\\w-]+"), - "Database name must only contain letters, numbers, underscores and dashes!"); - - this.mongoClient = mongoClient; - this.databaseName = databaseName; - this.mongoInstanceCreated = mongoInstanceCreated; - this.exceptionTranslator = new MongoExceptionTranslator(); - } - - /** - * Configures the {@link WriteConcern} to be used on the {@link DB} instance being created. - * - * @param writeConcern the writeConcern to set - */ - public void setWriteConcern(WriteConcern writeConcern) { - this.writeConcern = writeConcern; - } - - /* - * (non-Javadoc) - * @see org.springframework.data.mongodb.MongoDbFactory#getDb() - */ - public MongoDatabase getDb() throws DataAccessException { - return getDb(databaseName); - } - - /* - * (non-Javadoc) - * @see org.springframework.data.mongodb.MongoDbFactory#getDb(java.lang.String) - */ - public MongoDatabase getDb(String dbName) throws DataAccessException { - - Assert.hasText(dbName, "Database name must not be empty."); - - MongoDatabase db = mongoClient.getDatabase(dbName); - - if (writeConcern == null) { - return db; - } - - return db.withWriteConcern(writeConcern); - } - - /** - * Clean up the Mongo instance if it was created by the factory itself. - * - * @see DisposableBean#destroy() - */ - public void destroy() throws Exception { - if (mongoInstanceCreated) { - mongoClient.close(); - } - } - - /* - * (non-Javadoc) - * @see org.springframework.data.mongodb.MongoDbFactory#getExceptionTranslator() - */ - @Override - public PersistenceExceptionTranslator getExceptionTranslator() { - return this.exceptionTranslator; - } - - @SuppressWarnings("deprecation") - @Override - public DB getLegacyDb() { - return mongoClient.getDB(databaseName); - } -} +/* + * Copyright 2011-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.core; + +import java.net.UnknownHostException; + +import org.springframework.beans.factory.DisposableBean; +import org.springframework.dao.DataAccessException; +import org.springframework.dao.support.PersistenceExceptionTranslator; +import org.springframework.data.mongodb.MongoDbFactory; +import org.springframework.lang.Nullable; +import org.springframework.util.Assert; + +import com.mongodb.DB; +import com.mongodb.MongoClient; +import com.mongodb.MongoClientURI; +import com.mongodb.WriteConcern; +import com.mongodb.client.MongoDatabase; + +/** + * Factory to create {@link DB} instances from a {@link MongoClient} instance. + * + * @author Mark Pollack + * @author Oliver Gierke + * @author Thomas Darimont + * @author Christoph Strobl + */ +public class SimpleMongoDbFactory implements DisposableBean, MongoDbFactory { + + private final MongoClient mongoClient; + private final String databaseName; + private final boolean mongoInstanceCreated; + private final PersistenceExceptionTranslator exceptionTranslator; + + private @Nullable WriteConcern writeConcern; + + /** + * Creates a new {@link SimpleMongoDbFactory} instance from the given {@link MongoClientURI}. + * + * @param uri must not be {@literal null}. + * @throws UnknownHostException + * @since 1.7 + */ + public SimpleMongoDbFactory(MongoClientURI uri) { + this(new MongoClient(uri), uri.getDatabase(), true); + } + + /** + * Creates a new {@link SimpleMongoDbFactory} instance from the given {@link MongoClient}. + * + * @param mongoClient must not be {@literal null}. + * @param databaseName must not be {@literal null}. + * @since 1.7 + */ + public SimpleMongoDbFactory(MongoClient mongoClient, String databaseName) { + this(mongoClient, databaseName, false); + } + + /** + * @param client + * @param databaseName + * @param mongoInstanceCreated + * @since 1.7 + */ + private SimpleMongoDbFactory(MongoClient mongoClient, String databaseName, boolean mongoInstanceCreated) { + + Assert.notNull(mongoClient, "MongoClient must not be null!"); + Assert.hasText(databaseName, "Database name must not be empty!"); + Assert.isTrue(databaseName.matches("[\\w-]+"), + "Database name must only contain letters, numbers, underscores and dashes!"); + + this.mongoClient = mongoClient; + this.databaseName = databaseName; + this.mongoInstanceCreated = mongoInstanceCreated; + this.exceptionTranslator = new MongoExceptionTranslator(); + } + + /** + * Configures the {@link WriteConcern} to be used on the {@link DB} instance being created. + * + * @param writeConcern the writeConcern to set + */ + public void setWriteConcern(WriteConcern writeConcern) { + this.writeConcern = writeConcern; + } + + /* + * (non-Javadoc) + * @see org.springframework.data.mongodb.MongoDbFactory#getDb() + */ + public MongoDatabase getDb() throws DataAccessException { + return getDb(databaseName); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.mongodb.MongoDbFactory#getDb(java.lang.String) + */ + public MongoDatabase getDb(String dbName) throws DataAccessException { + + Assert.hasText(dbName, "Database name must not be empty."); + + MongoDatabase db = mongoClient.getDatabase(dbName); + + if (writeConcern == null) { + return db; + } + + return db.withWriteConcern(writeConcern); + } + + /** + * Clean up the Mongo instance if it was created by the factory itself. + * + * @see DisposableBean#destroy() + */ + public void destroy() throws Exception { + if (mongoInstanceCreated) { + mongoClient.close(); + } + } + + /* + * (non-Javadoc) + * @see org.springframework.data.mongodb.MongoDbFactory#getExceptionTranslator() + */ + @Override + public PersistenceExceptionTranslator getExceptionTranslator() { + return this.exceptionTranslator; + } + + @SuppressWarnings("deprecation") + @Override + public DB getLegacyDb() { + return mongoClient.getDB(databaseName); + } +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoConverter.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoConverter.java index 6867e4f4b..36d4ae5dc 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoConverter.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoConverter.java @@ -1,44 +1,44 @@ -/* - * Copyright 2010-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.mongodb.core.convert; - -import org.bson.Document; -import org.bson.conversions.Bson; -import org.springframework.data.convert.EntityConverter; -import org.springframework.data.convert.EntityReader; -import org.springframework.data.convert.TypeMapper; -import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; -import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; - -/** - * Central Mongo specific converter interface which combines {@link MongoWriter} and {@link MongoReader}. - * - * @author Oliver Gierke - * @author Thomas Darimont - * @author Christoph Strobl - */ -public interface MongoConverter - extends EntityConverter, MongoPersistentProperty, Object, Bson>, MongoWriter, - EntityReader { - - /** - * Returns thw {@link TypeMapper} being used to write type information into {@link Document}s created with that - * converter. - * - * @return will never be {@literal null}. - */ - MongoTypeMapper getTypeMapper(); -} +/* + * Copyright 2010-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.core.convert; + +import org.bson.Document; +import org.bson.conversions.Bson; +import org.springframework.data.convert.EntityConverter; +import org.springframework.data.convert.EntityReader; +import org.springframework.data.convert.TypeMapper; +import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; +import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; + +/** + * Central Mongo specific converter interface which combines {@link MongoWriter} and {@link MongoReader}. + * + * @author Oliver Gierke + * @author Thomas Darimont + * @author Christoph Strobl + */ +public interface MongoConverter + extends EntityConverter, MongoPersistentProperty, Object, Bson>, MongoWriter, + EntityReader { + + /** + * Returns thw {@link TypeMapper} being used to write type information into {@link Document}s created with that + * converter. + * + * @return will never be {@literal null}. + */ + MongoTypeMapper getTypeMapper(); +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoWriter.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoWriter.java index 39a31dec0..dd1456a7f 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoWriter.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoWriter.java @@ -1,69 +1,69 @@ -/* - * Copyright 2010-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.mongodb.core.convert; - -import org.bson.conversions.Bson; -import org.springframework.data.convert.EntityWriter; -import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; -import org.springframework.data.util.TypeInformation; -import org.springframework.lang.Nullable; - -import com.mongodb.DBRef; - -/** - * A MongoWriter is responsible for converting an object of type T to the native MongoDB representation Document. - * - * @param the type of the object to convert to a Document - * @author Mark Pollack - * @author Thomas Risberg - * @author Oliver Gierke - * @author Christoph Strobl - */ -public interface MongoWriter extends EntityWriter { - - /** - * Converts the given object into one Mongo will be able to store natively. If the given object can already be stored - * as is, no conversion will happen. - * - * @param obj can be {@literal null}. - * @return - */ - @Nullable - default Object convertToMongoType(@Nullable Object obj) { - return convertToMongoType(obj, null); - } - - /** - * Converts the given object into one Mongo will be able to store natively but retains the type information in case - * the given {@link TypeInformation} differs from the given object type. - * - * @param obj can be {@literal null}. - * @param typeInformation can be {@literal null}. - * @return - */ - @Nullable - Object convertToMongoType(@Nullable Object obj, @Nullable TypeInformation typeInformation); - - /** - * Creates a {@link DBRef} to refer to the given object. - * - * @param object the object to create a {@link DBRef} to link to. The object's type has to carry an id attribute. - * @param referingProperty the client-side property referring to the object which might carry additional metadata for - * the {@link DBRef} object to create. Can be {@literal null}. - * @return will never be {@literal null}. - */ - DBRef toDBRef(Object object, @Nullable MongoPersistentProperty referingProperty); -} +/* + * Copyright 2010-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.core.convert; + +import org.bson.conversions.Bson; +import org.springframework.data.convert.EntityWriter; +import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; +import org.springframework.data.util.TypeInformation; +import org.springframework.lang.Nullable; + +import com.mongodb.DBRef; + +/** + * A MongoWriter is responsible for converting an object of type T to the native MongoDB representation Document. + * + * @param the type of the object to convert to a Document + * @author Mark Pollack + * @author Thomas Risberg + * @author Oliver Gierke + * @author Christoph Strobl + */ +public interface MongoWriter extends EntityWriter { + + /** + * Converts the given object into one Mongo will be able to store natively. If the given object can already be stored + * as is, no conversion will happen. + * + * @param obj can be {@literal null}. + * @return + */ + @Nullable + default Object convertToMongoType(@Nullable Object obj) { + return convertToMongoType(obj, null); + } + + /** + * Converts the given object into one Mongo will be able to store natively but retains the type information in case + * the given {@link TypeInformation} differs from the given object type. + * + * @param obj can be {@literal null}. + * @param typeInformation can be {@literal null}. + * @return + */ + @Nullable + Object convertToMongoType(@Nullable Object obj, @Nullable TypeInformation typeInformation); + + /** + * Creates a {@link DBRef} to refer to the given object. + * + * @param object the object to create a {@link DBRef} to link to. The object's type has to carry an id attribute. + * @param referingProperty the client-side property referring to the object which might carry additional metadata for + * the {@link DBRef} object to create. Can be {@literal null}. + * @return will never be {@literal null}. + */ + DBRef toDBRef(Object object, @Nullable MongoPersistentProperty referingProperty); +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/AbstractMonitor.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/AbstractMonitor.java index 68462dd68..691e4d516 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/AbstractMonitor.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/AbstractMonitor.java @@ -1,49 +1,49 @@ -/* - * Copyright 2002-2017 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.mongodb.monitor; - -import org.bson.Document; - -import com.mongodb.MongoClient; -import com.mongodb.client.MongoDatabase; - -/** - * Base class to encapsulate common configuration settings when connecting to a database - * - * @author Mark Pollack - * @author Oliver Gierke - * @author Christoph Strobl - */ -public abstract class AbstractMonitor { - - private final MongoClient mongoClient; - - protected AbstractMonitor(MongoClient mongoClient) { - this.mongoClient = mongoClient; - } - - public Document getServerStatus() { - return getDb("admin").runCommand(new Document("serverStatus", 1).append("rangeDeleter", 1).append("repl", 1)); - } - - public MongoDatabase getDb(String databaseName) { - return mongoClient.getDatabase(databaseName); - } - - protected MongoClient getMongoClient() { - return mongoClient; - } -} +/* + * Copyright 2002-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.monitor; + +import org.bson.Document; + +import com.mongodb.MongoClient; +import com.mongodb.client.MongoDatabase; + +/** + * Base class to encapsulate common configuration settings when connecting to a database + * + * @author Mark Pollack + * @author Oliver Gierke + * @author Christoph Strobl + */ +public abstract class AbstractMonitor { + + private final MongoClient mongoClient; + + protected AbstractMonitor(MongoClient mongoClient) { + this.mongoClient = mongoClient; + } + + public Document getServerStatus() { + return getDb("admin").runCommand(new Document("serverStatus", 1).append("rangeDeleter", 1).append("repl", 1)); + } + + public MongoDatabase getDb(String databaseName) { + return mongoClient.getDatabase(databaseName); + } + + protected MongoClient getMongoClient() { + return mongoClient; + } +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/AssertMetrics.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/AssertMetrics.java index 32cec841d..e15889117 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/AssertMetrics.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/AssertMetrics.java @@ -1,70 +1,70 @@ -/* - * Copyright 2002-2017 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.mongodb.monitor; - -import com.mongodb.MongoClient; -import org.bson.Document; -import org.springframework.jmx.export.annotation.ManagedMetric; -import org.springframework.jmx.export.annotation.ManagedResource; -import org.springframework.jmx.support.MetricType; - -import com.mongodb.DBObject; -import com.mongodb.Mongo; - -/** - * JMX Metrics for assertions - * - * @author Mark Pollack - */ -@ManagedResource(description = "Assertion Metrics") -public class AssertMetrics extends AbstractMonitor { - - public AssertMetrics(MongoClient mongoClient) { - super(mongoClient); - } - - @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Regular") - public int getRegular() { - return getBtree("regular"); - } - - @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Warning") - public int getWarning() { - return getBtree("warning"); - } - - @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Msg") - public int getMsg() { - return getBtree("msg"); - } - - @ManagedMetric(metricType = MetricType.COUNTER, displayName = "User") - public int getUser() { - return getBtree("user"); - } - - @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Rollovers") - public int getRollovers() { - return getBtree("rollovers"); - } - - private int getBtree(String key) { - Document asserts = (Document) getServerStatus().get("asserts"); - // Class c = btree.get(key).getClass(); - return (Integer) asserts.get(key); - } - -} +/* + * Copyright 2002-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.monitor; + +import com.mongodb.MongoClient; +import org.bson.Document; +import org.springframework.jmx.export.annotation.ManagedMetric; +import org.springframework.jmx.export.annotation.ManagedResource; +import org.springframework.jmx.support.MetricType; + +import com.mongodb.DBObject; +import com.mongodb.Mongo; + +/** + * JMX Metrics for assertions + * + * @author Mark Pollack + */ +@ManagedResource(description = "Assertion Metrics") +public class AssertMetrics extends AbstractMonitor { + + public AssertMetrics(MongoClient mongoClient) { + super(mongoClient); + } + + @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Regular") + public int getRegular() { + return getBtree("regular"); + } + + @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Warning") + public int getWarning() { + return getBtree("warning"); + } + + @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Msg") + public int getMsg() { + return getBtree("msg"); + } + + @ManagedMetric(metricType = MetricType.COUNTER, displayName = "User") + public int getUser() { + return getBtree("user"); + } + + @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Rollovers") + public int getRollovers() { + return getBtree("rollovers"); + } + + private int getBtree(String key) { + Document asserts = (Document) getServerStatus().get("asserts"); + // Class c = btree.get(key).getClass(); + return (Integer) asserts.get(key); + } + +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/BackgroundFlushingMetrics.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/BackgroundFlushingMetrics.java index 6c6435018..7947b79ce 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/BackgroundFlushingMetrics.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/BackgroundFlushingMetrics.java @@ -1,76 +1,76 @@ -/* - * Copyright 2002-2017 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.mongodb.monitor; - -import java.util.Date; - -import org.bson.Document; -import org.springframework.jmx.export.annotation.ManagedMetric; -import org.springframework.jmx.export.annotation.ManagedResource; -import org.springframework.jmx.support.MetricType; - -import com.mongodb.MongoClient; - -/** - * JMX Metrics for Background Flushing - * - * @author Mark Pollack - */ -@ManagedResource(description = "Background Flushing Metrics") -public class BackgroundFlushingMetrics extends AbstractMonitor { - - public BackgroundFlushingMetrics(MongoClient mongoClient) { - super(mongoClient); - } - - @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Flushes") - public int getFlushes() { - return getFlushingData("flushes", java.lang.Integer.class); - } - - @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Total ms", unit = "ms") - public int getTotalMs() { - return getFlushingData("total_ms", java.lang.Integer.class); - } - - @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Average ms", unit = "ms") - public double getAverageMs() { - return getFlushingData("average_ms", java.lang.Double.class); - } - - @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Last Ms", unit = "ms") - public int getLastMs() { - return getFlushingData("last_ms", java.lang.Integer.class); - } - - @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Last finished") - public Date getLastFinished() { - return getLast(); - } - - @SuppressWarnings("unchecked") - private T getFlushingData(String key, Class targetClass) { - Document mem = (Document) getServerStatus().get("backgroundFlushing"); - return (T) mem.get(key); - } - - private Date getLast() { - Document bgFlush = (Document) getServerStatus().get("backgroundFlushing"); - Date lastFinished = (Date) bgFlush.get("last_finished"); - return lastFinished; - } - -} +/* + * Copyright 2002-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.monitor; + +import java.util.Date; + +import org.bson.Document; +import org.springframework.jmx.export.annotation.ManagedMetric; +import org.springframework.jmx.export.annotation.ManagedResource; +import org.springframework.jmx.support.MetricType; + +import com.mongodb.MongoClient; + +/** + * JMX Metrics for Background Flushing + * + * @author Mark Pollack + */ +@ManagedResource(description = "Background Flushing Metrics") +public class BackgroundFlushingMetrics extends AbstractMonitor { + + public BackgroundFlushingMetrics(MongoClient mongoClient) { + super(mongoClient); + } + + @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Flushes") + public int getFlushes() { + return getFlushingData("flushes", java.lang.Integer.class); + } + + @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Total ms", unit = "ms") + public int getTotalMs() { + return getFlushingData("total_ms", java.lang.Integer.class); + } + + @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Average ms", unit = "ms") + public double getAverageMs() { + return getFlushingData("average_ms", java.lang.Double.class); + } + + @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Last Ms", unit = "ms") + public int getLastMs() { + return getFlushingData("last_ms", java.lang.Integer.class); + } + + @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Last finished") + public Date getLastFinished() { + return getLast(); + } + + @SuppressWarnings("unchecked") + private T getFlushingData(String key, Class targetClass) { + Document mem = (Document) getServerStatus().get("backgroundFlushing"); + return (T) mem.get(key); + } + + private Date getLast() { + Document bgFlush = (Document) getServerStatus().get("backgroundFlushing"); + Date lastFinished = (Date) bgFlush.get("last_finished"); + return lastFinished; + } + +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/BtreeIndexCounters.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/BtreeIndexCounters.java index 2f4068222..8ca5f0188 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/BtreeIndexCounters.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/BtreeIndexCounters.java @@ -1,75 +1,75 @@ -/* - * Copyright 2002-2017 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.mongodb.monitor; - -import org.bson.Document; -import org.springframework.jmx.export.annotation.ManagedMetric; -import org.springframework.jmx.export.annotation.ManagedResource; -import org.springframework.jmx.support.MetricType; - -import com.mongodb.MongoClient; - -/** - * JMX Metrics for B-tree index counters - * - * @author Mark Pollack - */ -@ManagedResource(description = "Btree Metrics") -public class BtreeIndexCounters extends AbstractMonitor { - - public BtreeIndexCounters(MongoClient mongoClient) { - super(mongoClient); - } - - @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Accesses") - public int getAccesses() { - return getBtree("accesses"); - } - - @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Hits") - public int getHits() { - return getBtree("hits"); - } - - @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Misses") - public int getMisses() { - return getBtree("misses"); - } - - @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Resets") - public int getResets() { - return getBtree("resets"); - } - - @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Miss Ratio") - public int getMissRatio() { - return getBtree("missRatio"); - } - - private int getBtree(String key) { - Document indexCounters = (Document) getServerStatus().get("indexCounters"); - if (indexCounters.get("note") != null) { - String message = (String) indexCounters.get("note"); - if (message.contains("not supported")) { - return -1; - } - } - Document btree = (Document) indexCounters.get("btree"); - // Class c = btree.get(key).getClass(); - return (Integer) btree.get(key); - } - -} +/* + * Copyright 2002-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.monitor; + +import org.bson.Document; +import org.springframework.jmx.export.annotation.ManagedMetric; +import org.springframework.jmx.export.annotation.ManagedResource; +import org.springframework.jmx.support.MetricType; + +import com.mongodb.MongoClient; + +/** + * JMX Metrics for B-tree index counters + * + * @author Mark Pollack + */ +@ManagedResource(description = "Btree Metrics") +public class BtreeIndexCounters extends AbstractMonitor { + + public BtreeIndexCounters(MongoClient mongoClient) { + super(mongoClient); + } + + @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Accesses") + public int getAccesses() { + return getBtree("accesses"); + } + + @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Hits") + public int getHits() { + return getBtree("hits"); + } + + @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Misses") + public int getMisses() { + return getBtree("misses"); + } + + @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Resets") + public int getResets() { + return getBtree("resets"); + } + + @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Miss Ratio") + public int getMissRatio() { + return getBtree("missRatio"); + } + + private int getBtree(String key) { + Document indexCounters = (Document) getServerStatus().get("indexCounters"); + if (indexCounters.get("note") != null) { + String message = (String) indexCounters.get("note"); + if (message.contains("not supported")) { + return -1; + } + } + Document btree = (Document) indexCounters.get("btree"); + // Class c = btree.get(key).getClass(); + return (Integer) btree.get(key); + } + +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/ConnectionMetrics.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/ConnectionMetrics.java index 6f7153375..65d512066 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/ConnectionMetrics.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/ConnectionMetrics.java @@ -1,54 +1,54 @@ -/* - * Copyright 2002-2017 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.mongodb.monitor; - -import org.bson.Document; -import org.springframework.jmx.export.annotation.ManagedMetric; -import org.springframework.jmx.export.annotation.ManagedResource; -import org.springframework.jmx.support.MetricType; - -import com.mongodb.MongoClient; - -/** - * JMX Metrics for Connections - * - * @author Mark Pollack - */ -@ManagedResource(description = "Connection metrics") -public class ConnectionMetrics extends AbstractMonitor { - - public ConnectionMetrics(MongoClient mongoClient) { - super(mongoClient); - } - - @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Current Connections") - public int getCurrent() { - return getConnectionData("current", java.lang.Integer.class); - } - - @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Available Connections") - public int getAvailable() { - return getConnectionData("available", java.lang.Integer.class); - } - - @SuppressWarnings("unchecked") - private T getConnectionData(String key, Class targetClass) { - Document mem = (Document) getServerStatus().get("connections"); - // Class c = mem.get(key).getClass(); - return (T) mem.get(key); - } - -} +/* + * Copyright 2002-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.monitor; + +import org.bson.Document; +import org.springframework.jmx.export.annotation.ManagedMetric; +import org.springframework.jmx.export.annotation.ManagedResource; +import org.springframework.jmx.support.MetricType; + +import com.mongodb.MongoClient; + +/** + * JMX Metrics for Connections + * + * @author Mark Pollack + */ +@ManagedResource(description = "Connection metrics") +public class ConnectionMetrics extends AbstractMonitor { + + public ConnectionMetrics(MongoClient mongoClient) { + super(mongoClient); + } + + @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Current Connections") + public int getCurrent() { + return getConnectionData("current", java.lang.Integer.class); + } + + @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Available Connections") + public int getAvailable() { + return getConnectionData("available", java.lang.Integer.class); + } + + @SuppressWarnings("unchecked") + private T getConnectionData(String key, Class targetClass) { + Document mem = (Document) getServerStatus().get("connections"); + // Class c = mem.get(key).getClass(); + return (T) mem.get(key); + } + +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/GlobalLockMetrics.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/GlobalLockMetrics.java index 0ca8a5742..022af2fbb 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/GlobalLockMetrics.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/GlobalLockMetrics.java @@ -1,79 +1,79 @@ -/* - * Copyright 2002-2017 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.mongodb.monitor; - -import org.bson.Document; -import org.springframework.jmx.export.annotation.ManagedMetric; -import org.springframework.jmx.export.annotation.ManagedResource; -import org.springframework.jmx.support.MetricType; - -import com.mongodb.DBObject; -import com.mongodb.MongoClient; - -/** - * JMX Metrics for Global Locks - * - * @author Mark Pollack - */ -@ManagedResource(description = "Global Lock Metrics") -public class GlobalLockMetrics extends AbstractMonitor { - - public GlobalLockMetrics(MongoClient mongoClient) { - super(mongoClient); - } - - @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Total time") - public double getTotalTime() { - return getGlobalLockData("totalTime", java.lang.Double.class); - } - - @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Lock time", unit = "s") - public double getLockTime() { - return getGlobalLockData("lockTime", java.lang.Double.class); - } - - @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Lock time") - public double getLockTimeRatio() { - return getGlobalLockData("ratio", java.lang.Double.class); - } - - @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Current Queue") - public int getCurrentQueueTotal() { - return getCurrentQueue("total"); - } - - @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Reader Queue") - public int getCurrentQueueReaders() { - return getCurrentQueue("readers"); - } - - @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Writer Queue") - public int getCurrentQueueWriters() { - return getCurrentQueue("writers"); - } - - @SuppressWarnings("unchecked") - private T getGlobalLockData(String key, Class targetClass) { - DBObject globalLock = (DBObject) getServerStatus().get("globalLock"); - return (T) globalLock.get(key); - } - - private int getCurrentQueue(String key) { - Document globalLock = (Document) getServerStatus().get("globalLock"); - Document currentQueue = (Document) globalLock.get("currentQueue"); - return (Integer) currentQueue.get(key); - } -} +/* + * Copyright 2002-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.monitor; + +import org.bson.Document; +import org.springframework.jmx.export.annotation.ManagedMetric; +import org.springframework.jmx.export.annotation.ManagedResource; +import org.springframework.jmx.support.MetricType; + +import com.mongodb.DBObject; +import com.mongodb.MongoClient; + +/** + * JMX Metrics for Global Locks + * + * @author Mark Pollack + */ +@ManagedResource(description = "Global Lock Metrics") +public class GlobalLockMetrics extends AbstractMonitor { + + public GlobalLockMetrics(MongoClient mongoClient) { + super(mongoClient); + } + + @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Total time") + public double getTotalTime() { + return getGlobalLockData("totalTime", java.lang.Double.class); + } + + @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Lock time", unit = "s") + public double getLockTime() { + return getGlobalLockData("lockTime", java.lang.Double.class); + } + + @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Lock time") + public double getLockTimeRatio() { + return getGlobalLockData("ratio", java.lang.Double.class); + } + + @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Current Queue") + public int getCurrentQueueTotal() { + return getCurrentQueue("total"); + } + + @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Reader Queue") + public int getCurrentQueueReaders() { + return getCurrentQueue("readers"); + } + + @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Writer Queue") + public int getCurrentQueueWriters() { + return getCurrentQueue("writers"); + } + + @SuppressWarnings("unchecked") + private T getGlobalLockData(String key, Class targetClass) { + DBObject globalLock = (DBObject) getServerStatus().get("globalLock"); + return (T) globalLock.get(key); + } + + private int getCurrentQueue(String key) { + Document globalLock = (Document) getServerStatus().get("globalLock"); + Document currentQueue = (Document) globalLock.get("currentQueue"); + return (Integer) currentQueue.get(key); + } +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/MemoryMetrics.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/MemoryMetrics.java index 42a884fcd..d21e8c6dd 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/MemoryMetrics.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/MemoryMetrics.java @@ -1,69 +1,69 @@ -/* - * Copyright 2002-2011 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.mongodb.monitor; - -import org.bson.Document; -import org.springframework.jmx.export.annotation.ManagedMetric; -import org.springframework.jmx.export.annotation.ManagedResource; -import org.springframework.jmx.support.MetricType; - -import com.mongodb.MongoClient; - -/** - * JMX Metrics for Memory - * - * @author Mark Pollack - */ -@ManagedResource(description = "Memory Metrics") -public class MemoryMetrics extends AbstractMonitor { - - public MemoryMetrics(MongoClient mongoClient) { - super(mongoClient); - } - - @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Memory address size") - public int getBits() { - return getMemData("bits", java.lang.Integer.class); - } - - @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Resident in Physical Memory", unit = "MB") - public int getResidentSpace() { - return getMemData("resident", java.lang.Integer.class); - } - - @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Virtual Address Space", unit = "MB") - public int getVirtualAddressSpace() { - return getMemData("virtual", java.lang.Integer.class); - } - - @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Is memory info supported on this platform") - public boolean getMemoryInfoSupported() { - return getMemData("supported", java.lang.Boolean.class); - } - - @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Memory Mapped Space", unit = "MB") - public int getMemoryMappedSpace() { - return getMemData("mapped", java.lang.Integer.class); - } - - @SuppressWarnings("unchecked") - private T getMemData(String key, Class targetClass) { - Document mem = (Document) getServerStatus().get("mem"); - // Class c = mem.get(key).getClass(); - return (T) mem.get(key); - } - -} +/* + * Copyright 2002-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.monitor; + +import org.bson.Document; +import org.springframework.jmx.export.annotation.ManagedMetric; +import org.springframework.jmx.export.annotation.ManagedResource; +import org.springframework.jmx.support.MetricType; + +import com.mongodb.MongoClient; + +/** + * JMX Metrics for Memory + * + * @author Mark Pollack + */ +@ManagedResource(description = "Memory Metrics") +public class MemoryMetrics extends AbstractMonitor { + + public MemoryMetrics(MongoClient mongoClient) { + super(mongoClient); + } + + @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Memory address size") + public int getBits() { + return getMemData("bits", java.lang.Integer.class); + } + + @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Resident in Physical Memory", unit = "MB") + public int getResidentSpace() { + return getMemData("resident", java.lang.Integer.class); + } + + @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Virtual Address Space", unit = "MB") + public int getVirtualAddressSpace() { + return getMemData("virtual", java.lang.Integer.class); + } + + @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Is memory info supported on this platform") + public boolean getMemoryInfoSupported() { + return getMemData("supported", java.lang.Boolean.class); + } + + @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Memory Mapped Space", unit = "MB") + public int getMemoryMappedSpace() { + return getMemData("mapped", java.lang.Integer.class); + } + + @SuppressWarnings("unchecked") + private T getMemData(String key, Class targetClass) { + Document mem = (Document) getServerStatus().get("mem"); + // Class c = mem.get(key).getClass(); + return (T) mem.get(key); + } + +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/OperationCounters.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/OperationCounters.java index 49d48a162..e63a728ce 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/OperationCounters.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/OperationCounters.java @@ -1,71 +1,71 @@ -/* - * Copyright 2002-2011 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.mongodb.monitor; - -import org.bson.Document; -import org.springframework.jmx.export.annotation.ManagedMetric; -import org.springframework.jmx.export.annotation.ManagedResource; -import org.springframework.jmx.support.MetricType; - -import com.mongodb.MongoClient; - -/** - * JMX Metrics for Operation counters - * - * @author Mark Pollack - */ -@ManagedResource(description = "Operation Counters") -public class OperationCounters extends AbstractMonitor { - - public OperationCounters(MongoClient mongoClient) { - super(mongoClient); - } - - @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Insert operation count") - public int getInsertCount() { - return getOpCounter("insert"); - } - - @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Query operation count") - public int getQueryCount() { - return getOpCounter("query"); - } - - @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Update operation count") - public int getUpdateCount() { - return getOpCounter("update"); - } - - @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Delete operation count") - public int getDeleteCount() { - return getOpCounter("delete"); - } - - @ManagedMetric(metricType = MetricType.COUNTER, displayName = "GetMore operation count") - public int getGetMoreCount() { - return getOpCounter("getmore"); - } - - @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Command operation count") - public int getCommandCount() { - return getOpCounter("command"); - } - - private int getOpCounter(String key) { - Document opCounters = (Document) getServerStatus().get("opcounters"); - return (Integer) opCounters.get(key); - } -} +/* + * Copyright 2002-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.monitor; + +import org.bson.Document; +import org.springframework.jmx.export.annotation.ManagedMetric; +import org.springframework.jmx.export.annotation.ManagedResource; +import org.springframework.jmx.support.MetricType; + +import com.mongodb.MongoClient; + +/** + * JMX Metrics for Operation counters + * + * @author Mark Pollack + */ +@ManagedResource(description = "Operation Counters") +public class OperationCounters extends AbstractMonitor { + + public OperationCounters(MongoClient mongoClient) { + super(mongoClient); + } + + @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Insert operation count") + public int getInsertCount() { + return getOpCounter("insert"); + } + + @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Query operation count") + public int getQueryCount() { + return getOpCounter("query"); + } + + @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Update operation count") + public int getUpdateCount() { + return getOpCounter("update"); + } + + @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Delete operation count") + public int getDeleteCount() { + return getOpCounter("delete"); + } + + @ManagedMetric(metricType = MetricType.COUNTER, displayName = "GetMore operation count") + public int getGetMoreCount() { + return getOpCounter("getmore"); + } + + @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Command operation count") + public int getCommandCount() { + return getOpCounter("command"); + } + + private int getOpCounter(String key) { + Document opCounters = (Document) getServerStatus().get("opcounters"); + return (Integer) opCounters.get(key); + } +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/ServerInfo.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/ServerInfo.java index b531654f5..237fc8333 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/ServerInfo.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/monitor/ServerInfo.java @@ -1,76 +1,76 @@ -/* - * Copyright 2012-2015 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.mongodb.monitor; - -import java.net.UnknownHostException; - -import org.springframework.jmx.export.annotation.ManagedMetric; -import org.springframework.jmx.export.annotation.ManagedOperation; -import org.springframework.jmx.export.annotation.ManagedResource; -import org.springframework.jmx.support.MetricType; - -import com.mongodb.MongoClient; - -/** - * Expose basic server information via JMX - * - * @author Mark Pollack - * @author Thomas Darimont - * @author Christoph Strobl - */ -@ManagedResource(description = "Server Information") -public class ServerInfo extends AbstractMonitor { - - public ServerInfo(MongoClient mongoClient) { - super(mongoClient); - } - - /** - * Returns the hostname of the used server reported by MongoDB. - * - * @return the reported hostname can also be an IP address. - * @throws UnknownHostException - */ - @ManagedOperation(description = "Server host name") - public String getHostName() throws UnknownHostException { - - /* - * UnknownHostException is not necessary anymore, but clients could have - * called this method in a try..catch(UnknownHostException) already - */ - return getMongoClient().getAddress().getHost(); - } - - @ManagedMetric(displayName = "Uptime Estimate") - public double getUptimeEstimate() { - return (Double) getServerStatus().get("uptimeEstimate"); - } - - @ManagedOperation(description = "MongoDB Server Version") - public String getVersion() { - return (String) getServerStatus().get("version"); - } - - @ManagedOperation(description = "Local Time") - public String getLocalTime() { - return (String) getServerStatus().get("localTime"); - } - - @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Server uptime in seconds", unit = "seconds") - public double getUptime() { - return (Double) getServerStatus().get("uptime"); - } -} +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.monitor; + +import java.net.UnknownHostException; + +import org.springframework.jmx.export.annotation.ManagedMetric; +import org.springframework.jmx.export.annotation.ManagedOperation; +import org.springframework.jmx.export.annotation.ManagedResource; +import org.springframework.jmx.support.MetricType; + +import com.mongodb.MongoClient; + +/** + * Expose basic server information via JMX + * + * @author Mark Pollack + * @author Thomas Darimont + * @author Christoph Strobl + */ +@ManagedResource(description = "Server Information") +public class ServerInfo extends AbstractMonitor { + + public ServerInfo(MongoClient mongoClient) { + super(mongoClient); + } + + /** + * Returns the hostname of the used server reported by MongoDB. + * + * @return the reported hostname can also be an IP address. + * @throws UnknownHostException + */ + @ManagedOperation(description = "Server host name") + public String getHostName() throws UnknownHostException { + + /* + * UnknownHostException is not necessary anymore, but clients could have + * called this method in a try..catch(UnknownHostException) already + */ + return getMongoClient().getAddress().getHost(); + } + + @ManagedMetric(displayName = "Uptime Estimate") + public double getUptimeEstimate() { + return (Double) getServerStatus().get("uptimeEstimate"); + } + + @ManagedOperation(description = "MongoDB Server Version") + public String getVersion() { + return (String) getServerStatus().get("version"); + } + + @ManagedOperation(description = "Local Time") + public String getLocalTime() { + return (String) getServerStatus().get("localTime"); + } + + @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Server uptime in seconds", unit = "seconds") + public double getUptime() { + return (Double) getServerStatus().get("uptime"); + } +}