package org.bizobj.ftp.server;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;


import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.ftplet.UserManager;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.ssl.SslConfigurationFactory;
import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
import org.apache.ftpserver.usermanager.impl.BaseUser;
import org.apache.log4j.Logger;
import org.bizobj.ftp.auth.core.CustomAuthPropertiesUserManagerFactory;

public class Server {
	private static final Logger log = Logger.getLogger(Server.class);
	
	private FtpServer server;
	
	public void start(){
		FtpServer server;
		try {
			FtpServerFactory serverFactory = new FtpServerFactory();
			ListenerFactory factory = new ListenerFactory();
			// set the port and other attributes of the listener
			factory.setPort(CONFIG.getPort());
			log.info("FTP Port = " + factory.getPort());
			factory.setIdleTimeout(CONFIG.getMaxIdleTime());
			log.info("FTP IdleTimeout = " + factory.getIdleTimeout());
			// define SSL configuration
			SslConfigurationFactory ssl = new SslConfigurationFactory();
			ssl.setKeystoreFile(CONFIG.getKeystoreFile());
			ssl.setKeystorePassword(CONFIG.getKeystorePassword());
			// set the SSL configuration for the listener
			factory.setSslConfiguration(ssl.createSslConfiguration());
			//factory.setImplicitSsl(true);		//FIXME: setImplicitSsl(true) should cause client connection problem
			// Check and initialize user properties file
			File userRepoFile = CONFIG.getUserRepoFile();
			log.info("User repo properties file is " + userRepoFile.getCanonicalPath());
			boolean userRepoIsNew = false;
			if (! userRepoFile.exists()){
				log.warn("User repo properties file is not exist, begin to create it ...");
				userRepoIsNew = true;
				
				userRepoFile.getParentFile().mkdirs();
				OutputStream os = new FileOutputStream(userRepoFile);
				os.write(' ');
				os.close();
			}
			// replace the default listener
			serverFactory.addListener("default", factory.createListener());
			PropertiesUserManagerFactory userManagerFactory = new CustomAuthPropertiesUserManagerFactory();
			userManagerFactory.setFile(userRepoFile);
			String adminName = CONFIG.getAdminName();
			String adminPassword = CONFIG.getAdminPassword();
			userManagerFactory.setAdminName(adminName);
			UserManager userManager = userManagerFactory.createUserManager();
			serverFactory.setUserManager(userManager);
			// Check and initialize user properties file
			if (userRepoIsNew){
				log.warn("User repo properties file is new, begin to initialize it and create defaule admin user ...");
				
				BaseUser admin = CONFIG.buildWritePermissionUser(adminName,adminPassword);
				//Before save admin user, we need the file exists already
				userRepoFile.getParentFile().mkdirs();
				OutputStream os = new FileOutputStream(userRepoFile);
				os.write(' ');
				os.close();
				userManager.save(admin);
				
				log.warn("Admin user created, name = "+admin.getName()+", password="+admin.getPassword()+".");
			}
			// start the server
			server = serverFactory.createServer(); 
			server.start();
			this.server = server;
		} catch (FtpException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public void stop(){
		if (null!=this.server){
			this.server.stop();
		}
	}
}

@@ -1843,43 +1843,8 @@
 );%0D%0A
-%09%09%09boolean userRepoIsNew = false;%0D%0A
 %09%09%09i
@@ -1869,24 +1869,24 @@
 exists())%7B%0D%0A
+
 %09%09%09%09log.warn
@@ -1958,41 +1958,8 @@
 );%0D%0A
-%09%09%09%09userRepoIsNew = true;%0D%0A%09%09%09%09%0D%0A
 %09%09%09%09
@@ -2671,78 +2671,19 @@
 %0A%09%09%09
-if (userRepoIsNew)%7B%0D%0A%09%09%09%09log.warn(%22User repo properties file is new, b
+log.info(%22B
 egin
@@ -2738,23 +2738,16 @@
 ...%22);%0D%0A
-%09%09%09%09%0D%0A%09
 %09%09%09BaseU
@@ -2821,212 +2821,8 @@
 );%0D%0A
-%09%09%09%09//Before save admin user, we need the file exists already%0D%0A%09%09%09%09userRepoFile.getParentFile().mkdirs();%0D%0A%09%09%09%09OutputStream os = new FileOutputStream(userRepoFile);%0D%0A%09%09%09%09os.write(' ');%0D%0A%09%09%09%09os.close();%0D%0A%09
 %09%09%09u
@@ -2853,23 +2853,16 @@
 %0A%09%09%09
-%09%0D%0A%09%09%09%09log.warn
+log.info
 (%22Ad
@@ -2883,19 +2883,17 @@
 ed, name
- = 
+=
 %22+admin.
@@ -2939,30 +2939,24 @@
 rd()+%22.%22);%0D%0A
-%09%09%09%7D%0D%0A
 %09%09%09// start 

Change to re-create admin's user and password to fit the system property settings every time start up.