| career | drupal | java | mac | mysql | perl | scala | uml | unix  

Akka/Scala example source code file (BundleDelegatingClassLoader.scala)

This example Akka source code file (BundleDelegatingClassLoader.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Akka and Scala source code examples by using tags.

All credit for the original source code belongs to; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Akka tags/keywords

akka, annotation, bundle, bundlecontext, bundledelegatingclassloader, class, classloader, io, list, option, osgi, set, string, url, utilities

The BundleDelegatingClassLoader.scala Akka example source code

 * Copyright (C) 2009-2014 Typesafe Inc. <>
package akka.osgi

import language.existentials
import java.util.Enumeration
import org.osgi.framework.{ BundleContext, Bundle }
import scala.util.Try
import org.osgi.framework.wiring.{ BundleRevision, BundleWire, BundleWiring }
import scala.collection.JavaConverters._
import scala.util.Success
import scala.util.Failure
import scala.annotation.tailrec

 * Companion object to create bundle delegating ClassLoader instances
object BundleDelegatingClassLoader {

   * Create a bundle delegating ClassLoader for the bundle context's bundle
  def apply(context: BundleContext): BundleDelegatingClassLoader = new BundleDelegatingClassLoader(context.getBundle, null)

  def apply(context: BundleContext, fallBackCLassLoader: Option[ClassLoader]): BundleDelegatingClassLoader =
    new BundleDelegatingClassLoader(context.getBundle, fallBackCLassLoader.orNull)

 * A bundle delegating ClassLoader implementation - this will try to load classes and resources from the bundle
 * and the bundles transitive dependencies. If there's a ClassLoader specified, that will be used as a fallback.
class BundleDelegatingClassLoader(bundle: Bundle, fallBackClassLoader: ClassLoader) extends ClassLoader(fallBackClassLoader) {

  private val bundles = findTransitiveBundles(bundle).toList

  override def findClass(name: String): Class[_] = {
    @tailrec def find(remaining: List[Bundle]): Class[_] = {
      if (remaining.isEmpty) throw new ClassNotFoundException(name)
      else Try { remaining.head.loadClass(name) } match {
        case Success(cls) ⇒ cls
        case Failure(_)   ⇒ find(remaining.tail)

  override def findResource(name: String): URL = {
    @tailrec def find(remaining: List[Bundle]): URL = {
      if (remaining.isEmpty) getParent.getResource(name)
      else Option { remaining.head.getResource(name) } match {
        case Some(r) ⇒ r
        case None    ⇒ find(remaining.tail)

  override def findResources(name: String): Enumeration[URL] = {
    val resources = bundles.flatMap {
      bundle ⇒ Option(bundle.getResources(name)).map { _.asScala.toList }.getOrElse(Nil)

  private def findTransitiveBundles(bundle: Bundle): Set[Bundle] = {
    @tailrec def process(processed: Set[Bundle], remaining: Set[Bundle]): Set[Bundle] = {
      if (remaining.isEmpty) {
      } else {
        val (b, rest) = (remaining.head, remaining.tail)
        if (processed contains b) {
          process(processed, rest)
        } else {
          val wiring = b.adapt(classOf[BundleWiring])
          val direct: Set[Bundle] =
            if (wiring == null) Set.empty
            else {
              val requiredWires: List[BundleWire] =
              requiredWires.flatMap {
                wire ⇒ Option(wire.getProviderWiring) map { _.getBundle }
          process(processed + b, rest ++ (direct -- processed))
    process(Set.empty, Set(bundle))

Other Akka source code examples

Here is a short list of links related to this Akka BundleDelegatingClassLoader.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller


new blog posts


Copyright 1998-2021 Alvin Alexander,
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.