package de.grey.ownsync.config;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemException;
import de.grey.ownsync.exception.OwnSyncException;
import de.grey.ownsync.facade.OwnSyncStatus;
import de.grey.ownsync.logging.LoggingFileHandler;
import de.grey.ownsync.utils.DataFormatter;
import de.grey.ownsync.utils.FileUtils;
import de.grey.ownsync.utils.PropertiesHelper;
/**
* @author wogy
*/
public class OwnSyncConfiguration
{
public final static String SAMPLE_SYNC_CONFIGURATION_FILE= "SyncConfiguration.xml";
public final static String VersionNumberString = "0.4.0";
public final static String TestModus = "TestModus";
public final static String LastModifiedDelta = "LastModifiedDelta";
public final static String SyncFolderLogFile = "SyncFolderLogFile";
public final static String SyncFolderA = "SyncFolderA";
public final static String SyncFolderStateA = "SyncFolderStateA";
public final static String WastbasketFolderA = "WastbasketFolderA";
public final static String KeepLastFileVersionA = "KeepLastFileVersionA";
public final static String SyncExclusionPatternsA = "SyncExclusionPatternsA";
public final static String SyncExclusionRegExpPatternsA = "SyncExclusionRegExpPatternsA";
public final static String SyncFolderB = "SyncFolderB";
public final static String SyncFolderStateB = "SyncFolderStateB";
public final static String WastbasketFolderB = "WastbasketFolderB";
public final static String KeepLastFileVersionB = "KeepLastFileVersionB";
public final static String SyncExclusionPatternsB = "SyncExclusionPatternsB";
public final static String SyncExclusionRegExpPatternsB = "SyncExclusionRegExpPatternsB";
/*
* NON STATICs
*/
// TODOTO
public static Logger logger;
public boolean useTestModus = false;
private FolderConfiguration folderAConfiguration;
private FolderConfiguration folderBConfiguration;
private FileObject logFile;
private LoggingFileHandler fileHandler;
/**
* @throws OwnSyncException
*
*/
public OwnSyncConfiguration(String configFile) throws OwnSyncException
{
loadConfig(configFile);
}
public OwnSyncConfiguration(Properties props) throws OwnSyncException
{
loadConfigFromProperties(props);
}
private void loadConfig(String config) throws OwnSyncException
{
Properties props = null;
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
try
{
FileObject configFile = FileUtils.resolveFileObject(config);
if (!configFile.exists())
{
props = new Properties();
props.put(SyncFolderLogFile, "/Sample/global.log");
props.put(TestModus, "false");
props.put(LastModifiedDelta, "0");
props.put(SyncFolderA, "/Sample/Folder_A");
props.put(SyncFolderStateA, "/Sample/Folder_A_OldState.xml");
props.put(WastbasketFolderA, "/Sample/Folder_A_Trashbox");
props.put(KeepLastFileVersionA, "true");
props.put(SyncExclusionRegExpPatternsA, "");
props.put(SyncFolderB, "/Sample/Folder_B");
props.put(SyncFolderStateB, "/Sample/Folder_B_OldState.xml");
props.put(WastbasketFolderB, "/Sample/Folder_B_Trashbox");
props.put(KeepLastFileVersionB, "true");
props.put(SyncExclusionRegExpPatternsB, "");
bos = new BufferedOutputStream(configFile.getContent().getOutputStream());
props = PropertiesHelper.storeToXML(props, bos, "OwnSync sample configuration\n(C) Wilko Grey");
OwnSyncException ose = new OwnSyncException("Config file does not exist, is created and initialized!");
OwnSyncStatus.throwingMessage("OwnSyncStarter", "loadConfig(" + config + ")", ose);
throw ose;
}
bis = new BufferedInputStream(configFile.getContent().getInputStream());
props = new Properties();
props = PropertiesHelper.loadFromXML(props, bis);
loadConfigFromProperties(props);
OwnSyncStatus.setMessage("read config file " + config);
}
catch (FileSystemException e)
{
e.printStackTrace();
OwnSyncStatus.throwingMessage("OwnSyncStarter", "loadConfig", e);
throw new OwnSyncException(e);
}
finally
{
try
{
if (bis != null)
bis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
if (bos != null)
bos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
private void loadConfigFromProperties(Properties props) throws OwnSyncException
{
if (props == null)
{
printUsage();
return;
}
try
{
if (props.containsKey(TestModus))
useTestModus = Boolean.valueOf((String) props.get(TestModus)).booleanValue();
createLogger(props);
folderAConfiguration = new FolderConfiguration(SyncFolderA, SyncFolderStateA, WastbasketFolderA, KeepLastFileVersionA, SyncExclusionPatternsA, SyncExclusionRegExpPatternsA, LastModifiedDelta, props);
folderBConfiguration = new FolderConfiguration(SyncFolderB, SyncFolderStateB, WastbasketFolderB, KeepLastFileVersionB, SyncExclusionPatternsB, SyncExclusionRegExpPatternsB, LastModifiedDelta, props);
}
catch (FileSystemException e)
{
e.printStackTrace();
OwnSyncStatus.throwingMessage("OwnSyncStarter", "loadConfigFromProperties", e);
throw new OwnSyncException(e);
}
}
public static void printUsage()
{
System.out.println("Usage: java -Xmx128M -jar ownsync.jar "+OwnSyncConfiguration.SAMPLE_SYNC_CONFIGURATION_FILE+"\n\nCalled with an nonexisting filename would initially create a sample configuration file.\n");
}
private void createLogger(Properties props) throws OwnSyncException, FileSystemException
{
logFile = FolderConfiguration.getFile(SyncFolderLogFile, props);
if (logFile.exists() && FileUtils.getSize(logFile) > 0)
{
FileObject oldLogFile = FileUtils.resolveFileObject(FileUtils.getAbsolutePath(logFile).substring(0, FileUtils.getAbsolutePath(logFile).length()-4) + "_" + DataFormatter.getDateString4Filename(FileUtils.getLastModified(logFile)) + ".log");
logFile.moveTo(oldLogFile);
}
logger = Logger.getAnonymousLogger();
logger.setLevel(Level.ALL);
fileHandler = new LoggingFileHandler(logFile);
logger.addHandler(fileHandler);
logger.setUseParentHandlers(true);
logger.setLevel(Level.ALL);
OwnSyncStatus.setMessage("############################################");
OwnSyncStatus.setMessage("# OwnSync #");
OwnSyncStatus.setMessage("# Version "+VersionNumberString+" #");
OwnSyncStatus.setMessage("# Copyright by Wilko Grey #");
OwnSyncStatus.setMessage("# http://sourceforge.net/projects/ownsync/ #");
OwnSyncStatus.setMessage("############################################");
OwnSyncStatus.setMessage("");
}
public void close()
{
closeLogger();
}
private void closeLogger()
{
if (fileHandler == null)
return;
logger.removeHandler(fileHandler);
fileHandler.flush();
fileHandler.close();
fileHandler = null;
logFile = null;
logger = null;
}
public FolderConfiguration getFolderAConfiguration()
{
return folderAConfiguration;
}
public FolderConfiguration getFolderBConfiguration()
{
return folderBConfiguration;
}
}