HpccClassLoader.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2018 HPCC Systems(R).
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. package com.HPCCSystems;
  14. import java.net.*;
  15. import java.util.Hashtable;
  16. import java.util.List;
  17. import java.util.ArrayList;
  18. import java.lang.reflect.Method;
  19. import java.lang.Throwable;
  20. public class HpccClassLoader extends java.lang.ClassLoader
  21. {
  22. private long bytecode;
  23. private int bytecodeLen;
  24. private native Class<?> defineClassForEmbed(int bytecodeLen, long bytecode, String name);
  25. private Hashtable<String, Class<?>> classes = new Hashtable<>();
  26. static private Hashtable<String, java.net.URLClassLoader> pathLoaders = new Hashtable<>();
  27. private java.net.URLClassLoader pathLoader;
  28. private HpccClassLoader(String classPath, ClassLoader parent, int _bytecodeLen, long _bytecode, String dllname)
  29. {
  30. super(parent);
  31. if (classPath != null && !classPath.isEmpty())
  32. {
  33. synchronized(pathLoaders)
  34. {
  35. pathLoader = pathLoaders.get(classPath);
  36. if (pathLoader == null)
  37. {
  38. List<URL> urls = new ArrayList<>();
  39. String[] paths = classPath.split(";");
  40. for (String path : paths)
  41. {
  42. try
  43. {
  44. if (path.contains(":"))
  45. urls.add(new URL(path));
  46. else
  47. urls.add(new URL("file:" + path));
  48. }
  49. catch (MalformedURLException E)
  50. {
  51. // Ignore any that we don't recognize
  52. // System.out.print(E.toString());
  53. }
  54. }
  55. pathLoader = new URLClassLoader(urls.toArray(new URL[urls.size()]));
  56. pathLoaders.put(classPath, pathLoader);
  57. }
  58. }
  59. }
  60. System.load(dllname);
  61. bytecodeLen = _bytecodeLen;
  62. bytecode = _bytecode;
  63. }
  64. public synchronized Class<?> findClass(String className) throws ClassNotFoundException
  65. {
  66. Class<?> result = classes.get(className);
  67. if (result == null)
  68. {
  69. if (bytecodeLen != 0)
  70. result = defineClassForEmbed(bytecodeLen, bytecode, className.replace(".","/"));
  71. if ( result == null && pathLoader != null)
  72. result = pathLoader.loadClass(className);
  73. if (result == null)
  74. return super.findClass(className);
  75. classes.put(className, result);
  76. }
  77. return result;
  78. }
  79. public static HpccClassLoader newInstance(String classPath, ClassLoader parent, int _bytecodeLen, long _bytecode, String dllname)
  80. {
  81. return new HpccClassLoader(classPath, parent, _bytecodeLen, _bytecode, dllname);
  82. }
  83. public static String getSignature(Method m)
  84. {
  85. StringBuilder sb = new StringBuilder("(");
  86. for(Class<?> c : m.getParameterTypes())
  87. {
  88. String sig=java.lang.reflect.Array.newInstance(c, 0).toString();
  89. sb.append(sig.substring(1, sig.indexOf('@')));
  90. }
  91. sb.append(')');
  92. if (m.getReturnType()==void.class)
  93. sb.append("V");
  94. else
  95. {
  96. String sig=java.lang.reflect.Array.newInstance(m.getReturnType(), 0).toString();
  97. sb.append(sig.substring(1, sig.indexOf('@')));
  98. }
  99. return sb.toString();
  100. }
  101. /* get signature for first method with given name */
  102. public static String getSignature ( Class<?> clazz, String simpleName )
  103. {
  104. Method[] methods = clazz.getMethods();
  105. for (Method m : methods)
  106. if (m.getName().equals(simpleName))
  107. return getSignature(m);
  108. return null;
  109. }
  110. }