JarClassLoader.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.util;
  16. import java.io.DataInputStream;
  17. import java.io.File;
  18. import java.io.IOException;
  19. import java.util.HashSet;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import java.util.zip.ZipEntry;
  23. import java.util.zip.ZipFile;
  24. /**
  25. * This is a class loader for the dynamic extensions used by DynamicExtension class.
  26. *
  27. * @version $Revision: $ $Date: $
  28. * @author galun
  29. */
  30. public class JarClassLoader extends ClassLoader
  31. {
  32. private static Logger _log = Logger.getLogger(JarClassLoader.class.getCanonicalName());
  33. HashSet<String> _jars = new HashSet<String>();
  34. public void addJarFile(String filename)
  35. {
  36. _jars.add(filename);
  37. }
  38. @Override
  39. public Class<?> findClass(String name) throws ClassNotFoundException
  40. {
  41. try
  42. {
  43. byte[] b = loadClassData(name);
  44. return defineClass(name, b, 0, b.length);
  45. }
  46. catch (Exception e)
  47. {
  48. throw new ClassNotFoundException(name);
  49. }
  50. }
  51. private byte[] loadClassData(String name) throws IOException
  52. {
  53. byte[] classData = null;
  54. for (String jarFile : _jars)
  55. {
  56. ZipFile zipFile = null;
  57. DataInputStream zipStream = null;
  58. try
  59. {
  60. File file = new File(jarFile);
  61. zipFile = new ZipFile(file);
  62. String fileName = name.replace('.', '/') + ".class";
  63. ZipEntry entry = zipFile.getEntry(fileName);
  64. if (entry == null)
  65. continue;
  66. classData = new byte[(int) entry.getSize()];
  67. zipStream = new DataInputStream(zipFile.getInputStream(entry));
  68. zipStream.readFully(classData, 0, (int) entry.getSize());
  69. break;
  70. }
  71. catch (IOException e)
  72. {
  73. _log.log(Level.WARNING, jarFile + ": " + e.getMessage(), e);
  74. continue;
  75. }
  76. finally
  77. {
  78. try
  79. {
  80. zipFile.close();
  81. }
  82. catch (Exception e)
  83. {
  84. }
  85. try
  86. {
  87. zipStream.close();
  88. }
  89. catch (Exception e)
  90. {
  91. }
  92. }
  93. }
  94. if (classData == null)
  95. throw new IOException("class not found in " + _jars);
  96. return classData;
  97. }
  98. }