forked from mongodb/mongo-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMongoURI.java
More file actions
290 lines (251 loc) · 8.41 KB
/
Copy pathMongoURI.java
File metadata and controls
290 lines (251 loc) · 8.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/**
* Copyright (C) 2008 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mongodb;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Logger;
/**
* Represents a <a href="http://www.mongodb.org/display/DOCS/Connections">URI</a>
* which can be used to create a Mongo instance. The URI describes the hosts to
* be used and options.
*
* The Java driver supports the following options (case insensitive):<br />
*
* <ul>
* <li>maxpoolsize</li>
* <li>waitqueuemultiple</li>
* <li>waitqueuetimeoutms</li>
* <li>connecttimeoutms</li>
* <li>sockettimeoutms</li>
* <li>autoconnectretry</li>
* <li>slaveok</li>
* <li>safe</li>
* <li>w</li>
* <li>wtimeout</li>
* <li>fsync</li>
* </ul>
*/
public class MongoURI {
public static final String MONGODB_PREFIX = "mongodb://";
/**
* Creates a MongoURI described by a String.
* examples
* mongodb://127.0.0.1
* mongodb://fred:foobar@127.0.0.1/
* @param uri the URI
* @dochub connections
*/
public MongoURI( String uri ){
_uri = uri;
if ( ! uri.startsWith( MONGODB_PREFIX ) )
throw new IllegalArgumentException( "uri needs to start with " + MONGODB_PREFIX );
uri = uri.substring(MONGODB_PREFIX.length());
String serverPart;
String nsPart;
String optionsPart;
{
int idx = uri.lastIndexOf( "/" );
if ( idx < 0 ){
serverPart = uri;
nsPart = null;
optionsPart = null;
}
else {
serverPart = uri.substring( 0 , idx );
nsPart = uri.substring( idx + 1 );
idx = nsPart.indexOf( "?" );
if ( idx >= 0 ){
optionsPart = nsPart.substring( idx + 1 );
nsPart = nsPart.substring( 0 , idx );
}
else {
optionsPart = null;
}
}
}
{ // _username,_password,_hosts
List<String> all = new LinkedList<String>();
int idx = serverPart.indexOf( "@" );
if ( idx > 0 ){
String authPart = serverPart.substring( 0 , idx );
serverPart = serverPart.substring( idx + 1 );
idx = authPart.indexOf( ":" );
_username = authPart.substring( 0, idx );
_password = authPart.substring( idx + 1 ).toCharArray();
}
else {
_username = null;
_password = null;
}
for ( String s : serverPart.split( "," ) )
all.add( s );
_hosts = Collections.unmodifiableList( all );
}
if ( nsPart != null ){ // _database,_collection
int idx = nsPart.indexOf( "." );
if ( idx < 0 ){
_database = nsPart;
_collection = null;
}
else {
_database = nsPart.substring( 0 , idx );
_collection = nsPart.substring( idx + 1 );
}
}
else {
_database = null;
_collection = null;
}
if ( optionsPart != null && optionsPart.length() > 0 ) parseOptions( optionsPart );
}
@SuppressWarnings("deprecation")
private void parseOptions( String optionsPart ){
for ( String _part : optionsPart.split( "&|;" ) ){
int idx = _part.indexOf( "=" );
if ( idx >= 0 ){
String key = _part.substring( 0, idx ).toLowerCase();
String value = _part.substring( idx + 1 );
if ( key.equals( "maxpoolsize" ) ) _options.connectionsPerHost = Integer.parseInt( value );
else if ( key.equals( "minpoolsize" ) )
LOGGER.warning( "Currently No support in Java driver for Min Pool Size." );
else if ( key.equals( "waitqueuemultiple" ) )
_options.threadsAllowedToBlockForConnectionMultiplier = Integer.parseInt( value );
else if ( key.equals( "waitqueuetimeoutms" ) ) _options.maxWaitTime = Integer.parseInt( value );
else if ( key.equals( "connecttimeoutms" ) ) _options.connectTimeout = Integer.parseInt( value );
else if ( key.equals( "sockettimeoutms" ) ) _options.socketTimeout = Integer.parseInt( value );
else if ( key.equals( "autoconnectretry" ) ) _options.autoConnectRetry = _parseBoolean( value );
else if ( key.equals( "slaveok" ) ) _options.slaveOk = _parseBoolean( value );
else if ( key.equals( "safe" ) ) _options.safe = _parseBoolean( value );
else if ( key.equals( "w" ) ) _options.w = Integer.parseInt( value );
else if ( key.equals( "wtimeout" ) ) _options.wtimeout = Integer.parseInt( value );
else if ( key.equals( "fsync" ) ) _options.fsync = _parseBoolean( value );
else LOGGER.warning( "Unknown or Unsupported Option '" + value + "'" );
}
}
}
boolean _parseBoolean( String _in ){
String in = _in.trim();
if ( in != null && in.length() > 0 && ( in.equals( "1" ) || in.toLowerCase().equals( "true" ) || in.toLowerCase()
.equals( "yes" ) ) )
return true;
else return false;
}
// ---------------------------------
/**
* Gets the username
* @return
*/
public String getUsername(){
return _username;
}
/**
* Gets the password
* @return
*/
public char[] getPassword(){
return _password;
}
/**
* Gets the list of hosts
* @return
*/
public List<String> getHosts(){
return _hosts;
}
/**
* Gets the database name
* @return
*/
public String getDatabase(){
return _database;
}
/**
* Gets the collection name
* @return
*/
public String getCollection(){
return _collection;
}
/**
* Gets the options
* @return
*/
public MongoOptions getOptions(){
return _options;
}
/**
* creates a Mongo instance based on the URI
* @return
* @throws MongoException
* @throws UnknownHostException
*/
public Mongo connect()
throws MongoException , UnknownHostException {
// TODO caching?
return new Mongo( this );
}
/**
* returns the DB object from a newly created Mongo instance based on this URI
* @return
* @throws MongoException
* @throws UnknownHostException
*/
public DB connectDB()
throws MongoException , UnknownHostException {
// TODO auth
return connect().getDB( _database );
}
/**
* returns the URI's DB object from a given Mongo instance
* @param m
* @return
*/
public DB connectDB( Mongo m ){
// TODO auth
return m.getDB( _database );
}
/**
* returns the URI's Collection from a given DB object
* @param db
* @return
*/
public DBCollection connectCollection( DB db ){
return db.getCollection( _collection );
}
/**
* returns the URI's Collection from a given Mongo instance
* @param m
* @return
*/
public DBCollection connectCollection( Mongo m ){
return connectDB( m ).getCollection( _collection );
}
// ---------------------------------
final String _username;
final char[] _password;
final List<String> _hosts;
final String _database;
final String _collection;
final MongoOptions _options = new MongoOptions();
final String _uri;
static final Logger LOGGER = Logger.getLogger( "com.mongodb.MongoURI" );
@Override
public String toString() {
return _uri;
}
}