/
AkkaBean.scala
87 lines (70 loc) · 2.83 KB
/
AkkaBean.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package osgidemo.impl
import java.util.{Dictionary, Properties}
import akka.actor.{ActorRef, ActorSystem, ExtendedActorSystem, Props}
import akka.camel.ContextProvider
import akka.osgi.OsgiActorSystemFactory
import com.typesafe.config.{Config, ConfigFactory}
import org.apache.camel.blueprint.BlueprintCamelContext
import org.apache.camel.impl.DefaultCamelContext
import org.osgi.framework.{BundleContext, ServiceRegistration}
import osgidemo.impl.util.MyOsgiActorSystemFactory
//remove if not needed
import scala.collection.JavaConversions._
object AkkaCamelContextProvider {
@volatile var contextProvider: DefaultCamelContext = null
}
final class AkkaCamelContextProvider extends ContextProvider {
override def getContext(system: ExtendedActorSystem): DefaultCamelContext = {
AkkaCamelContextProvider.contextProvider
}
}
class AkkaBean {
def getSystem = system
var bundleContext: BundleContext = _
var camelContext: BlueprintCamelContext = _
def setBundleContext(bcontext: BundleContext) {
this.bundleContext = bcontext
}
def setCamelContext(camelContext: BlueprintCamelContext) {
this.camelContext = camelContext
}
private var system: Option[ActorSystem] = None
private var registration: Option[ServiceRegistration[_]] = None
private var akkaCamelBean: ActorRef = _
def start(): Unit = {
println("Start 1")
AkkaCamelContextProvider.contextProvider = camelContext.asInstanceOf[DefaultCamelContext]
println("Start 2")
val sysConfig = getActorSystemConfiguration(bundleContext)
println("Start 3")
val actorFactory = MyOsgiActorSystemFactory(bundleContext, sysConfig)
println("Start 4")
system = Some(actorFactory.createActorSystem(Option(getActorSystemName(bundleContext))))
println("Start 5")
//system foreach (addLogServiceListener(context, _))
system foreach (configure(bundleContext, _))
println("Start 6 end")
}
def stop(): Unit = {
stopWithContext(bundleContext)
}
def configure(context: BundleContext, system: ActorSystem): Unit = {
// Registers this ActorSystem as a service so other blueprints can use this ActorSystem
registerService(context, system)
}
def getActorSystemConfiguration(context: BundleContext): Config = ConfigFactory.empty
def getActorSystemName(context: BundleContext): String = "RemoteSystem"
// @todo Wire up blueprint stop
def stopWithContext(context: BundleContext): Unit = {
registration foreach (_.unregister())
system foreach (_.shutdown())
}
def registerService(context: BundleContext, system: ActorSystem): Unit = {
println("Register 1")
registration.foreach(_.unregister()) //Cleanup
val properties = new Properties()
properties.put("name", system.name)
registration = Some(context.registerService(classOf[ActorSystem].getName, system,
properties.asInstanceOf[Dictionary[String, Any]]))
}
}