Initial commit from SVN.
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
package com.eua.controller;
|
||||
|
||||
import com.foundation.application.*;
|
||||
import com.eua.application.*;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public abstract class AbstractController extends com.foundation.controller.Controller {
|
||||
/**
|
||||
* AbstractController constructor.
|
||||
*/
|
||||
public AbstractController() {
|
||||
super();
|
||||
}//AbstractController()//
|
||||
/**
|
||||
* Gets the application object that this object is running under. The application object provides the object various services that it will need.
|
||||
* @return The application reference for the application this object is running under.
|
||||
*/
|
||||
public IApplication getApplication() {
|
||||
return EncryptionUtilityApplication.getSingleton();
|
||||
}//getApplication()//
|
||||
}//AbstractController//
|
||||
@@ -0,0 +1,215 @@
|
||||
package com.eua.controller;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.channels.FileLock;
|
||||
import java.util.Properties;
|
||||
|
||||
import com.common.debug.Debug;
|
||||
import com.foundation.metadata.Attribute;
|
||||
|
||||
/**
|
||||
* Copyright Declarative Engineering LLC 2009<p>
|
||||
*/
|
||||
public class SettingsController extends AbstractController {
|
||||
private static final File settingsFilePath = new File(new File(System.getProperty("user.home"), "Brainstorm Archiver"), "settings.properties");
|
||||
private static final File settingsLockFilePath = new File(new File(System.getProperty("user.home"), "Brainstorm Archiver"), "settings.lock");
|
||||
private static final String ENCRYPTION_OUTPUT_DIRECTORY = "encryption.output.directory";
|
||||
private static final String ENCRYPTED_FILE_DIRECTORY = "encrypted.file.directory";
|
||||
private static final String ENCRYPTED_FILE_NAME = "encrypted.file.name";
|
||||
private static final String DECRYPTION_OUTPUT_DIRECTORY = "decryption.output.directory";
|
||||
private static final String INPUT_DIRECTORY = "input.directory";
|
||||
|
||||
public static final Attribute PROPERTIES = registerAttribute(SettingsController.class, "properties", AO_REFERENCED);
|
||||
public static final Attribute PROPERTIES_DATE = registerAttribute(SettingsController.class, "propertiesDate");
|
||||
|
||||
/** The one and only instance of this class. */
|
||||
private static final SettingsController singleton = new SettingsController();
|
||||
/**
|
||||
* Gets the only instance of this class.
|
||||
* @return The one and only instance.
|
||||
*/
|
||||
public static SettingsController getSingleton() {
|
||||
return singleton;
|
||||
}//getSingleton()//
|
||||
/**
|
||||
* SettingsController constructor.
|
||||
*/
|
||||
private SettingsController() {
|
||||
updateProperties();
|
||||
}//SettingsController()//
|
||||
/**
|
||||
* Reads the properties file.
|
||||
* @return The properties read from disk.
|
||||
*/
|
||||
private void updateProperties() {
|
||||
Properties properties = new Properties();
|
||||
Long lastModified = null;
|
||||
|
||||
if(settingsFilePath.exists() && settingsFilePath.canRead()) {
|
||||
FileInputStream fin = null;
|
||||
|
||||
lastModified = new Long(settingsFilePath.lastModified());
|
||||
|
||||
try {
|
||||
fin = new FileInputStream(settingsFilePath);
|
||||
properties.load(fin);
|
||||
fin.close();
|
||||
fin = null;
|
||||
}//try//
|
||||
catch(Throwable e) {
|
||||
Debug.log(e);
|
||||
}//catch//
|
||||
finally {
|
||||
if(fin != null) try {fin.close();} catch(Throwable e) {}
|
||||
}//finally//
|
||||
}//if//
|
||||
else {
|
||||
try {
|
||||
settingsFilePath.getParentFile().mkdirs();
|
||||
settingsFilePath.createNewFile();
|
||||
lastModified = new Long(settingsFilePath.lastModified());
|
||||
}//try//
|
||||
catch(Throwable e) {
|
||||
Debug.log(e);
|
||||
}//catch//
|
||||
}//else//
|
||||
|
||||
setProperties(properties);
|
||||
setPropertiesDate(lastModified);
|
||||
}//updateProperties()//
|
||||
/**
|
||||
* Modifies the settings properties using a file lock.
|
||||
* <p>Locks on the settings properties file, then updates if necessary the in memory properties, then runs the operation passed to this method, then stores the properties back to file.</p>
|
||||
* @param operation The operation that will update the properties object.
|
||||
*/
|
||||
private void modifyProperties(Runnable operation) {
|
||||
//The properties date will only be null if we cannot store properties due to permission problems.//
|
||||
if(getPropertiesDate() != null) {
|
||||
FileLock lock = null;
|
||||
FileChannel channel = null;
|
||||
FileOutputStream fout = null;
|
||||
|
||||
try {
|
||||
long lastModifiedDate = settingsFilePath.lastModified();
|
||||
|
||||
channel = new RandomAccessFile(settingsLockFilePath, "rw").getChannel();
|
||||
lock = channel.lock();
|
||||
|
||||
//Update the in memory settings first.//
|
||||
if(getPropertiesDate().longValue() != lastModifiedDate) {
|
||||
updateProperties();
|
||||
}//if//
|
||||
|
||||
//Modify the properties.//
|
||||
operation.run();
|
||||
//Write the properties to a byte array.//
|
||||
fout = new FileOutputStream(settingsFilePath, false);
|
||||
getProperties().store(fout, "Brainstorm Archiver Settings");
|
||||
}//try//
|
||||
catch(Throwable e) {
|
||||
Debug.log(e);
|
||||
}//catch//
|
||||
finally {
|
||||
if(fout != null) try {fout.close();} catch(Throwable e) {}
|
||||
if(lock != null) try {lock.release();} catch(Throwable e) {}
|
||||
if(channel != null) try {channel.close();} catch(Throwable e) {}
|
||||
}//if//
|
||||
|
||||
setPropertiesDate(new Long(settingsFilePath.lastModified()));
|
||||
}//if//
|
||||
}//modifyProperties()//
|
||||
/**
|
||||
* Gets the default output path for encrypted files.
|
||||
* @return The last used path for placing encrypted files.
|
||||
*/
|
||||
public String getEncryptionOutputDirectory() {
|
||||
return getProperties().getProperty(ENCRYPTION_OUTPUT_DIRECTORY);
|
||||
}//getEncryptionOutputDirectory()//
|
||||
/**
|
||||
* Gets the default file name for the encrypted files.
|
||||
* @return The last used file name for encrypting files.
|
||||
*/
|
||||
public String getEncryptedFileName() {
|
||||
return getProperties().getProperty(ENCRYPTED_FILE_NAME);
|
||||
}//getEncryptedFileName()//
|
||||
/**
|
||||
* Gets the last used directory for selecting input files for encrypting.
|
||||
* @return The last used directory for the selection of files to encrypt.
|
||||
*/
|
||||
public String getInputDirectory() {
|
||||
return getProperties().getProperty(INPUT_DIRECTORY);
|
||||
}//getInputDirectory()//
|
||||
/**
|
||||
* Sets settings for encrypting.
|
||||
* @param encryptionOutputDirectory The last used path for placing encrypted files.
|
||||
* @param encryptedFileName The last used file name for encrypting files.
|
||||
*/
|
||||
public void setEncryptionSettings(final String encryptionOutputDirectory, final String encryptedFileName, final String inputDirectory) {
|
||||
modifyProperties(new Runnable() {
|
||||
public void run() {
|
||||
getProperties().setProperty(ENCRYPTION_OUTPUT_DIRECTORY, encryptionOutputDirectory);
|
||||
getProperties().setProperty(ENCRYPTED_FILE_NAME, encryptedFileName);
|
||||
getProperties().setProperty(INPUT_DIRECTORY, inputDirectory);
|
||||
}//run()//
|
||||
});
|
||||
}//setEncryptionSettings()//
|
||||
/**
|
||||
* Gets the default directory containing encrypted files to be decrypted.
|
||||
* @return The directory of the last decrypted file.
|
||||
*/
|
||||
public String getEncryptedFileDirectory() {
|
||||
return getProperties().getProperty(ENCRYPTED_FILE_DIRECTORY);
|
||||
}//getEncryptedFileDirectory()//
|
||||
/**
|
||||
* Gets the default output path for decrypted files.
|
||||
* @return The last used path for placing decrypted files.
|
||||
*/
|
||||
public String getDecryptionOutputDirectory() {
|
||||
return getProperties().getProperty(DECRYPTION_OUTPUT_DIRECTORY);
|
||||
}//getDecryptionOutputDirectory()//
|
||||
/**
|
||||
* Sets the settings for decrypting.
|
||||
* @param decryptionOutputDirectory The directory of the last decrypted file.
|
||||
* @param encryptedFileDirectory The directory of the last decrypted file.
|
||||
*/
|
||||
public void setDecryptionSettings(final String decryptionOutputDirectory, final String encryptedFileDirectory) {
|
||||
modifyProperties(new Runnable() {
|
||||
public void run() {
|
||||
getProperties().setProperty(ENCRYPTED_FILE_DIRECTORY, encryptedFileDirectory);
|
||||
getProperties().setProperty(DECRYPTION_OUTPUT_DIRECTORY, decryptionOutputDirectory);
|
||||
}//run()//
|
||||
});
|
||||
}//setDecryptionSettings()//
|
||||
/**
|
||||
* Gets the properties value.
|
||||
* @return The properties value.
|
||||
*/
|
||||
private Properties getProperties() {
|
||||
return (Properties) getAttributeValue(PROPERTIES);
|
||||
}//getProperties()//
|
||||
/**
|
||||
* Sets the properties value.
|
||||
* @param properties The properties value.
|
||||
*/
|
||||
private void setProperties(Properties properties) {
|
||||
setAttributeValue(PROPERTIES, properties);
|
||||
}//setProperties()//
|
||||
/**
|
||||
* Gets the propertiesDate value.
|
||||
* @return The propertiesDate value.
|
||||
*/
|
||||
private Long getPropertiesDate() {
|
||||
return (Long) getAttributeValue(PROPERTIES_DATE);
|
||||
}//getPropertiesDate()//
|
||||
/**
|
||||
* Sets the propertiesDate value.
|
||||
* @param propertiesDate The propertiesDate value.
|
||||
*/
|
||||
private void setPropertiesDate(Long propertiesDate) {
|
||||
setAttributeValue(PROPERTIES_DATE, propertiesDate);
|
||||
}//setPropertiesDate()//
|
||||
}//SettingsController//
|
||||
Reference in New Issue
Block a user