Making sure that OSGi docs don't break the build and making sure that the osgi artifacts are bundled in the project

This commit is contained in:
Viktor Klang 2012-07-01 13:31:11 +02:00
parent 34b05e554b
commit 3797b72c45
5 changed files with 53 additions and 76 deletions

View file

@ -1,18 +1,24 @@
import akka.actor.{ Props, ActorSystem }
import akka.osgi.ActorSystemActivator
import org.apache.servicemix.examples.akka.Listener
import org.apache.servicemix.examples.akka.Master
package docs.osgi
case object SomeMessage
class SomeActor extends akka.actor.Actor {
def receive = { case SomeMessage }
}
//#Activator
class Activator extends ActorSystemActivator("PiSystem") {
import akka.actor.{ Props, ActorSystem }
import org.osgi.framework.BundleContext
import akka.osgi.ActorSystemActivator
class Activator extends ActorSystemActivator {
def configure(context: BundleContext, system: ActorSystem) {
// optionally register the ActorSystem in the OSGi Service Registry
registerService(context, system)
val listener = system.actorOf(Props[Listener], name = "listener")
val master = system.actorOf(Props(new Master(4, 10000, 10000, listener)), name = "master")
master ! Calculate
val someActor = system.actorOf(Props[SomeActor], name = "someName")
someActor ! SomeMessage
}
}

View file

@ -35,18 +35,17 @@ class SimpleNamespaceHandlerTest extends WordSpec with MustMatchers with PojoSRT
"simple.xml" must {
"set up ActorSystem when bundle starts" in {
val system = serviceForType[ActorSystem]
assert(system != null)
serviceForType[ActorSystem] must not be (null)
}
"stop the ActorSystem when bundle stops" in {
val system = serviceForType[ActorSystem]
assert(!system.isTerminated)
system.isTerminated must be(false)
bundleForName(TEST_BUNDLE_NAME).stop()
system.awaitTermination()
assert(system.isTerminated)
system.isTerminated must be(true)
}
}
@ -63,19 +62,19 @@ class ConfigNamespaceHandlerTest extends WordSpec with MustMatchers with PojoSRT
"config.xml" must {
"set up ActorSystem when bundle starts" in {
val system = serviceForType[ActorSystem]
assert(system != null)
system must not be (null)
assert(system.settings.config.getString("some.config.key") == "value")
system.settings.config.getString("some.config.key") must be("value")
}
"stop the ActorSystem when bundle stops" in {
val system = serviceForType[ActorSystem]
assert(!system.isTerminated)
system.isTerminated must be(false)
bundleForName(TEST_BUNDLE_NAME).stop()
system.awaitTermination()
assert(system.isTerminated)
system.isTerminated must be(true)
}
}
@ -93,8 +92,8 @@ class DependencyInjectionNamespaceHandlerTest extends WordSpec with MustMatchers
"set up bean containing ActorSystem" in {
val bean = serviceForType[ActorSystemAwareBean]
assert(bean != null)
assert(bean.system != null)
bean must not be (null)
bean.system must not be (null)
}
}

View file

@ -37,19 +37,17 @@ class PingPongActorSystemActivatorTest extends WordSpec with MustMatchers with P
val actor = system.actorFor("/user/pong")
implicit val timeout = Timeout(5 seconds)
val future = actor ? Ping
val result = Await.result(future, timeout.duration)
assert(result != null)
Await.result(actor ? Ping, timeout.duration) must be(Pong)
}
"stop the ActorSystem when bundle stops" in {
val system = serviceForType[ActorSystem]
assert(!system.isTerminated)
system.isTerminated must be(false)
bundleForName(TEST_BUNDLE_NAME).stop()
system.awaitTermination()
assert(system.isTerminated)
system.isTerminated must be(true)
}
}
@ -59,15 +57,12 @@ class RuntimeNameActorSystemActivatorTest extends WordSpec with MustMatchers wit
import ActorSystemActivatorTest._
val testBundles: Seq[BundleDescriptor] = buildTestBundles(Seq(
bundle(TEST_BUNDLE_NAME).withActivator(classOf[RuntimeNameActorSystemActivator])))
val testBundles: Seq[BundleDescriptor] = buildTestBundles(Seq(bundle(TEST_BUNDLE_NAME).withActivator(classOf[RuntimeNameActorSystemActivator])))
"RuntimeNameActorSystemActivator" must {
"register an ActorSystem and add the bundle id to the system name" in {
val system = serviceForType[ActorSystem]
val bundle = bundleForName(TEST_BUNDLE_NAME)
system.name must equal(TestActivators.ACTOR_SYSTEM_NAME_PATTERN.format(bundle.getBundleId))
serviceForType[ActorSystem].name must equal(TestActivators.ACTOR_SYSTEM_NAME_PATTERN.format(bundleForName(TEST_BUNDLE_NAME).getBundleId))
}
}

