DATAMONGO-1762 - Fix line endings.

Convert line endings from CRLF to LF.
This commit is contained in:
Mark Paluch
2017-08-28 16:33:11 +02:00
parent 0be4d1345e
commit 3012bcd575
21 changed files with 1644 additions and 1644 deletions

View File

@@ -1,114 +1,114 @@
/* /*
* Copyright 2011-2017 the original author or authors. * Copyright 2011-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.mongodb.config; package org.springframework.data.mongodb.config;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory; import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.core.convert.DbRefResolver; import org.springframework.data.mongodb.core.convert.DbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver; import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter; import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import com.mongodb.MongoClient; import com.mongodb.MongoClient;
/** /**
* Base class for Spring Data MongoDB configuration using JavaConfig. * Base class for Spring Data MongoDB configuration using JavaConfig.
* *
* @author Mark Pollack * @author Mark Pollack
* @author Oliver Gierke * @author Oliver Gierke
* @author Thomas Darimont * @author Thomas Darimont
* @author Ryan Tenney * @author Ryan Tenney
* @author Christoph Strobl * @author Christoph Strobl
* @author Mark Paluch * @author Mark Paluch
* @see MongoConfigurationSupport * @see MongoConfigurationSupport
*/ */
@Configuration @Configuration
public abstract class public abstract class
AbstractMongoConfiguration extends MongoConfigurationSupport { AbstractMongoConfiguration extends MongoConfigurationSupport {
/** /**
* Return the {@link MongoClient} instance to connect to. Annotate with {@link Bean} in case you want to expose a * 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}. * {@link MongoClient} instance to the {@link org.springframework.context.ApplicationContext}.
* *
* @return * @return
*/ */
public abstract MongoClient mongoClient(); public abstract MongoClient mongoClient();
/** /**
* Creates a {@link MongoTemplate}. * Creates a {@link MongoTemplate}.
* *
* @return * @return
*/ */
@Bean @Bean
public MongoTemplate mongoTemplate() throws Exception { public MongoTemplate mongoTemplate() throws Exception {
return new MongoTemplate(mongoDbFactory(), mappingMongoConverter()); return new MongoTemplate(mongoDbFactory(), mappingMongoConverter());
} }
/** /**
* Creates a {@link SimpleMongoDbFactory} to be used by the {@link MongoTemplate}. Will use the {@link MongoClient} * Creates a {@link SimpleMongoDbFactory} to be used by the {@link MongoTemplate}. Will use the {@link MongoClient}
* instance configured in {@link #mongo()}. * instance configured in {@link #mongo()}.
* *
* @see #mongoClient() * @see #mongoClient()
* @see #mongoTemplate() * @see #mongoTemplate()
* @return * @return
*/ */
@Bean @Bean
public MongoDbFactory mongoDbFactory() { public MongoDbFactory mongoDbFactory() {
return new SimpleMongoDbFactory(mongoClient(), getDatabaseName()); return new SimpleMongoDbFactory(mongoClient(), getDatabaseName());
} }
/** /**
* Return the base package to scan for mapped {@link Document}s. Will return the package name of the configuration * 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 * 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 * {@link AbstractMongoConfiguration} the base package will be considered {@code com.acme} unless the method is
* overridden to implement alternate behavior. * overridden to implement alternate behavior.
* *
* @return the base package to scan for mapped {@link Document} classes or {@literal null} to not enable scanning for * @return the base package to scan for mapped {@link Document} classes or {@literal null} to not enable scanning for
* entities. * entities.
* @deprecated use {@link #getMappingBasePackages()} instead. * @deprecated use {@link #getMappingBasePackages()} instead.
*/ */
@Deprecated @Deprecated
@Nullable @Nullable
protected String getMappingBasePackage() { protected String getMappingBasePackage() {
Package mappingBasePackage = getClass().getPackage(); Package mappingBasePackage = getClass().getPackage();
return mappingBasePackage == null ? null : mappingBasePackage.getName(); return mappingBasePackage == null ? null : mappingBasePackage.getName();
} }
/** /**
* Creates a {@link MappingMongoConverter} using the configured {@link #mongoDbFactory()} and * Creates a {@link MappingMongoConverter} using the configured {@link #mongoDbFactory()} and
* {@link #mongoMappingContext()}. Will get {@link #customConversions()} applied. * {@link #mongoMappingContext()}. Will get {@link #customConversions()} applied.
* *
* @see #customConversions() * @see #customConversions()
* @see #mongoMappingContext() * @see #mongoMappingContext()
* @see #mongoDbFactory() * @see #mongoDbFactory()
* @return * @return
* @throws Exception * @throws Exception
*/ */
@Bean @Bean
public MappingMongoConverter mappingMongoConverter() throws Exception { public MappingMongoConverter mappingMongoConverter() throws Exception {
DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory()); DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory());
MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, mongoMappingContext()); MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, mongoMappingContext());
converter.setCustomConversions(customConversions()); converter.setCustomConversions(customConversions());
return converter; return converter;
} }
} }

View File

@@ -1,76 +1,76 @@
/* /*
* Copyright 2011-2017 the original author or authors. * Copyright 2011-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.mongodb.config; package org.springframework.data.mongodb.config;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.parsing.BeanComponentDefinition; import org.springframework.beans.factory.parsing.BeanComponentDefinition;
import org.springframework.beans.factory.parsing.CompositeComponentDefinition; import org.springframework.beans.factory.parsing.CompositeComponentDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.BeanDefinitionParser; import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext; import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.data.mongodb.core.MongoAdmin; import org.springframework.data.mongodb.core.MongoAdmin;
import org.springframework.data.mongodb.monitor.*; import org.springframework.data.mongodb.monitor.*;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.w3c.dom.Element; import org.w3c.dom.Element;
/** /**
* @author Mark Pollack * @author Mark Pollack
* @author Thomas Risberg * @author Thomas Risberg
* @author John Brisbin * @author John Brisbin
* @author Oliver Gierke * @author Oliver Gierke
* @author Christoph Strobl * @author Christoph Strobl
*/ */
public class MongoJmxParser implements BeanDefinitionParser { public class MongoJmxParser implements BeanDefinitionParser {
public BeanDefinition parse(Element element, ParserContext parserContext) { public BeanDefinition parse(Element element, ParserContext parserContext) {
String name = element.getAttribute("mongo-ref"); String name = element.getAttribute("mongo-ref");
if (!StringUtils.hasText(name)) { if (!StringUtils.hasText(name)) {
name = BeanNames.MONGO_BEAN_NAME; name = BeanNames.MONGO_BEAN_NAME;
} }
registerJmxComponents(name, element, parserContext); registerJmxComponents(name, element, parserContext);
return null; return null;
} }
protected void registerJmxComponents(String mongoRefName, Element element, ParserContext parserContext) { protected void registerJmxComponents(String mongoRefName, Element element, ParserContext parserContext) {
Object eleSource = parserContext.extractSource(element); Object eleSource = parserContext.extractSource(element);
CompositeComponentDefinition compositeDef = new CompositeComponentDefinition(element.getTagName(), eleSource); CompositeComponentDefinition compositeDef = new CompositeComponentDefinition(element.getTagName(), eleSource);
createBeanDefEntry(AssertMetrics.class, compositeDef, mongoRefName, eleSource, parserContext); createBeanDefEntry(AssertMetrics.class, compositeDef, mongoRefName, eleSource, parserContext);
createBeanDefEntry(BackgroundFlushingMetrics.class, compositeDef, mongoRefName, eleSource, parserContext); createBeanDefEntry(BackgroundFlushingMetrics.class, compositeDef, mongoRefName, eleSource, parserContext);
createBeanDefEntry(BtreeIndexCounters.class, compositeDef, mongoRefName, eleSource, parserContext); createBeanDefEntry(BtreeIndexCounters.class, compositeDef, mongoRefName, eleSource, parserContext);
createBeanDefEntry(ConnectionMetrics.class, compositeDef, mongoRefName, eleSource, parserContext); createBeanDefEntry(ConnectionMetrics.class, compositeDef, mongoRefName, eleSource, parserContext);
createBeanDefEntry(GlobalLockMetrics.class, compositeDef, mongoRefName, eleSource, parserContext); createBeanDefEntry(GlobalLockMetrics.class, compositeDef, mongoRefName, eleSource, parserContext);
createBeanDefEntry(MemoryMetrics.class, compositeDef, mongoRefName, eleSource, parserContext); createBeanDefEntry(MemoryMetrics.class, compositeDef, mongoRefName, eleSource, parserContext);
createBeanDefEntry(OperationCounters.class, compositeDef, mongoRefName, eleSource, parserContext); createBeanDefEntry(OperationCounters.class, compositeDef, mongoRefName, eleSource, parserContext);
createBeanDefEntry(ServerInfo.class, compositeDef, mongoRefName, eleSource, parserContext); createBeanDefEntry(ServerInfo.class, compositeDef, mongoRefName, eleSource, parserContext);
createBeanDefEntry(MongoAdmin.class, compositeDef, mongoRefName, eleSource, parserContext); createBeanDefEntry(MongoAdmin.class, compositeDef, mongoRefName, eleSource, parserContext);
parserContext.registerComponent(compositeDef); parserContext.registerComponent(compositeDef);
} }
protected void createBeanDefEntry(Class<?> clazz, CompositeComponentDefinition compositeDef, String mongoRefName, protected void createBeanDefEntry(Class<?> clazz, CompositeComponentDefinition compositeDef, String mongoRefName,
Object eleSource, ParserContext parserContext) { Object eleSource, ParserContext parserContext) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(clazz); BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(clazz);
builder.getRawBeanDefinition().setSource(eleSource); builder.getRawBeanDefinition().setSource(eleSource);
builder.addConstructorArgReference(mongoRefName); builder.addConstructorArgReference(mongoRefName);
BeanDefinition assertDef = builder.getBeanDefinition(); BeanDefinition assertDef = builder.getBeanDefinition();
String assertName = parserContext.getReaderContext().registerWithGeneratedName(assertDef); String assertName = parserContext.getReaderContext().registerWithGeneratedName(assertDef);
compositeDef.addNestedComponent(new BeanComponentDefinition(assertDef, assertName)); compositeDef.addNestedComponent(new BeanComponentDefinition(assertDef, assertName));
} }
} }

