Skip to content

Commit 7a10e90

Browse files
committed
Store driver version in a generated class
Accessing the driver version from META-INF/MANIFEST.MF has proved to be too fragile, as it fails when the driver is deployed in ways in which that file is difficult to access or no longer even exists. Generating a class at build time and storing it in the class path will be robust against any attempts to re-package. JAVA-2900
1 parent 93a5eb3 commit 7a10e90

File tree

6 files changed

+35
-53
lines changed

6 files changed

+35
-53
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,21 @@ $ mongod --dbpath ./data/db --logpath ./data/mongod.log --port 27017 --logappend
121121
If you encounter `"Too many open files"` errors when running the tests then you will need to increase
122122
the number of available file descriptors prior to starting mongod as described in [https://docs.mongodb.com/manual/reference/ulimit/](https://docs.mongodb.com/manual/reference/ulimit/)
123123

124+
## IntelliJ IDEA
125+
126+
A couple of manual configuration steps are required to run the code in IntelliJ:
127+
128+
- Java 9 is required to build and compile the source.
129+
130+
- **Error:** `java: cannot find symbol: class SNIHostName location: package javax.net.ssl`<br>
131+
**Fix:** Settings > Build, Execution, Deployment > Compiler > Java Compiler - untick "Use '--release' option for cross-compilation (Java 9 and later)"
132+
133+
- **Error:** `java: package com.mongodb.internal.build does not exist`<br>
134+
**Fixes:** Any of the following: <br>
135+
- Run the `compileBuildConfig` task: eg: `./gradlew compileBuildConfig` or via Gradle > driver-core > Tasks > other > compileBuildConfig
136+
- Set `compileBuildConfig` to execute Before Build. via Gradle > Tasks > other > right click compileBuildConfig - click on "Execute Before Build"
137+
- Delegate all build actions to Gradle: Settings > Build, Execution, Deployment > Build Tools > Gradle > Runner - tick "Delegate IDE build/run actions to gradle"
138+
124139
### Build status:
125140

126141
[![Build Status](https://travis-ci.org/mongodb/mongo-java-driver.svg?branch=master)](https://travis-ci.org/mongodb/mongo-java-driver)

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ buildscript {
3838
classpath 'com.netflix.nebula:gradle-extra-configurations-plugin:1.12.+'
3939
classpath 'com.bmuschko:gradle-nexus-plugin:2.2'
4040
classpath "gradle.plugin.com.github.spotbugs:spotbugs-gradle-plugin:1.6.1"
41+
classpath 'gradle.plugin.de.fuerstenau:BuildConfigPlugin:1.1.8'
4142
}
4243
}
4344

driver-core/build.gradle

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17+
apply plugin: 'idea'
1718
apply plugin: 'osgi'
1819
apply plugin: 'org.kordamp.gradle.clirr'
20+
apply plugin: 'de.fuerstenau.buildconfig'
1921

2022
def configDir = new File(rootDir, 'config')
2123
archivesBaseName = 'mongodb-driver-core'
@@ -38,6 +40,14 @@ dependencies {
3840
testCompile project(':bson').sourceSets.test.output
3941
}
4042

43+
buildConfig {
44+
appName = 'mongo-java-driver'
45+
version = getGitVersion()
46+
47+
clsName = 'MongoDriverVersion'
48+
packageName = 'com.mongodb.internal.build'
49+
}
50+
4151
jar {
4252
manifest {
4353
instruction 'Automatic-Module-Name', 'org.mongodb.driver.core'

driver-core/src/main/com/mongodb/internal/connection/ClientMetadataHelper.java

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,16 @@
1717
package com.mongodb.internal.connection;
1818

1919
import com.mongodb.MongoDriverInformation;
20+
import com.mongodb.internal.build.MongoDriverVersion;
2021
import org.bson.BsonBinaryWriter;
2122
import org.bson.BsonDocument;
2223
import org.bson.BsonString;
2324
import org.bson.codecs.BsonDocumentCodec;
2425
import org.bson.codecs.EncoderContext;
2526
import org.bson.io.BasicOutputBuffer;
2627

27-
import java.io.IOException;
28-
import java.net.JarURLConnection;
29-
import java.net.URL;
3028
import java.nio.charset.Charset;
31-
import java.security.CodeSource;
32-
import java.security.ProtectionDomain;
3329
import java.util.List;
34-
import java.util.jar.Attributes;
35-
import java.util.jar.Manifest;
3630

3731
import static com.mongodb.assertions.Assertions.isTrueArgument;
3832
import static java.lang.String.format;
@@ -104,47 +98,6 @@ private static boolean nameMatches(final String name, final String... prefixes)
10498
return false;
10599
}
106100

107-
private static String getDriverVersion() {
108-
String driverVersion = "unknown";
109-
String path = getCodeSourcePath();
110-
if (path != null) {
111-
try {
112-
URL jarUrl = path.endsWith(".jar") ? new URL("jar:file:" + path + "!/") : null;
113-
if (jarUrl != null) {
114-
JarURLConnection jarURLConnection = (JarURLConnection) jarUrl.openConnection();
115-
Manifest manifest = jarURLConnection.getManifest();
116-
if (manifest != null) {
117-
Attributes mainAttributes = manifest.getMainAttributes();
118-
if (mainAttributes != null) {
119-
String version = (String) mainAttributes.get(new Attributes.Name("Build-Version"));
120-
if (version != null) {
121-
driverVersion = version;
122-
}
123-
}
124-
}
125-
}
126-
} catch (IOException e) {
127-
// do nothing
128-
}
129-
}
130-
return driverVersion;
131-
}
132-
133-
private static String getCodeSourcePath() {
134-
String path = null;
135-
ProtectionDomain protectionDomain = InternalStreamConnectionInitializer.class.getProtectionDomain();
136-
if (protectionDomain != null) {
137-
CodeSource codeSource = protectionDomain.getCodeSource();
138-
if (codeSource != null) {
139-
URL location = codeSource.getLocation();
140-
if (location != null) {
141-
path = location.getPath();
142-
}
143-
}
144-
}
145-
return path;
146-
}
147-
148101
static BsonDocument createClientMetadataDocument(final String applicationName) {
149102
return createClientMetadataDocument(applicationName, null);
150103
}
@@ -215,8 +168,8 @@ static MongoDriverInformation getDriverInformation(final MongoDriverInformation
215168
MongoDriverInformation.Builder builder = mongoDriverInformation != null ? MongoDriverInformation.builder(mongoDriverInformation)
216169
: MongoDriverInformation.builder();
217170
return builder
218-
.driverName("mongo-java-driver")
219-
.driverVersion(getDriverVersion())
171+
.driverName(MongoDriverVersion.NAME)
172+
.driverVersion(MongoDriverVersion.VERSION)
220173
.driverPlatform(format("Java/%s/%s", getProperty("java.vendor", "unknown-vendor"),
221174
getProperty("java.runtime.version", "unknown-version")))
222175
.build();

driver-core/src/test/unit/com/mongodb/internal/connection/ClientMetadataHelperSpecification.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.mongodb.internal.connection
1818

1919
import com.mongodb.MongoDriverInformation
20+
import com.mongodb.internal.build.MongoDriverVersion
2021
import org.bson.BsonBinaryWriter
2122
import org.bson.BsonDocument
2223
import org.bson.BsonString
@@ -149,9 +150,8 @@ class ClientMetadataHelperSpecification extends Specification {
149150
}
150151

151152
static BsonDocument createExpectedClientMetadataDocument(String appName) {
152-
String driverVersion = 'unknown'
153-
def expectedDriverDocument = new BsonDocument('name', new BsonString('mongo-java-driver'))
154-
.append('version', new BsonString(driverVersion))
153+
def expectedDriverDocument = new BsonDocument('name', new BsonString(MongoDriverVersion.NAME))
154+
.append('version', new BsonString(MongoDriverVersion.VERSION))
155155
def expectedOperatingSystemDocument = new BsonDocument('type',
156156
new BsonString(ClientMetadataHelper.getOperatingSystemType(System.getProperty('os.name'))))
157157
.append('name', new BsonString(System.getProperty('os.name')))

mongo-java-driver/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@ sourceSets {
3535
srcDirs = ["$rootDir/driver-legacy/src/main",
3636
"$rootDir/driver-sync/src/main",
3737
"$rootDir/driver-core/src/main",
38+
"$rootDir/driver-core/build/gen/buildconfig/src/main",
3839
"$rootDir/bson/src/main"]
3940
}
4041
}
4142
}
4243

44+
compileJava.dependsOn(":driver-core:compileBuildConfig")
45+
4346
// copied from driver-core
4447
jar {
4548
manifest {

0 commit comments

Comments
 (0)