[JAVA-2512] Remove assumption that driver code source can be accessed via a "jar"-schemed URL Created: 07/May/17  Updated: 29/Oct/23  Resolved: 16/Aug/18

Status: Closed
Project: Java Driver
Component/s: Connection Management
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Jeffrey Yemin Assignee: Unassigned
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
is depended on by JAVA-2522 Build with Java 9 Closed
Duplicate
duplicates JAVA-2900 Auto generate Build Information Closed
Epic Link: Java9

 Description   

ClientMetadataHelper assumes that the driver version is found by building an URL to the jar file using the "jar" scheme, e.g. "jar:file:/path/to/jar/mongo-java-driver-3.4.2.jar!/"

On Java 9 this may no longer work, depending on how an application deploys the Java driver. There are three possibilities:

  1. Deployed on the classpath
    • the existing code will continue to work
  2. Deployed on the module path as a Java 8-style jar file (like we currently create):
    • tests of java 9 early access builds show that un-named modules like this can not be added to a runtime image via jlink, so the existing code looks like it will still work: the code source of the un-named module will just be an URL with the "file" scheme, as above
  3. Deployed on the module path as a Java 9-style versioned modular jar file (of our creation)
    • Replace with call to ClientMetadataHelper.class.getModule().getDescriptor().rawVersion(). This will work whether the jar file is available as a "file" scheme URL or as part of a runtime image.


 Comments   
Comment by Jeffrey Yemin [ 16/Aug/18 ]

JAVA-2900 removes any dependency on the location of the jar file, so closing this as a duplicate.

Comment by Jeffrey Yemin [ 07/May/17 ]

Note that a jar file named "mongo-java-driver-3.4.2", when deployed as an automatic module, will have the module name "mongo.java.driver" and the module version 3.4.2.

Comment by Jeffrey Yemin [ 07/May/17 ]

    String getVersion(final Class clazz) throws IOException {
        String version = "unknown";
        // note that Class.getModule is new to Java 9 so this will have to be handled accordingly for pre-Java 9 platforms
        if (clazz.getModule() != null && clazz.getModule().getDescriptor() != null) {   
            if (clazz.getModule().getDescriptor().rawVersion().isPresent()) {
                version = clazz.getModule().getDescriptor().rawVersion().get();
            } else {
                try (InputStream manifestInputStream = clazz.getModule().getResourceAsStream("META-INF/MANIFEST.MF")) {
                    version = (String) new Manifest(manifestInputStream).getMainAttributes().get(new Attributes.Name("Build-Version"));
                }
            }
        } else {
            // as current
        }
        return version;
    }

Generated at Thu Feb 08 08:57:24 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.