-
Notifications
You must be signed in to change notification settings - Fork 17
adds support for mongo-oidc #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,19 @@ | ||
| package com.dbschema; | ||
|
|
||
| import com.dbschema.mongo.DriverPropertyInfoHelper; | ||
| import com.dbschema.mongo.MongoClientWrapper; | ||
| import com.dbschema.mongo.MongoConnection; | ||
| import com.dbschema.mongo.MongoService; | ||
| import com.dbschema.mongo.mongosh.LazyShellHolder; | ||
| import com.dbschema.mongo.mongosh.PrecalculatingShellHolder; | ||
| import com.dbschema.mongo.mongosh.ShellHolder; | ||
| import com.dbschema.mongo.oidc.OidcCallback; | ||
| import org.graalvm.polyglot.Engine; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| import java.sql.*; | ||
| import java.util.Optional; | ||
| import java.util.Properties; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.logging.Logger; | ||
|
|
@@ -28,127 +32,136 @@ | |
| * The URL excepting the jdbc: prefix is passed as it is to the MongoDb native Java driver. | ||
| */ | ||
| public class MongoJdbcDriver implements Driver { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The changes are hard to read because the diff includes unnecessary style edits (formatting, etc.). Please remove them or split the commits so that only the actual logic changes are visible |
||
| private final DriverPropertyInfoHelper propertyInfoHelper = new DriverPropertyInfoHelper(); | ||
| private @Nullable ExecutorService executorService; | ||
| private @Nullable Engine sharedEngine; | ||
| private @NotNull ShellHolder shellHolder; | ||
|
|
||
| static { | ||
| try { | ||
| DriverManager.registerDriver(new MongoJdbcDriver()); | ||
| private final DriverPropertyInfoHelper propertyInfoHelper = new DriverPropertyInfoHelper(); | ||
| private @Nullable ExecutorService executorService; | ||
| private @Nullable Engine sharedEngine; | ||
| private @NotNull ShellHolder shellHolder; | ||
| private MongoConnection mongoConnection; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not cache the DB connection. If you need to cache the token, cache that instead |
||
|
|
||
| static { | ||
| try { | ||
| DriverManager.registerDriver(new MongoJdbcDriver()); | ||
| } catch (SQLException ex) { | ||
| ex.printStackTrace(); | ||
| } | ||
| } | ||
| catch (SQLException ex) { | ||
| ex.printStackTrace(); | ||
|
|
||
| public MongoJdbcDriver() { | ||
| shellHolder = createShellHolder(); | ||
| } | ||
| } | ||
|
|
||
| public MongoJdbcDriver() { | ||
| shellHolder = createShellHolder(); | ||
| } | ||
| @NotNull | ||
| private ShellHolder createShellHolder() { | ||
| if ("true".equals(System.getProperty("mongosh.disableShellPrecalculation"))) { | ||
| return new LazyShellHolder(); | ||
| } | ||
| if (executorService == null) { | ||
| executorService = newFixedThreadPool(10, newNamedThreadFactory("MongoShell ExecutorService")); | ||
| } | ||
| Engine engine = null; | ||
| if (!"true".equals(System.getProperty("mongosh.disableSharedEngine"))) { | ||
| if (sharedEngine == null) { | ||
| sharedEngine = Engine.create("js"); | ||
| } | ||
| engine = sharedEngine; | ||
| } | ||
| return new PrecalculatingShellHolder(executorService, engine); | ||
| } | ||
|
|
||
| @NotNull | ||
| private ShellHolder createShellHolder() { | ||
| if ("true".equals(System.getProperty("mongosh.disableShellPrecalculation"))) { | ||
| return new LazyShellHolder(); | ||
| /** | ||
| * Connect to the database using a URL like : | ||
| * jdbc:mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]] | ||
| * The URL excepting the jdbc: prefix is passed as it is to the MongoDb native Java driver. | ||
| */ | ||
| public Connection connect(String url, Properties info) throws SQLException { | ||
| if (url == null || !acceptsURL(url)) return null; | ||
|
|
||
| int fetchDocumentsForMeta = FETCH_DOCUMENTS_FOR_METAINFO_DEFAULT; | ||
| if (info.getProperty(FETCH_DOCUMENTS_FOR_METAINFO) != null) { | ||
| try { | ||
| fetchDocumentsForMeta = Integer.parseInt(info.getProperty(FETCH_DOCUMENTS_FOR_METAINFO)); | ||
| } catch (NumberFormatException ignored) { | ||
| } | ||
| } | ||
| if (fetchDocumentsForMeta < 0) fetchDocumentsForMeta = 0; | ||
|
|
||
| if (url.startsWith("jdbc:")) { | ||
| url = url.substring("jdbc:".length()); | ||
| } | ||
|
|
||
| String username = info.getProperty("user"); | ||
| String password = info.getProperty("password"); | ||
| synchronized (this) { | ||
| ShellHolder shellHolder = this.shellHolder; | ||
| this.shellHolder = createShellHolder(); | ||
|
|
||
| var existingResult = Optional.ofNullable(this.mongoConnection) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't use "var" |
||
| .map(MongoConnection::getService) | ||
| .map(MongoService::getClient) | ||
| .map(MongoClientWrapper::getOidcCallback) | ||
| .map(OidcCallback::getCallbackContext) | ||
| .orElse(null); | ||
|
|
||
| this.mongoConnection = new MongoConnection(url, info, username, password, fetchDocumentsForMeta, shellHolder, existingResult); | ||
|
|
||
| return this.mongoConnection; | ||
| } | ||
| } | ||
| if (executorService == null) { | ||
| executorService = newFixedThreadPool(10, newNamedThreadFactory("MongoShell ExecutorService")); | ||
|
|
||
|
|
||
| /** | ||
| * URLs accepted are of the form: jdbc:mongodb[+srv]://<server>[:27017]/<db-name> | ||
| * | ||
| * @see java.sql.Driver#acceptsURL(java.lang.String) | ||
| */ | ||
| @Override | ||
| public boolean acceptsURL(String url) { | ||
| if (url.startsWith("jdbc:")) { | ||
| url = url.substring("jdbc:".length()); | ||
| } | ||
| return url.startsWith("mongodb://") || url.startsWith("mongodb+srv://"); | ||
| } | ||
| Engine engine = null; | ||
| if (!"true".equals(System.getProperty("mongosh.disableSharedEngine"))) { | ||
| if (sharedEngine == null) { | ||
| sharedEngine = Engine.create("js"); | ||
| } | ||
| engine = sharedEngine; | ||
|
|
||
| /** | ||
| * @see java.sql.Driver#getPropertyInfo(java.lang.String, java.util.Properties) | ||
| */ | ||
| @Override | ||
| public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) { | ||
| return propertyInfoHelper.getPropertyInfo(); | ||
| } | ||
| return new PrecalculatingShellHolder(executorService, engine); | ||
| } | ||
|
|
||
| /** | ||
| * Connect to the database using a URL like : | ||
| * jdbc:mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]] | ||
| * The URL excepting the jdbc: prefix is passed as it is to the MongoDb native Java driver. | ||
| */ | ||
| public Connection connect(String url, Properties info) throws SQLException { | ||
| if (url == null || !acceptsURL(url)) return null; | ||
|
|
||
| int fetchDocumentsForMeta = FETCH_DOCUMENTS_FOR_METAINFO_DEFAULT; | ||
| if (info.getProperty(FETCH_DOCUMENTS_FOR_METAINFO) != null) { | ||
| try { | ||
| fetchDocumentsForMeta = Integer.parseInt(info.getProperty(FETCH_DOCUMENTS_FOR_METAINFO)); | ||
| } | ||
| catch (NumberFormatException ignored) { | ||
| } | ||
|
|
||
| /** | ||
| * @see java.sql.Driver#getMajorVersion() | ||
| */ | ||
| @Override | ||
| public int getMajorVersion() { | ||
| return 1; | ||
| } | ||
|
|
||
| /** | ||
| * @see java.sql.Driver#getMinorVersion() | ||
| */ | ||
| @Override | ||
| public int getMinorVersion() { | ||
| return 0; | ||
| } | ||
| if (fetchDocumentsForMeta < 0) fetchDocumentsForMeta = 0; | ||
|
|
||
| if (url.startsWith("jdbc:")) { | ||
| url = url.substring("jdbc:".length()); | ||
| /** | ||
| * @see java.sql.Driver#jdbcCompliant() | ||
| */ | ||
| @Override | ||
| public boolean jdbcCompliant() { | ||
| return true; | ||
| } | ||
|
|
||
| String username = info.getProperty("user"); | ||
| String password = info.getProperty("password"); | ||
| synchronized (this) { | ||
| ShellHolder shellHolder = this.shellHolder; | ||
| this.shellHolder = createShellHolder(); | ||
| return new MongoConnection(url, info, username, password, fetchDocumentsForMeta, shellHolder); | ||
| @Override | ||
| public Logger getParentLogger() { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * URLs accepted are of the form: jdbc:mongodb[+srv]://<server>[:27017]/<db-name> | ||
| * | ||
| * @see java.sql.Driver#acceptsURL(java.lang.String) | ||
| */ | ||
| @Override | ||
| public boolean acceptsURL(String url) { | ||
| if (url.startsWith("jdbc:")) { | ||
| url = url.substring("jdbc:".length()); | ||
|
|
||
| public void close() { | ||
| shellHolder.close(); | ||
| if (sharedEngine != null) sharedEngine.close(); | ||
| if (executorService != null) executorService.shutdownNow(); | ||
| } | ||
| return url.startsWith("mongodb://") || url.startsWith("mongodb+srv://"); | ||
| } | ||
|
|
||
| /** | ||
| * @see java.sql.Driver#getPropertyInfo(java.lang.String, java.util.Properties) | ||
| */ | ||
| @Override | ||
| public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) { | ||
| return propertyInfoHelper.getPropertyInfo(); | ||
| } | ||
|
|
||
| /** | ||
| * @see java.sql.Driver#getMajorVersion() | ||
| */ | ||
| @Override | ||
| public int getMajorVersion() { | ||
| return 1; | ||
| } | ||
|
|
||
| /** | ||
| * @see java.sql.Driver#getMinorVersion() | ||
| */ | ||
| @Override | ||
| public int getMinorVersion() { | ||
| return 0; | ||
| } | ||
|
|
||
| /** | ||
| * @see java.sql.Driver#jdbcCompliant() | ||
| */ | ||
| @Override | ||
| public boolean jdbcCompliant() { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public Logger getParentLogger() { | ||
| return null; | ||
| } | ||
|
|
||
| public void close() { | ||
| shellHolder.close(); | ||
| if (sharedEngine != null) sharedEngine.close(); | ||
| if (executorService != null) executorService.shutdownNow(); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please describe the purpose of this change