Documentation Archive Developer
Search
PATH  Documentation > WebObjects 4.5 > Creating a Java Client Application: A Tutorial

   

Invoking Server Methods Remotely

In a Java Client application you may want some methods to execute only on the server. This is particularly the case when security is an issue, but performance can be a reason as well (as when the method consumes a lot of system resources). Objects on the client side of a Java Client application can use two methods to invoke a server method:

In our StudioManager example, let's say that you want to give studios the ability to buy all of the movies that star a specified actor, but you consider this a sensitive computation. You can implement a method such as the following in Studio . java:

Studio.java (client)

public void buyAllMoviesStarringTalent(Talent talent) {

invokeRemoteMethod("clientSideRequestBuyAllMoviesStarringTalent",
   new Object[] {talent});
}

The method begins with "clientSideRequest"; this is not accidental. The EODistributionContext object on the server-side EODistribution layer will reject a remote invocation unless it has this prefix or its delegate implements the proper delegation methods (see the reference documentation for EODistributionContext or EODistributedObjectStore for more information).

The following is the invoked method, which is implemented in the server's Studio.java:

Studio.java (server)


public void clientSideRequestBuyAllMoviesStarringTalent(Talent talent) {
   int i, count;
   NSArray talentMovies;
   EOEnterpriseObject movie, studio;

   talentMovies = talent.moviesStarredIn();
   count = talentMovies.count();
   for (i = 0; i < count; i++) {
      movie =
         (EOEnterpriseObject)(talentMovies.objectAtIndex(i));
      if (!(movies().containsObject(movie))) {
         studio =
            (EOEnterpriseObject)(movie.valueForKey("studio"));
         if (studio != null)
            studio.
            removeObjectFromBothSidesOfRelationshipWithKey
            (movie,"movies");
         addObjectToBothSidesOfRelationshipWithKey
            (movie,"movies");
      }
   }
}

This method invokes the moviesStarredIn method:

Talent.java (server)


public NSArray moviesStarredIn() {
      int i, count;
      NSArray movies;
      NSMutableArray moviesStarredIn;
      EOEnterpriseObject movie;

      moviesStarredIn = new NSMutableArray();
      movies = (NSArray)(roles().valueForKey("movie"));

      count = movies.count();
      for (i = 0; i < count; i++) {
         movie = (EOEnterpriseObject)(movies.objectAtIndex(i));
         if (!(moviesStarredIn.containsObject(movie))) {
            moviesStarredIn.addObject(movie);
         }
      }
      return moviesStarredIn;
}

You can associate the buyAllMoviesStarringTalent method with a user interface control. But first you need to add to your user interface a table view that lists all actors (talent).

  1. Add a new table view to your user interface.

    Drag the Talent entity from your model into the nib file window in Interface Builder.

    Drag a table view from the Palette onto your window.

    Control-drag from each table view column to the Talent EODisplayGroup.

    Using the value aspect of the EOColumnAssoc, connect the table view columns to the firstName and lastName class keys, respectively.

  2. Add a button to the window.

    Drag a button into the window.

    Place it below the Revenue field.

    Resize it.

    Give it the title "Buy Movies Starring Selected Talent".

    Now that you've added the table view, connected it to the firstName and lastName properties of the Talent EODisplayGroup, and added a Buy button to the window, you're ready to use an EOActionAssociation to connect the button to the buyAllMoviesStarringTalent method.

  3. Associate a method with a user interface control.

    Display the Attributes view of the Inspector for the Studio EODisplayGroup.

    In the text field type the name of the method ( buyAllMoviesStarringTalent ) you want to use in an association.

    Click Add.

    You can now use the buyAllMoviesStarringTalent method in associations.

    Control-drag from the "Buy Movies Starring Selected Talent" button to the Studio EODisplayGroup.

    In the Connections Inspector, choose EOActionAssociation from the pop-up list at the top of the left column.

    Select action in the left column, and the method you want to connect to ( buyAllMoviesStarringTalent ) in the right column.

    Click Connect.

    Because the buyAllMoviesStarringTalent method takes a Talent object as an argument, you also need to make a connection from the Buy button to the Talent EODisplayGroup.

    Control-drag from the "Buy Movies Starring Selected Talent" button to the Talent EODisplayGroup.

    In the Inspector, select argument in the left column. The argument aspect takes the destination of the connection (Talent) as an argument, which will be supplied to the buyAllMoviesStarringTalent method.

    Click Connect.

    Once you finish connecting the button, you can use it to purchase all of the movies starring the selected actor for the selected studio.


© 1999 Apple Computer, Inc. – (Last Updated 13 Sep 99)