You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@daffodil.apache.org by GitBox <gi...@apache.org> on 2019/10/24 15:56:06 UTC

[GitHub] [incubator-daffodil] mbeckerle commented on a change in pull request #279: WIP: User Defined Functions feature

mbeckerle commented on a change in pull request #279: WIP: User Defined Functions feature
URL: https://github.com/apache/incubator-daffodil/pull/279#discussion_r338657424
 
 

 ##########
 File path: daffodil-runtime1/src/main/scala/org/apache/daffodil/udf/UserDefinedFunctionService.scala
 ##########
 @@ -0,0 +1,271 @@
+/*
+ * 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.udf
+
+import collection.JavaConverters._
+import collection.mutable._;
+import java.util.ServiceLoader
+import java.util.ServiceConfigurationError
+import org.apache.daffodil.util.Misc
+import org.apache.daffodil.util.LogLevel
+import org.apache.daffodil.util.Logging
+import java.lang.reflect.Method
+import org.apache.daffodil.dpath.NodeInfo
+import java.io.ObjectOutputStream
+import java.io.Serializable
+import java.io.ObjectInputStream
+
+/**
+ * Loads, validates and caches (for use at schema compile time) all User Defined Functions
+ * and their Providers from the classpath.
+ *
+ */
+object UserDefinedFunctionService extends Logging {
+  lazy val evaluateMethodName = "evaluate"
+
+  /**
+   * Class to make the evaluate Method serializable and to optimize our reflection lookups
+   * to happen once (per call) during deserialization, rather than at evaluation time.
+   */
+  case class UserDefinedFunctionMethod(
+      val decClass: Class[_],
+      val methodName: String,
+      val paramTypes: Array[Class[_]])
+      extends Serializable {
+
+    @transient lazy val method: Method = {
+      lookupMethod
+    }
+
+    def lookupMethod() = {
+     val m = decClass.getMethod(methodName, paramTypes: _*)
+     m
+    }
+
+    @throws(classOf[java.io.IOException])
+    private def writeObject(out: ObjectOutputStream) : Unit = {
+      out.defaultWriteObject()
+    }
+
+    @throws(classOf[java.io.IOException])
+    @throws(classOf[java.lang.ClassNotFoundException])
+    private def readObject(in: ObjectInputStream): Unit = {
+      in.defaultReadObject()
+      this.method
+    }
+  }
 
 Review comment:
   I think the right behavior should be only UDFs that are used by the schema should get looked up. I should be able to have some older, stale/broken UDF on the classpath, so long as I don't use it it shouldn't bother me. Make sense?
   
   In general, object construction time should never do anything that can possibly fail/throw an exception. Otherwise when it fails, well the object itself isn't even fully constructed and that can make it hard to figure out what is going on.  ( This is one of the reasons scala lazy val is so useful just for ordinary object-oriented programming. )

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services