|
| 1 | +/* |
| 2 | + * Copyright 2016 MongoDB, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +package com.mongodb.client.jndi; |
| 19 | + |
| 20 | +import com.mongodb.MongoClient; |
| 21 | +import com.mongodb.MongoClientURI; |
| 22 | +import com.mongodb.MongoException; |
| 23 | +import com.mongodb.diagnostics.logging.Logger; |
| 24 | +import com.mongodb.diagnostics.logging.Loggers; |
| 25 | + |
| 26 | +import javax.naming.Context; |
| 27 | +import javax.naming.Name; |
| 28 | +import javax.naming.RefAddr; |
| 29 | +import javax.naming.Reference; |
| 30 | +import javax.naming.spi.ObjectFactory; |
| 31 | +import java.util.Enumeration; |
| 32 | +import java.util.Hashtable; |
| 33 | + |
| 34 | +import static java.lang.String.format; |
| 35 | + |
| 36 | +/** |
| 37 | + * An ObjectFactory for MongoClient instances. |
| 38 | + * |
| 39 | + * @since 3.3 |
| 40 | + */ |
| 41 | +public class MongoClientFactory implements ObjectFactory { |
| 42 | + |
| 43 | + private static final Logger LOGGER = Loggers.getLogger("client.jndi"); |
| 44 | + |
| 45 | + private static final String CONNECTION_STRING = "connectionString"; |
| 46 | + |
| 47 | + /** |
| 48 | + * This implementation will create instances of {@link MongoClient} based on a connection string conforming to the format specified in |
| 49 | + * {@link MongoClientURI}. |
| 50 | + * <p>The connection string is specified in one of two ways:</p> |
| 51 | + * <ul> |
| 52 | + * <li>As the {@code String} value of a property in the {@code environment} parameter with a key of {@code "connectionString"}</li> |
| 53 | + * <li>As the {@code String} value of a {@link RefAddr} with type {@code "connectionString"} in an {@code obj} parameter |
| 54 | + * of type {@link Reference}</li> |
| 55 | + * </ul> |
| 56 | + * |
| 57 | + * Specification of the connection string in the {@code environment} parameter takes precedence over specification in the {@code obj} |
| 58 | + * parameter. The {@code name} and {@code nameCtx} parameters are ignored. |
| 59 | + * |
| 60 | + * If a non-empty connection string is not specified in either of these two ways, a {@link MongoException} is thrown. |
| 61 | + * @return an instance of {@link MongoClient} based on the specified connection string |
| 62 | + * @throws MongoException |
| 63 | + * |
| 64 | + * Note: Not all options that can be specified via {@link com.mongodb.MongoClientOptions} can be specified via the connection string. |
| 65 | + */ |
| 66 | + @Override |
| 67 | + public Object getObjectInstance(final Object obj, final Name name, final Context nameCtx, final Hashtable<?, ?> environment) |
| 68 | + throws Exception { |
| 69 | + |
| 70 | + // Some app servers, e.g. Wildfly, use the environment to pass location information to an ObjectFactory |
| 71 | + String connectionString = null; |
| 72 | + |
| 73 | + if (environment.get(CONNECTION_STRING) instanceof String) { |
| 74 | + connectionString = (String) environment.get(CONNECTION_STRING); |
| 75 | + } |
| 76 | + |
| 77 | + if (connectionString == null || connectionString.isEmpty()) { |
| 78 | + LOGGER.debug(format("No '%s' property in environment. Casting 'obj' to java.naming.Reference to look for a " |
| 79 | + + "javax.naming.RefAddr with type equal to '%s'", CONNECTION_STRING, CONNECTION_STRING)); |
| 80 | + |
| 81 | + // Some app servers, e.g. Tomcat, pass obj as an instance of javax.naming.Reference and pass location information in a |
| 82 | + // javax.naming.RefAddr |
| 83 | + if (obj instanceof Reference) { |
| 84 | + Enumeration<RefAddr> props = ((Reference) obj).getAll(); |
| 85 | + |
| 86 | + while (props.hasMoreElements()) { |
| 87 | + RefAddr addr = props.nextElement(); |
| 88 | + if (addr != null) { |
| 89 | + if (CONNECTION_STRING.equals(addr.getType())) { |
| 90 | + if (addr.getContent() instanceof String) { |
| 91 | + connectionString = (String) addr.getContent(); |
| 92 | + break; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + if (connectionString == null || connectionString.isEmpty()) { |
| 101 | + throw new MongoException(format("Could not locate '%s' in either environment or obj", CONNECTION_STRING)); |
| 102 | + } |
| 103 | + |
| 104 | + MongoClientURI uri = new MongoClientURI(connectionString); |
| 105 | + |
| 106 | + return new MongoClient(uri); |
| 107 | + } |
| 108 | +} |
0 commit comments