JarClassLoader.java 2.5 KB

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