/hudson-core/src/main/java/hudson/PluginFirstClassLoader.java

http://github.com/hudson/hudson · Java · 105 lines · 61 code · 12 blank · 32 comment · 1 complexity · a283bf3174dcd92ceceefdca02dfdb95 MD5 · raw file

  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2004-2009, Sun Microsystems, Inc., Olivier Lamy
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package hudson;
  25. import java.io.Closeable;
  26. import java.io.File;
  27. import java.io.IOException;
  28. import java.io.InputStream;
  29. import java.net.URL;
  30. import java.util.ArrayList;
  31. import java.util.Collection;
  32. import java.util.Enumeration;
  33. import java.util.List;
  34. import org.apache.tools.ant.AntClassLoader;
  35. /**
  36. * classLoader which use first /WEB-INF/lib/*.jar and /WEB-INF/classes before core classLoader
  37. * <b>you must use the pluginFirstClassLoader true in the maven-hpi-plugin</b>
  38. * @author olamy
  39. * @since 1.371
  40. */
  41. public class PluginFirstClassLoader
  42. extends AntClassLoader
  43. implements Closeable
  44. {
  45. private List<URL> urls = new ArrayList<URL>();
  46. public void addPathFiles( Collection<File> paths )
  47. throws IOException
  48. {
  49. for ( File f : paths )
  50. {
  51. urls.add( f.toURI().toURL() );
  52. addPathFile( f );
  53. }
  54. }
  55. /**
  56. * @return List of jar used by the plugin /WEB-INF/lib/*.jar and classes directory /WEB-INF/classes
  57. */
  58. public List<URL> getURLs()
  59. {
  60. return urls;
  61. }
  62. public void close()
  63. throws IOException
  64. {
  65. cleanup();
  66. }
  67. @Override
  68. protected Enumeration findResources( String arg0, boolean arg1 )
  69. throws IOException
  70. {
  71. Enumeration enu = super.findResources( arg0, arg1 );
  72. return enu;
  73. }
  74. @Override
  75. protected Enumeration findResources( String name )
  76. throws IOException
  77. {
  78. Enumeration enu = super.findResources( name );
  79. return enu;
  80. }
  81. @Override
  82. public URL getResource( String arg0 )
  83. {
  84. URL url = super.getResource( arg0 );
  85. return url;
  86. }
  87. @Override
  88. public InputStream getResourceAsStream( String name )
  89. {
  90. InputStream is = super.getResourceAsStream( name );
  91. return is;
  92. }
  93. }