Controller Part 1S2C Home « Controller Part 1

In this lesson of the case study we do the first part of our controller code by creating an exception class for our services and the services and RemoteServices interfaces.

Whether accessing the Manufacturer file locally or remotely problems can arise using I/O and we can also have problems with remote access. So we need to catch and pass back exception information to the GUI users about exceptions that are not covered elsewhere within the code. In this lesson we will create a ServicesException class to handle exceptions thrown when using our services.

Compiling The ServicesException Class Top

The following ServicesException class will be be thrown when an exception occurs when using our services.

Cut and paste the following code into your text editor and save it in the   c:\_Case_Study\src\services directory.


package services;

/**
 * Holds all services exceptions that may occur in Services
 * 
 * @author Charlie 
 * @version 1.0
 * @see Services
 *
 */
public class ServicesException extends RuntimeException {

    /**
     * A version number for the ServicesException class so that serialisation 
     * can occur without worrying about the underlying class changing 
     * between serialisation and deserialisation.
     * 
     */
    private static final long serialVersionUID = 2498052502L;

    /**
     * Create a default ServicesException instance.
     * 
     */
    public ServicesException() {
        super();
    }

    /**
     * Create a ServicesException that returns a String.
     * 
     */
    public ServicesException(String e) {
        super(e);
    }

    /**
     * Create a ServicesException instance and chains an exception.
     * 
     * @param e The exception to wrap
     */
    public ServicesException(Throwable e) {
        super(e);
    }
}

Compiling Our Source File With the -d Option

Open your command line editor:

Change to directory  cd c:\_Case_Study\src\services

Compile ServicesException.java using the java compiler with the -d option
  javac -d ..\..\classes ServicesException.java

Because we have used a package (package services;) at the top of the code the compiler has created this directory within the classes directory. When we look in the services directory we can see the compiled ServicesException.class file.

compile ServicesException

Services Interfacesgo to top of page Top

We will need to seamlessly connect local or remote clients to a Manufacturer file a user has chosen and to do this we will need two interfaces that connect locally which we will call Services and remotely which we will call RemoteServices. The interfaces will need to include the stocking, unstocking and search functionality requested by the stakeholder Stocking Goods Limited. In this lesson we create the interfaces and methods required.

Compiling The Services Interface Top

The following Services interface will have to have methods for implementation that will encompass the following processes requested by the stakeholder Stocking Goods Limited:

  1. Stocking - A user of the system should be able to stock goods up to but not exceeding the amount of stock level held.
    • If there is a problem accessing the Manufacturer file we should throw a IOException back to the GUI.
    • If there is a problem updating the Manufacturer file or problems accessing records from the cache map we should throw a ServicesException back to the GUI.
    • A situation may arise where a user tries to stock goods that another user has already stocked and in this scenario we should throw a StockingException back to the GUI.
  2. Unstocking - A user of the system should be able to stock goods up to but not exceeding the amount of stock level held.
    • If there is a problem accessing the Manufacturer file we should throw a IOException back to the GUI.
  3. Searching - A user of the system should be able search for goods by name, by location, by name and location or search all records. We will use the client.ManufacturerTableModel class to pass back search results to the GUI.
    • If there is a problem accessing the Manufacturer file we should throw a IOException back to the GUI.

Cut and paste the following code into your text editor and save it in the   c:\_Case_Study\src\services directory.


package services;

import java.io.IOException;

import model.StockingException;

import client.ManufacturerTableModel;

/**
 * An interface implemented by services that provide access to and interaction 
 * with the Manufacturer file from a GUI.
 * 
 * @author Charlie 
 * @version 1.0
 */
public interface Services {

    /**
     * Attempts to stock the Manufacturer product specified by the GUI user.
     * 
     * @param name The name of the Manufacturer.
     * @param location The location where the Manufacturer is based.
     * @param stockLevel The amount of stock remaining.
     * @param stockOrdered The amount of stock order.
     * @param stockLevel 
     * 
     * @throws IOException Indicates there was a problem accessing the data.
     * @throws ServicesException Indicates there was a problem updating the data.
     * @throws StockingException Indicates stocking already done by another user.
     */
    public void stockFromManufacturer( String name, String location,
            int stockLevel, int stockOrdered) 
            throws IOException, ServicesException, StockingException;

    /**
     * Attempts to unstock the Manufacturer product specified by the GUI user.
     * 
     * @param name The name of the Manufacturer.
     * @param location The location where the Manufacturer is based.
     * @param stockOrdered The amount of stock order.
     * 
     * @throws IOException Indicates there was a problem updating the data.
     */
    public void unstockBackToManufacturer(String name, String location,
            int stockOrdered) throws IOException;

    /**
     * Search contractor cache map for Manufacturer records matching the
     * criteria specified by the GUI user.
     * 
     * @param name The name of the Manufacturer.
     * @param location The location where the Manufacturer is based.
     * 
     * @return A ManufacturerTableModel holding selected records.
     * 
     * @throws IOException Indicates there was a problem accessing the data.
     */
    public ManufacturerTableModel searchManufacturers(String name, String location)
            throws IOException;
			
}

Compiling Our Source File With the -cp and -d Options

Open your command line editor:

Change to directory  cd c:\_Case_Study\src\services

Compile Services.java using the java compiler with the -cp and -d options
  javac -cp ..\..\classes -d ..\..\classes Services.java

Compiling The RemoteServices Interface Top

The RemoteServices interface is a marker interface that is implemented by services that provide remote access to and interaction with the Manufacturer file from a GUI.

Cut and paste the following code into your text editor and save it in the   c:\_Case_Study\src\remoteservices directory.


package remoteservices;

import java.rmi.Remote;

import services.Services;

/**
 * An interface implemented by services that provide remote access to and interaction 
 * with the Manufacturer file from a GUI. This is a marker interface that has no body.
 * 
 * @author Charlie 
 * @version 1.0
 */
public interface RemoteServices extends Remote, Services {

}

Compiling Our Source File With the -cp and -d Options

Open your command line editor:

Change to directory  cd c:\_Case_Study\src\remoteservices

Compile RemoteServices.java using the java compiler with the -cp and -d options
  javac -cp ..\..\classes -d ..\..\classes RemoteServices.java

Because we have used a package (package remoteservices;) at the top of the code the compiler has created this directory within the classes directory. When we look in the services directory we can see the compiled Services.class file and when we look in the newly created remoteservices directory we can see the compiled RemoteServices.class file.

compile Services Interfaces

Lesson 11 Complete

In this lesson we set up the initial Controller elements of the MVC pattern that can be derived from the project proposal.

Related Java Tutorials

Beginning Java - Primitive Variables
Beginning Java - Conditional Statements
Beginning Java - Loop Statements
Objects & Classes - Arrays
Objects & Classes - Class Structure and Syntax
Objects & Classes - Reference Variables
Objects & Classes - Methods
Objects & Classes - Instance Variables & Scope
Objects & Classes - Constructors
Objects & Classes - Static Members
Objects & Classes - Enumerations
OO Concepts - Encapsulation
OO Concepts - Inheritance Concepts - Using the super keyword
Swing - RMI - Serialization
Exceptions - Handling Exceptions
API Contents - Inheritance - Using the package keyword
API Contents - Inheritance - Using the import keyword
Concurrency - Synchronization - Synchronized Blocks

What's Next?

In the next section we finish coding the Model elements of the MVC pattern for our case study.