diff options
author | Michael Petretti | 2014-05-26 15:22:13 +0200 |
---|---|---|
committer | Michael Petretti | 2014-05-26 15:22:13 +0200 |
commit | a00f154064936fb3ec8d2ed37a7cf9cc681e0421 (patch) | |
tree | 2e0970077f4a740547e2dbe89b1f120f7e513c80 | |
parent | Added some notes (diff) | |
download | satellite-daemon-a00f154064936fb3ec8d2ed37a7cf9cc681e0421.tar.gz satellite-daemon-a00f154064936fb3ec8d2ed37a7cf9cc681e0421.tar.xz satellite-daemon-a00f154064936fb3ec8d2ed37a7cf9cc681e0421.zip |
Added a Globals.java class for properties handling.
3 files changed, 117 insertions, 15 deletions
diff --git a/src/main/java/org/openslx/satellitedaemon/App.java b/src/main/java/org/openslx/satellitedaemon/App.java index 7faf365..490ceba 100644 --- a/src/main/java/org/openslx/satellitedaemon/App.java +++ b/src/main/java/org/openslx/satellitedaemon/App.java @@ -9,6 +9,7 @@ import java.security.SignatureException; import java.security.UnrecoverableKeyException; import java.security.cert.CertificateException; +import org.apache.log4j.Logger; import org.openslx.imagemaster.thrift.iface.ServerAuthenticationException; import org.openslx.satellitedaemon.ftp.FtpUploadWorker; @@ -18,20 +19,33 @@ import org.openslx.satellitedaemon.ftp.FtpUploadWorker; */ public class App { - public static void main( String[] args ) throws NoSuchAlgorithmException, KeyStoreException, CertificateException, FileNotFoundException, IOException, ServerAuthenticationException, UnrecoverableKeyException, InvalidKeyException, SignatureException + private static Logger log = Logger.getLogger( App.class ); + + public static void main( String[] args ) + throws NoSuchAlgorithmException, KeyStoreException, CertificateException, FileNotFoundException, IOException, ServerAuthenticationException, UnrecoverableKeyException, + InvalidKeyException, SignatureException { - -// // TODO: A Thread that starts the call for new credentials and the upload -// // whenever a new image was sceduled in the db. -// ImageData imDat = new ImageData( UUID.randomUUID().toString(), 113, -// "TestImage", System.currentTimeMillis(), System.currentTimeMillis(), "me", "anyThing", -// true, false, "best", "theVeryBest", 1024 ); -// FtpCredentials ftpc = ThriftConnection.getFtpCredentials(imDat); -// Util.notNullFatal( ftpc, "ftpc was null" ); -// FtpImageUploader ftpIU = new FtpImageUploader( ftpc ); -// Util.notNullFatal( ftpIU, "ftpIU was null" ); -// ftpIU.connectTest(); - Thread uploadWorker = new Thread(new FtpUploadWorker()); + try { + Globals.loadProperties(); + if ( !Globals.propertiesValid() ) { + log.error( "Config file contains errors." ); + System.exit( 1 ); + } + } catch ( IOException e ) { + log.error( "Could not load config file. Quitting." ); + System.exit( 1 ); + } + // // TODO: A Thread that starts the call for new credentials and the upload + // // whenever a new image was sceduled in the db. + // ImageData imDat = new ImageData( UUID.randomUUID().toString(), 113, + // "TestImage", System.currentTimeMillis(), System.currentTimeMillis(), "me", "anyThing", + // true, false, "best", "theVeryBest", 1024 ); + // FtpCredentials ftpc = ThriftConnection.getFtpCredentials(imDat); + // Util.notNullFatal( ftpc, "ftpc was null" ); + // FtpImageUploader ftpIU = new FtpImageUploader( ftpc ); + // Util.notNullFatal( ftpIU, "ftpIU was null" ); + // ftpIU.connectTest(); + Thread uploadWorker = new Thread( new FtpUploadWorker() ); uploadWorker.start(); } } diff --git a/src/main/java/org/openslx/satellitedaemon/Globals.java b/src/main/java/org/openslx/satellitedaemon/Globals.java new file mode 100644 index 0000000..ae631f1 --- /dev/null +++ b/src/main/java/org/openslx/satellitedaemon/Globals.java @@ -0,0 +1,85 @@ +package org.openslx.satellitedaemon; + +import java.io.BufferedInputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Properties; + +public class Globals +{ + private static final Properties properties = new Properties(); + private static boolean loadedProperties = false; + + /** + * If there are more ints or Strings which should be added to config/global.properties, + * add to suiting enum, add a 'case' to getPropertyInt/String() and add checks to + * propertiesValid() + */ + public static enum PropInt + { + FTPPORT + } + + public static enum PropString + { + FTPSERVERIP + } + + public static boolean loadProperties() throws IOException + { + if ( loadedProperties ) + return false; + + // Load properties + BufferedInputStream stream = new BufferedInputStream( new FileInputStream( "config/global.properties" ) ); + properties.load( stream ); + stream.close(); + + return true; + } + + public static int getPropertyInt( Globals.PropInt props ) + { + String result = null; + + switch ( props ) { + case FTPPORT: + result = properties.getProperty( "ftp_port" ); + break; + default: + result = "0"; + break; + } + if ( result == null ) + return 0; + + return Integer.valueOf( result ); + } + + public static String getPropertyString( Globals.PropString props ) + { + String result = null; + + switch ( props ) { + case FTPSERVERIP: + result = properties.getProperty( "ftp_server_ip" ); + break; + default: + result = ""; + break; + } + return result; + } + + public static boolean propertiesValid() + { + if (Globals.getPropertyInt( PropInt.FTPPORT ) == 0 + || Globals.getPropertyString( PropString.FTPSERVERIP ).isEmpty() + || Globals.getPropertyString( PropString.FTPSERVERIP ) == null) { + return false; + } + else { + return true; + } + } +} diff --git a/src/main/java/org/openslx/satellitedaemon/ftp/FtpUploadWorker.java b/src/main/java/org/openslx/satellitedaemon/ftp/FtpUploadWorker.java index e4e4b7c..69c73a9 100644 --- a/src/main/java/org/openslx/satellitedaemon/ftp/FtpUploadWorker.java +++ b/src/main/java/org/openslx/satellitedaemon/ftp/FtpUploadWorker.java @@ -19,6 +19,9 @@ import javax.net.ssl.TrustManagerFactory; import org.apache.commons.net.ftp.FTPSClient; import org.openslx.imagemaster.thrift.iface.FtpCredentials; import org.openslx.imagemaster.thrift.iface.ImageData; +import org.openslx.satellitedaemon.Globals; +import org.openslx.satellitedaemon.Globals.PropInt; +import org.openslx.satellitedaemon.Globals.PropString; import org.openslx.satellitedaemon.db.DbImage; public class FtpUploadWorker implements Runnable @@ -55,7 +58,7 @@ public class FtpUploadWorker implements Runnable FTPSClient ftpClient = new FTPSClient( "SSL", true ); ftpClient.setTrustManager( trustManager ); try { - ftpClient.connect( nilsIp, ftpPort ); + ftpClient.connect( Globals.getPropertyString( PropString.FTPSERVERIP ), Globals.getPropertyInt( PropInt.FTPPORT ) ); if ( !ftpClient.login( ftpc.username, ftpc.password ) ) { throw new ConnectException( "Could not login." ); // TODO: Should not throw exception, otherwise we'd exit the run() method } @@ -63,7 +66,7 @@ public class FtpUploadWorker implements Runnable // + ". Reply code: " + ftpClient.getReplyCode() ); // TODO: Where do I find the path to the db-image? <-- in DbImage. It's relative, base path should come from config/???.properties (global static config class, see masterserver's Globals class) - File file = new File( "/path/to/File" ); + File file = new File( imageList.get( 0 ).path ); FileInputStream fis = new FileInputStream( file ); // TODO: What is the path where it should be stored? <-- Should be set in the FtpCredentials you get via Thrift, but doesn't really matter, Server can decide to ignore the name and pick own (talk to Nils) |