Skip to content
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

release: 0.1.0-alpha.13 #38

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "0.1.0-alpha.12"
".": "0.1.0-alpha.13"
}
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 0.1.0-alpha.13 (2024-10-28)

Full Changelog: [v0.1.0-alpha.12...v0.1.0-alpha.13](https://github.com/OneBusAway/java-sdk/compare/v0.1.0-alpha.12...v0.1.0-alpha.13)

### Chores

* rebuild project due to codegen change ([#37](https://github.com/OneBusAway/java-sdk/issues/37)) ([cadf9d3](https://github.com/OneBusAway/java-sdk/commit/cadf9d3c803c89e67896bd8aaf38e13b6f4c0664))

## 0.1.0-alpha.12 (2024-10-25)

Full Changelog: [v0.1.0-alpha.11...v0.1.0-alpha.12](https://github.com/OneBusAway/java-sdk/compare/v0.1.0-alpha.11...v0.1.0-alpha.12)
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<!-- x-release-please-start-version -->

[![Maven Central](https://img.shields.io/maven-central/v/org.onebusaway/onebusaway-sdk-java)](https://central.sonatype.com/artifact/org.onebusaway/onebusaway-sdk-java/0.1.0-alpha.12)
[![Maven Central](https://img.shields.io/maven-central/v/org.onebusaway/onebusaway-sdk-java)](https://central.sonatype.com/artifact/org.onebusaway/onebusaway-sdk-java/0.1.0-alpha.13)

<!-- x-release-please-end -->

Expand All @@ -27,7 +27,7 @@ The REST API documentation can be found on [developer.onebusaway.org](https://d
<!-- x-release-please-start-version -->

```kotlin
implementation("org.onebusaway:onebusaway-sdk-java:0.1.0-alpha.12")
implementation("org.onebusaway:onebusaway-sdk-java:0.1.0-alpha.13")
```

#### Maven
Expand All @@ -36,7 +36,7 @@ implementation("org.onebusaway:onebusaway-sdk-java:0.1.0-alpha.12")
<dependency>
<groupId>org.onebusaway</groupId>
<artifactId>onebusaway-sdk-java</artifactId>
<version>0.1.0-alpha.12</version>
<version>0.1.0-alpha.13</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {

allprojects {
group = "org.onebusaway"
version = "0.1.0-alpha.12" // x-release-please-version
version = "0.1.0-alpha.13" // x-release-please-version
}


Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.google.common.collect.ArrayListMultimap
import com.google.common.collect.ListMultimap
import java.time.Clock
import org.onebusaway.core.http.HttpClient
import org.onebusaway.core.http.PhantomReachableClosingHttpClient
import org.onebusaway.core.http.RetryingHttpClient

class ClientOptions
Expand Down Expand Up @@ -142,11 +143,13 @@ private constructor(

return ClientOptions(
httpClient!!,
RetryingHttpClient.builder()
.httpClient(httpClient!!)
.clock(clock)
.maxRetries(maxRetries)
.build(),
PhantomReachableClosingHttpClient(
RetryingHttpClient.builder()
.httpClient(httpClient!!)
.clock(clock)
.maxRetries(maxRetries)
.build()
),
jsonMapper ?: jsonMapper(),
clock,
baseUrl,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
@file:JvmName("PhantomReachable")

package org.onebusaway.core

import java.lang.reflect.InvocationTargetException
import org.onebusaway.errors.OnebusawaySdkException

/**
* Closes [closeable] when [observed] becomes only phantom reachable.
*
* This is a wrapper around a Java 9+ [java.lang.ref.Cleaner], or a no-op in older Java versions.
*/
@JvmSynthetic
internal fun closeWhenPhantomReachable(observed: Any, closeable: AutoCloseable) {
check(observed !== closeable) {
"`observed` cannot be the same object as `closeable` because it would never become phantom reachable"
}
closeWhenPhantomReachable?.let { it(observed, closeable::close) }
}

private val closeWhenPhantomReachable: ((Any, AutoCloseable) -> Unit)? by lazy {
try {
val cleanerClass = Class.forName("java.lang.ref.Cleaner")
val cleanerCreate = cleanerClass.getMethod("create")
val cleanerRegister =
cleanerClass.getMethod("register", Any::class.java, Runnable::class.java)
val cleanerObject = cleanerCreate.invoke(null);

{ observed, closeable ->
try {
cleanerRegister.invoke(cleanerObject, observed, Runnable { closeable.close() })
} catch (e: ReflectiveOperationException) {
if (e is InvocationTargetException) {
when (val cause = e.cause) {
is RuntimeException,
is Error -> throw cause
}
}
throw OnebusawaySdkException("Unexpected reflective invocation failure", e)
}
}
} catch (e: ReflectiveOperationException) {
// We're running Java 8, which has no Cleaner.
null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.onebusaway.core.http

import java.util.concurrent.CompletableFuture
import org.onebusaway.core.RequestOptions
import org.onebusaway.core.closeWhenPhantomReachable

internal class PhantomReachableClosingHttpClient(private val httpClient: HttpClient) : HttpClient {
init {
closeWhenPhantomReachable(this, httpClient)
}

override fun execute(request: HttpRequest, requestOptions: RequestOptions): HttpResponse =
httpClient.execute(request, requestOptions)

override fun executeAsync(
request: HttpRequest,
requestOptions: RequestOptions
): CompletableFuture<HttpResponse> = httpClient.executeAsync(request, requestOptions)

override fun close() = httpClient.close()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.onebusaway.core

import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test

internal class PhantomReachableTest {

@Test
fun closeWhenPhantomReachable_whenObservedIsGarbageCollected_closesCloseable() {
var closed = false
val closeable = AutoCloseable { closed = true }

closeWhenPhantomReachable(
// Pass an inline object for the object to observe so that it becomes immediately
// unreachable.
Any(),
closeable
)

assertThat(closed).isFalse()

System.gc()
Thread.sleep(3000)

assertThat(closed).isTrue()
}
}
Loading