View File

@@ -1,170 +1,170 @@
/* /*
* Copyright 2011-2017 the original author or authors. * Copyright 2011-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.mongodb.config; package org.springframework.data.mongodb.config;
import static org.springframework.data.config.ParsingUtils.*; import static org.springframework.data.config.ParsingUtils.*;
import java.util.Map; import java.util.Map;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.CustomEditorConfigurer; import org.springframework.beans.factory.config.CustomEditorConfigurer;
import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ManagedMap; import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.beans.factory.xml.BeanDefinitionParser; import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.data.mongodb.core.MongoClientOptionsFactoryBean; import org.springframework.data.mongodb.core.MongoClientOptionsFactoryBean;
import org.springframework.util.xml.DomUtils; import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element; import org.w3c.dom.Element;
/** /**
* Utility methods for {@link BeanDefinitionParser} implementations for MongoDB. * Utility methods for {@link BeanDefinitionParser} implementations for MongoDB.
* *
* @author Mark Pollack * @author Mark Pollack
* @author Oliver Gierke * @author Oliver Gierke
* @author Thomas Darimont * @author Thomas Darimont
* @author Christoph Strobl * @author Christoph Strobl
*/ */
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
abstract class MongoParsingUtils { abstract class MongoParsingUtils {
private MongoParsingUtils() {} private MongoParsingUtils() {}
/** /**
* Parses the mongo replica-set element. * Parses the mongo replica-set element.
* *
* @param parserContext the parser context * @param parserContext the parser context
* @param element the mongo element * @param element the mongo element
* @param mongoBuilder the bean definition builder to populate * @param mongoBuilder the bean definition builder to populate
* @return * @return
*/ */
static void parseReplicaSet(Element element, BeanDefinitionBuilder mongoBuilder) { static void parseReplicaSet(Element element, BeanDefinitionBuilder mongoBuilder) {
setPropertyValue(mongoBuilder, element, "replica-set", "replicaSetSeeds"); setPropertyValue(mongoBuilder, element, "replica-set", "replicaSetSeeds");
} }
/** /**
* Parses the {@code mongo:client-options} sub-element. Populates the given attribute factory with the proper * Parses the {@code mongo:client-options} sub-element. Populates the given attribute factory with the proper
* attributes. * attributes.
* *
* @param element must not be {@literal null}. * @param element must not be {@literal null}.
* @param mongoClientBuilder must not be {@literal null}. * @param mongoClientBuilder must not be {@literal null}.
* @return * @return
* @since 1.7 * @since 1.7
*/ */
public static boolean parseMongoClientOptions(Element element, BeanDefinitionBuilder mongoClientBuilder) { public static boolean parseMongoClientOptions(Element element, BeanDefinitionBuilder mongoClientBuilder) {
Element optionsElement = DomUtils.getChildElementByTagName(element, "client-options"); Element optionsElement = DomUtils.getChildElementByTagName(element, "client-options");
if (optionsElement == null) { if (optionsElement == null) {
return false; return false;
} }
BeanDefinitionBuilder clientOptionsDefBuilder = BeanDefinitionBuilder BeanDefinitionBuilder clientOptionsDefBuilder = BeanDefinitionBuilder
.genericBeanDefinition(MongoClientOptionsFactoryBean.class); .genericBeanDefinition(MongoClientOptionsFactoryBean.class);
setPropertyValue(clientOptionsDefBuilder, optionsElement, "description", "description"); setPropertyValue(clientOptionsDefBuilder, optionsElement, "description", "description");
setPropertyValue(clientOptionsDefBuilder, optionsElement, "min-connections-per-host", "minConnectionsPerHost"); setPropertyValue(clientOptionsDefBuilder, optionsElement, "min-connections-per-host", "minConnectionsPerHost");
setPropertyValue(clientOptionsDefBuilder, optionsElement, "connections-per-host", "connectionsPerHost"); setPropertyValue(clientOptionsDefBuilder, optionsElement, "connections-per-host", "connectionsPerHost");
setPropertyValue(clientOptionsDefBuilder, optionsElement, "threads-allowed-to-block-for-connection-multiplier", setPropertyValue(clientOptionsDefBuilder, optionsElement, "threads-allowed-to-block-for-connection-multiplier",
"threadsAllowedToBlockForConnectionMultiplier"); "threadsAllowedToBlockForConnectionMultiplier");
setPropertyValue(clientOptionsDefBuilder, optionsElement, "max-wait-time", "maxWaitTime"); setPropertyValue(clientOptionsDefBuilder, optionsElement, "max-wait-time", "maxWaitTime");
setPropertyValue(clientOptionsDefBuilder, optionsElement, "max-connection-idle-time", "maxConnectionIdleTime"); setPropertyValue(clientOptionsDefBuilder, optionsElement, "max-connection-idle-time", "maxConnectionIdleTime");
setPropertyValue(clientOptionsDefBuilder, optionsElement, "max-connection-life-time", "maxConnectionLifeTime"); setPropertyValue(clientOptionsDefBuilder, optionsElement, "max-connection-life-time", "maxConnectionLifeTime");
setPropertyValue(clientOptionsDefBuilder, optionsElement, "connect-timeout", "connectTimeout"); setPropertyValue(clientOptionsDefBuilder, optionsElement, "connect-timeout", "connectTimeout");
setPropertyValue(clientOptionsDefBuilder, optionsElement, "socket-timeout", "socketTimeout"); setPropertyValue(clientOptionsDefBuilder, optionsElement, "socket-timeout", "socketTimeout");
setPropertyValue(clientOptionsDefBuilder, optionsElement, "socket-keep-alive", "socketKeepAlive"); setPropertyValue(clientOptionsDefBuilder, optionsElement, "socket-keep-alive", "socketKeepAlive");
setPropertyValue(clientOptionsDefBuilder, optionsElement, "read-preference", "readPreference"); setPropertyValue(clientOptionsDefBuilder, optionsElement, "read-preference", "readPreference");
setPropertyValue(clientOptionsDefBuilder, optionsElement, "write-concern", "writeConcern"); setPropertyValue(clientOptionsDefBuilder, optionsElement, "write-concern", "writeConcern");
setPropertyValue(clientOptionsDefBuilder, optionsElement, "heartbeat-frequency", "heartbeatFrequency"); setPropertyValue(clientOptionsDefBuilder, optionsElement, "heartbeat-frequency", "heartbeatFrequency");
setPropertyValue(clientOptionsDefBuilder, optionsElement, "min-heartbeat-frequency", "minHeartbeatFrequency"); setPropertyValue(clientOptionsDefBuilder, optionsElement, "min-heartbeat-frequency", "minHeartbeatFrequency");
setPropertyValue(clientOptionsDefBuilder, optionsElement, "heartbeat-connect-timeout", "heartbeatConnectTimeout"); setPropertyValue(clientOptionsDefBuilder, optionsElement, "heartbeat-connect-timeout", "heartbeatConnectTimeout");
setPropertyValue(clientOptionsDefBuilder, optionsElement, "heartbeat-socket-timeout", "heartbeatSocketTimeout"); setPropertyValue(clientOptionsDefBuilder, optionsElement, "heartbeat-socket-timeout", "heartbeatSocketTimeout");
setPropertyValue(clientOptionsDefBuilder, optionsElement, "ssl", "ssl"); setPropertyValue(clientOptionsDefBuilder, optionsElement, "ssl", "ssl");
setPropertyReference(clientOptionsDefBuilder, optionsElement, "ssl-socket-factory-ref", "sslSocketFactory"); setPropertyReference(clientOptionsDefBuilder, optionsElement, "ssl-socket-factory-ref", "sslSocketFactory");
setPropertyValue(clientOptionsDefBuilder, optionsElement, "server-selection-timeout", "serverSelectionTimeout"); setPropertyValue(clientOptionsDefBuilder, optionsElement, "server-selection-timeout", "serverSelectionTimeout");
mongoClientBuilder.addPropertyValue("mongoClientOptions", clientOptionsDefBuilder.getBeanDefinition()); mongoClientBuilder.addPropertyValue("mongoClientOptions", clientOptionsDefBuilder.getBeanDefinition());
return true; return true;
} }
/** /**
* Returns the {@link BeanDefinitionBuilder} to build a {@link BeanDefinition} for a * Returns the {@link BeanDefinitionBuilder} to build a {@link BeanDefinition} for a
* {@link WriteConcernPropertyEditor}. * {@link WriteConcernPropertyEditor}.
* *
* @return * @return
*/ */
static BeanDefinitionBuilder getWriteConcernPropertyEditorBuilder() { static BeanDefinitionBuilder getWriteConcernPropertyEditorBuilder() {
Map<String, Class<?>> customEditors = new ManagedMap<String, Class<?>>(); Map<String, Class<?>> customEditors = new ManagedMap<String, Class<?>>();
customEditors.put("com.mongodb.WriteConcern", WriteConcernPropertyEditor.class); customEditors.put("com.mongodb.WriteConcern", WriteConcernPropertyEditor.class);
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(CustomEditorConfigurer.class); BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(CustomEditorConfigurer.class);
builder.addPropertyValue("customEditors", customEditors); builder.addPropertyValue("customEditors", customEditors);
return builder; return builder;
} }
/** /**
* One should only register one bean definition but want to have the convenience of using * 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 * AbstractSingleBeanDefinitionParser but have the side effect of registering a 'default' property editor with the
* container. * container.
*/ */
static BeanDefinitionBuilder getServerAddressPropertyEditorBuilder() { static BeanDefinitionBuilder getServerAddressPropertyEditorBuilder() {
Map<String, String> customEditors = new ManagedMap<String, String>(); Map<String, String> customEditors = new ManagedMap<String, String>();
customEditors.put("com.mongodb.ServerAddress[]", customEditors.put("com.mongodb.ServerAddress[]",
"org.springframework.data.mongodb.config.ServerAddressPropertyEditor"); "org.springframework.data.mongodb.config.ServerAddressPropertyEditor");
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(CustomEditorConfigurer.class); BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(CustomEditorConfigurer.class);
builder.addPropertyValue("customEditors", customEditors); builder.addPropertyValue("customEditors", customEditors);
return builder; return builder;
} }
/** /**
* Returns the {@link BeanDefinitionBuilder} to build a {@link BeanDefinition} for a * Returns the {@link BeanDefinitionBuilder} to build a {@link BeanDefinition} for a
* {@link ReadPreferencePropertyEditor}. * {@link ReadPreferencePropertyEditor}.
* *
* @return * @return
* @since 1.7 * @since 1.7
*/ */
static BeanDefinitionBuilder getReadPreferencePropertyEditorBuilder() { static BeanDefinitionBuilder getReadPreferencePropertyEditorBuilder() {
Map<String, Class<?>> customEditors = new ManagedMap<String, Class<?>>(); Map<String, Class<?>> customEditors = new ManagedMap<String, Class<?>>();
customEditors.put("com.mongodb.ReadPreference", ReadPreferencePropertyEditor.class); customEditors.put("com.mongodb.ReadPreference", ReadPreferencePropertyEditor.class);
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(CustomEditorConfigurer.class); BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(CustomEditorConfigurer.class);
builder.addPropertyValue("customEditors", customEditors); builder.addPropertyValue("customEditors", customEditors);
return builder; return builder;
} }
/** /**
* Returns the {@link BeanDefinitionBuilder} to build a {@link BeanDefinition} for a * Returns the {@link BeanDefinitionBuilder} to build a {@link BeanDefinition} for a
* {@link MongoCredentialPropertyEditor}. * {@link MongoCredentialPropertyEditor}.
* *
* @return * @return
* @since 1.7 * @since 1.7
*/ */
static BeanDefinitionBuilder getMongoCredentialPropertyEditor() { static BeanDefinitionBuilder getMongoCredentialPropertyEditor() {
Map<String, Class<?>> customEditors = new ManagedMap<String, Class<?>>(); Map<String, Class<?>> customEditors = new ManagedMap<String, Class<?>>();
customEditors.put("com.mongodb.MongoCredential[]", MongoCredentialPropertyEditor.class); customEditors.put("com.mongodb.MongoCredential[]", MongoCredentialPropertyEditor.class);
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(CustomEditorConfigurer.class); BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(CustomEditorConfigurer.class);
builder.addPropertyValue("customEditors", customEditors); builder.addPropertyValue("customEditors", customEditors);
return builder; return builder;
} }
} }

