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

 ##########
 File path: common/scala/src/main/scala/whisk/spi/SpiLoader.scala
 ##########
 @@ -0,0 +1,94 @@
+package whisk.spi
+
+import com.typesafe.config.Config
+import java.util.concurrent.atomic.AtomicReference
+import scala.reflect.ClassTag
+
+/*
+ * 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.
+ */
+
+trait Spi {
+}
+
+trait SpiFactory[T <: Spi] {
+    def apply(dependencies: Dependencies): T
+}
+
+trait SingletonSpiFactory[T <: Spi] extends SpiFactory[T]{
+    private val ref = new AtomicReference[T]()
+    def buildInstance(dependencies: Dependencies): T
+    override def apply(dependencies: Dependencies): T = {
+        val oldValue = ref.get()
+        if (oldValue != null.asInstanceOf[T]) {
+            oldValue
+        } else {
+            val newValue = buildInstance(dependencies)
+            if (ref.compareAndSet(null.asInstanceOf[T], newValue)){
+                newValue
+            } else {
+                ref.get()
+            }
+        }
+    }
+}
+
+trait SpiClassResolver {
+    def getClassnameForKey(key: String): String
+}
+
+object SpiLoader {
+    def instanceOf[A <: Spi](key: String, deps: Dependencies = new 
Dependencies())(implicit resolver: SpiClassResolver = ClassnameResolver, tag: 
ClassTag[A]): A = {
+        val clazz = Class.forName(resolver.getClassnameForKey(key) + "$")
+        
clazz.getField("MODULE$").get(clazz).asInstanceOf[SpiFactory[A]].apply(deps)
+    }
+}
+
+/**
+ * Classname is specified directly at the time of lookup
+ */
+object ClassnameResolver extends SpiClassResolver {
+    override def getClassnameForKey(key: String): String = key
+}
+
+/**
+ * Lookup the classname for the SPI impl based on a key in the provided Config
+ * @param config
+ */
+class TypesafeConfigClassResolver(config: Config) extends SpiClassResolver {
+    override def getClassnameForKey(key: String): String = 
config.getString(key)
+}
 
 Review comment:
   AFIK the resolver that depends on config cannot be an object since it 
requires the config? Or maybe I'm not understanding - do you have an example or 
suggestion on how to do this. 
   
   The problem as I see it is that the contract that SpiLoader.getInstance() 
should enforce is that you should not pass a "string", rather a "config value" 
that contains the classname, so that if you are using SpiLoader, you are 
explicitly exposing a configuration value that can be replaced. 
 
----------------------------------------------------------------
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