tysonnorris commented on a change in pull request #2414: SPI approach for pluggable implementations URL: https://github.com/apache/incubator-openwhisk/pull/2414#discussion_r131129993
########## 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: I find it weird to use a classname when that classname is always abstracted away in a config. So either I have to create a variable to hold the classname, or have an extra tedious signature like `SpiLoader.getInstance[Spi](config.get("this.is.the.key"))` instead of simply `SpiLoader.getInstance[Spi]("this.is.the.key")`. Its already independent of the impl, in a way, since you can always provide a different resolver. In general I would expect to use the same resolver everywhere, although that it not required. I also don't think its a good idea to make it too easy to specify the classname directly in the code since this would be a bad idea. (Although I had to do it in some test classes to get them to work) ---------------------------------------------------------------- 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
