HpccClassLoader.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.reflect.Modifier;
  20. import java.lang.Throwable;
  21. public class HpccClassLoader extends java.lang.ClassLoader
  22. {
  23. private long bytecode;
  24. private int bytecodeLen;
  25. private native Class<?> defineClassForEmbed(int bytecodeLen, long bytecode, String name);
  26. private Hashtable<String, Class<?>> classes = new Hashtable<>();
  27. static private Hashtable<String, java.net.URLClassLoader> pathLoaders = new Hashtable<>();
  28. private java.net.URLClassLoader pathLoader;
  29. private HpccClassLoader(String classPath, ClassLoader parent, int _bytecodeLen, long _bytecode, String dllname)
  30. {
  31. super(parent);
  32. if (classPath != null && !classPath.isEmpty())
  33. {
  34. synchronized(pathLoaders)
  35. {
  36. pathLoader = pathLoaders.get(classPath);
  37. if (pathLoader == null)
  38. {
  39. List<URL> urls = new ArrayList<>();
  40. String[] paths = classPath.split(";");
  41. for (String path : paths)
  42. {
  43. try
  44. {
  45. if (path.contains(":"))
  46. urls.add(new URL(path));
  47. else
  48. urls.add(new URL("file:" + path));
  49. }
  50. catch (MalformedURLException E)
  51. {
  52. // Ignore any that we don't recognize
  53. // System.out.print(E.toString());
  54. }
  55. }
  56. pathLoader = new URLClassLoader(urls.toArray(new URL[urls.size()]));
  57. pathLoaders.put(classPath, pathLoader);
  58. }
  59. }
  60. }
  61. System.load(dllname);
  62. bytecodeLen = _bytecodeLen;
  63. bytecode = _bytecode;
  64. }
  65. public synchronized Class<?> findClass(String className) throws ClassNotFoundException
  66. {
  67. Class<?> result = classes.get(className);
  68. if (result == null)
  69. {
  70. if (bytecodeLen != 0)
  71. result = defineClassForEmbed(bytecodeLen, bytecode, className.replace(".","/"));
  72. if ( result == null && pathLoader != null)
  73. result = pathLoader.loadClass(className);
  74. if (result == null)
  75. return super.findClass(className);
  76. classes.put(className, result);
  77. }
  78. return result;
  79. }
  80. public static HpccClassLoader newInstance(String classPath, ClassLoader parent, int _bytecodeLen, long _bytecode, String dllname)
  81. {
  82. return new HpccClassLoader(classPath, parent, _bytecodeLen, _bytecode, dllname);
  83. }
  84. public static String getSignature(Method m)
  85. {
  86. StringBuilder sb = new StringBuilder();
  87. if ((m.getModifiers() & Modifier.STATIC) == 0)
  88. sb.append('@');
  89. sb.append('(');
  90. for(Class<?> c : m.getParameterTypes())
  91. {
  92. String sig=java.lang.reflect.Array.newInstance(c, 0).toString();
  93. sb.append(sig.substring(1, sig.indexOf('@')));
  94. }
  95. sb.append(')');
  96. if (m.getReturnType()==void.class)
  97. sb.append("V");
  98. else
  99. {
  100. String sig=java.lang.reflect.Array.newInstance(m.getReturnType(), 0).toString();
  101. sb.append(sig.substring(1, sig.indexOf('@')));
  102. }
  103. return sb.toString();
  104. }
  105. /* get signature for method with given name, so long as there is only one */
  106. public static String getSignature ( Class<?> clazz, String simpleName ) throws Exception
  107. {
  108. String ret = null;
  109. Method[] methods = clazz.getMethods();
  110. for (Method m : methods)
  111. {
  112. if ((m.getModifiers() & Modifier.PUBLIC) != 0 && m.getName().equals(simpleName))
  113. {
  114. if (ret == null)
  115. ret = getSignature(m);
  116. else
  117. throw new Exception("Multiple signatures found"); // multiple matches
  118. }
  119. }
  120. return ret;
  121. }
  122. }