orbeon-bluedb-integration/documents-explorer/src/main/java/eu/mooseinc/documents/explorer/ApplicationBean.java

103 lines
3.0 KiB
Java

/*********************************************************************************************************************************
* Autorskie Prawa Majątkowe - Moose Spółka z ograniczoną odpowiedzialnością
* Copyright 2019 Moose Spółka z ograniczoną odpowiedzialnością
********************************************************************************************************************************/
package eu.mooseinc.documents.explorer;
import org.apache.log4j.Logger;
import javax.annotation.PostConstruct;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.file.Paths;
import java.text.MessageFormat;
import java.util.Properties;
/**
* Bean used for application initialization.
*/
@ApplicationScoped
@ManagedBean(eager = true, name = "applicationBean")
public class ApplicationBean {
private static final Logger logger = Logger.getLogger( ApplicationBean.class );
/**
* Path to user configuration.
*/
private static final String CUSTOM_PROPERTIES_PATH = "/opt/documents-explorer/application.properties";
/**
* The path to the directory used to store documents.
*/
private String documentsDirectory;
/**
* Reads the configuration from the configuration file.
*/
@PostConstruct
public void readProperties() {
Properties properties = new Properties( );
properties.putAll( loadDefaultProperties() );
properties.putAll( loadCustomProperties() );
final String directoryPath = properties.getProperty("documents.directory");
final File directory = Paths.get(directoryPath).toFile();
if (!directory.exists()) {
if (!directory.mkdirs()) {
logger.error( "Create directory error" );
documentsDirectory = "";
}
}
else
documentsDirectory = directoryPath;
logger.info(MessageFormat.format("Using the {0} directory for file storing.", documentsDirectory));
}
/**
* Load custom properties.
*
* @return properties.
*/
private Properties loadCustomProperties() {
try (InputStream in = new FileInputStream( CUSTOM_PROPERTIES_PATH )) {
final Properties properties = new Properties( );
properties.load( in );
return properties;
}
catch (Exception e) {
logger.error( "The application encountered an error while loading the custom properties.", e );
return new Properties( );
}
}
/**
* Load default properties.
*
* @return properties.
*/
private Properties loadDefaultProperties() {
try (InputStream in = getClass().getClassLoader().getResourceAsStream("application.properties")) {
final Properties properties = new Properties( );
properties.load( in );
return properties;
}
catch (Exception e) {
logger.error( "The application encountered an error while loading the default properties.", e );
return new Properties( );
}
}
/**
* @return Path to the directory with files.
*/
public String getDocumentsDirectory() {
return documentsDirectory;
}
}