tysonnorris commented on a change in pull request #2414: SPI approach for 
pluggable implementations
URL: 
https://github.com/apache/incubator-openwhisk/pull/2414#discussion_r124875188
 
 

 ##########
 File path: common/scala/src/main/scala/whisk/spi/SpiProvider.scala
 ##########
 @@ -0,0 +1,195 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package whisk.spi
+
+import java.util.ServiceLoader
+
+import akka.actor.ActorSystem
+import akka.actor.ExtendedActorSystem
+import akka.actor.Extension
+import akka.actor.ExtensionId
+import akka.actor.ExtensionIdProvider
+import scaldi.Binding
+import scaldi.DynamicModule
+import scaldi.Identifier
+import scaldi.ImmutableWrapper
+import scaldi.Injectable
+import scaldi.Injector
+import scaldi.MutableInjector
+
+import scala.collection.JavaConverters._
+import scala.collection.mutable.ListBuffer
+import scala.reflect.ClassTag
+import scala.reflect.runtime.universe._
+
+/**
+  * An Spi (Service Provider Interface) is an extension point.
+  * At runtime, multiple Spi implementations may exist in the classpath, but 
only a single implementation
+  * will be available for consumers to use. The specific implementation 
available at runtime is governed by the
+  * configKey specified in the SpiProvider
+  */
+trait Spi  extends Extension {
+
+}
+
+/**
+  * Spi implementations must extend SpiModule to register a factory for their 
implementation.
+  * The SpiModule implementations will be loaded at runtime via ServiceLoader
+  * @param tag
+  * @param classTag
+  * @tparam SpiImpl
+  */
+abstract class SpiModule[SpiImpl <: Spi ](implicit tag:TypeTag[SpiImpl], 
classTag:ClassTag[SpiImpl])  {
+  //lazy so that we can FIRST load the module, but THEN setup injectables and 
finally THEN initialize the instance
+  // (we call getInstance exactly once per module!)
+  lazy val spiImplInstance:SpiImpl = getInstance
+  val spiTypeName:String = classTag.runtimeClass.getName
+  protected [spi] def getInstance:SpiImpl
+}
+
+/**
+  * An extended SpiModule that will register a factory that uses an Injector 
for binding additional dependencies
+  * during instantiation of the Spi implementation
+  * @param ev$1
+  * @param tag
+  * @tparam SpiImpl
+  */
+abstract class SpiFactoryModule[SpiImpl <: Spi : TypeTag](implicit 
tag:ClassTag[SpiImpl])
+  extends SpiModule[SpiImpl] with Injectable{
+  protected [spi] final def getInstance:SpiImpl = 
getInstance(SharedModules.sharedModulesInjector)
+  protected [spi] def getInstance(implicit injector:Injector): SpiImpl
+}
+
+/**
+  * Spi providers must extend SpiProvider to expose an Extension for accessing 
the Spi impl
+  *
+  * @param configKey an key in application.conf which indicates which impl 
will be used at runtime
+  * @param tag
+  * @param classTag
+  * @tparam T
+  */
+abstract class SpiProvider[T <: Spi](configKey: String)(implicit tag: 
TypeTag[T], classTag:ClassTag[T])extends ExtensionId[T] with 
ExtensionIdProvider {
+
+  override def apply(system: ActorSystem): T = {
 
 Review comment:
   Because Extensions uses the ActorSystem as a service registry; see details 
at http://doc.akka.io/docs/akka/snapshot/scala/extending-akka.html
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to