|
| 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 | +package com.mongodb.client; |
| 18 | + |
| 19 | +import com.mongodb.annotations.NotThreadSafe; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.List; |
| 24 | + |
| 25 | +import static com.mongodb.assertions.Assertions.isTrue; |
| 26 | +import static com.mongodb.assertions.Assertions.notNull; |
| 27 | + |
| 28 | +/** |
| 29 | + * The MongoDriverInformation class allows driver and library authors to add extra information about their library. This information is |
| 30 | + * then available in the MongoD/MongoS logs. |
| 31 | + * |
| 32 | + * <p> |
| 33 | + * The following metadata can be included when creating a {@code MongoClient}. |
| 34 | + * </p> |
| 35 | + * <ul> |
| 36 | + * <li>The driver name. Eg: {@code mongo-scala-driver}</li> |
| 37 | + * <li>The driver version. Eg: {@code 1.2.0}</li> |
| 38 | + * <li>Extra platform information. Eg: {@code Scala 2.11}</li> |
| 39 | + * </ul> |
| 40 | + * <p> |
| 41 | + * Note: Library authors are responsible for accepting {@code MongoDriverInformation} from external libraries using their library. |
| 42 | + * Also all the meta data is limited to 512 bytes and any excess data will be truncated. |
| 43 | + * </p> |
| 44 | + * |
| 45 | + * @since 3.4 |
| 46 | + * @mongodb.server.release 3.4 |
| 47 | + */ |
| 48 | +public final class MongoDriverInformation { |
| 49 | + private final List<String> driverNames; |
| 50 | + private final List<String> driverVersions; |
| 51 | + private final List<String> driverPlatforms; |
| 52 | + |
| 53 | + /** |
| 54 | + * Convenience method to create a Builder. |
| 55 | + * |
| 56 | + * @return a builder |
| 57 | + */ |
| 58 | + public static Builder builder() { |
| 59 | + return new Builder(); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Convenience method to create a Builder. |
| 64 | + * |
| 65 | + * @param mongoDriverInformation the mongoDriverInformation to extend |
| 66 | + * @return a builder |
| 67 | + */ |
| 68 | + public static Builder builder(final MongoDriverInformation mongoDriverInformation) { |
| 69 | + return new Builder(mongoDriverInformation); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Returns the driverNames |
| 74 | + * |
| 75 | + * @return the driverNames |
| 76 | + */ |
| 77 | + public List<String> getDriverNames() { |
| 78 | + return driverNames; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Returns the driverVersions |
| 83 | + * |
| 84 | + * @return the driverVersions |
| 85 | + */ |
| 86 | + public List<String> getDriverVersions() { |
| 87 | + return driverVersions; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Returns the driverPlatforms |
| 92 | + * |
| 93 | + * @return the driverPlatforms |
| 94 | + */ |
| 95 | + public List<String> getDriverPlatforms() { |
| 96 | + return driverPlatforms; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * |
| 101 | + */ |
| 102 | + @NotThreadSafe |
| 103 | + public static final class Builder { |
| 104 | + private final MongoDriverInformation driverInformation; |
| 105 | + private String driverName; |
| 106 | + private String driverVersion; |
| 107 | + private String driverPlatform; |
| 108 | + |
| 109 | + /** |
| 110 | + * Sets the name |
| 111 | + * |
| 112 | + * @param driverName the name |
| 113 | + * @return this |
| 114 | + */ |
| 115 | + public Builder driverName(final String driverName) { |
| 116 | + this.driverName = notNull("driverName", driverName); |
| 117 | + return this; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Sets the version |
| 122 | + * |
| 123 | + * <p> |
| 124 | + * Note: You must also set a driver name if setting a driver version. |
| 125 | + * </p> |
| 126 | + * |
| 127 | + * @param driverVersion the version |
| 128 | + * @return this |
| 129 | + */ |
| 130 | + public Builder driverVersion(final String driverVersion) { |
| 131 | + this.driverVersion = notNull("driverVersion", driverVersion); |
| 132 | + return this; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Sets the platform |
| 137 | + * |
| 138 | + * @param driverPlatform the platform |
| 139 | + * @return this |
| 140 | + */ |
| 141 | + public Builder driverPlatform(final String driverPlatform) { |
| 142 | + this.driverPlatform = notNull("driverPlatform", driverPlatform); |
| 143 | + return this; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * @return the driver information |
| 148 | + */ |
| 149 | + public MongoDriverInformation build() { |
| 150 | + isTrue("You must also set the driver name when setting the driver version", !(driverName == null && driverVersion != null)); |
| 151 | + |
| 152 | + List<String> names = prependToList(driverInformation.getDriverNames(), driverName); |
| 153 | + List<String> versions = prependToList(driverInformation.getDriverVersions(), driverVersion); |
| 154 | + List<String> platforms = prependToList(driverInformation.getDriverPlatforms(), driverPlatform); |
| 155 | + return new MongoDriverInformation(names, versions, platforms); |
| 156 | + } |
| 157 | + |
| 158 | + private List<String> prependToList(final List<String> stringList, final String value) { |
| 159 | + if (value == null) { |
| 160 | + return stringList; |
| 161 | + } else { |
| 162 | + ArrayList<String> newList = new ArrayList<String>(); |
| 163 | + newList.add(value); |
| 164 | + newList.addAll(stringList); |
| 165 | + return Collections.unmodifiableList(newList); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + private Builder() { |
| 170 | + List<String> immutableEmptyList = Collections.unmodifiableList(Collections.<String>emptyList()); |
| 171 | + driverInformation = new MongoDriverInformation(immutableEmptyList, immutableEmptyList, immutableEmptyList); |
| 172 | + } |
| 173 | + |
| 174 | + private Builder(final MongoDriverInformation driverInformation) { |
| 175 | + this.driverInformation = notNull("driverInformation", driverInformation); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + private MongoDriverInformation(final List<String> driverNames, final List<String> driverVersions, final List<String> driverPlatforms) { |
| 180 | + this.driverNames = driverNames; |
| 181 | + this.driverVersions = driverVersions; |
| 182 | + this.driverPlatforms = driverPlatforms; |
| 183 | + } |
| 184 | +} |
0 commit comments