Table of Contents Previous Section

Starting the Request-Response Loop

A WebObjects application can start up in one of two ways: automatically, when it receives a request (autostarting), or manually, when it's run from the command line. Either way, its entry point is the same as any C program: the main() function. In a WebObjects application, main() is usually very short. Its job is to create and run the application:

int main(int argc, const char *argv[]) {

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    WOApplication *application = [[[WOApplication alloc] init] autorelease];
    [application run];
    [pool release];
    exit(0);       // ensure the process exit status is 0

    return 0;      // ...and make main fit the ANSI spec.
}

The WODefaultApp application uses a main() function very much like this one. When Project Builder creates a WebObjects application project (for compiled applications) it also generates a similar main() function. If you write a main() function, it should look identical or much the same.

This version of the main() function is deceptively simple. First, it creates an autorelease pool that's used for the automatic deallocation of objects that receive an autorelease message. Then it creates a WOApplication object. This seems fairly straightforward, but in the init method the application creates and stores, in an instance variable, one or more adaptors. These adaptors, all instances of a WOAdaptor subclass, handle communication between an HTTP server and the WOApplication object. The application first parses the command line for specified adaptors (with necessary arguments); if none are specified, as happens when the application is autostarted, it creates a suitable default adaptor.

The run method initiates the request-response loop. When run is invoked, the application sends registerForEvents to each of its adaptors to tell the adaptor to begin receiving events. Then the application begins running in its run loop.

As shown in Figure 2, in each cycle of the loop a WOAdaptor object for the application receives an incoming HTTP request, packages the request in a WORequest object, forwards this object to the WOApplication object in a handleRequest message, and returns the response from the WOApplication to the HTTP server in a form it can understand.



Figure 2: Adaptor and Application in the Request-Response Loop

The last message in this version of main() releases the autorelease pool, which in turn releases any object that has been added to the pool since the application began. See the introduction to the Foundation Framework for a discussion of autorelease pools and object disposal.

Table of Contents Next Section