mbeckerle commented on a change in pull request #651:
URL: https://github.com/apache/daffodil/pull/651#discussion_r728168753
##########
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:
I think perhaps the right thing is that I'll just add a comment to the
UDF code mentioning that it does *not* use the SimpleNamedServiceLoader (new
name for what I had called DynamicLoader) because it's not quite as simple, but
this will give a user hoping to copy a ServiceLoader pattern a hint to look at
how the Validator and Layering stuff do it.
##########
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.
##########
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:
I fixed this to use a shared Misc utility that shares the guts of
Misc.getNameFromClass which was already excluding the package name part.
##########
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:
How about SimpleNamedServiceLoader ?
##########
File path:
daffodil-runtime1/src/main/scala/org/apache/daffodil/layers/LayerCompiler.scala
##########
@@ -0,0 +1,130 @@
+/*
+ * 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.layers
+
+import org.apache.daffodil.api.WarnID
+import org.apache.daffodil.dpath.NodeInfo.PrimType
+import org.apache.daffodil.dsom.ImplementsThrowsOrSavesSDE
+import org.apache.daffodil.processors.SequenceRuntimeData
+import org.apache.daffodil.processors.VariableRuntimeData
+import org.apache.daffodil.processors.charset.BitsCharsetJava
+import org.apache.daffodil.processors.charset.BitsCharsetNonByteSize
+import org.apache.daffodil.schema.annotation.props.gen.LayerLengthKind
+import org.apache.daffodil.schema.annotation.props.gen.LayerLengthUnits
+import org.apache.daffodil.xml.NS
+import org.apache.daffodil.xml.RefQName
+
+/**
+ * Must be implemented by all layers.
+ *
+ * These are the classes which must be dynamically loaded in order to add a
layer implementation
+ * to Daffodil.
+ *
+ * These instances are NOT serialized as part of a saved processor. The
compileLayer method
+ * is called and the resulting LayerTransformerFactory is the serialized
object.
+ */
+abstract class LayerCompiler(nom: String) {
+
+ def name() = nom
Review comment:
I was getting errors about name vs. name() due to the type constraint
which requires a name() method. This silenced them.
##########
File path:
daffodil-runtime1-layers/src/main/resources/META-INF/services/org.apache.daffodil.layers.LayerCompiler
##########
@@ -0,0 +1,23 @@
+# 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.
+#
+# These are the layers that are built-in to Daffodil's runtime1.
+#
+org.apache.daffodil.layers.Base64MIMELayerCompiler
+org.apache.daffodil.layers.FourByteSwapLayerCompiler
+org.apache.daffodil.layers.GZIPLayerCompiler
+org.apache.daffodil.layers.LineFoldedIMFLayerCompiler
+org.apache.daffodil.layers.LineFoldedICalendarLayerCompiler
Review comment:
Yes, these things will only get added to Daffodil if they become
standard. I expect these things to be defined mostly in DFDL schemas.
--
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]