mbeckerle commented on a change in pull request #651: URL: https://github.com/apache/daffodil/pull/651#discussion_r728170257
########## 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: I find messing with the class identifier like this misleading. The user setting this up really does need the exact spelling of the class name, so they can see if that's what's in the jar file by grep searching the contents of jars, etc. -- 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]