View file

@ -36,10 +36,7 @@ trait PojoSRTestSupport extends Suite with BeforeAndAfterAll {
bundles.addAll(testBundles)
config.put(PojoServiceRegistryFactory.BUNDLE_DESCRIPTORS, bundles)
val loader: ServiceLoader[PojoServiceRegistryFactory] = ServiceLoader.load(classOf[PojoServiceRegistryFactory])
val registry = loader.iterator.next.newPojoServiceRegistry(config)
registry.getBundleContext
ServiceLoader.load(classOf[PojoServiceRegistryFactory]).iterator.next.newPojoServiceRegistry(config).getBundleContext
}
// Ensure bundles get stopped at the end of the test to release resources and stop threads
@ -48,25 +45,21 @@ trait PojoSRTestSupport extends Suite with BeforeAndAfterAll {
/**
* Convenience method to find a bundle by symbolic name
*/
def bundleForName(name: String) = context.getBundles.find(_.getSymbolicName == name) match {
case Some(bundle) bundle
case None fail("Unable to find bundle with symbolic name %s".format(name))
}
def bundleForName(name: String) =
context.getBundles.find(_.getSymbolicName == name).getOrElse(fail("Unable to find bundle with symbolic name %s".format(name)))
/**
* Convenience method to find a service by interface. If the service is not already available in the OSGi Service
* Registry, this method will wait for a few seconds for the service to appear.
*/
def serviceForType[T](implicit manifest: Manifest[T]): T = {
val reference = awaitReference(manifest.erasure)
context.getService(reference).asInstanceOf[T]
}
def serviceForType[T](implicit manifest: Manifest[T]): T =
context.getService(awaitReference(manifest.erasure)).asInstanceOf[T]
def awaitReference(serviceType: Class[_]): ServiceReference = awaitReference(serviceType, START_WAIT_TIME)
def awaitReference(serviceType: Class[_], wait: Long): ServiceReference = {
val option = Option(context.getServiceReference(serviceType.getName))
Thread.sleep(wait)
Thread.sleep(wait) //FIXME No sleep please
option match {
case Some(reference) reference
case None if (wait > MAX_WAIT_TIME) fail("Gave up waiting for service of type %s".format(serviceType))
@ -78,12 +71,10 @@ trait PojoSRTestSupport extends Suite with BeforeAndAfterAll {
}
object PojoSRTestSupport {
/**
* Convenience method to define additional test bundles
*/
def bundle(name: String) = new BundleDescriptorBuilder(name)
}
/**
@ -98,22 +89,24 @@ class BundleDescriptorBuilder(name: String) {
/**
* Add a Blueprint XML file to our test bundle
*/
def withBlueprintFile(name: String, contents: URL): BundleDescriptorBuilder =
returnBuilder(tinybundle.add("OSGI-INF/blueprint/%s".format(name), contents))
def withBlueprintFile(name: String, contents: URL): BundleDescriptorBuilder = {
tinybundle.add("OSGI-INF/blueprint/%s".format(name), contents)
this
}
/**
* Add a Blueprint XML file to our test bundle
*/
def withBlueprintFile(contents: URL): BundleDescriptorBuilder = withBlueprintFile(filename(contents), contents)
def withBlueprintFile(contents: URL): BundleDescriptorBuilder = {
val filename = contents.getFile.split("/").last
withBlueprintFile(filename, contents)
}
/**
* Add a Bundle activator to our test bundle
*/
def withActivator(activator: Class[_ <: BundleActivator]): BundleDescriptorBuilder =
returnBuilder(tinybundle.set(Constants.BUNDLE_ACTIVATOR, activator.getName))
private def returnBuilder(block: Unit) = {
block
def withActivator(activator: Class[_ <: BundleActivator]): BundleDescriptorBuilder = {
tinybundle.set(Constants.BUNDLE_ACTIVATOR, activator.getName)
this
}
@ -122,11 +115,7 @@ class BundleDescriptorBuilder(name: String) {
*/
def build: BundleDescriptor = {
val file: File = tinybundleToJarFile(name)
new BundleDescriptor(
getClass().getClassLoader(),
new URL("jar:" + file.toURI().toString() + "!/"),
extractHeaders(file))
new BundleDescriptor(getClass().getClassLoader(), new URL("jar:" + file.toURI().toString() + "!/"), extractHeaders(file))
}
def extractHeaders(file: File): HashMap[String, String] = {
@ -134,12 +123,9 @@ class BundleDescriptorBuilder(name: String) {
val jis = new JarInputStream(new FileInputStream(file))
try {
for (entry jis.getManifest().getMainAttributes().entrySet()) {
for (entry jis.getManifest().getMainAttributes().entrySet())
headers.put(entry.getKey().toString(), entry.getValue().toString())
}
} finally {
jis.close()
}
} finally jis.close()
headers
}
@ -147,14 +133,9 @@ class BundleDescriptorBuilder(name: String) {
def tinybundleToJarFile(name: String): File = {
val file = new File("target/%s-%tQ.jar".format(name, new Date()))
val fos = new FileOutputStream(file)
try {
copy(tinybundle.build(), fos)
} finally {
fos.close()
}
try copy(tinybundle.build(), fos) finally fos.close()
file
}
private[this] def filename(url: URL) = url.getFile.split("/").last
}

View file

@ -43,7 +43,7 @@ object AkkaBuild extends Build {
sphinxLatex <<= sphinxLatex in LocalProject(docs.id),
sphinxPdf <<= sphinxPdf in LocalProject(docs.id)
),
aggregate = Seq(actor, testkit, actorTests, remote, remoteTests, camel, cluster, slf4j, agent, transactor, mailboxes, zeroMQ, kernel, akkaSbtPlugin, samples, tutorials, docs)
aggregate = Seq(actor, testkit, actorTests, remote, remoteTests, camel, cluster, slf4j, agent, transactor, mailboxes, zeroMQ, kernel, akkaSbtPlugin, samples, tutorials, osgi, osgiAries, docs)
)
lazy val actor = Project(
@ -299,7 +299,7 @@ object AkkaBuild extends Build {
id = "akka-docs",
base = file("akka-docs"),
dependencies = Seq(actor, testkit % "test->test", mailboxesCommon % "compile;test->test",
remote, cluster, slf4j, agent, transactor, fileMailbox, zeroMQ, camel),
remote, cluster, slf4j, agent, transactor, fileMailbox, zeroMQ, camel, osgi, osgiAries),
settings = defaultSettings ++ Sphinx.settings ++ Seq(
unmanagedSourceDirectories in Test <<= baseDirectory { _ ** "code" get },
libraryDependencies ++= Dependencies.docs,
@ -560,13 +560,9 @@ object OSGi {
val mailboxesCommon = exports(Seq("akka.actor.mailbox.*"))
val osgi = exports(Seq("akka.osgi")) ++ Seq(
OsgiKeys.privatePackage := Seq("akka.osgi.impl")
)
val osgi = exports(Seq("akka.osgi")) ++ Seq(OsgiKeys.privatePackage := Seq("akka.osgi.impl"))
val osgiAries = exports() ++ Seq(
OsgiKeys.privatePackage := Seq("akka.osgi.aries.*")
)
val osgiAries = exports() ++ Seq(OsgiKeys.privatePackage := Seq("akka.osgi.aries.*"))
val remote = exports(Seq("akka.remote.*", "akka.routing.*", "akka.serialization.*"))