All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tornadofx.osgi.impl.OSGISupport.kt Maven / Gradle / Ivy

There is a newer version: 1.7.20
Show newest version
@file:Suppress("UNCHECKED_CAST")

package tornadofx.osgi.impl

import org.osgi.framework.Bundle
import org.osgi.framework.BundleContext
import org.osgi.framework.FrameworkUtil
import org.osgi.framework.ServiceEvent
import org.osgi.util.tracker.ServiceTracker
import tornadofx.FX
import tornadofx.withEach
import java.util.logging.Level
import kotlin.reflect.KClass

val ServiceEvent.objectClass: String
    get() = (serviceReference.getProperty("objectClass") as Array)[0]

val fxBundle: Bundle get() = FrameworkUtil.getBundle(Activator::class.java)
val fxBundleContext: BundleContext get() = fxBundle.bundleContext

/**
 * Try to resolve the OSGi Bundle Id of the given class. This function can only be called
 * if OSGi is available on the classpath.
 */
fun getBundleId(classFromBundle: KClass<*>): Long? {
    try {
        return FrameworkUtil.getBundle(classFromBundle.java)?.bundleId
    } catch (ex: Exception) {
        FX.log.log(Level.WARNING, "OSGi was on the classpath but no Framework did not respond correctly", ex)
        return null
    }
}
inline fun  getBundleId(): Long? = getBundleId(T::class)

inline fun  ServiceTracker.withEach(fn: (S) -> Unit) {
    services?.withEach {fn(this as S) }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy