tysonnorris commented on a change in pull request #2414: SPI approach for pluggable implementations URL: https://github.com/apache/incubator-openwhisk/pull/2414#discussion_r131161515
########## 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: AFAIK config is not loaded globally for WhiskConfig or Akka, but maybe I'm mistaken? If that needs changing I don't think it is a good idea to include in the PR but a separate one. For `generalize the config key`, I think that is a good idea, but it would still require a way to expose the config binding to the loader (via resolver or globally or some other approach). ---------------------------------------------------------------- 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
