Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

(Remote Phone Book Server) Create a remote phone book server that maintains a file of names and phone numbers. Define interface PhoneBookServer with the following methods: public PhoneBookEntry[] getPhoneBook() public void addEntry( PhoneBookEntry entry ) public void modifyEntry( PhoneBookEntry entry ) public void deleteEntry( PhoneBookEntry entry ) Create Activatable remote object class PhoneBookServerImpl, which implements interface PhoneBookServer. Class PhoneBookEntry should contain String instance variables that represent the first name, last name and phone number for one person. The class should also provide appropriate set/get methods and perform validation on the phone number format. Remember that class PhoneBookEntry also must implement Serializable, so that RMI can serialize objects of this class.

Short Answer

Expert verified
Create PhoneBookEntry and PhoneBookServer classes, implement methods, and ensure serialization.

Step by step solution

01

Understanding Classes and Interfaces

Identify the primary classes and interfaces needed to implement the remote phone book server. We need the `PhoneBookServer` interface and the `PhoneBookEntry` class. The `PhoneBookServer` interface will define the remote methods, while the `PhoneBookEntry` class will represent each phone book entry.
02

Defining PhoneBookEntry Class

Create a class `PhoneBookEntry` that implements `Serializable`. The class should have three private `String` fields: `firstName`, `lastName`, and `phoneNumber`, with public getter and setter methods for each. Ensure that the setter for `phoneNumber` includes validation to check the format of the phone number.
03

Implementing PhoneBookServer Interface

Define the `PhoneBookServer` interface with four methods: `PhoneBookEntry[] getPhoneBook()`, `void addEntry(PhoneBookEntry entry)`, `void modifyEntry(PhoneBookEntry entry)`, and `void deleteEntry(PhoneBookEntry entry)`. These methods outline the operations that the server can perform.
04

Creating Remote Object Class

Create the `PhoneBookServerImpl` class that implements the `PhoneBookServer` interface. This class should extend `Activatable` to support remote method invocation. Implement each method from the interface, ensuring that they manipulate a list or file of `PhoneBookEntry` objects.
05

Validating Phone Number Format

In the `PhoneBookEntry` class's setter for the phone number, use a regular expression or other validation method to ensure that the phone number follows a specific format, such as `(XXX) XXX-XXXX` or `XXX-XXX-XXXX`. Throw an exception if the validation fails.
06

Serializing PhoneBookEntry Objects

Ensure the `PhoneBookEntry` class implements `Serializable`. This allows instances of `PhoneBookEntry` to be passed over the network. No additional methods are needed for serialization since the class does not contain any complex objects.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

Key Concepts

These are the key concepts you need to understand to accurately answer the question.

Java RMI
Java Remote Method Invocation (RMI) allows for the execution of methods across different Java Virtual Machines, even if they are on different physical machines. This makes it a perfect fit for developing distributed applications like our remote phone book server. In our setup,
  • The server will host the instance of the remote object, in this case, `PhoneBookServerImpl`.
  • The clients will invoke methods on the server instance, such as adding or retrieving phone book entries.
RMI uses a stub and skeleton model, where the stub acts as a client-side proxy communicating with the server skeleton to pass requests and get responses. Understanding this model is crucial for developing efficient remote servers.
PhoneBookEntry Validation
When dealing with user input for phone book entries, validation is key. The `PhoneBookEntry` class requires validation to ensure that phone numbers adhere to a specific format. This prevents erroneous data from being entered, which can lead to issues later on. For example:
  • Define a specific format, such as `(XXX) XXX-XXXX`.
  • Use a regular expression within the setter method of `phoneNumber` to check for compliance with this format.
If the input does not match the expected pattern, it should throw an exception. This helps maintain high data integrity and ensure uniformity in phone numbers recorded in the phone book.
Serializable Interface
The `Serializable` interface is vital when you want to transmit objects over a network using Java RMI. By implementing this marker interface in our `PhoneBookEntry` class, we allow Java to automatically handle converting objects into a format that can be sent over the network and reassembled at the destination.
  • Objects without complex references can be serialized directly as is.
  • No additional methods are needed because `Serializable` is a marker interface, meaning it contains no methods to implement.
This process ensures that all the necessary data in `PhoneBookEntry` can be shared between client and server seamlessly, promoting a consistent view of the phone book across different components of the application.
Activatable Remote Object
To ensure our server remains efficient, the `PhoneBookServerImpl` class extends `Activatable`. This special class allows objects to be activated remotely when needed. It helps manage resources like memory and processing power on the server since not all objects need to be loaded at all times.
  • When a method is invoked on an inactive remote object, it's automatically activated to handle the request.
  • Post-execution, the object can be passivated, saving resources until needed again.
This on-demand activation approach ensures efficient management of remote objects and reduces the server's overhead by not having to keep all resources in use at all times.
Phone Book Management
Efficient phone book management goes beyond just adding and retrieving entries. With the `PhoneBookServer` interface, we have structured methods for managing these operations:
  • `getPhoneBook()` allows retrieval of entries.
  • `addEntry()` enables the addition of new `PhoneBookEntry` objects.
  • `modifyEntry()` supports updating existing entries.
  • `deleteEntry()` permits removal of entries no longer needed.
These methods ensure that phone book data is not only accessible but also modifiable according to user demands. By using a list or file to store the entries, the server can persistently manage its phone book state, offering an up-to-date view to clients at all times. Understanding this flow is crucial for setting up a robust phone book server system.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

State whether each of the following is true or false. If false, explain why. a) Not starting the RMI registry before attempting to bind the remote object to the registry results in a RuntimeException refusing connection to the registry. b) Every remote method must be part of an interface that extends java.rmi.Remote. c) The stubcompiler creates a stub class that performs the networking which allows the client to connect to the server and use the remote object’s methods. d) Class UnicastRemoteObject provides basic functionality required by remote objects. e) An object of a class that implements interface Serializable can be registered as a remote object and receive a remote method call. f) All methods in a Remote interface must have a throws clause indicating the potential for a RemoteException. g) RMI clients assume that they should connect to port 80 on a server computer when attempting to locate a remote object through the RMI registry. h) Once a remote object is bound to the RMI registry with method bind or rebind of class Naming, the client can look up the remote object with Naming method lookup. i) Method find of class Naming interacts with the RMI registry to help the client obtain a reference to a remote object so the client can use the remote object’s services.

Class PhoneBookClient should provide a user interface that allows the user to scroll through entries, add a new entry, modify an existing entry and delete an existing entry. The client and the server should provide proper error handling (e.g., the client cannot modify an entry that does not exist).

Visit the NWS Web site for the format of each line of information. Next, modify class WeatherServiceImpl to implement the new features of the interface. Finally, modify class WeatherServiceClient to allow the user to select the weather forecast for either day. Modify the support classes WeatherBean and WeatherItem as necessary to support the changes to classes WeatherServiceImpl and WeatherServiceClient.

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free