View File

@@ -1,46 +1,46 @@
/* /*
* Copyright 2010-2017 the original author or authors. * Copyright 2010-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.mongodb.core; package org.springframework.data.mongodb.core;
import org.bson.Document; import org.bson.Document;
import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessException;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import com.mongodb.MongoException; import com.mongodb.MongoException;
import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoCollection;
/** /**
* Callback interface for executing actions against a {@link MongoCollection}. * Callback interface for executing actions against a {@link MongoCollection}.
* *
* @author Mark Pollak * @author Mark Pollak
* @author Grame Rocher * @author Grame Rocher
* @author Oliver Gierke * @author Oliver Gierke
* @author John Brisbin * @author John Brisbin
* @auhtor Christoph Strobl * @auhtor Christoph Strobl
* @since 1.0 * @since 1.0
*/ */
public interface CollectionCallback<T> { public interface CollectionCallback<T> {
/** /**
* @param collection never {@literal null}. * @param collection never {@literal null}.
* @return can be {@literal null}. * @return can be {@literal null}.
* @throws MongoException * @throws MongoException
* @throws DataAccessException * @throws DataAccessException
*/ */
@Nullable @Nullable
T doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException; T doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException;
} }

View File

@@ -1,166 +1,166 @@
/* /*
* Copyright 2010-2017 the original author or authors. * Copyright 2010-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.mongodb.core; package org.springframework.data.mongodb.core;
import java.util.Optional; import java.util.Optional;
import org.springframework.data.mongodb.core.query.Collation; import org.springframework.data.mongodb.core.query.Collation;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
* Provides a simple wrapper to encapsulate the variety of settings you can use when creating a collection. * Provides a simple wrapper to encapsulate the variety of settings you can use when creating a collection.
* *
* @author Thomas Risberg * @author Thomas Risberg
* @author Christoph Strobl * @author Christoph Strobl
* @author Mark Paluch * @author Mark Paluch
*/ */
public class CollectionOptions { public class CollectionOptions {
private @Nullable Long maxDocuments; private @Nullable Long maxDocuments;
private @Nullable Long size; private @Nullable Long size;
private @Nullable Boolean capped; private @Nullable Boolean capped;
private @Nullable Collation collation; private @Nullable Collation collation;
/** /**
* Constructs a new <code>CollectionOptions</code> instance. * Constructs a new <code>CollectionOptions</code> instance.
* *
* @param size the collection size in bytes, this data space is preallocated. Can be {@literal null}. * @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 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), * @param capped true to created a "capped" collection (fixed size with auto-FIFO behavior based on insertion order),
* false otherwise. Can be {@literal null}. * false otherwise. Can be {@literal null}.
* @deprecated since 2.0 please use {@link CollectionOptions#empty()} as entry point. * @deprecated since 2.0 please use {@link CollectionOptions#empty()} as entry point.
*/ */
@Deprecated @Deprecated
public CollectionOptions(@Nullable Long size, @Nullable Long maxDocuments, @Nullable Boolean capped) { public CollectionOptions(@Nullable Long size, @Nullable Long maxDocuments, @Nullable Boolean capped) {
this(size, maxDocuments, capped, null); this(size, maxDocuments, capped, null);
} }
private CollectionOptions(@Nullable Long size, @Nullable Long maxDocuments, @Nullable Boolean capped, private CollectionOptions(@Nullable Long size, @Nullable Long maxDocuments, @Nullable Boolean capped,
@Nullable Collation collation) { @Nullable Collation collation) {
this.maxDocuments = maxDocuments; this.maxDocuments = maxDocuments;
this.size = size; this.size = size;
this.capped = capped; this.capped = capped;
this.collation = collation; this.collation = collation;
} }
/** /**
* Create new {@link CollectionOptions} by just providing the {@link Collation} to use. * Create new {@link CollectionOptions} by just providing the {@link Collation} to use.
* *
* @param collation must not be {@literal null}. * @param collation must not be {@literal null}.
* @return new {@link CollectionOptions}. * @return new {@link CollectionOptions}.
* @since 2.0 * @since 2.0
*/ */
public static CollectionOptions just(Collation collation) { public static CollectionOptions just(Collation collation) {
Assert.notNull(collation, "Collation must not be null!"); Assert.notNull(collation, "Collation must not be null!");
return new CollectionOptions(null, null, null, collation); return new CollectionOptions(null, null, null, collation);
} }
/** /**
* Create new empty {@link CollectionOptions}. * Create new empty {@link CollectionOptions}.
* *
* @return new {@link CollectionOptions}. * @return new {@link CollectionOptions}.
* @since 2.0 * @since 2.0
*/ */
public static CollectionOptions empty() { public static CollectionOptions empty() {
return new CollectionOptions(null, null, null, null); return new CollectionOptions(null, null, null, null);
} }
/** /**
* Create new {@link CollectionOptions} with already given settings and capped set to {@literal true}. <br /> * Create new {@link CollectionOptions} with already given settings and capped set to {@literal true}. <br />
* <strong>NOTE</strong> Using capped collections requires defining {@link #size(int)}. * <strong>NOTE</strong> Using capped collections requires defining {@link #size(int)}.
* *
* @return new {@link CollectionOptions}. * @return new {@link CollectionOptions}.
* @since 2.0 * @since 2.0
*/ */
public CollectionOptions capped() { public CollectionOptions capped() {
return new CollectionOptions(size, maxDocuments, true, collation); return new CollectionOptions(size, maxDocuments, true, collation);
} }
/** /**
* Create new {@link CollectionOptions} with already given settings and {@code maxDocuments} set to given value. * Create new {@link CollectionOptions} with already given settings and {@code maxDocuments} set to given value.
* *
* @param maxDocuments can be {@literal null}. * @param maxDocuments can be {@literal null}.
* @return new {@link CollectionOptions}. * @return new {@link CollectionOptions}.
* @since 2.0 * @since 2.0
*/ */
public CollectionOptions maxDocuments(long maxDocuments) { public CollectionOptions maxDocuments(long maxDocuments) {
return new CollectionOptions(size, maxDocuments, capped, collation); return new CollectionOptions(size, maxDocuments, capped, collation);
} }
/** /**
* Create new {@link CollectionOptions} with already given settings and {@code size} set to given value. * Create new {@link CollectionOptions} with already given settings and {@code size} set to given value.
* *
* @param size can be {@literal null}. * @param size can be {@literal null}.
* @return new {@link CollectionOptions}. * @return new {@link CollectionOptions}.
* @since 2.0 * @since 2.0
*/ */
public CollectionOptions size(long size) { public CollectionOptions size(long size) {
return new CollectionOptions(size, maxDocuments, capped, collation); return new CollectionOptions(size, maxDocuments, capped, collation);
} }
/** /**
* Create new {@link CollectionOptions} with already given settings and {@code collation} set to given value. * Create new {@link CollectionOptions} with already given settings and {@code collation} set to given value.
* *
* @param collation can be {@literal null}. * @param collation can be {@literal null}.
* @return new {@link CollectionOptions}. * @return new {@link CollectionOptions}.
* @since 2.0 * @since 2.0
*/ */
public CollectionOptions collation(@Nullable Collation collation) { public CollectionOptions collation(@Nullable Collation collation) {
return new CollectionOptions(size, maxDocuments, capped, collation); return new CollectionOptions(size, maxDocuments, capped, collation);
} }
/** /**
* Get the max number of documents the collection should be limited to. * Get the max number of documents the collection should be limited to.
* *
* @return {@link Optional#empty()} if not set. * @return {@link Optional#empty()} if not set.
*/ */
public Optional<Long> getMaxDocuments() { public Optional<Long> getMaxDocuments() {
return Optional.ofNullable(maxDocuments); return Optional.ofNullable(maxDocuments);
} }
/** /**
* Get the {@literal size} in bytes the collection should be limited to. * Get the {@literal size} in bytes the collection should be limited to.
* *
* @return {@link Optional#empty()} if not set. * @return {@link Optional#empty()} if not set.
*/ */
public Optional<Long> getSize() { public Optional<Long> getSize() {
return Optional.ofNullable(size); return Optional.ofNullable(size);
} }
/** /**
* Get if the collection should be capped. * Get if the collection should be capped.
* *
* @return {@link Optional#empty()} if not set. * @return {@link Optional#empty()} if not set.
* @since 2.0 * @since 2.0
*/ */
public Optional<Boolean> getCapped() { public Optional<Boolean> getCapped() {
return Optional.ofNullable(capped); return Optional.ofNullable(capped);
} }
/** /**
* Get the {@link Collation} settings. * Get the {@link Collation} settings.
* *
* @return {@link Optional#empty()} if not set. * @return {@link Optional#empty()} if not set.
* @since 2.0 * @since 2.0
*/ */
public Optional<Collation> getCollation() { public Optional<Collation> getCollation() {
return Optional.ofNullable(collation); return Optional.ofNullable(collation);
} }
} }

