pl.gda.pg.eti.ask.przemyslawbielicki.aeh.translation
Class ExceptionTranslator

java.lang.Object
  extended bypl.gda.pg.eti.ask.przemyslawbielicki.aeh.translation.ExceptionTranslator

public class ExceptionTranslator
extends Object

ExceptionTranslator is a singleton class. That means that only one instance of this class can exist.

ExceptionTranslator translates exception given by user to resource path (if translation exists). User that want to retrieve exception translation can automatically add translation for unexistent exception to XML database.

XML database is automatically loaded and saved from and to filesystem.

TODO convert to EJB class

Version:
0.9
Author:
Przemyslaw Bielicki
Gdansk University of Technology
Faculty of Electronics, Telecommunication and Computer Science
Distributed Computer Systems

Field Summary
private static String databasePath_
          Path where XML database is stored - file name
private  Hashtable exceptionTranslations_
          Table containing translations for concrete classes.
private static ExceptionTranslator exceptionTranslator_
          Instance of singleton class.
private static boolean initialized_
          Indicates if object is initialized_ or not.
 
Constructor Summary
private ExceptionTranslator()
          Constructor is private to prevent calling it by anyone.
 
Method Summary
private  String addDefaultTranslation(Throwable throwable)
          Adds default translation for given exception.
 void addExceptionTranslation(ExceptionTranslation translation)
          Adds default translation for given exception.
 boolean deserializeTranslations(String fileName)
          Deserializes translation XML database from file fileName to objects form.
static ExceptionTranslator getInstance()
          Typical getInstance method.
 String getTranslation(Throwable throwable)
          Retrieves translation resource path for given exception.
 String getTranslation(Throwable throwable, boolean addDefault)
          Retrieves translation resource path for given exception.
 boolean serializeTranslations(String fileName)
          Serializes translation XML database from objects form to file fileName.
static void setDatabasePath(String path)
          Setter method for XML database
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

exceptionTranslations_

private Hashtable exceptionTranslations_
Table containing translations for concrete classes. Keys in this table are class names ExceptionTranslation.exceptionClass_.


initialized_

private static boolean initialized_
Indicates if object is initialized_ or not.


exceptionTranslator_

private static ExceptionTranslator exceptionTranslator_
Instance of singleton class.


databasePath_

private static String databasePath_
Path where XML database is stored - file name

Constructor Detail

ExceptionTranslator

private ExceptionTranslator()
Constructor is private to prevent calling it by anyone. It is because this class is a singleton.

Method Detail

getInstance

public static ExceptionTranslator getInstance()
Typical getInstance method. If object is not initialized_ this method runs private constructor ExceptionTranslator() and creates a singleton instance.

Returns:
ExceptionTranslator instance

setDatabasePath

public static void setDatabasePath(String path)
Setter method for XML database

Parameters:
path - to XML database

getTranslation

public String getTranslation(Throwable throwable)
                      throws TranslationNotFoundException,
                             ResourceNotDefinedException
Retrieves translation resource path for given exception.

Parameters:
throwable - exception to translate
Returns:
translation resource path
Throws:
TranslationNotFoundException - translation for given exception doesn't exist
ResourceNotDefinedException - translation for given exception exists but resource path is not defined

getTranslation

public String getTranslation(Throwable throwable,
                             boolean addDefault)
                      throws TranslationNotFoundException,
                             ResourceNotDefinedException
Retrieves translation resource path for given exception. If there is no translation in XML database for given exception and addDefault argument is set to true default translation for given exception is added to XML structure. If addDefault argument is set to false this method is equal getTranslation(Throwable)

Parameters:
throwable - exception to translate
Returns:
translation resource path
Throws:
TranslationNotFoundException - translation for given exception doesn't exist
ResourceNotDefinedException - translation for given exception exists but resource path is not defined
See Also:
getTranslation(Throwable, boolean)

addDefaultTranslation

private String addDefaultTranslation(Throwable throwable)
Adds default translation for given exception. If there doesn't exist translation for throwable class this method adds this class to XML database. Next message translation is added to database. Please remember that newly added translation for given exception is not configured in database (in the meaning that its translation resource is null).

Parameters:
throwable - exception to add default translation for

addExceptionTranslation

public void addExceptionTranslation(ExceptionTranslation translation)
                             throws DuplicateTranslationException
Adds default translation for given exception. If translation for translation class ExceptionTranslation.exceptionClass_ already exists DuplicateTranslationException is thrown.

Parameters:
translation - translation to add to XML database
Throws:
DuplicateTranslationException - is thrown when translation for given class already exists

deserializeTranslations

public boolean deserializeTranslations(String fileName)
Deserializes translation XML database from file fileName to objects form. Simply creates objects structure from XML file.

Parameters:
fileName - path to file where translation XML database is
Returns:
true when deserializing finishes correctly and false in other case

serializeTranslations

public boolean serializeTranslations(String fileName)
Serializes translation XML database from objects form to file fileName. Simply creates XML document and then writes it to file.

Parameters:
fileName - path to file where translation XML database is
Returns:
true when serializing finishes correctly and false in other case