You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by sc...@apache.org on 2018/07/02 20:37:15 UTC

svn commit: r1834889 - /uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/Class_TCCL.java

Author: schor
Date: Mon Jul  2 20:37:15 2018
New Revision: 1834889

URL: http://svn.apache.org/viewvc?rev=1834889&view=rev
Log:
[UIMA-5802] merge from v2, consolidate most class loading or setting extension class loader parents, and include the Thread context class loader if defined

Added:
    uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/Class_TCCL.java

Added: uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/Class_TCCL.java
URL: http://svn.apache.org/viewvc/uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/Class_TCCL.java?rev=1834889&view=auto
==============================================================================
--- uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/Class_TCCL.java (added)
+++ uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/internal/util/Class_TCCL.java Mon Jul  2 20:37:15 2018
@@ -0,0 +1,91 @@
+/*
+ * 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.uima.internal.util;
+
+import java.util.Map;
+import java.util.function.Supplier;
+
+import org.apache.uima.resource.Resource;
+import org.apache.uima.resource.ResourceInitializationException;
+import org.apache.uima.resource.ResourceManager;
+
+/**
+ * Utilities supporting a unified approach to loading classes,
+ * incorporating the resource manager's classloader if available,
+ * and making use of the Thread Context Class Loader (TCCL)
+ */
+public class Class_TCCL {
+
+  static public <T> Class<T> forName(String className) 
+      throws ClassNotFoundException {
+    return forName(className, null, true);
+  }
+  
+  static public <T> Class<T> forName(String className, ResourceManager rm) 
+      throws ClassNotFoundException {
+    return forName(className, rm, true);
+  }  
+  
+  static public <T> Class<T> forName(String className, ResourceManager rm, boolean resolve) 
+      throws ClassNotFoundException {
+    return (Class<T>) Class.forName(className, resolve, get_cl(rm));
+  }
+  
+  static public <T> Class<T> forName(String className, Map<String, Object> additionalParams) 
+      throws ClassNotFoundException {
+    ResourceManager rm = (additionalParams != null)
+                           ? (ResourceManager) additionalParams.get(Resource.PARAM_RESOURCE_MANAGER)
+                           : null;
+    return forName(className, rm);
+  }
+  
+  static public ClassLoader get_cl(ResourceManager rm) {
+    
+    ClassLoader cl = (rm == null) ? null : rm.getExtensionClassLoader();
+    
+    if (cl == null) 
+      cl = get_parent_cl();Thread.currentThread().getContextClassLoader();
+    
+    if (cl == null) 
+      cl = Class_TCCL.class.getClassLoader();  // this class's classloader
+    
+    return cl;
+  }
+  
+  static public ClassLoader get_parent_cl() {
+    ClassLoader cl = Thread.currentThread().getContextClassLoader();
+    return (cl == null) 
+             ? Class_TCCL.class.getClassLoader()
+             : cl;
+  }
+
+  // only for Java 8 + , needs lambda
+//  static public <T> Class<T> loadclass_throw_if_not_found(
+//      String className, ResourceManager rm, Supplier<String> sourceUrl) 
+//          throws ResourceInitializationException {
+//    try {
+//      Class<T> c = forName(className, rm);
+//      return c;
+//    } catch (ClassNotFoundException e) {
+//      throw new ResourceInitializationException(ResourceInitializationException.CLASS_NOT_FOUND,
+//              new Object[] { className, sourceUrl.get() }, e);
+//    }
+//  }
+}