View File

@@ -1,44 +1,44 @@
/* /*
* Copyright 2010-2017 the original author or authors. * Copyright 2010-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.mongodb.core; package org.springframework.data.mongodb.core;
import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessException;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import com.mongodb.MongoException; import com.mongodb.MongoException;
import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoDatabase;
/** /**
* Callback interface for executing actions against a {@link MongoDatabase}. * Callback interface for executing actions against a {@link MongoDatabase}.
* *
* @author Mark Pollak * @author Mark Pollak
* @author Graeme Rocher * @author Graeme Rocher
* @author Thomas Risberg * @author Thomas Risberg
* @author Oliver Gierke * @author Oliver Gierke
* @author John Brisbin * @author John Brisbin
* @author Christoph Strobl * @author Christoph Strobl
*/ */
public interface DbCallback<T> { public interface DbCallback<T> {
/** /**
* @param db must not be {@literal null}. * @param db must not be {@literal null}.
* @return can be {@literal null}. * @return can be {@literal null}.
* @throws MongoException * @throws MongoException
* @throws DataAccessException * @throws DataAccessException
*/ */
@Nullable @Nullable
T doInDB(MongoDatabase db) throws MongoException, DataAccessException; T doInDB(MongoDatabase db) throws MongoException, DataAccessException;
} }

View File

