Skip to content

Instantly share code, notes, and snippets.

@mpilquist
Last active December 19, 2015 02:19
Show Gist options
  • Save mpilquist/5882185 to your computer and use it in GitHub Desktop.
Save mpilquist/5882185 to your computer and use it in GitHub Desktop.
Example of creating an akka ActorSystem in OSGi without using akka-osgi built in support.
import scala.collection.JavaConverters._
import scalaz.std.option._
import java.net.URL
import java.util.Enumeration
class CompositeClassLoader(parent: ClassLoader, members: List[ClassLoader]) extends ClassLoader(parent) {
override def findClass(name: String): Class[_] = {
def tryLoadClass(cl: ClassLoader): Option[Class[_]] =
try some(cl.loadClass(name))
catch { case cnfe: ClassNotFoundException => none }
val cls = members.foldLeft(none[Class[_]]) { (cls, cl) => cls orElse tryLoadClass(cl) }
cls getOrElse this.getParent.loadClass(name)
}
override def findResource(name: String): URL = {
def tryGetResource(cl: ClassLoader): Option[URL] =
Option(cl.getResource(name))
val cls = members.foldLeft(none[URL]) { (cls, cl) => cls orElse tryGetResource(cl) }
cls getOrElse getParent.getResource(name)
}
override def findResources(name: String): Enumeration[URL] = {
val resources = members.map { _.getResources(name).asScala.toList }.flatten
java.util.Collections.enumeration(resources.asJava)
}
}
import akka.actor.ActorSystem
import com.typesafe.config.ConfigFactory
import org.eclipse.gemini.blueprint.util.BundleDelegatingClassLoader
import org.osgi.framework.BundleContext
object OsgiActorSystem {
def apply(bundleContext: BundleContext, name: Option[String], additionalLibraryClassLoaders: List[ClassLoader]): ActorSystem = {
val akkaActorClassLoader = classOf[ActorSystem].getClassLoader
val libraryClassLoader = new CompositeClassLoader(getClass.getClassLoader, akkaActorClassLoader :: additionalLibraryClassLoaders)
val mergedClassLoader = BundleDelegatingClassLoader.createBundleClassLoaderFor(bundleContext.getBundle, libraryClassLoader)
val config = OsgiConfigFactory.load(bundleContext). // OsgiConfigFactory just loads config for a specific bundle
withFallback(ConfigFactory.load(mergedClassLoader)).
withFallback(ConfigFactory.defaultReference(libraryClassLoader))
val nme = name getOrElse s"bundle-${bundleContext.getBundle.getBundleId}"
ActorSystem(nme, config, mergedClassLoader)
}
def apply(bundleContext: BundleContext): ActorSystem =
apply(bundleContext, None, Nil)
}
val system = OsgiActorSystem(bundleContext, None, List(
spray.util.Utils.getClass.getClassLoader,
spray.can.Http.getClass.getClassLoader,
classOf[spray.io.Pipelines].getClassLoader,
spray.routing.Directives.getClass.getClassLoader,
classOf[spray.servlet.WebBoot].getClassLoader))
@briantopping
Copy link

Hi Michael, hope you are well and happy holidays!

Is this still the preferred method of loading merged configs across all the spray bundles plus the user's application bundle? I've got a relatively persistent problem in bootstrapping with the new 1.2.0 code, getting "No configuration setting found for key 'manager-dispatcher'" when spray-can is loading, and it seems that there is a chain of configs that requires that they are resolved only after the fallbacks are in place.

Thanks for any insight you can provide here!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment