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.

URLClassPath.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2003 Shigeru Chiba. All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. Alternatively, the contents of this file may be used under
  8. * the terms of the GNU Lesser General Public License Version 2.1 or later.
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. */
  15. package javassist;
  16. import java.io.*;
  17. import java.net.*;
  18. /**
  19. * A class search-path specified with URL (http).
  20. *
  21. * @see javassist.ClassPath
  22. * @see ClassPool#insertClassPath(ClassPath)
  23. * @see ClassPool#appendClassPath(ClassPath)
  24. */
  25. public class URLClassPath implements ClassPath {
  26. protected String hostname;
  27. protected int port;
  28. protected String directory;
  29. protected String packageName;
  30. /**
  31. * Creates a search path specified with URL (http).
  32. *
  33. * <p>This search path is used only if a requested
  34. * class name starts with the name specified by <code>packageName</code>.
  35. * If <code>packageName</code> is "mypack" and a requested class is
  36. * "mypack.sub.Test", then the given URL is used for loading that class.
  37. * If <code>packageName</code> is <code>null</code>, the URL is used
  38. * for loading any class.
  39. *
  40. * @param host host name
  41. * @param port port number
  42. * @param directory directory name ending with "/".
  43. * It can be "/" (root directory).
  44. * @param packageName package name.
  45. */
  46. public URLClassPath(String host, int port,
  47. String directory, String packageName) {
  48. hostname = host;
  49. this.port = port;
  50. this.directory = directory;
  51. this.packageName = packageName;
  52. }
  53. public String toString() {
  54. return hostname + ":" + port + directory;
  55. }
  56. /**
  57. * Opens a class file with http.
  58. */
  59. public InputStream openClassfile(String classname) {
  60. try {
  61. if (packageName == null || classname.startsWith(packageName)) {
  62. String jarname
  63. = directory + classname.replace('.', '/') + ".class";
  64. URLConnection con = fetchClass0(hostname, port, jarname);
  65. return con.getInputStream();
  66. }
  67. }
  68. catch (IOException e) {}
  69. return null; // not found
  70. }
  71. /**
  72. * Closes this class path.
  73. */
  74. public void close() {}
  75. /**
  76. * Reads a class file on an http server.
  77. *
  78. * @param host host name
  79. * @param port port number
  80. * @param directory directory name ending with "/".
  81. * It can be "/" (root directory).
  82. * @param classname fully-qualified class name
  83. */
  84. public static byte[] fetchClass(String host, int port,
  85. String directory, String classname)
  86. throws IOException
  87. {
  88. byte[] b;
  89. URLConnection con = fetchClass0(host, port,
  90. directory + classname.replace('.', '/') + ".class");
  91. int size = con.getContentLength();
  92. InputStream s = con.getInputStream();
  93. if (size <= 0)
  94. b = ClassPoolTail.readStream(s);
  95. else {
  96. b = new byte[size];
  97. int len = 0;
  98. do {
  99. int n = s.read(b, len, size - len);
  100. if (n < 0) {
  101. s.close();
  102. throw new IOException("the stream was closed: "
  103. + classname);
  104. }
  105. len += n;
  106. } while (len < size);
  107. }
  108. s.close();
  109. return b;
  110. }
  111. private static URLConnection fetchClass0(String host, int port,
  112. String filename)
  113. throws IOException
  114. {
  115. URL url;
  116. try {
  117. url = new URL("http", host, port, filename);
  118. }
  119. catch (MalformedURLException e) {
  120. // should never reache here.
  121. throw new IOException("invalid URL?");
  122. }
  123. URLConnection con = url.openConnection();
  124. con.connect();
  125. return con;
  126. }
  127. }