diff options
author | Simon Rettberg | 2014-05-07 17:12:39 +0200 |
---|---|---|
committer | Simon Rettberg | 2014-05-07 17:12:39 +0200 |
commit | 7b9a2ff2e30e127f82040fc56ea1561cbc8b87e5 (patch) | |
tree | 7feaa56ebabc79226653685f1aa10e1451dbc358 /src | |
parent | initial commit (diff) | |
download | satellite-daemon-7b9a2ff2e30e127f82040fc56ea1561cbc8b87e5.tar.gz satellite-daemon-7b9a2ff2e30e127f82040fc56ea1561cbc8b87e5.tar.xz satellite-daemon-7b9a2ff2e30e127f82040fc56ea1561cbc8b87e5.zip |
Add DB classes + dummy DbImage class
Diffstat (limited to 'src')
3 files changed, 176 insertions, 0 deletions
diff --git a/src/main/java/org/openslx/satellitedaemon/db/DbImage.java b/src/main/java/org/openslx/satellitedaemon/db/DbImage.java new file mode 100644 index 0000000..b7fb67d --- /dev/null +++ b/src/main/java/org/openslx/satellitedaemon/db/DbImage.java @@ -0,0 +1,26 @@ +package org.openslx.satellitedaemon.db; + +import java.util.ArrayList; +import java.util.List; + +/** + * Represents an image in the satellite's database (mostly from + * m_VLData_imageInfo) + */ +public class DbImage +{ + + /** + * Returns a list of all images on this satellite that should be + * uploaded to the central server. + * + * @return list of images that are marked for upload, where the upload + * was either not started yet, or is incomplete + */ + public List<DbImage> getAllMarkedForUpload() + { + // TODO: Implement + return new ArrayList<>(); + } + +} diff --git a/src/main/java/org/openslx/satellitedaemon/db/MySQL.java b/src/main/java/org/openslx/satellitedaemon/db/MySQL.java new file mode 100644 index 0000000..c5f2fce --- /dev/null +++ b/src/main/java/org/openslx/satellitedaemon/db/MySQL.java @@ -0,0 +1,118 @@ +package org.openslx.satellitedaemon.db; + +import java.io.FileInputStream; +import java.io.BufferedInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.List; +import java.util.Properties; + +import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; + +import org.apache.log4j.Logger; +import org.openslx.satellitedaemon.util.Util; + +import fi.evident.dalesbred.Database; + +/** + * Class for talking to the DB via the dalesbred jdbc wrapper. Package private, + * so only the Db* classes can actually communicate with the database. + * + */ +class MySQL +{ + private static final Logger log = Logger.getLogger( MySQL.class ); + private static Database db = null; + + /** + * Static initializer for setting up the database connection. + * This gets called implicitly as soon as the class loader loads + * the class. In most cases that happens when the class is being + * accessed for the first time during run time. + */ + static + { + // Load connection info from class (TODO: Make pretty) + Properties properties = new Properties(); + try { + final BufferedInputStream stream = new BufferedInputStream( new FileInputStream( "config/mysql.properties" ) ); + properties.load( stream ); + stream.close(); + } catch ( FileNotFoundException e ) { + log.fatal( "config/mysql.properties not found!" ); + System.exit( 1 ); + } catch ( IOException e ) { + log.fatal( "Error reading from config/mysql.properties: " + e.getMessage() ); + System.exit( 1 ); + } catch ( Exception e ) { + log.fatal( "Generic error loading mysql properties file." ); + e.printStackTrace(); + System.exit( 1 ); + } + final String host = properties.getProperty( "host" ); + final String dbname = properties.getProperty( "db" ); + final String user = properties.getProperty( "user" ); + final String password = properties.getProperty( "password" ); + + Util.notNullFatal( host, "host not set in mysql properties" ); + Util.notNullFatal( dbname, "db not set in mysql properties" ); + Util.notNullFatal( user, "user not set in mysql properties" ); + Util.notNullFatal( password, "password not set in mysql properties" ); + + // Setup db connection + try { + MysqlDataSource ds = new MysqlDataSource(); + ds.setServerName( host ); + ds.setDatabaseName( dbname ); + ds.setUser( user ); + ds.setPassword( password ); + db = Database.forDataSource( ds ); + } catch ( Exception e ) { + log.fatal( "Error initializing mysql data source!" ); + e.printStackTrace(); + System.exit( 1 ); + } + } + + /** + * Get a list of objects of the given class from the database. + * The class needs a matching constructor for the query you pass in, i.e. number of + * arguments has to be equal to number of columns returned by query. + * + * @param clazz The class to instanciate for the result(s) + * @param sql The sql query to run + * @param args Any number of arguments to the query (using the '?' placeholder) + * @return A list containing the rows returned by the query, represented by the given class + */ + protected static <T> List<T> findAll( final Class<T> clazz, final String sql, final Object... args ) + { + return db.findAll( clazz, sql, args ); + } + + /** + * Run a query on the database that will return at most one result. + * If the query returns a row, it will be used to instanciate the given class. If + * it doesn't return a row, null will be returned. + * + * @param clazz The class to instanciate for the result (if any) + * @param sql The sql query to run + * @param args Any number of arguments to the query (using the '?' placeholder) + * @return Instance of clazz or null + */ + protected static <T> T findUniqueOrNull( final Class<T> clazz, final String sql, final Object... args ) + { + return db.findUniqueOrNull( clazz, sql, args ); + } + + /** + * Run an update on the database, return number of rows affected. + * + * @param sql The update/insert query to run + * @param args Any number of arguments to the query (using the '?' placeholder) + * @return Number of rows affected by query + */ + protected static int update( String sql, Object... args ) + { + return db.update( sql, args ); + } +} diff --git a/src/main/java/org/openslx/satellitedaemon/util/Util.java b/src/main/java/org/openslx/satellitedaemon/util/Util.java new file mode 100644 index 0000000..4f39e71 --- /dev/null +++ b/src/main/java/org/openslx/satellitedaemon/util/Util.java @@ -0,0 +1,32 @@ +package org.openslx.satellitedaemon.util; + +import org.apache.log4j.Logger; + +public class Util +{ + private static Logger log = Logger.getLogger( Util.class ); + + /** + * Check if the given object is null, abort program if true. An optional + * message to be printed can be passed. A stack trace will be printed, too. + * Finally the application terminates with exit code 2. + * + * This comes in handy if something must not be null, and you want user + * friendly output. A perfect example would be reading settings from a + * config file. You can use this on mandatory fields. + * + * @param something + * the object to compare to null + * @param message + * the message to be printed if something is null + */ + public static void notNullFatal( Object something, String message ) + { + if ( something == null ) { + if ( message != null ) + log.fatal( "[NOTNULL] " + message ); + log.warn( Thread.currentThread().getStackTrace().toString() ); + System.exit( 2 ); + } + } +} |