Skip to content

Commit 44aabf8

Browse files
committed
refactor(ort-utils): Split Extensions.kt and Utils.kt
Similar to 6d34ce5, split `ort-utils` by functionality instead of function type. Signed-off-by: Sebastian Schuberth <[email protected]>
1 parent d1d0a3d commit 44aabf8

File tree

13 files changed

+914
-776
lines changed

13 files changed

+914
-776
lines changed

plugins/scanners/fossid/src/test/kotlin/FossIdUrlProviderTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ private fun mockAuthenticator(
137137
port: Int = PORT,
138138
authentication: PasswordAuthentication? = AUTHENTICATION
139139
) {
140-
mockkStatic("org.ossreviewtoolkit.utils.ort.UtilsKt")
140+
mockkStatic("org.ossreviewtoolkit.utils.ort.AuthenticationUtilsKt")
141141
every { requestPasswordAuthentication(any()) } answers {
142142
val uri = firstArg<URI>()
143143
authentication.takeIf { uri.host == host && uri.port == port && uri.scheme == "https" && uri.userInfo == null }

plugins/version-control-systems/git/src/test/kotlin/GitTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ private val testUriAsUrl = URI.create(testUri.toString()).toURL()
185185
* Mocks the utility function to query password authentication for the test URI. Return the [result] provided.
186186
*/
187187
private fun mockAuthentication(result: PasswordAuthentication?) {
188-
mockkStatic("org.ossreviewtoolkit.utils.ort.UtilsKt")
188+
mockkStatic("org.ossreviewtoolkit.utils.ort.AuthenticationUtilsKt")
189189

190190
every {
191191
requestPasswordAuthentication(testUri.host, testUri.port, testUri.scheme, testUriAsUrl)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (C) 2017 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
package org.ossreviewtoolkit.utils.ort
21+
22+
import java.net.Authenticator
23+
import java.net.PasswordAuthentication
24+
import java.net.URI
25+
import java.net.URL
26+
27+
/**
28+
* Request a [PasswordAuthentication] object for the given [host], [port], [scheme], and optional [url]. Install the
29+
* [OrtAuthenticator] and the [OrtProxySelector] beforehand to ensure they are active.
30+
*/
31+
fun requestPasswordAuthentication(host: String, port: Int, scheme: String, url: URL? = null): PasswordAuthentication? {
32+
OrtAuthenticator.install()
33+
OrtProxySelector.install()
34+
35+
return Authenticator.requestPasswordAuthentication(
36+
/* host = */ host,
37+
/* addr = */ null,
38+
/* port = */ port,
39+
/* protocol = */ scheme,
40+
/* prompt = */ null,
41+
/* scheme = */ null,
42+
/* url = */ url,
43+
/* reqType = */ Authenticator.RequestorType.SERVER
44+
)
45+
}
46+
47+
/**
48+
* Request a [PasswordAuthentication] object for the given [uri]. Install the [OrtAuthenticator] and the
49+
* [OrtProxySelector] beforehand to ensure they are active.
50+
*/
51+
fun requestPasswordAuthentication(uri: URI): PasswordAuthentication? =
52+
requestPasswordAuthentication(uri.host, uri.port, uri.scheme, uri.toURL())
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (C) 2017 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
package org.ossreviewtoolkit.utils.ort
21+
22+
import kotlin.coroutines.CoroutineContext
23+
import kotlin.coroutines.EmptyCoroutineContext
24+
25+
import kotlinx.coroutines.CoroutineScope
26+
27+
import org.apache.logging.log4j.kotlin.CoroutineThreadContext
28+
29+
/**
30+
* Global variable that gets toggled by a command line parameter parsed in the main entry points of the modules.
31+
*/
32+
var printStackTrace = false
33+
34+
/**
35+
* Print the stack trace of the [Throwable] if [printStackTrace] is set to true.
36+
*/
37+
fun Throwable.showStackTrace(): Unit = run { if (printStackTrace) printStackTrace() }
38+
39+
/**
40+
* A wrapper for [kotlinx.coroutines.runBlocking] which always adds a [CoroutineThreadContext] to the newly created
41+
* coroutine context. This ensures that Log4j's MDC context is not lost when `runBlocking` is used.
42+
*
43+
* This function should be used instead of [kotlinx.coroutines.runBlocking] in all code that can be used as a library to
44+
* preserve any MDC context set by a consumer.
45+
*/
46+
fun <T> runBlocking(context: CoroutineContext = EmptyCoroutineContext, block: suspend CoroutineScope.() -> T): T =
47+
@Suppress("ForbiddenMethodCall")
48+
kotlinx.coroutines.runBlocking(context + CoroutineThreadContext()) { block() }

utils/ort/src/main/kotlin/Extensions.kt renamed to utils/ort/src/main/kotlin/TempUtils.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,3 @@ fun createOrtTempFile(prefix: String? = null, suffix: String? = null): File =
4848
*/
4949
fun Any.createOrtTempFile(prefix: String? = null, suffix: String? = null): File =
5050
kotlin.io.path.createTempFile(createOrtTempDir().toPath(), prefix, suffix).toFile()
51-
52-
/**
53-
* Print the stack trace of the [Throwable] if [printStackTrace] is set to true.
54-
*/
55-
fun Throwable.showStackTrace(): Unit = run { if (printStackTrace) printStackTrace() }

utils/ort/src/main/kotlin/Utils.kt

Lines changed: 0 additions & 234 deletions
This file was deleted.

0 commit comments

Comments
 (0)