stevedlawrence commented on a change in pull request #651: URL: https://github.com/apache/daffodil/pull/651#discussion_r727096829
########## File path: daffodil-lib/src/main/scala/org/apache/daffodil/util/DynamicLoader.scala ########## @@ -0,0 +1,71 @@ +/* + * 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 org.apache.daffodil.util + +import java.util.ServiceConfigurationError +import java.util.ServiceLoader +import scala.collection.mutable.ArrayBuffer +import scala.language.reflectiveCalls + +/** + * Contains methods for dynamic loading of classes from the class path. + */ +object DynamicLoader { + + /** + * Load all instances of a particular class from the class path that are + * declared service providers + * @param clazz The class to be loaded. E.g., classOf[LayerCompiler] + * @tparam T The type to be loaded. Usually this is inferred and isn't explicitly supplied. + * It must have a name member returning a string. It must have a default (no-arg) constructor. + * @return A map from the name (string) to the corresponding instance of the class. + */ + def loadClass[T <: { def name(): String }](clazz : Class[T]): Map[String, T] = { + val thingName = { + val classNameWithPackage = clazz.getName + val packageName = clazz.getPackageName + val classNameAlone = + if (classNameWithPackage.startsWith(packageName)) + classNameWithPackage.substring(packageName.length) + else + classNameWithPackage + } + val iter = + ServiceLoader + .load(clazz) + .iterator() + val instanceBuf = new ArrayBuffer[T] + while(iter.hasNext()) { // a throw from hasNext() just propagates. It's fatal. + val compilerOpt = try { + instanceBuf += iter.next() + } catch { + case e: ServiceConfigurationError => + Logger.log.warn(s"A $thingName failed to load, ignoring: ${e.getMessage}") Review comment: For consistency with the UDF error messages, thoughts on modifying thingName to do something like this: ```scala val thingName = classNameAlone.split("(?=[A-Z])").mkString(" ") ``` So it adds spaces before all the upper case letters. Also suggest dropping the "A" and just say `$thingName failed to load, ignoring: ...`, just to keep messages sort and succinct. ########## File path: daffodil-lib/src/main/scala/org/apache/daffodil/util/DynamicLoader.scala ########## @@ -0,0 +1,71 @@ +/* + * 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 org.apache.daffodil.util + +import java.util.ServiceConfigurationError +import java.util.ServiceLoader +import scala.collection.mutable.ArrayBuffer +import scala.language.reflectiveCalls + +/** + * Contains methods for dynamic loading of classes from the class path. + */ +object DynamicLoader { + + /** + * Load all instances of a particular class from the class path that are + * declared service providers + * @param clazz The class to be loaded. E.g., classOf[LayerCompiler] + * @tparam T The type to be loaded. Usually this is inferred and isn't explicitly supplied. + * It must have a name member returning a string. It must have a default (no-arg) constructor. + * @return A map from the name (string) to the corresponding instance of the class. + */ + def loadClass[T <: { def name(): String }](clazz : Class[T]): Map[String, T] = { + val thingName = { + val classNameWithPackage = clazz.getName + val packageName = clazz.getPackageName + val classNameAlone = + if (classNameWithPackage.startsWith(packageName)) + classNameWithPackage.substring(packageName.length) + else + classNameWithPackage Review comment: This line isn't hit, is there a case where the classNaeWithPackage, doesn't start with the package name? ########## File path: daffodil-lib/src/main/scala/org/apache/daffodil/util/DynamicLoader.scala ########## @@ -0,0 +1,71 @@ +/* + * 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 org.apache.daffodil.util + +import java.util.ServiceConfigurationError +import java.util.ServiceLoader +import scala.collection.mutable.ArrayBuffer +import scala.language.reflectiveCalls + +/** + * Contains methods for dynamic loading of classes from the class path. + */ +object DynamicLoader { + + /** + * Load all instances of a particular class from the class path that are + * declared service providers + * @param clazz The class to be loaded. E.g., classOf[LayerCompiler] + * @tparam T The type to be loaded. Usually this is inferred and isn't explicitly supplied. + * It must have a name member returning a string. It must have a default (no-arg) constructor. + * @return A map from the name (string) to the corresponding instance of the class. + */ + def loadClass[T <: { def name(): String }](clazz : Class[T]): Map[String, T] = { + val thingName = { + val classNameWithPackage = clazz.getName + val packageName = clazz.getPackageName + val classNameAlone = + if (classNameWithPackage.startsWith(packageName)) + classNameWithPackage.substring(packageName.length) + else + classNameWithPackage + } + val iter = + ServiceLoader + .load(clazz) + .iterator() + val instanceBuf = new ArrayBuffer[T] + while(iter.hasNext()) { // a throw from hasNext() just propagates. It's fatal. + val compilerOpt = try { + instanceBuf += iter.next() + } catch { + case e: ServiceConfigurationError => + Logger.log.warn(s"A $thingName failed to load, ignoring: ${e.getMessage}") + } + } + val instancesFound: Map[String, Seq[T]] = instanceBuf.toSeq.groupBy{ _.name() } + val instanceMap: Map[String, T] = instancesFound.toSeq.flatMap { + case (name, Seq(lc)) => Some((name, lc)) + case (name, seq) => { + Logger.log.warn(s"More than one $thingName with name '$name' found." ++ + s" The classes are: ${seq.map{ Misc.getNameFromClass(_) }.mkString(", ")}. All are being ignored.") Review comment: It's a bit more verbose, but I kindof like the way UDF does this, where it puts out a single line per thign being ignored. Makes it easier to see what's being ignored. So instead, somethign like this? ```scala seq.foreach { s => val className = Misc.getNameFromClass(s) Logger.log.warn(s"$thingName ignored: $classname. Duplicate name found: ${name} } ``` ########## File path: daffodil-lib/src/main/scala/org/apache/daffodil/util/DynamicLoader.scala ########## @@ -0,0 +1,71 @@ +/* + * 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 org.apache.daffodil.util + +import java.util.ServiceConfigurationError +import java.util.ServiceLoader +import scala.collection.mutable.ArrayBuffer +import scala.language.reflectiveCalls + +/** + * Contains methods for dynamic loading of classes from the class path. + */ +object DynamicLoader { Review comment: Suggestion renaming to make it more clear that this is specific to SPI. My first instinct based on the name and comment is that it's about loading a class via relfection. ########## File path: daffodil-lib/src/main/scala/org/apache/daffodil/validation/Validators.scala ########## @@ -17,25 +17,18 @@ package org.apache.daffodil.validation -import java.util.ServiceLoader - import org.apache.daffodil.api.ValidatorFactory - -import scala.collection.JavaConverters._ +import org.apache.daffodil.util.DynamicLoader /** * Access SPI registered [[org.apache.daffodil.api.ValidatorFactory]] instances. * * Registered instances provide a unique name for lookup. */ object Validators { - private lazy val impls: Map[String, ValidatorFactory] = - ServiceLoader - .load(classOf[ValidatorFactory]) - .iterator() - .asScala - .map(v => v.name() -> v) - .toMap + private lazy val impls: Map[String, ValidatorFactory] = { + DynamicLoader.loadClass[ValidatorFactory](classOf[ValidatorFactory]) + } Review comment: Looks like you don't update the UDF (which is why I suggested all the error message changes to keep those message consistent). I guess that isn't possible because UDFProviders don't have a name? They just have a list of things that they provide, and those things are what get added to a Map? Is it worth opening a ticket to refactor things so that this can be shared with the UDF? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
