Package de.chris_soft.nanodoa

Source Code of de.chris_soft.nanodoa.NanoDoAMain

/**
* NanoDoA - File based document archive
*
* Copyright (C) 2011-2012 Christian Packenius, christian.packenius@googlemail.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package de.chris_soft.nanodoa;

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

import javax.swing.JOptionPane;

import de.chris_soft.nanoarchive.Archive;
import de.chris_soft.nanoarchive.ArchiveKeys;
import de.chris_soft.nanoarchive.DerbyArchive;
import de.chris_soft.nanodoa.web.DocumentServer;
import de.chris_soft.nanodoa.web.DocumentServerKeys;
import de.chris_soft.utilities.AppProperties;
import de.chris_soft.utilities.FileUtils;
import de.chris_soft.utilities.LogUtils;
import de.chris_soft.utilities.StringLists;

/**
* Start class for NanoDoA application.
* @author Christian Packenius.
*/
public class NanoDoAMain implements MailKeys, DocumentServerKeys {
  /**
   * Set to <i>true</i> to stop full NanoDaO system.
   */
  public volatile boolean stopAll = false;

  private final TrayIconAndMenu trayIAM;

  /**
   * File for all properties of NanoDoA.
   */
  public static final File nanodoaPropertiesFile = new File("nanodoa.properties");

  final Archive archive;

  final DocumentInputManagement documentInputManagement;

  private DocumentServer documentServer = null;

  /**
   * Start method.
   * @param args May have a single argument with the base directory.
   * @throws Exception
   */
  public static void main(String[] args) throws Exception {
    try {
      new NanoDoAMain();
    }
    catch (Throwable t) {
      FileUtils.storeStringInFile(new File(System.currentTimeMillis() + ".err"), LogUtils.toString(t));
    }
  }

  /**
   * Constructor.
   * @throws Exception
   */
  public NanoDoAMain() throws Exception {
    checkAppProperties();
    AppProperties.setPropertiesFile(nanodoaPropertiesFile);

    trayIAM = new TrayIconAndMenu(this);

    String archivePath = AppProperties.getProperty(ArchiveKeys.PROP_KEY_ARCHIVE_DIRECTORY_PATH);
    if (archivePath == null) {
      archivePath = ".";
    }
    archive = new DerbyArchive(new File(archivePath));
    documentInputManagement = new DocumentInputManagement(archive, trayIAM);

    setDocumentInputManagementDirectories();

    startDocumentServer();
  }

  private void startDocumentServer() {
    if (Boolean.parseBoolean(AppProperties.getProperty(PROP_KEY_DOCUMENT_SERVER_SHALL_USE))) {
      int port = Integer.parseInt(AppProperties.getProperty(PROP_KEY_DOCUMENT_SERVER_PORT));
      try {
        documentServer = new DocumentServer(archive, port);
      }
      catch (Exception exception) {
        LogUtils.log(exception);
        JOptionPane
            .showMessageDialog(null, "Couldn't start document HTTP server!", "Error!", JOptionPane.ERROR_MESSAGE);
        stopSystem();
      }
    }
  }

  private void setDocumentInputManagementDirectories() {
    List<File> list = new ArrayList<File>();
    for (String s : StringLists.string2list(AppProperties.getProperty(ArchiveKeys.PROP_KEY_OBSERVE_DIRECTORIES))) {
      list.add(new File(s));
    }
    documentInputManagement.setDestinationDirectories(list);
  }

  private void checkAppProperties() throws IOException {
    if (!nanodoaPropertiesFile.exists()) {
      Properties props = getPropertiesFromConfigurationWizard();

      if (props == null) {
        stopSystem();
      }

      FileUtils.storeProperties(nanodoaPropertiesFile, props);
    }
  }

  private Properties getPropertiesFromConfigurationWizard() {
    ConfigurationWizard cw = new ConfigurationWizard();
    return cw.start();
  }

  /**
   * Stop full NanoDoA system.
   */
  public void stopSystem() {
    try {
      stopAll = true;
      if (documentInputManagement != null) {
        documentInputManagement.stopObservation();
        while (!documentInputManagement.isStopped()) {
          try {
            Thread.sleep(500);
          }
          catch (InterruptedException exception) {
            // Ignore.
          }
          if (trayIAM != null) {
            trayIAM.setNextImage();
          }
        }
      }

      if (trayIAM != null) {
        trayIAM.removeSystemTrayIcon();
      }

      if (documentServer != null) {
        documentServer.stop();
      }
    }
    finally {
      // Last fallback:
      // Stop all visible frames hard.
      System.exit(0);
    }
  }
}
TOP

Related Classes of de.chris_soft.nanodoa.NanoDoAMain

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.