Receiving a Class Handling JAR files

• HTTP is an ASCII-based protocol. An HTTP message consists of a series of ASCII headers followed by a stringified version of any binary data that may need to be sent along with the message. • The first line of an HTTP request consists of a method, a path, and a protocol. The path frequently corresponds to the physical path required to access a file on the server machine. • The first line of an HTTP response consists of a status code and a description of the return value. After that, the response contains content headers metadata that further describes the response and then the content body. HTTP messages are remarkably simple and easy to read. Even if you dont know much about HTTP, request-response cycles, or the World Wide Web, you can generally figure out whats going on. This simplicity is crucial to the success of HTTP and one of the big reasons why the Web is so important today. Given this, an instance of URLClassLoader takes a URL of the form ht t p: m achineNam e: por t pat h and a classname and does the following: 1. It creates a path from the classname by interpreting each package as a directory. For example, the class com.ora.rmibook.chapter9.valueobjects.Money becomes the path comorarmibookchapter9valueobjectsMoney.class. 2. It prepends the path from the URL to the path it just created in order to form a request path. 3. It then issues an HTTP GET to a web server running on port of machineName. This request has the following format: GET request-path HTTP1.1 For example, if the URL is http:localhost:80, and the classname is com.ora.rmibook.chapter9.valueobjects.Money , an instance of URLClassLoader will send the following request to port 80 of the localHost : GET comorarmibookchapter9valueobjectsMoney.class HTTP1.1

19.4.2 Receiving a Class

Once an instance of URLClassLoader sends a request, it needs to get back an HTTP response that contains the class. That is, it expects to receive a message with the following four characteristics: • The response code must be HTTP 200 indicating success. • The Content-Length header must be set and must be accurate. • The Content-Type header must be set and must be equal to applicationjava . • After all the headers, the bytecodes for the class must be included. This is fairly simple stuff. Almost any web server can be easily configured to send a correct response to a request coming from an instance of URLClassLoader .

19.4.3 Handling JAR files

What Ive described works very well for individual class files. But it doesnt work for class files contained in a JAR file. The key to retrieving classes from a JAR is to remove the ending from the URL. The retrieval algorithm used by URLClassLoader checks to see whether the URL ends in a . If the URL ends with , it follows the algorithm described earlier. Its very easy to forget to end a codebase URL with a . And, for some reason, if you do forget, you wont spot the er ror in your code. Instead, youll probably spend half a day or so making sure that the filesystem is working. On the other hand, if the URL doesnt end with a , it is assumed to be complete and to describe the location of a JAR file. An instance of URLClassLoader will attempt to retrieve the JAR file, which must be returned the same way as an individual class, and then attempts to find the class from within the JAR file. Thus, if the URL was http:localhost:80myclasses.jar, and the class was com.ora.rmibook.chapter9.valueobjects.Money , an instance of URLClassLoader would do the following: 1. Send the following HTTP command to the web server listening on port 80 of localHost : GET myClasses.jar HTTP1.1. 1. Attempt to interpret the return value as a JAR and load com.ora.rmibook.chapter9.valueobjects.Money from the JAR file.

19.4.4 Suns Class Server