You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PluginClassLoader.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package dmdirc.plugins;
  23. import java.io.DataInputStream;
  24. import java.io.File;
  25. import java.io.FileInputStream;
  26. import java.io.IOException;
  27. /**
  28. * A custom ClassLoader to load and unload plugins.
  29. */
  30. class PluginClassLoader extends ClassLoader {
  31. private String baseDir = null;
  32. /**
  33. * Constructs new PluginClassLoader
  34. *
  35. * @param baseDir plugin base dir
  36. */
  37. public PluginClassLoader(String baseDir) {
  38. this.baseDir = baseDir;
  39. }
  40. /**
  41. * Loads a plugin from disk.
  42. *
  43. * @param name plugin class name to load
  44. * @return plugin class
  45. */
  46. public Class loadClass(String name) throws ClassNotFoundException {
  47. Class loadedClass = findLoadedClass(name);
  48. if ( loadedClass == null ) {
  49. try {
  50. loadedClass = findSystemClass(name);
  51. } catch ( Exception e ) {
  52. // do nothing
  53. }
  54. if ( loadedClass == null ) {
  55. String fileName = baseDir + File.separator +
  56. name.replace('.', File.separatorChar) + ".class";
  57. byte[] data = null;
  58. System.out.println("Trying to load: " + fileName);
  59. try {
  60. data = loadClassData(fileName);
  61. } catch( IOException e ) {
  62. System.out.println(e);
  63. throw new ClassNotFoundException(e.getMessage());
  64. }
  65. loadedClass = defineClass(name,data, 0, data.length);
  66. if ( loadedClass == null ) {
  67. System.out.println("loadedClass == null");
  68. throw new ClassNotFoundException("Could not load " + name);
  69. } else {
  70. resolveClass(loadedClass);
  71. }
  72. } else {
  73. //Found loaded system class
  74. }
  75. } else {
  76. //Found loaded class
  77. }
  78. return loadedClass;
  79. }
  80. /**
  81. * Loads binary class data from disk.
  82. *
  83. * @param fileName file name
  84. * @return bytecodes
  85. */
  86. private byte[] loadClassData(String fileName) throws IOException {
  87. File file = new File(fileName);
  88. byte buffer[] = new byte[(int)file.length()];
  89. FileInputStream in = new FileInputStream(file);
  90. DataInputStream dataIn = new DataInputStream(in);
  91. dataIn.readFully(buffer);
  92. dataIn.close();
  93. return buffer;
  94. }
  95. }