You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tika.apache.org by ma...@apache.org on 2016/08/14 18:46:14 UTC
[02/15] tika git commit: Added an utility to load and insatiate
classes
Added an utility to load and insatiate classes
Project: http://git-wip-us.apache.org/repos/asf/tika/repo
Commit: http://git-wip-us.apache.org/repos/asf/tika/commit/eccc1538
Tree: http://git-wip-us.apache.org/repos/asf/tika/tree/eccc1538
Diff: http://git-wip-us.apache.org/repos/asf/tika/diff/eccc1538
Branch: refs/heads/TIKA-1508
Commit: eccc15387f0d4a5c62d8d12e6579878dba2f52a8
Parents: 9b5dc7f
Author: Thamme Gowda <th...@apache.org>
Authored: Sat Jun 11 19:04:28 2016 -0700
Committer: Thamme Gowda <th...@apache.org>
Committed: Sat Jun 11 19:04:28 2016 -0700
----------------------------------------------------------------------
.../apache/tika/utils/ServiceLoaderUtils.java | 30 ++++++++++++++++++++
1 file changed, 30 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/tika/blob/eccc1538/tika-core/src/main/java/org/apache/tika/utils/ServiceLoaderUtils.java
----------------------------------------------------------------------
diff --git a/tika-core/src/main/java/org/apache/tika/utils/ServiceLoaderUtils.java b/tika-core/src/main/java/org/apache/tika/utils/ServiceLoaderUtils.java
index 5ee1fe8..a1ccacb 100644
--- a/tika-core/src/main/java/org/apache/tika/utils/ServiceLoaderUtils.java
+++ b/tika-core/src/main/java/org/apache/tika/utils/ServiceLoaderUtils.java
@@ -20,6 +20,8 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.List;
+import org.apache.tika.config.ServiceLoader;
+
/**
* Service Loading and Ordering related utils
*/
@@ -45,4 +47,32 @@ public class ServiceLoaderUtils {
}
});
}
+
+ /**
+ * Loads a class and instantiates it
+ * @param className service class name
+ * @param <T> service type
+ * @return instance of service
+ */
+ public static <T> T newInstance(String className) {
+ return newInstance(className, ServiceLoader.class.getClassLoader());
+ }
+
+ /**
+ * Loads a class and instantiates it
+ * @param className service class name
+ * @param loader class loader
+ * @param <T> service type
+ * @return instance of service
+ */
+ public static <T> T newInstance(String className, ClassLoader loader){
+ try {
+ Class loadedClass = loader.loadClass(className);
+ Class<T> castedClass = loadedClass;
+ T instance = castedClass.newInstance();
+ return instance;
+ } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
+ throw new RuntimeException(e);
+ }
+ }
}