001/* 002 * Copyright 2012-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.autoconfigure.mongo; 018 019import com.mongodb.MongoClientURI; 020 021import org.springframework.boot.context.properties.ConfigurationProperties; 022 023/** 024 * Configuration properties for Mongo. 025 * 026 * @author Dave Syer 027 * @author Phillip Webb 028 * @author Josh Long 029 * @author Andy Wilkinson 030 * @author EddĂș MelĂ©ndez 031 * @author Stephane Nicoll 032 * @author Nasko Vasilev 033 * @author Mark Paluch 034 */ 035@ConfigurationProperties(prefix = "spring.data.mongodb") 036public class MongoProperties { 037 038 /** 039 * Default port used when the configured port is {@code null}. 040 */ 041 public static final int DEFAULT_PORT = 27017; 042 043 /** 044 * Default URI used when the configured URI is {@code null}. 045 */ 046 public static final String DEFAULT_URI = "mongodb://localhost/test"; 047 048 /** 049 * Mongo server host. Cannot be set with URI. 050 */ 051 private String host; 052 053 /** 054 * Mongo server port. Cannot be set with URI. 055 */ 056 private Integer port = null; 057 058 /** 059 * Mongo database URI. Cannot be set with host, port and credentials. 060 */ 061 private String uri; 062 063 /** 064 * Database name. 065 */ 066 private String database; 067 068 /** 069 * Authentication database name. 070 */ 071 private String authenticationDatabase; 072 073 /** 074 * GridFS database name. 075 */ 076 private String gridFsDatabase; 077 078 /** 079 * Login user of the mongo server. Cannot be set with URI. 080 */ 081 private String username; 082 083 /** 084 * Login password of the mongo server. Cannot be set with URI. 085 */ 086 private char[] password; 087 088 /** 089 * Fully qualified name of the FieldNamingStrategy to use. 090 */ 091 private Class<?> fieldNamingStrategy; 092 093 public String getHost() { 094 return this.host; 095 } 096 097 public void setHost(String host) { 098 this.host = host; 099 } 100 101 public String getDatabase() { 102 return this.database; 103 } 104 105 public void setDatabase(String database) { 106 this.database = database; 107 } 108 109 public String getAuthenticationDatabase() { 110 return this.authenticationDatabase; 111 } 112 113 public void setAuthenticationDatabase(String authenticationDatabase) { 114 this.authenticationDatabase = authenticationDatabase; 115 } 116 117 public String getUsername() { 118 return this.username; 119 } 120 121 public void setUsername(String username) { 122 this.username = username; 123 } 124 125 public char[] getPassword() { 126 return this.password; 127 } 128 129 public void setPassword(char[] password) { 130 this.password = password; 131 } 132 133 public Class<?> getFieldNamingStrategy() { 134 return this.fieldNamingStrategy; 135 } 136 137 public void setFieldNamingStrategy(Class<?> fieldNamingStrategy) { 138 this.fieldNamingStrategy = fieldNamingStrategy; 139 } 140 141 public String getUri() { 142 return this.uri; 143 } 144 145 public String determineUri() { 146 return (this.uri != null) ? this.uri : DEFAULT_URI; 147 } 148 149 public void setUri(String uri) { 150 this.uri = uri; 151 } 152 153 public Integer getPort() { 154 return this.port; 155 } 156 157 public void setPort(Integer port) { 158 this.port = port; 159 } 160 161 public String getGridFsDatabase() { 162 return this.gridFsDatabase; 163 } 164 165 public void setGridFsDatabase(String gridFsDatabase) { 166 this.gridFsDatabase = gridFsDatabase; 167 } 168 169 public String getMongoClientDatabase() { 170 if (this.database != null) { 171 return this.database; 172 } 173 return new MongoClientURI(determineUri()).getDatabase(); 174 } 175 176}