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:-
invokeRemoteMethod. An enterprise object on the client side can use this method to invoke a method in the corresponding enterprise object on the server. The arguments are the name of the method to invoke and an array of arguments. Before the method is invoked on the server, the current state of the client-side editing context is "pushed" to the server to ensure that the method executes in an identical context. (Note that EODistributedObjectStore has a version of this method that includes a flag as an argument; setting this flag to false prevents the client from pushing its editing-context state to the server.)
-
invokeRemoteMethodWithKeyPath. You can send a message to any object on the server with this method, which is defined in EODistributedObjectStore. For more on this method, see the specification for this EODistribution class.
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).
-
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.
-
Add a button to the window.
Drag a button into the window.
Place it below the Revenue field.
Give it the title "Buy Movies Starring Selected Talent".
-
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.
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.
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.
© 1999 Apple Computer, Inc. (Last Updated 13 Sep 99)