OOP Lab Task 4

De la WikiLabs
Versiunea din 2 decembrie 2015 03:33, autor: Zhascsi (Discuție | contribuții) (Requirements)

Required Tutorials

Requirements

  1. Write a class called ServerConfig whose purpose is to store configuration values read from a text file. This class will be used later on during the development of the server application, and needs to meet the following requirements:
    • The class must have two constructors:
      1. One with an argument of type String, which is the name of the file to read from;
      2. One without arguments, which assumes that the name of the configuration file is server.cfg.
    • Upon its instantiation the ServerConfig object reads the file, line by line, till its end, and retains the value of the congiguration parameters. If the configuration file cannot be accessed or does not obey its format, the instantiation fails and throws an exception according to the cause of failure.
    • The constructors in class ServerConfig must throw exceptions if any issues arise when reading or parsing the file:
      1. IOException if the file is missing or it cannot be opened (this exception is already being thrown by stream related classes, so you only need to rethrow it further up the execution stack or declare that it is thrown).
      2. InvalidFormatException if at least one line from the file doesn't match the expected pattern (this type of exception doesn't exist and it needs to be defined).
      3. UnknownKeyException if an unknown property is present in the file (this type of exception doesn't exist and it needs to be defined).
      4. MissingKeyException if one of the expected properties is missing from the file (this type of exception doesn't exist and it needs to be defined).
    • The ServerConfig class must be immutable. The configuration parameters read from file must be stored as instance constants inside the ServerConfig object.
    • The class must have a public method, named read, with only one argument, a string. When called with a valid configuration parameter name the method returns the value of that parameter, otherwise it throws an exception of type UnknownKeyException, with the invalid name given as the exception's detail message string.
  2. InvalidFormatException, UnknownKeyException and MissingKeyException types are all derived from the ConfigureException type, which is in its turn derived from Exception class, and must also be defined.
    • The types of exceptions which must be defined have very simple implementations. You need to define them as classes that extend their base classes. Only the UnknownKeyException needs to contain a constructor with an argument of type String needed to retain the invalid/unknown configuration parameter name, and that constructor will simply call the constructor of the super-class.
  3. Only these following property names are allowed in the configuration file:
    • TCP_PORT - the TCP port on which the server will be listening on.
    • MAX_CLIENTS - the maximum number of clients that the server application may serve.
  4. All property values are integers.
  5. Write a test for this class (which should open a file, read its contents and print the read parameters on the screen) and add it to the main method in class Main.

The configuration file that the ServerConfig class constructor reads is a plain text file, containing only comments, blank spaces, and parameter-value pairs.

  • A parameter-value pair must be written as an assignment:
PROPERTY=value
  • Spaces or tabs around the equal sign are also ignored.
  • Any line starting with the character #, preceded or not by spaces or "tab" characters is considered to be a comment and will be ignored.
  • Lines that do not contain any printable character but only spaces and/or tabs, or are empty will also be ignored.
  • Spaces or tabs at the beginning and end of lines are ignored.

An example of a configuration file:

#this is a config file for John's server

MAX_CLIENT=100
  # changed by Mary

    TCP_PORT = 9000

Notes:

  • The submitted files will be only Java implementations of the following classes: ServerConfig, Main, ConfigureException, InvalidFormatException, UnknownKeyException and MissingKeyException.
  • The following classes and methods may be of use for your implementation. Look them up in the API documentation:
    • String.trim
    • String.charAt
    • String.contains
    • String.startsWith
    • String.equals
    • String.split
    • Integer.parseInt
    • BufferedReader.readLine
  • Because you can have two different objects of type String (with different references) which contain the same character sequence, and because the operator == is used to compare references, the correct way to compare two Strings is by calling the method equals:
String a = "abc";
String b = new String(a);
if(a == b) { // <- wrong, this is evaluated to false
  //...
}
if(a.equals(b)) { // <- correct, this is evaluated to true
  //...
}

Submitting

  • The assignment will be evaluated automatically by the Web-CAT platform.
  • You could access the Web-CAT platform using the username and the password with which you acces the electronica.curs.pub.ro intranet.
  • Select the OOP Lab Task 4 assignment.
  • Submit your work as a single .zip archive (give it whatever name you choose) containing only the Java source code files.
  • Attention Any deviation from these instructions may lead to the loss of the entire amount of points.