@@ -1,78 +1,78 @@
/* /*
* Copyright 2011-2017 the original author or authors. * Copyright 2011-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.mongodb.core; package org.springframework.data.mongodb.core;
import org.bson.Document; import org.bson.Document;
import org.springframework.jmx.export.annotation.ManagedOperation; import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedResource; import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import com.mongodb.MongoClient; import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoDatabase;
/** /**
* Mongo server administration exposed via JMX annotations * Mongo server administration exposed via JMX annotations
* *
* @author Mark Pollack * @author Mark Pollack
* @author Thomas Darimont * @author Thomas Darimont
* @author Mark Paluch * @author Mark Paluch
* @author Christoph Strobl * @author Christoph Strobl
*/ */
@ManagedResource(description = "Mongo Admin Operations") @ManagedResource(description = "Mongo Admin Operations")
public class MongoAdmin implements MongoAdminOperations { public class MongoAdmin implements MongoAdminOperations {
private final MongoClient mongoClient; private final MongoClient mongoClient;
public MongoAdmin(MongoClient mongoClient) { public MongoAdmin(MongoClient mongoClient) {
Assert.notNull(mongoClient, "MongoClient must not be null!"); Assert.notNull(mongoClient, "MongoClient must not be null!");
this.mongoClient = mongoClient; this.mongoClient = mongoClient;
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see org.springframework.data.mongodb.core.core.MongoAdminOperations#dropDatabase(java.lang.String) * @see org.springframework.data.mongodb.core.core.MongoAdminOperations#dropDatabase(java.lang.String)
*/ */
@ManagedOperation @ManagedOperation
public void dropDatabase(String databaseName) { public void dropDatabase(String databaseName) {
getDB(databaseName).drop(); getDB(databaseName).drop();
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see org.springframework.data.mongodb.core.core.MongoAdminOperations#createDatabase(java.lang.String) * @see org.springframework.data.mongodb.core.core.MongoAdminOperations#createDatabase(java.lang.String)
*/ */
@ManagedOperation @ManagedOperation
public void createDatabase(String databaseName) { public void createDatabase(String databaseName) {
getDB(databaseName); getDB(databaseName);
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see org.springframework.data.mongodb.core.core.MongoAdminOperations#getDatabaseStats(java.lang.String) * @see org.springframework.data.mongodb.core.core.MongoAdminOperations#getDatabaseStats(java.lang.String)
*/ */
@ManagedOperation @ManagedOperation
public String getDatabaseStats(String databaseName) { public String getDatabaseStats(String databaseName) {
return getDB(databaseName).runCommand(new Document("dbStats", 1).append("scale", 1024)).toJson(); return getDB(databaseName).runCommand(new Document("dbStats", 1).append("scale", 1024)).toJson();
} }
@ManagedOperation @ManagedOperation
public String getServerStatus() { public String getServerStatus() {
return getDB("admin").runCommand(new Document("serverStatus", 1).append("rangeDeleter", 1).append("repl", 1)) return getDB("admin").runCommand(new Document("serverStatus", 1).append("rangeDeleter", 1).append("repl", 1))
.toJson(); .toJson();
} }
MongoDatabase getDB(String databaseName) { MongoDatabase getDB(String databaseName) {
return mongoClient.getDatabase(databaseName); return mongoClient.getDatabase(databaseName);
} }
} }

View File

@@ -1,34 +1,34 @@
/* /*
* Copyright 2011-2014 the original author or authors. * Copyright 2011-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.mongodb.core; package org.springframework.data.mongodb.core;
import org.springframework.jmx.export.annotation.ManagedOperation; import org.springframework.jmx.export.annotation.ManagedOperation;
/** /**
* @author Mark Pollack * @author Mark Pollack
* @author Oliver Gierke * @author Oliver Gierke
*/ */
public interface MongoAdminOperations { public interface MongoAdminOperations {
@ManagedOperation @ManagedOperation
void dropDatabase(String databaseName); void dropDatabase(String databaseName);
@ManagedOperation @ManagedOperation
void createDatabase(String databaseName); void createDatabase(String databaseName);
@ManagedOperation @ManagedOperation
String getDatabaseStats(String databaseName); String getDatabaseStats(String databaseName);
} }

View File

@@ -1,34 +1,34 @@
/* /*
* Copyright 2016 the original author or authors. * Copyright 2016-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.mongodb.core; package org.springframework.data.mongodb.core;
import org.springframework.dao.DataAccessException; import org.bson.Document;
import org.reactivestreams.Publisher;
import com.mongodb.MongoException; import org.springframework.dao.DataAccessException;
import com.mongodb.reactivestreams.client.MongoCollection;
import org.bson.Document; import com.mongodb.MongoException;
import org.reactivestreams.Publisher; import com.mongodb.reactivestreams.client.MongoCollection;
/** /**
* @author Mark Paluch * @author Mark Paluch
* @param <T> * @param <T>
* @since 2.0 * @since 2.0
*/ */
public interface ReactiveCollectionCallback<T> { public interface ReactiveCollectionCallback<T> {
Publisher<T> doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException; Publisher<T> doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException;
} }

View File

@@ -1,150 +1,150 @@
/* /*
* Copyright 2011-2017 the original author or authors. * Copyright 2011-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.mongodb.core; package org.springframework.data.mongodb.core;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.DisposableBean;
import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessException;
import org.springframework.dao.support.PersistenceExceptionTranslator; import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import com.mongodb.DB; import com.mongodb.DB;
import com.mongodb.MongoClient; import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI; import com.mongodb.MongoClientURI;
import com.mongodb.WriteConcern; import com.mongodb.WriteConcern;
import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoDatabase;
/** /**
* Factory to create {@link DB} instances from a {@link MongoClient} instance. * Factory to create {@link DB} instances from a {@link MongoClient} instance.
* *
* @author Mark Pollack * @author Mark Pollack
* @author Oliver Gierke * @author Oliver Gierke
* @author Thomas Darimont * @author Thomas Darimont
* @author Christoph Strobl * @author Christoph Strobl
*/ */
public class SimpleMongoDbFactory implements DisposableBean, MongoDbFactory { public class SimpleMongoDbFactory implements DisposableBean, MongoDbFactory {
private final MongoClient mongoClient; private final MongoClient mongoClient;
private final String databaseName; private final String databaseName;
private final boolean mongoInstanceCreated; private final boolean mongoInstanceCreated;
private final PersistenceExceptionTranslator exceptionTranslator; private final PersistenceExceptionTranslator exceptionTranslator;
private @Nullable WriteConcern writeConcern; private @Nullable WriteConcern writeConcern;
/** /**
* Creates a new {@link SimpleMongoDbFactory} instance from the given {@link MongoClientURI}. * Creates a new {@link SimpleMongoDbFactory} instance from the given {@link MongoClientURI}.
* *
* @param uri must not be {@literal null}. * @param uri must not be {@literal null}.
* @throws UnknownHostException * @throws UnknownHostException
* @since 1.7 * @since 1.7
*/ */
public SimpleMongoDbFactory(MongoClientURI uri) { public SimpleMongoDbFactory(MongoClientURI uri) {
this(new MongoClient(uri), uri.getDatabase(), true); this(new MongoClient(uri), uri.getDatabase(), true);
} }
/** /**
* Creates a new {@link SimpleMongoDbFactory} instance from the given {@link MongoClient}. * Creates a new {@link SimpleMongoDbFactory} instance from the given {@link MongoClient}.
* *
* @param mongoClient must not be {@literal null}. * @param mongoClient must not be {@literal null}.
* @param databaseName must not be {@literal null}. * @param databaseName must not be {@literal null}.
* @since 1.7 * @since 1.7
*/ */
public SimpleMongoDbFactory(MongoClient mongoClient, String databaseName) { public SimpleMongoDbFactory(MongoClient mongoClient, String databaseName) {
this(mongoClient, databaseName, false); this(mongoClient, databaseName, false);
} }
/** /**
* @param client * @param client
* @param databaseName * @param databaseName
* @param mongoInstanceCreated * @param mongoInstanceCreated
* @since 1.7 * @since 1.7
*/ */
private SimpleMongoDbFactory(MongoClient mongoClient, String databaseName, boolean mongoInstanceCreated) { private SimpleMongoDbFactory(MongoClient mongoClient, String databaseName, boolean mongoInstanceCreated) {
Assert.notNull(mongoClient, "MongoClient must not be null!"); Assert.notNull(mongoClient, "MongoClient must not be null!");
Assert.hasText(databaseName, "Database name must not be empty!"); Assert.hasText(databaseName, "Database name must not be empty!");
Assert.isTrue(databaseName.matches("[\\w-]+"), Assert.isTrue(databaseName.matches("[\\w-]+"),
"Database name must only contain letters, numbers, underscores and dashes!"); "Database name must only contain letters, numbers, underscores and dashes!");
this.mongoClient = mongoClient; this.mongoClient = mongoClient;
this.databaseName = databaseName; this.databaseName = databaseName;
this.mongoInstanceCreated = mongoInstanceCreated; this.mongoInstanceCreated = mongoInstanceCreated;
this.exceptionTranslator = new MongoExceptionTranslator(); this.exceptionTranslator = new MongoExceptionTranslator();
} }
/** /**
* Configures the {@link WriteConcern} to be used on the {@link DB} instance being created. * Configures the {@link WriteConcern} to be used on the {@link DB} instance being created.
* *
* @param writeConcern the writeConcern to set * @param writeConcern the writeConcern to set
*/ */
public void setWriteConcern(WriteConcern writeConcern) { public void setWriteConcern(WriteConcern writeConcern) {
this.writeConcern = writeConcern; this.writeConcern = writeConcern;
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.mongodb.MongoDbFactory#getDb() * @see org.springframework.data.mongodb.MongoDbFactory#getDb()
*/ */
public MongoDatabase getDb() throws DataAccessException { public MongoDatabase getDb() throws DataAccessException {
return getDb(databaseName); return getDb(databaseName);
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.mongodb.MongoDbFactory#getDb(java.lang.String) * @see org.springframework.data.mongodb.MongoDbFactory#getDb(java.lang.String)
*/ */
public MongoDatabase getDb(String dbName) throws DataAccessException { public MongoDatabase getDb(String dbName) throws DataAccessException {
Assert.hasText(dbName, "Database name must not be empty."); Assert.hasText(dbName, "Database name must not be empty.");
MongoDatabase db = mongoClient.getDatabase(dbName); MongoDatabase db = mongoClient.getDatabase(dbName);
if (writeConcern == null) { if (writeConcern == null) {
return db; return db;
} }
return db.withWriteConcern(writeConcern); return db.withWriteConcern(writeConcern);
} }
/** /**
* Clean up the Mongo instance if it was created by the factory itself. * Clean up the Mongo instance if it was created by the factory itself.
* *
* @see DisposableBean#destroy() * @see DisposableBean#destroy()
*/ */
public void destroy() throws Exception { public void destroy() throws Exception {
if (mongoInstanceCreated) { if (mongoInstanceCreated) {
mongoClient.close(); mongoClient.close();
} }
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.mongodb.MongoDbFactory#getExceptionTranslator() * @see org.springframework.data.mongodb.MongoDbFactory#getExceptionTranslator()
*/ */
@Override @Override
public PersistenceExceptionTranslator getExceptionTranslator() { public PersistenceExceptionTranslator getExceptionTranslator() {
return this.exceptionTranslator; return this.exceptionTranslator;
} }
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@Override @Override
public DB getLegacyDb() { public DB getLegacyDb() {
return mongoClient.getDB(databaseName); return mongoClient.getDB(databaseName);
} }
} }

View File

@@ -1,44 +1,44 @@
/* /*
* Copyright 2010-2016 the original author or authors. * Copyright 2010-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.mongodb.core.convert; package org.springframework.data.mongodb.core.convert;
import org.bson.Document; import org.bson.Document;
import org.bson.conversions.Bson; import org.bson.conversions.Bson;
import org.springframework.data.convert.EntityConverter; import org.springframework.data.convert.EntityConverter;
import org.springframework.data.convert.EntityReader; import org.springframework.data.convert.EntityReader;
import org.springframework.data.convert.TypeMapper; import org.springframework.data.convert.TypeMapper;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
/** /**
* Central Mongo specific converter interface which combines {@link MongoWriter} and {@link MongoReader}. * Central Mongo specific converter interface which combines {@link MongoWriter} and {@link MongoReader}.
* *
* @author Oliver Gierke * @author Oliver Gierke
* @author Thomas Darimont * @author Thomas Darimont
* @author Christoph Strobl * @author Christoph Strobl
*/ */
public interface MongoConverter public interface MongoConverter
extends EntityConverter<MongoPersistentEntity<?>, MongoPersistentProperty, Object, Bson>, MongoWriter<Object>, extends EntityConverter<MongoPersistentEntity<?>, MongoPersistentProperty, Object, Bson>, MongoWriter<Object>,
EntityReader<Object, Bson> { EntityReader<Object, Bson> {
/** /**
* Returns thw {@link TypeMapper} being used to write type information into {@link Document}s created with that * Returns thw {@link TypeMapper} being used to write type information into {@link Document}s created with that
* converter. * converter.
* *
* @return will never be {@literal null}. * @return will never be {@literal null}.
*/ */
MongoTypeMapper getTypeMapper(); MongoTypeMapper getTypeMapper();
} }

View File

@@ -1,69 +1,69 @@
/* /*
* Copyright 2010-2016 the original author or authors. * Copyright 2010-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.mongodb.core.convert; package org.springframework.data.mongodb.core.convert;
import org.bson.conversions.Bson; import org.bson.conversions.Bson;
import org.springframework.data.convert.EntityWriter; import org.springframework.data.convert.EntityWriter;
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
import org.springframework.data.util.TypeInformation; import org.springframework.data.util.TypeInformation;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import com.mongodb.DBRef; import com.mongodb.DBRef;
/** /**
* A MongoWriter is responsible for converting an object of type T to the native MongoDB representation Document. * A MongoWriter is responsible for converting an object of type T to the native MongoDB representation Document.
* *
* @param <T> the type of the object to convert to a Document * @param <T> the type of the object to convert to a Document
* @author Mark Pollack * @author Mark Pollack
* @author Thomas Risberg * @author Thomas Risberg
* @author Oliver Gierke * @author Oliver Gierke
* @author Christoph Strobl * @author Christoph Strobl
*/ */
public interface MongoWriter<T> extends EntityWriter<T, Bson> { public interface MongoWriter<T> extends EntityWriter<T, Bson> {
/** /**
* Converts the given object into one Mongo will be able to store natively. If the given object can already be stored * 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. * as is, no conversion will happen.
* *
* @param obj can be {@literal null}. * @param obj can be {@literal null}.
* @return * @return
*/ */
@Nullable @Nullable
default Object convertToMongoType(@Nullable Object obj) { default Object convertToMongoType(@Nullable Object obj) {
return convertToMongoType(obj, null); return convertToMongoType(obj, null);
} }
/** /**
* Converts the given object into one Mongo will be able to store natively but retains the type information in case * 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. * the given {@link TypeInformation} differs from the given object type.
* *
* @param obj can be {@literal null}. * @param obj can be {@literal null}.
* @param typeInformation can be {@literal null}. * @param typeInformation can be {@literal null}.
* @return * @return
*/ */
@Nullable @Nullable
Object convertToMongoType(@Nullable Object obj, @Nullable TypeInformation<?> typeInformation); Object convertToMongoType(@Nullable Object obj, @Nullable TypeInformation<?> typeInformation);
/** /**
* Creates a {@link DBRef} to refer to the given object. * 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 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 * @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}. * the {@link DBRef} object to create. Can be {@literal null}.
* @return will never be {@literal null}. * @return will never be {@literal null}.
*/ */
DBRef toDBRef(Object object, @Nullable MongoPersistentProperty referingProperty); DBRef toDBRef(Object object, @Nullable MongoPersistentProperty referingProperty);
} }

View File

@@ -1,49 +1,49 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.mongodb.monitor; package org.springframework.data.mongodb.monitor;
import org.bson.Document; import org.bson.Document;
import com.mongodb.MongoClient; import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoDatabase;
/** /**
* Base class to encapsulate common configuration settings when connecting to a database * Base class to encapsulate common configuration settings when connecting to a database
* *
* @author Mark Pollack * @author Mark Pollack
* @author Oliver Gierke * @author Oliver Gierke
* @author Christoph Strobl * @author Christoph Strobl
*/ */
public abstract class AbstractMonitor { public abstract class AbstractMonitor {
private final MongoClient mongoClient; private final MongoClient mongoClient;
protected AbstractMonitor(MongoClient mongoClient) { protected AbstractMonitor(MongoClient mongoClient) {
this.mongoClient = mongoClient; this.mongoClient = mongoClient;
} }
public Document getServerStatus() { public Document getServerStatus() {
return getDb("admin").runCommand(new Document("serverStatus", 1).append("rangeDeleter", 1).append("repl", 1)); return getDb("admin").runCommand(new Document("serverStatus", 1).append("rangeDeleter", 1).append("repl", 1));
} }
public MongoDatabase getDb(String databaseName) { public MongoDatabase getDb(String databaseName) {
return mongoClient.getDatabase(databaseName); return mongoClient.getDatabase(databaseName);
} }
protected MongoClient getMongoClient() { protected MongoClient getMongoClient() {
return mongoClient; return mongoClient;
} }
} }

View File

@@ -1,70 +1,70 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.mongodb.monitor; package org.springframework.data.mongodb.monitor;
import com.mongodb.MongoClient; import com.mongodb.MongoClient;
import org.bson.Document; import org.bson.Document;
import org.springframework.jmx.export.annotation.ManagedMetric; import org.springframework.jmx.export.annotation.ManagedMetric;
import org.springframework.jmx.export.annotation.ManagedResource; import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.jmx.support.MetricType; import org.springframework.jmx.support.MetricType;
import com.mongodb.DBObject; import com.mongodb.DBObject;
import com.mongodb.Mongo; import com.mongodb.Mongo;
/** /**
* JMX Metrics for assertions * JMX Metrics for assertions
* *
* @author Mark Pollack * @author Mark Pollack
*/ */
@ManagedResource(description = "Assertion Metrics") @ManagedResource(description = "Assertion Metrics")
public class AssertMetrics extends AbstractMonitor { public class AssertMetrics extends AbstractMonitor {
public AssertMetrics(MongoClient mongoClient) { public AssertMetrics(MongoClient mongoClient) {
super(mongoClient); super(mongoClient);
} }
@ManagedMetric(metricType = MetricType.COUNTER, displayName = "Regular") @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Regular")
public int getRegular() { public int getRegular() {
return getBtree("regular"); return getBtree("regular");
} }
@ManagedMetric(metricType = MetricType.COUNTER, displayName = "Warning") @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Warning")
public int getWarning() { public int getWarning() {
return getBtree("warning"); return getBtree("warning");
} }
@ManagedMetric(metricType = MetricType.COUNTER, displayName = "Msg") @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Msg")
public int getMsg() { public int getMsg() {
return getBtree("msg"); return getBtree("msg");
} }
@ManagedMetric(metricType = MetricType.COUNTER, displayName = "User") @ManagedMetric(metricType = MetricType.COUNTER, displayName = "User")
public int getUser() { public int getUser() {
return getBtree("user"); return getBtree("user");
} }
@ManagedMetric(metricType = MetricType.GAUGE, displayName = "Rollovers") @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Rollovers")
public int getRollovers() { public int getRollovers() {
return getBtree("rollovers"); return getBtree("rollovers");
} }
private int getBtree(String key) { private int getBtree(String key) {
Document asserts = (Document) getServerStatus().get("asserts"); Document asserts = (Document) getServerStatus().get("asserts");
// Class c = btree.get(key).getClass(); // Class c = btree.get(key).getClass();
return (Integer) asserts.get(key); return (Integer) asserts.get(key);
} }
} }

View File

@@ -1,76 +1,76 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.mongodb.monitor; package org.springframework.data.mongodb.monitor;
import java.util.Date; import java.util.Date;
import org.bson.Document; import org.bson.Document;
import org.springframework.jmx.export.annotation.ManagedMetric; import org.springframework.jmx.export.annotation.ManagedMetric;
import org.springframework.jmx.export.annotation.ManagedResource; import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.jmx.support.MetricType; import org.springframework.jmx.support.MetricType;
import com.mongodb.MongoClient; import com.mongodb.MongoClient;
/** /**
* JMX Metrics for Background Flushing * JMX Metrics for Background Flushing
* *
* @author Mark Pollack * @author Mark Pollack
*/ */
@ManagedResource(description = "Background Flushing Metrics") @ManagedResource(description = "Background Flushing Metrics")
public class BackgroundFlushingMetrics extends AbstractMonitor { public class BackgroundFlushingMetrics extends AbstractMonitor {
public BackgroundFlushingMetrics(MongoClient mongoClient) { public BackgroundFlushingMetrics(MongoClient mongoClient) {
super(mongoClient); super(mongoClient);
} }
@ManagedMetric(metricType = MetricType.COUNTER, displayName = "Flushes") @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Flushes")
public int getFlushes() { public int getFlushes() {
return getFlushingData("flushes", java.lang.Integer.class); return getFlushingData("flushes", java.lang.Integer.class);
} }
@ManagedMetric(metricType = MetricType.COUNTER, displayName = "Total ms", unit = "ms") @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Total ms", unit = "ms")
public int getTotalMs() { public int getTotalMs() {
return getFlushingData("total_ms", java.lang.Integer.class); return getFlushingData("total_ms", java.lang.Integer.class);
} }
@ManagedMetric(metricType = MetricType.GAUGE, displayName = "Average ms", unit = "ms") @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Average ms", unit = "ms")
public double getAverageMs() { public double getAverageMs() {
return getFlushingData("average_ms", java.lang.Double.class); return getFlushingData("average_ms", java.lang.Double.class);
} }
@ManagedMetric(metricType = MetricType.GAUGE, displayName = "Last Ms", unit = "ms") @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Last Ms", unit = "ms")
public int getLastMs() { public int getLastMs() {
return getFlushingData("last_ms", java.lang.Integer.class); return getFlushingData("last_ms", java.lang.Integer.class);
} }
@ManagedMetric(metricType = MetricType.GAUGE, displayName = "Last finished") @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Last finished")
public Date getLastFinished() { public Date getLastFinished() {
return getLast(); return getLast();
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private <T> T getFlushingData(String key, Class<T> targetClass) { private <T> T getFlushingData(String key, Class<T> targetClass) {
Document mem = (Document) getServerStatus().get("backgroundFlushing"); Document mem = (Document) getServerStatus().get("backgroundFlushing");
return (T) mem.get(key); return (T) mem.get(key);
} }
private Date getLast() { private Date getLast() {
Document bgFlush = (Document) getServerStatus().get("backgroundFlushing"); Document bgFlush = (Document) getServerStatus().get("backgroundFlushing");
Date lastFinished = (Date) bgFlush.get("last_finished"); Date lastFinished = (Date) bgFlush.get("last_finished");
return lastFinished; return lastFinished;
} }
} }

View File

@@ -1,75 +1,75 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.mongodb.monitor; package org.springframework.data.mongodb.monitor;
import org.bson.Document; import org.bson.Document;
import org.springframework.jmx.export.annotation.ManagedMetric; import org.springframework.jmx.export.annotation.ManagedMetric;
import org.springframework.jmx.export.annotation.ManagedResource; import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.jmx.support.MetricType; import org.springframework.jmx.support.MetricType;
import com.mongodb.MongoClient; import com.mongodb.MongoClient;
/** /**
* JMX Metrics for B-tree index counters * JMX Metrics for B-tree index counters
* *
* @author Mark Pollack * @author Mark Pollack
*/ */
@ManagedResource(description = "Btree Metrics") @ManagedResource(description = "Btree Metrics")
public class BtreeIndexCounters extends AbstractMonitor { public class BtreeIndexCounters extends AbstractMonitor {
public BtreeIndexCounters(MongoClient mongoClient) { public BtreeIndexCounters(MongoClient mongoClient) {
super(mongoClient); super(mongoClient);
} }
@ManagedMetric(metricType = MetricType.GAUGE, displayName = "Accesses") @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Accesses")
public int getAccesses() { public int getAccesses() {
return getBtree("accesses"); return getBtree("accesses");
} }
@ManagedMetric(metricType = MetricType.GAUGE, displayName = "Hits") @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Hits")
public int getHits() { public int getHits() {
return getBtree("hits"); return getBtree("hits");
} }
@ManagedMetric(metricType = MetricType.GAUGE, displayName = "Misses") @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Misses")
public int getMisses() { public int getMisses() {
return getBtree("misses"); return getBtree("misses");
} }
@ManagedMetric(metricType = MetricType.GAUGE, displayName = "Resets") @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Resets")
public int getResets() { public int getResets() {
return getBtree("resets"); return getBtree("resets");
} }
@ManagedMetric(metricType = MetricType.GAUGE, displayName = "Miss Ratio") @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Miss Ratio")
public int getMissRatio() { public int getMissRatio() {
return getBtree("missRatio"); return getBtree("missRatio");
} }
private int getBtree(String key) { private int getBtree(String key) {
Document indexCounters = (Document) getServerStatus().get("indexCounters"); Document indexCounters = (Document) getServerStatus().get("indexCounters");
if (indexCounters.get("note") != null) { if (indexCounters.get("note") != null) {
String message = (String) indexCounters.get("note"); String message = (String) indexCounters.get("note");
if (message.contains("not supported")) { if (message.contains("not supported")) {
return -1; return -1;
} }
} }
Document btree = (Document) indexCounters.get("btree"); Document btree = (Document) indexCounters.get("btree");
// Class c = btree.get(key).getClass(); // Class c = btree.get(key).getClass();
return (Integer) btree.get(key); return (Integer) btree.get(key);
} }
} }

View File

@@ -1,54 +1,54 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.mongodb.monitor; package org.springframework.data.mongodb.monitor;
import org.bson.Document; import org.bson.Document;
import org.springframework.jmx.export.annotation.ManagedMetric; import org.springframework.jmx.export.annotation.ManagedMetric;
import org.springframework.jmx.export.annotation.ManagedResource; import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.jmx.support.MetricType; import org.springframework.jmx.support.MetricType;
import com.mongodb.MongoClient; import com.mongodb.MongoClient;
/** /**
* JMX Metrics for Connections * JMX Metrics for Connections
* *
* @author Mark Pollack * @author Mark Pollack
*/ */
@ManagedResource(description = "Connection metrics") @ManagedResource(description = "Connection metrics")
public class ConnectionMetrics extends AbstractMonitor { public class ConnectionMetrics extends AbstractMonitor {
public ConnectionMetrics(MongoClient mongoClient) { public ConnectionMetrics(MongoClient mongoClient) {
super(mongoClient); super(mongoClient);
} }
@ManagedMetric(metricType = MetricType.GAUGE, displayName = "Current Connections") @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Current Connections")
public int getCurrent() { public int getCurrent() {
return getConnectionData("current", java.lang.Integer.class); return getConnectionData("current", java.lang.Integer.class);
} }
@ManagedMetric(metricType = MetricType.GAUGE, displayName = "Available Connections") @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Available Connections")
public int getAvailable() { public int getAvailable() {
return getConnectionData("available", java.lang.Integer.class); return getConnectionData("available", java.lang.Integer.class);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private <T> T getConnectionData(String key, Class<T> targetClass) { private <T> T getConnectionData(String key, Class<T> targetClass) {
Document mem = (Document) getServerStatus().get("connections"); Document mem = (Document) getServerStatus().get("connections");
// Class c = mem.get(key).getClass(); // Class c = mem.get(key).getClass();
return (T) mem.get(key); return (T) mem.get(key);
} }
} }

View File

@@ -1,79 +1,79 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.mongodb.monitor; package org.springframework.data.mongodb.monitor;
import org.bson.Document; import org.bson.Document;
import org.springframework.jmx.export.annotation.ManagedMetric; import org.springframework.jmx.export.annotation.ManagedMetric;
import org.springframework.jmx.export.annotation.ManagedResource; import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.jmx.support.MetricType; import org.springframework.jmx.support.MetricType;
import com.mongodb.DBObject; import com.mongodb.DBObject;
import com.mongodb.MongoClient; import com.mongodb.MongoClient;
/** /**
* JMX Metrics for Global Locks * JMX Metrics for Global Locks
* *
* @author Mark Pollack * @author Mark Pollack
*/ */
@ManagedResource(description = "Global Lock Metrics") @ManagedResource(description = "Global Lock Metrics")
public class GlobalLockMetrics extends AbstractMonitor { public class GlobalLockMetrics extends AbstractMonitor {
public GlobalLockMetrics(MongoClient mongoClient) { public GlobalLockMetrics(MongoClient mongoClient) {
super(mongoClient); super(mongoClient);
} }
@ManagedMetric(metricType = MetricType.COUNTER, displayName = "Total time") @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Total time")
public double getTotalTime() { public double getTotalTime() {
return getGlobalLockData("totalTime", java.lang.Double.class); return getGlobalLockData("totalTime", java.lang.Double.class);
} }
@ManagedMetric(metricType = MetricType.COUNTER, displayName = "Lock time", unit = "s") @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Lock time", unit = "s")
public double getLockTime() { public double getLockTime() {
return getGlobalLockData("lockTime", java.lang.Double.class); return getGlobalLockData("lockTime", java.lang.Double.class);
} }
@ManagedMetric(metricType = MetricType.GAUGE, displayName = "Lock time") @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Lock time")
public double getLockTimeRatio() { public double getLockTimeRatio() {
return getGlobalLockData("ratio", java.lang.Double.class); return getGlobalLockData("ratio", java.lang.Double.class);
} }
@ManagedMetric(metricType = MetricType.GAUGE, displayName = "Current Queue") @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Current Queue")
public int getCurrentQueueTotal() { public int getCurrentQueueTotal() {
return getCurrentQueue("total"); return getCurrentQueue("total");
} }
@ManagedMetric(metricType = MetricType.GAUGE, displayName = "Reader Queue") @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Reader Queue")
public int getCurrentQueueReaders() { public int getCurrentQueueReaders() {
return getCurrentQueue("readers"); return getCurrentQueue("readers");
} }
@ManagedMetric(metricType = MetricType.GAUGE, displayName = "Writer Queue") @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Writer Queue")
public int getCurrentQueueWriters() { public int getCurrentQueueWriters() {
return getCurrentQueue("writers"); return getCurrentQueue("writers");
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private <T> T getGlobalLockData(String key, Class<T> targetClass) { private <T> T getGlobalLockData(String key, Class<T> targetClass) {
DBObject globalLock = (DBObject) getServerStatus().get("globalLock"); DBObject globalLock = (DBObject) getServerStatus().get("globalLock");
return (T) globalLock.get(key); return (T) globalLock.get(key);
} }
private int getCurrentQueue(String key) { private int getCurrentQueue(String key) {
Document globalLock = (Document) getServerStatus().get("globalLock"); Document globalLock = (Document) getServerStatus().get("globalLock");
Document currentQueue = (Document) globalLock.get("currentQueue"); Document currentQueue = (Document) globalLock.get("currentQueue");
return (Integer) currentQueue.get(key); return (Integer) currentQueue.get(key);
} }
} }

View File

@@ -1,69 +1,69 @@
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.mongodb.monitor; package org.springframework.data.mongodb.monitor;
import org.bson.Document; import org.bson.Document;
import org.springframework.jmx.export.annotation.ManagedMetric; import org.springframework.jmx.export.annotation.ManagedMetric;
import org.springframework.jmx.export.annotation.ManagedResource; import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.jmx.support.MetricType; import org.springframework.jmx.support.MetricType;
import com.mongodb.MongoClient; import com.mongodb.MongoClient;
/** /**
* JMX Metrics for Memory * JMX Metrics for Memory
* *
* @author Mark Pollack * @author Mark Pollack
*/ */
@ManagedResource(description = "Memory Metrics") @ManagedResource(description = "Memory Metrics")
public class MemoryMetrics extends AbstractMonitor { public class MemoryMetrics extends AbstractMonitor {
public MemoryMetrics(MongoClient mongoClient) { public MemoryMetrics(MongoClient mongoClient) {
super(mongoClient); super(mongoClient);
} }
@ManagedMetric(metricType = MetricType.COUNTER, displayName = "Memory address size") @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Memory address size")
public int getBits() { public int getBits() {
return getMemData("bits", java.lang.Integer.class); return getMemData("bits", java.lang.Integer.class);
} }
@ManagedMetric(metricType = MetricType.GAUGE, displayName = "Resident in Physical Memory", unit = "MB") @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Resident in Physical Memory", unit = "MB")
public int getResidentSpace() { public int getResidentSpace() {
return getMemData("resident", java.lang.Integer.class); return getMemData("resident", java.lang.Integer.class);
} }
@ManagedMetric(metricType = MetricType.GAUGE, displayName = "Virtual Address Space", unit = "MB") @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Virtual Address Space", unit = "MB")
public int getVirtualAddressSpace() { public int getVirtualAddressSpace() {
return getMemData("virtual", java.lang.Integer.class); return getMemData("virtual", java.lang.Integer.class);
} }
@ManagedMetric(metricType = MetricType.GAUGE, displayName = "Is memory info supported on this platform") @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Is memory info supported on this platform")
public boolean getMemoryInfoSupported() { public boolean getMemoryInfoSupported() {
return getMemData("supported", java.lang.Boolean.class); return getMemData("supported", java.lang.Boolean.class);
} }
@ManagedMetric(metricType = MetricType.GAUGE, displayName = "Memory Mapped Space", unit = "MB") @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Memory Mapped Space", unit = "MB")
public int getMemoryMappedSpace() { public int getMemoryMappedSpace() {
return getMemData("mapped", java.lang.Integer.class); return getMemData("mapped", java.lang.Integer.class);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private <T> T getMemData(String key, Class<T> targetClass) { private <T> T getMemData(String key, Class<T> targetClass) {
Document mem = (Document) getServerStatus().get("mem"); Document mem = (Document) getServerStatus().get("mem");
// Class c = mem.get(key).getClass(); // Class c = mem.get(key).getClass();
return (T) mem.get(key); return (T) mem.get(key);
} }
} }

View File

@@ -1,71 +1,71 @@
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.mongodb.monitor; package org.springframework.data.mongodb.monitor;
import org.bson.Document; import org.bson.Document;
import org.springframework.jmx.export.annotation.ManagedMetric; import org.springframework.jmx.export.annotation.ManagedMetric;
import org.springframework.jmx.export.annotation.ManagedResource; import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.jmx.support.MetricType; import org.springframework.jmx.support.MetricType;
import com.mongodb.MongoClient; import com.mongodb.MongoClient;
/** /**
* JMX Metrics for Operation counters * JMX Metrics for Operation counters
* *
* @author Mark Pollack * @author Mark Pollack
*/ */
@ManagedResource(description = "Operation Counters") @ManagedResource(description = "Operation Counters")
public class OperationCounters extends AbstractMonitor { public class OperationCounters extends AbstractMonitor {
public OperationCounters(MongoClient mongoClient) { public OperationCounters(MongoClient mongoClient) {
super(mongoClient); super(mongoClient);
} }
@ManagedMetric(metricType = MetricType.COUNTER, displayName = "Insert operation count") @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Insert operation count")
public int getInsertCount() { public int getInsertCount() {
return getOpCounter("insert"); return getOpCounter("insert");
} }
@ManagedMetric(metricType = MetricType.COUNTER, displayName = "Query operation count") @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Query operation count")
public int getQueryCount() { public int getQueryCount() {
return getOpCounter("query"); return getOpCounter("query");
} }
@ManagedMetric(metricType = MetricType.COUNTER, displayName = "Update operation count") @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Update operation count")
public int getUpdateCount() { public int getUpdateCount() {
return getOpCounter("update"); return getOpCounter("update");
} }
@ManagedMetric(metricType = MetricType.COUNTER, displayName = "Delete operation count") @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Delete operation count")
public int getDeleteCount() { public int getDeleteCount() {
return getOpCounter("delete"); return getOpCounter("delete");
} }
@ManagedMetric(metricType = MetricType.COUNTER, displayName = "GetMore operation count") @ManagedMetric(metricType = MetricType.COUNTER, displayName = "GetMore operation count")
public int getGetMoreCount() { public int getGetMoreCount() {
return getOpCounter("getmore"); return getOpCounter("getmore");
} }
@ManagedMetric(metricType = MetricType.COUNTER, displayName = "Command operation count") @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Command operation count")
public int getCommandCount() { public int getCommandCount() {
return getOpCounter("command"); return getOpCounter("command");
} }
private int getOpCounter(String key) { private int getOpCounter(String key) {
Document opCounters = (Document) getServerStatus().get("opcounters"); Document opCounters = (Document) getServerStatus().get("opcounters");
return (Integer) opCounters.get(key); return (Integer) opCounters.get(key);
} }
} }

View File

@@ -1,76 +1,76 @@
/* /*
* Copyright 2012-2015 the original author or authors. * Copyright 2012-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.data.mongodb.monitor; package org.springframework.data.mongodb.monitor;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import org.springframework.jmx.export.annotation.ManagedMetric; import org.springframework.jmx.export.annotation.ManagedMetric;
import org.springframework.jmx.export.annotation.ManagedOperation; import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedResource; import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.jmx.support.MetricType; import org.springframework.jmx.support.MetricType;
import com.mongodb.MongoClient; import com.mongodb.MongoClient;
/** /**
* Expose basic server information via JMX * Expose basic server information via JMX
* *
* @author Mark Pollack * @author Mark Pollack
* @author Thomas Darimont * @author Thomas Darimont
* @author Christoph Strobl * @author Christoph Strobl
*/ */
@ManagedResource(description = "Server Information") @ManagedResource(description = "Server Information")
public class ServerInfo extends AbstractMonitor { public class ServerInfo extends AbstractMonitor {
public ServerInfo(MongoClient mongoClient) { public ServerInfo(MongoClient mongoClient) {
super(mongoClient); super(mongoClient);
} }
/** /**
* Returns the hostname of the used server reported by MongoDB. * Returns the hostname of the used server reported by MongoDB.
* *
* @return the reported hostname can also be an IP address. * @return the reported hostname can also be an IP address.
* @throws UnknownHostException * @throws UnknownHostException
*/ */
@ManagedOperation(description = "Server host name") @ManagedOperation(description = "Server host name")
public String getHostName() throws UnknownHostException { public String getHostName() throws UnknownHostException {
/* /*
* UnknownHostException is not necessary anymore, but clients could have * UnknownHostException is not necessary anymore, but clients could have
* called this method in a try..catch(UnknownHostException) already * called this method in a try..catch(UnknownHostException) already
*/ */
return getMongoClient().getAddress().getHost(); return getMongoClient().getAddress().getHost();
} }
@ManagedMetric(displayName = "Uptime Estimate") @ManagedMetric(displayName = "Uptime Estimate")
public double getUptimeEstimate() { public double getUptimeEstimate() {
return (Double) getServerStatus().get("uptimeEstimate"); return (Double) getServerStatus().get("uptimeEstimate");
} }
@ManagedOperation(description = "MongoDB Server Version") @ManagedOperation(description = "MongoDB Server Version")
public String getVersion() { public String getVersion() {
return (String) getServerStatus().get("version"); return (String) getServerStatus().get("version");
} }
@ManagedOperation(description = "Local Time") @ManagedOperation(description = "Local Time")
public String getLocalTime() { public String getLocalTime() {
return (String) getServerStatus().get("localTime"); return (String) getServerStatus().get("localTime");
} }
@ManagedMetric(metricType = MetricType.COUNTER, displayName = "Server uptime in seconds", unit = "seconds") @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Server uptime in seconds", unit = "seconds")
public double getUptime() { public double getUptime() {
return (Double) getServerStatus().get("uptime"); return (Double) getServerStatus().get("uptime");
} }
} }