|
| 1 | +[#ref-socket-factory] |
| 2 | +=== Custom socket factory |
| 3 | + |
| 4 | +[.since]_Jaybird 6_ |
| 5 | + |
| 6 | +A custom socket factory can be specified to customize the creation of the `java.net.Socket` instance of a pure Java database or service connection. |
| 7 | + |
| 8 | +The connection property `socketFactory` accepts the class name of an implementation of `javax.net.SocketFactory`. |
| 9 | +This socket factory is created anew for each connection. |
| 10 | +If `socketFactory` is not specified, Jaybird will use `SocketFactory.getDefault()` as its factory. |
| 11 | + |
| 12 | +The `SocketFactory` implementation must adhere to the following rules: |
| 13 | + |
| 14 | +- The class must have a public constructor accepting a `java.util.Properties` object, or a public no-arg constructor. |
| 15 | +- The implementation of `SocketFactory#createSocket()` must return an unconnected socket; |
| 16 | +the other `createSocket` methods are not called by Jaybird. |
| 17 | ++ |
| 18 | +If you don't want to implement the other `createSocket` methods, we recommend throwing `java.lang.UnsupportedOperationException` with a clear message from those methods. |
| 19 | +
|
| 20 | +It is possible to pass custom connection properties to the socket factory if it has a public single-arg constructor accepting a `Properties` object. |
| 21 | +Jaybird will instantiate the socket factory with a `Properties` object containing _only_ the connection properties with the suffix `@socketFactory` and non-``null`` values; |
| 22 | +non-string values are converted to string. |
| 23 | +In the future, we may also -- selectively -- pass other connection properties, but for now we only expose those properties that are explicitly set for the socket factory. |
| 24 | + |
| 25 | +For example, say we have some custom socket factory called `org.example.CustomProxySocketFactory` with a `CustomProxySocketFactory(Properties)` constructor: |
| 26 | + |
| 27 | +[source,java] |
| 28 | +---- |
| 29 | +var props = new Properties() |
| 30 | +props.setProperty("user", "sysdba"); |
| 31 | +props.setProperty("password", "masterkey"); |
| 32 | +props.setProperty("socketFactory", "org.example.CustomProxySocketFactory"); |
| 33 | +props.setProperty("proxyHost@socketFactory", "localhost"); |
| 34 | +props.setProperty("proxyPort@socketFactory", "1234"); |
| 35 | +props.setProperty("proxyUser@socketFactory", "proxy-user"); |
| 36 | +props.setProperty("proxyPassword@socketFactory", "proxy-password"); |
| 37 | +
|
| 38 | +try (var connection = DriverManager.getConnection( |
| 39 | + "jdbc:firebird://remoteserver.example.org/db", props)) { |
| 40 | + // use connection |
| 41 | +} |
| 42 | +---- |
| 43 | + |
| 44 | +This will create the specified socket factory, passing a `Properties` object containing *only* the four custom properties ending in `@socketFactory`. |
| 45 | +The other properties -- here `user`, `password` and `socketFactory` -- are *not* passed to the socket factory. |
| 46 | + |
| 47 | +See also https://github.com/FirebirdSQL/jaybird/blob/master/devdoc/jdp/jdp-2024-09-custom-socket-factory-for-pure-java-connections.adoc[jdp-2024-09: Custom socket factory for pure Java connections] |
0 commit comments