summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/App.java
blob: 7f004fdfc5c0a544ef2663436c32b40f8b2e52da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package org.openslx.imagemaster;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.openslx.imagemaster.Globals.PropInt;
import org.openslx.imagemaster.ftp.FtpCredentialsScheduler;
import org.openslx.imagemaster.ftp.ImageProcessor;
import org.openslx.imagemaster.ftp.MasterFtpServer;
import org.openslx.imagemaster.thrift.server.BinaryListener;
import org.slf4j.LoggerFactory;

public class App
{

	private static Logger log = Logger.getLogger( App.class );

	private static List<Thread> servers = new ArrayList<>();
	
	public static final MasterFtpServer ftpServer = new MasterFtpServer();
	
	static {
		// TODO:
		// This is a temporary workaround for this annoying log4j error msg.
		// It's initializing the logger before anything else is done.
		LoggerFactory.getLogger("ROOT");
	}

	public static void main( String[] args )
	{
		// Init logging
		log.info( "Starting Application" );

		// Load properties
		try {
			Globals.loadProperties(); // don't need to check return, because this should be the first time where props are loaded.
			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 );
		}
		log.info( "Loaded config file" );
		
		log.info( "Checking !uploading! db entries." );
		ImageProcessor.checkUploading();

		// Create binary listener
		Thread t;
		t = new Thread( new BinaryListener(), "BinaryListener" );
		servers.add( t );
		t.start();

		// Create Ftp Server
		ftpServer.init( Globals.getPropertyInt( PropInt.FTPPORT ) ); // this init needs to be done, because we have to wait for the config file
		Thread f;
		f = new Thread( ftpServer, "FtpServer" );
		servers.add( f );
		f.start();
		
		// add ftp users from database
		ftpServer.addDbFtpUsers();
		
		// start  FtpCredentialsScheduler
		FtpCredentialsScheduler.startScheduling();
		
		// testtesteset
		ftpServer.addUser( "asdfasdfasdf", MasterFtpServer.Mode.DOWNLOADING, "windows7.vmdk" );

		// Run more servers
		// ...
		// Wait for all servers to die
		for ( Thread wait : servers ) {
			boolean success = false;
			while ( !success ) {
				try {
					wait.join();
					success = true;
				} catch ( InterruptedException e ) {
					// Do nothing...
				}
			}
		}

		log.info( "All Servers shut down, exiting..." );
	}
}