1 /*
2 * #%L
3 * MongoClientConfigurationBeanInfo.java - mongodb-async-driver - Allanbank Consulting, Inc.
4 * %%
5 * Copyright (C) 2011 - 2014 Allanbank Consulting, Inc.
6 * %%
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * #L%
19 */
20 package com.allanbank.mongodb;
21
22 import java.beans.BeanInfo;
23 import java.beans.IntrospectionException;
24 import java.beans.Introspector;
25 import java.beans.PropertyDescriptor;
26 import java.beans.PropertyEditorSupport;
27 import java.beans.SimpleBeanInfo;
28 import java.util.Collections;
29
30 import com.allanbank.mongodb.util.log.Log;
31 import com.allanbank.mongodb.util.log.LogFactory;
32
33 /**
34 * MongoClientConfigurationBeanInfo provides specialization for the properties
35 * of the {@link MongoClientConfiguration}.
36 *
37 * @api.yes This class is part of the driver's API. Public and protected members
38 * will be deprecated for at least 1 non-bugfix release (version
39 * numbers are <major>.<minor>.<bugfix>) before being
40 * removed or modified.
41 * @copyright 2014, Allanbank Consulting, Inc., All Rights Reserved
42 */
43 public class MongoClientConfigurationBeanInfo extends SimpleBeanInfo {
44
45 /**
46 * Creates a new MongoClientConfigurationBeanInfo.
47 */
48 public MongoClientConfigurationBeanInfo() {
49 super();
50 }
51
52 /**
53 * {@inheritDoc}
54 * <p>
55 * Overridden to return <code>null</code> to trigger normal bean
56 * introspection.
57 * </p>
58 */
59 @Override
60 public PropertyDescriptor[] getPropertyDescriptors() {
61
62 try {
63 final BeanInfo beanInfo = Introspector.getBeanInfo(
64 MongoClientConfiguration.class,
65 Introspector.IGNORE_IMMEDIATE_BEANINFO);
66 final PropertyDescriptor[] descriptors = beanInfo
67 .getPropertyDescriptors();
68
69 for (final PropertyDescriptor descriptor : descriptors) {
70 if ("credentials".equalsIgnoreCase(descriptor.getName())) {
71 descriptor
72 .setPropertyEditorClass(CredentialListEditor.class);
73 }
74 }
75
76 return descriptors;
77 }
78 catch (final IntrospectionException e) {
79 // Just use the defaults.
80 return super.getPropertyDescriptors();
81 }
82 }
83
84 /**
85 * CredentialListEditor provides the ability to parse the list of
86 * credentials from a MongoDB URI. This will always be a singleton list.
87 *
88 * @api.no This class is <b>NOT</b> part of the drivers API. This class may
89 * be mutated in incompatible ways between any two releases of the
90 * driver.
91 * @copyright 2014, Allanbank Consulting, Inc., All Rights Reserved
92 */
93 protected static class CredentialListEditor extends PropertyEditorSupport {
94
95 /** The logger for the {@link CredentialListEditor}. */
96 protected static final Log LOG = LogFactory
97 .getLog(CredentialListEditor.class);
98
99 /**
100 * Creates a new CredentialEditor.
101 */
102 public CredentialListEditor() {
103 super();
104 }
105
106 /**
107 * {@inheritDoc}
108 * <p>
109 * Overridden to parse a string to a {@link Credential}.
110 * </p>
111 *
112 * @throws IllegalArgumentException
113 * If the string cannot be parsed into a {@link Credential}.
114 */
115 @Override
116 public void setAsText(final String credentialString)
117 throws IllegalArgumentException {
118
119 final CredentialEditor realEditor = new CredentialEditor();
120 realEditor.setAsText(credentialString);
121 final Object value = realEditor.getValue();
122
123 if (value != null) {
124 setValue(Collections.singletonList(value));
125 }
126 else {
127 setValue(Collections.EMPTY_LIST);
128 }
129 }
130 }
131 }