Requesting a Class The Class Server

One solution to this is to use instances of MarshalledObject , which we previously discussed in Chapt er 17 . Recall that MarshalledObject is a class with instances that contain a single serialized instance of another class. In Chapt er 17 , I stated that MarshalledObject uses RMIs customized version of serialization to store data. This means that any instance serialized into an instance of MarshalledObject has a codebase, and when the instance is deserialized using get , the class is retrieved from the codebase location if necessary. Thus, if you want to implement a simple client-side persistence layer using serialization for an RMI application, you should use the following three-step procedure: 1. Create an instance of FileInputStream . 2. Serialize the instances you want to store into instances of MarshalledObject by passing them as arguments to MarshalledObject s constructor. 3. Serialize the instances of MarshalledObject to the instance of FileInputStream . This is more complicated than directly serializing the instances to a file, but will help to guarantee that the serialized instances can be read back in later.

19.4 The Class Server

Implicit in the discussion so far is the idea that a class file can be downloaded from a URL. That is, from a codebase annotation for a class, which contains a string-delimited sequence of URLs, and a classname, the RMI deserialization algorithm can find and load a class. Ive already mentioned that RMI uses instances of the URLClassLoader class to do this. In this section, we will discuss how URLClassloader works, and how you can create a server that responds to requests from a URLClassLoader . In the discussion that follows, we will assume that all URLs are of the form http:. Instances of URLClassLoader can also handle URLs of the form ftp:. The decision to focus on http: URLs has two justifications. First, most applications use http: rather than ftp: URLs. Second, ftp: URLs are handled in pretty much the same way anyway. If you understand how the http: URLs are handled, you can figure out the ftp: URLs in under 10 minutes.

19.4.1 Requesting a Class

At this point, its important for us to discuss how HTTP works in a little detail. The important points are these: • HTTP works in request-response cycles. A client makes a request, typically through a GET or POST request, and a server sends back a response. After the response is sent, the connection is over, and there are no obligations on either side. • 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