From: Roland Haeder Date: Wed, 12 Aug 2015 09:53:01 +0000 (+0200) Subject: Let's get rid of abortProgramWithException() where calls from application containers... X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=2bd5ad9deec28d35c8eb415fb554e35163a26423;p=jcore.git Let's get rid of abortProgramWithException() where calls from application containers may come Signed-off-by:Roland Häder --- diff --git a/src/org/mxchange/jcore/BaseFrameworkSystem.java b/src/org/mxchange/jcore/BaseFrameworkSystem.java index 68e74ab..2473ac4 100644 --- a/src/org/mxchange/jcore/BaseFrameworkSystem.java +++ b/src/org/mxchange/jcore/BaseFrameworkSystem.java @@ -178,9 +178,10 @@ public class BaseFrameworkSystem implements FrameworkInterface { * @param columnName Column name * @return Value from field * @throws IllegalArgumentException Some implementations may throw this. + * @throws NoSuchMethodException Some implementations may throw this. */ @Override - public Object getValueFromColumn (final String columnName) throws IllegalArgumentException { + public Object getValueFromColumn (final String columnName) throws IllegalArgumentException, NoSuchMethodException { throw new UnsupportedOperationException(MessageFormat.format("Not implemented. columnName={0}", columnName)); //NOI18N } @@ -223,30 +224,25 @@ public class BaseFrameworkSystem implements FrameworkInterface { * @param methodName Method name * @return A Method instance */ - private Method getMethodFromName (final FrameworkInterface instance, final String targetClass, final String methodName) { + private Method getMethodFromName (final FrameworkInterface instance, final String targetClass, final String methodName) throws NoSuchMethodException { // Trace messahe this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName)); //NOI18N - + // Get target class instance Class c = this.getClassFromTarget(instance, targetClass); - + // Init field instance Method method = null; - + // Use reflection to get all attributes - try { - method = c.getDeclaredMethod(methodName, new Class[0]); - } catch (final SecurityException | NoSuchMethodException ex) { - // Security problem - this.abortProgramWithException(ex); - } - + method = c.getDeclaredMethod(methodName, new Class[0]); + // Assert on field assert (method instanceof Method) : "method is not a Method instance"; //NOI18N - + // Trace message this.getLogger().trace(MessageFormat.format("method={0} - EXIT!", method)); //NOI18N - + // Return it return method; } @@ -264,7 +260,7 @@ public class BaseFrameworkSystem implements FrameworkInterface { * * @param throwable Any type of Throwable */ - protected void abortProgramWithException (final Throwable throwable) { + protected final void abortProgramWithException (final Throwable throwable) { // Log exception ... this.getLogger().catching(throwable); @@ -324,9 +320,10 @@ public class BaseFrameworkSystem implements FrameworkInterface { * @param columnName Column name to check * @param bool Boolean value * @return Whether all conditions are met + * @throws NoSuchMethodException May be thrown by some implementations */ @Override - public boolean isValueEqual (final String columnName, final boolean bool) { + public boolean isValueEqual (final String columnName, final boolean bool) throws NoSuchMethodException { // Not implemented throw new UnsupportedOperationException(MessageFormat.format("Not implemented. columnName={0},bool={1}", columnName, bool)); //NOI18N } @@ -366,16 +363,12 @@ public class BaseFrameworkSystem implements FrameworkInterface { /** * Writes the properties file to disk */ - private void writePropertiesFile () { + private void writePropertiesFile () throws IOException { // Trace message this.getLogger().trace("CALLED!"); //NOI18N - try { - // Write it - BaseFrameworkSystem.properties.store(new PrintWriter(FrameworkInterface.PROPERTIES_CONFIG_FILE), "This file is automatically generated. You may wish to alter it."); //NOI18N - } catch (final IOException ex) { - this.abortProgramWithException(ex); - } + // Write it + BaseFrameworkSystem.properties.store(new PrintWriter(FrameworkInterface.PROPERTIES_CONFIG_FILE), "This file is automatically generated. You may wish to alter it."); //NOI18N // Trace message this.getLogger().trace("EXIT!"); //NOI18N @@ -525,8 +518,11 @@ public class BaseFrameworkSystem implements FrameworkInterface { * @param targetClass Target class to look in * @param methodName Method name to look for * @return Boolean value from field + * @throws java.lang.NoSuchMethodException If the method was not found + * @throws java.lang.IllegalAccessException If the method cannot be accessed + * @throws java.lang.reflect.InvocationTargetException Some other problems? */ - protected boolean getBooleanField (final FrameworkInterface instance, final String targetClass, final String methodName) { + protected boolean getBooleanField (final FrameworkInterface instance, final String targetClass, final String methodName) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { // Trace messahe this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName)); //NOI18N @@ -536,12 +532,8 @@ public class BaseFrameworkSystem implements FrameworkInterface { // Get value from field Boolean value = false; - try { - value = (Boolean) method.invoke(instance); - } catch (final IllegalArgumentException | IllegalAccessException | InvocationTargetException ex) { - // Other problem - this.abortProgramWithException(ex); - } + // Try to get the value by invoking the method + value = (Boolean) method.invoke(instance); // Return value return value; @@ -563,8 +555,11 @@ public class BaseFrameworkSystem implements FrameworkInterface { * @param targetClass Target class to look in * @param methodName Method name to look for * @return Any value from field + * @throws java.lang.NoSuchMethodException If the method was not found + * @throws java.lang.IllegalAccessException If the method cannot be accessed + * @throws java.lang.reflect.InvocationTargetException Some other problems? */ - protected Object getField (final FrameworkInterface instance, final String targetClass, final String methodName) { + protected Object getField (final FrameworkInterface instance, final String targetClass, final String methodName) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { // Trace messahe this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName)); //NOI18N @@ -572,14 +567,7 @@ public class BaseFrameworkSystem implements FrameworkInterface { Method method = this.getMethodFromName(instance, targetClass, methodName); // Get value from field - Object object = null; - - try { - object = method.invoke(instance); - } catch (final IllegalArgumentException | IllegalAccessException | InvocationTargetException ex) { - // Other problem - this.abortProgramWithException(ex); - } + Object object = method.invoke(instance); // Return value return object; @@ -633,8 +621,9 @@ public class BaseFrameworkSystem implements FrameworkInterface { * @param instance Instance to run getter calls on * @param className Class name to iterate over * @return An iterator over all object's fields + * @throws java.lang.NoSuchMethodException If the called method does not exist */ - protected Iterator> fieldIterator (final FrameworkInterface instance, final String className) { + protected Iterator> fieldIterator (final FrameworkInterface instance, final String className) throws IllegalArgumentException, NoSuchMethodException { // Trace message this.getLogger().trace(MessageFormat.format("instance={0},className={1} - CALLED!", instance, className)); //NOI18N @@ -751,8 +740,10 @@ public class BaseFrameworkSystem implements FrameworkInterface { /** * Prepares all properties, the file is written if it is not found + * + * @throws java.io.IOException If any IO problem occurs */ - protected void initProperties () { + protected void initProperties () throws IOException { // Trace message this.getLogger().trace("CALLED!"); //NOI18N @@ -783,9 +774,6 @@ public class BaseFrameworkSystem implements FrameworkInterface { // Write file this.writePropertiesFile(); - } catch (final IOException ex) { - // Something else didn't work - this.abortProgramWithException(ex); } // Trace message @@ -822,9 +810,6 @@ public class BaseFrameworkSystem implements FrameworkInterface { // Not found isBool = false; - } catch (final SecurityException ex) { - // Really bad? - this.abortProgramWithException(ex); } // Trace message diff --git a/src/org/mxchange/jcore/FrameworkInterface.java b/src/org/mxchange/jcore/FrameworkInterface.java index 57c6436..8ebc003 100644 --- a/src/org/mxchange/jcore/FrameworkInterface.java +++ b/src/org/mxchange/jcore/FrameworkInterface.java @@ -86,8 +86,9 @@ public interface FrameworkInterface { * @param columnName Column name to check * @param bool Boolean value * @return Whether all conditions are met + * @throws java.lang.NoSuchMethodException If called method was not found */ - public boolean isValueEqual (final String columnName, final boolean bool); + public boolean isValueEqual (final String columnName, final boolean bool) throws NoSuchMethodException; /** * Some "getter for a value from given column name. This name will be @@ -96,6 +97,7 @@ public interface FrameworkInterface { * @param columnName Column name * @return Value from field * @throws IllegalArgumentException Some implementations may throw this + * @throws java.lang.NoSuchMethodException If the invoked method was not found */ - public Object getValueFromColumn (final String columnName) throws IllegalArgumentException; + public Object getValueFromColumn (final String columnName) throws IllegalArgumentException, NoSuchMethodException; } diff --git a/src/org/mxchange/jcore/contact/BaseContact.java b/src/org/mxchange/jcore/contact/BaseContact.java index d5dc47b..26dc172 100644 --- a/src/org/mxchange/jcore/contact/BaseContact.java +++ b/src/org/mxchange/jcore/contact/BaseContact.java @@ -484,7 +484,7 @@ public class BaseContact extends BaseFrameworkSystem implements Contact { * @return Value from field */ @Override - public Object getValueFromColumn (final String columnName) throws IllegalArgumentException { + public Object getValueFromColumn (final String columnName) throws IllegalArgumentException, NoSuchMethodException { // Trace message this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName)); @@ -569,7 +569,7 @@ public class BaseContact extends BaseFrameworkSystem implements Contact { * @return Whether all conditions are met */ @Override - public boolean isValueEqual (final String columnName, final boolean bool) { + public boolean isValueEqual (final String columnName, final boolean bool) throws NoSuchMethodException { // Trace message this.getLogger().trace(MessageFormat.format("columnName={0},bool={1} - CALLED!", columnName, bool)); @@ -600,7 +600,7 @@ public class BaseContact extends BaseFrameworkSystem implements Contact { * @return An iterator */ @Override - public Iterator> iterator () { + public Iterator> iterator () throws NoSuchMethodException { return this.fieldIterator(this, "BaseContact"); } diff --git a/src/org/mxchange/jcore/contact/Contact.java b/src/org/mxchange/jcore/contact/Contact.java index c3f5476..1d7c501 100644 --- a/src/org/mxchange/jcore/contact/Contact.java +++ b/src/org/mxchange/jcore/contact/Contact.java @@ -32,8 +32,9 @@ public interface Contact extends FrameworkInterface { * Returns an iterator of all values from this object * * @return An iterator + * @throws java.lang.NoSuchMethodException If the invoked method was not found */ - public Iterator> iterator (); + public Iterator> iterator ()throws NoSuchMethodException; /** * Some "getter" for translated gender of the contact diff --git a/src/org/mxchange/jcore/database/backend/DatabaseBackend.java b/src/org/mxchange/jcore/database/backend/DatabaseBackend.java index 26a6c4c..9f0dbef 100644 --- a/src/org/mxchange/jcore/database/backend/DatabaseBackend.java +++ b/src/org/mxchange/jcore/database/backend/DatabaseBackend.java @@ -16,6 +16,7 @@ */ package org.mxchange.jcore.database.backend; +import java.io.IOException; import java.sql.SQLException; import org.mxchange.jcore.FrameworkInterface; import org.mxchange.jcore.criteria.searchable.SearchableCritera; @@ -48,6 +49,9 @@ public interface DatabaseBackend extends FrameworkInterface { /** * Shuts down this backend + * + * @throws java.sql.SQLException If any SQL error occurs + * @throws java.io.IOException If any IO error occurs */ - public void doShutdown (); + public void doShutdown () throws SQLException, IOException; } diff --git a/src/org/mxchange/jcore/database/backend/base64/Base64CsvDatabaseBackend.java b/src/org/mxchange/jcore/database/backend/base64/Base64CsvDatabaseBackend.java index de3eeba..7df0c54 100644 --- a/src/org/mxchange/jcore/database/backend/base64/Base64CsvDatabaseBackend.java +++ b/src/org/mxchange/jcore/database/backend/base64/Base64CsvDatabaseBackend.java @@ -49,8 +49,9 @@ public class Base64CsvDatabaseBackend extends BaseDatabaseBackend implements Dat * Constructor with table name * * @param frontend Wrapper instance to call back + * @throws java.io.FileNotFoundException If the file was not found */ - public Base64CsvDatabaseBackend (final DatabaseFrontend frontend) { + public Base64CsvDatabaseBackend (final DatabaseFrontend frontend) throws FileNotFoundException { // Trace message this.getLogger().trace(MessageFormat.format("frontend={0} - CALLED!", frontend)); //NOI18N @@ -78,7 +79,7 @@ public class Base64CsvDatabaseBackend extends BaseDatabaseBackend implements Dat } catch (final FileNotFoundException ex) { // Did not work this.getLogger().error(MessageFormat.format("File {0} cannot be opened: {1}", fileName, ex.toString())); //NOI18N - this.abortProgramWithException(ex); + throw ex; } // Output message @@ -102,58 +103,12 @@ public class Base64CsvDatabaseBackend extends BaseDatabaseBackend implements Dat * Shuts down this backend */ @Override - public void doShutdown () { + public void doShutdown () throws SQLException, IOException { // Trace message this.getLogger().trace("CALLED!"); //NOI18N - try { - // Close file - this.getStorageFile().close(); - } catch (final IOException ex) { - // Abort program - this.abortProgramWithException(ex); - } - - // Trace message - this.getLogger().trace("EXIT!"); //NOI18N - } - - /** - * Get length of underlaying file - * - * @return Length of underlaying file - */ - private long length () { - long length = 0; - - try { - length = this.getStorageFile().length(); - this.getLogger().debug(MessageFormat.format("length={0}", length)); //NOI18N - } catch (final IOException ex) { - // Length cannot be determined - // Abort program - this.abortProgramWithException(ex); - } - - // Return result - this.getLogger().trace(MessageFormat.format("length={0} : EXIT!", length)); //NOI18N - return length; - } - - /** - * Rewinds backend - */ - private void rewind () { - // Trace message - this.getLogger().trace("CALLED!"); //NOI18N - - try { - // Rewind underlaying database file - this.getStorageFile().seek(0); - } catch (final IOException ex) { - // Abort program - this.abortProgramWithException(ex); - } + // Close file + this.getStorageFile().close(); // Trace message this.getLogger().trace("EXIT!"); //NOI18N @@ -230,6 +185,23 @@ public class Base64CsvDatabaseBackend extends BaseDatabaseBackend implements Dat return isEof; } + /** + * Get length of underlaying file + * + * @return Length of underlaying file + */ + private long length () throws IOException { + long length = 0; + + // Try to get length from file + length = this.getStorageFile().length(); + this.getLogger().debug(MessageFormat.format("length={0}", length)); //NOI18N + + // Return result + this.getLogger().trace(MessageFormat.format("length={0} : EXIT!", length)); //NOI18N + return length; + } + /** * Reads a line from file base * @@ -274,7 +246,7 @@ public class Base64CsvDatabaseBackend extends BaseDatabaseBackend implements Dat * * @return A list with Contact instances */ - private List readList () throws BadTokenException { + private List readList () throws BadTokenException, IOException { this.getLogger().trace("CALLED!"); //NOI18N // First rewind @@ -313,4 +285,18 @@ public class Base64CsvDatabaseBackend extends BaseDatabaseBackend implements Dat this.getLogger().trace(MessageFormat.format("list.size()={0} : EXIT!", list.size())); //NOI18N return list; } + + /** + * Rewinds backend + */ + private void rewind () throws IOException { + // Trace message + this.getLogger().trace("CALLED!"); //NOI18N + + // Rewind underlaying database file + this.getStorageFile().seek(0); + + // Trace message + this.getLogger().trace("EXIT!"); //NOI18N + } } diff --git a/src/org/mxchange/jcore/database/backend/mysql/MySqlDatabaseBackend.java b/src/org/mxchange/jcore/database/backend/mysql/MySqlDatabaseBackend.java index cf77ec6..c87f46f 100644 --- a/src/org/mxchange/jcore/database/backend/mysql/MySqlDatabaseBackend.java +++ b/src/org/mxchange/jcore/database/backend/mysql/MySqlDatabaseBackend.java @@ -16,6 +16,7 @@ */ package org.mxchange.jcore.database.backend.mysql; +import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; @@ -120,16 +121,11 @@ public class MySqlDatabaseBackend extends BaseDatabaseBackend implements Databas } @Override - public void doShutdown () { + public void doShutdown () throws SQLException, IOException { // This should not happen: assert(MySqlDatabaseBackend.connection instanceof Connection) : MessageFormat.format("connection is not valid: {0}", MySqlDatabaseBackend.connection); //NOI18N - try { - // Close down database connection - MySqlDatabaseBackend.connection.close(); - } catch (final SQLException ex) { - // Something happened during close() - this.abortProgramWithException(ex); - } + // Close down database connection + MySqlDatabaseBackend.connection.close(); } }