From 509b8a86d106cd1311a060cd21fde670a468dc37 Mon Sep 17 00:00:00 2001 From: Emil Ivov Date: Sat, 28 Jan 2006 14:26:00 +0000 Subject: [PATCH] sip-comm's directory should start with a dot so that it is hidden for unix systems --- .../fileaccess/FileAccessServiceImpl.java | 336 +++++++++--------- .../service/fileaccess/FileAccessService.java | 130 +++---- 2 files changed, 233 insertions(+), 233 deletions(-) diff --git a/src/net/java/sip/communicator/impl/fileaccess/FileAccessServiceImpl.java b/src/net/java/sip/communicator/impl/fileaccess/FileAccessServiceImpl.java index 7b1b9508d..e2579ce94 100644 --- a/src/net/java/sip/communicator/impl/fileaccess/FileAccessServiceImpl.java +++ b/src/net/java/sip/communicator/impl/fileaccess/FileAccessServiceImpl.java @@ -15,88 +15,88 @@ /** * Default FileAccessService implementation. - * + * * @author Alexander Pelov */ public class FileAccessServiceImpl implements FileAccessService { - /** - * The logger for this class. - */ - private static Logger logger = Logger - .getLogger(FileAccessServiceImpl.class); - - /** - * The file prefix for all temp files. - */ - public static final String TEMP_FILE_PREFIX = "SIPCOMM"; - - /** - * The file suffix for all temp files. - */ - public static final String TEMP_FILE_SUFFIX = "TEMP"; - - /** - * List of available configuration services. - */ + /** + * The logger for this class. + */ + private static Logger logger = Logger + .getLogger(FileAccessServiceImpl.class); + + /** + * The file prefix for all temp files. + */ + public static final String TEMP_FILE_PREFIX = "SIPCOMM"; + + /** + * The file suffix for all temp files. + */ + public static final String TEMP_FILE_SUFFIX = "TEMP"; + + /** + * List of available configuration services. + */ private ConfigurationService configurationService = null; - + /** * An synchronization object. - * - * A lock should be obtained whenever the configuration service is + * + * A lock should be obtained whenever the configuration service is * accessed. */ private Object syncRoot = new Object(); /** * Set the configuration service. - * + * * @param configurationService */ public void setConfigurationService( - ConfigurationService configurationService) - { - synchronized(this.syncRoot) { - this.configurationService = configurationService; - logger.debug("New configuration service registered."); - } + ConfigurationService configurationService) + { + synchronized(this.syncRoot) { + this.configurationService = configurationService; + logger.debug("New configuration service registered."); + } } - + /** * Remove a configuration service. - * + * * @param configurationService */ public void unsetConfigurationService( - ConfigurationService configurationService) + ConfigurationService configurationService) { - synchronized(this.syncRoot) { - if(this.configurationService == configurationService) { - this.configurationService = null; - logger.debug("Configuration service unregistered."); - } - } + synchronized(this.syncRoot) { + if(this.configurationService == configurationService) { + this.configurationService = null; + logger.debug("Configuration service unregistered."); + } + } } - public File getTemporaryFile() throws IOException { - File retVal = null; + public File getTemporaryFile() throws IOException { + File retVal = null; - try { - logger.logEntry(); + try { + logger.logEntry(); - retVal = TempFileManager.createTempFile(TEMP_FILE_PREFIX, - TEMP_FILE_SUFFIX); - } finally { - logger.logExit(); - } + retVal = TempFileManager.createTempFile(TEMP_FILE_PREFIX, + TEMP_FILE_SUFFIX); + } finally { + logger.logExit(); + } + + return retVal; + } - return retVal; - } - public File getTemporaryDirectory() throws IOException { File file = getTemporaryFile(); - + if(!file.delete()) { throw new IOException("Could not create temporary directory, " + "because: could not delete temporary file."); @@ -104,91 +104,91 @@ public File getTemporaryDirectory() throws IOException { if(!file.mkdirs()) { throw new IOException("Could not create temporary directory"); } - + return file; } - - /** - * @throws IllegalStateException - * Thrown if the configuration service is not set - */ - public File getPrivatePersistentFile(String fileName) throws Exception { - // TODO: Validate: Assert.assertNonNull(fileName, "Parameter fileName should be non-null"); - - File file = null; - - try { - logger.logEntry(); - - + + /** + * @throws IllegalStateException + * Thrown if the configuration service is not set + */ + public File getPrivatePersistentFile(String fileName) throws Exception { + // TODO: Validate: Assert.assertNonNull(fileName, "Parameter fileName should be non-null"); + + File file = null; + + try { + logger.logEntry(); + + String fullPath = getFullPath(fileName); - file = this.accessibleFile(fullPath, fileName); - - if(file == null) { - throw new SecurityException("Insufficient rights to access " + - "this file in current user's home directory: " - + file.getAbsolutePath()); - } - } finally { - logger.logExit(); - } - - return file; - } - - public File getPrivatePersistentDirectory(String dirName) + file = this.accessibleFile(fullPath, fileName); + + if(file == null) { + throw new SecurityException("Insufficient rights to access " + + "this file in current user's home directory: " + + file.getAbsolutePath()); + } + } finally { + logger.logExit(); + } + + return file; + } + + public File getPrivatePersistentDirectory(String dirName) throws Exception { - // TODO: Validate: Assert.assertNonNull(dirName, "Parameter dirName should be non-null"); - + // TODO: Validate: Assert.assertNonNull(dirName, "Parameter dirName should be non-null"); + String fullPath = getFullPath(dirName); File dir = new File(fullPath, dirName); - + if(dir.exists()) { - if(!dir.isDirectory()) { - throw new RuntimeException("Could not create directory " + - "because: A file exists with this name:" + - dir.getAbsolutePath()); + if(!dir.isDirectory()) { + throw new RuntimeException("Could not create directory " + + "because: A file exists with this name:" + + dir.getAbsolutePath()); } } else { if(!dir.mkdirs()) { throw new IOException("Could not create directory"); } } - + return dir; } - + public File getPrivatePersistentDirectory(String[] dirNames) throws Exception { - // TODO: Validate: Assert.assertNonNull(dirNames, "Parameter dirNames should be non-null"); - // TODO: Validate: Assert.assertTrue(dirNames.length > 0, "dirNames.length should be > 0"); - - StringBuffer dirName = new StringBuffer(); - for(int i = 0; i < dirNames.length; i++) { - if(i > 0) { - dirName.append(File.separatorChar); - } - dirName.append(dirNames[i]); - } - - return getPrivatePersistentDirectory(dirName.toString()); + // TODO: Validate: Assert.assertNonNull(dirNames, "Parameter dirNames should be non-null"); + // TODO: Validate: Assert.assertTrue(dirNames.length > 0, "dirNames.length should be > 0"); + + StringBuffer dirName = new StringBuffer(); + for(int i = 0; i < dirNames.length; i++) { + if(i > 0) { + dirName.append(File.separatorChar); + } + dirName.append(dirNames[i]); + } + + return getPrivatePersistentDirectory(dirName.toString()); } private String getFullPath(String fileName) { // TODO: Validate: Assert.assertNonNull(fileName, "The filename should be non-null."); - - String userhome = null; - String sipSubdir = null; - - // Obtain configuration service lock - synchronized(this.syncRoot) { - // TODO: Assert: Assert.assertNonNull(this.configurationService, - // "The configurationService should be non-null."); + + String userhome = null; + String sipSubdir = null; + + // Obtain configuration service lock + synchronized(this.syncRoot) { + // TODO: Assert: Assert.assertNonNull(this.configurationService, + // "The configurationService should be non-null."); userhome = this.configurationService.getString(FileAccessService.CONFPROPERTYKEY_USER_HOME); - sipSubdir = this.configurationService.getString(FileAccessService.CONFPROPERTYKEY_SIP_DIRECTORY); - } - + sipSubdir = this.configurationService.getString(FileAccessService.CONFPROPERTYKEY_SIPCOMM_DIRECTORY); + } + if(userhome == null) { userhome = System.getProperty(FileAccessService.SYSPROPERTYKEY_USER_HOME); @@ -197,70 +197,70 @@ private String getFullPath(String fileName) { } } if(sipSubdir == null) { - sipSubdir = FileAccessService.DEFAULT_SIP_DIRECTORY; + sipSubdir = FileAccessService.DEFAULT_SIPCOMM_DIRECTORY; } - + if(!userhome.endsWith(File.separator)) { userhome += File.separator; } if(!sipSubdir.endsWith(File.separator)) { - sipSubdir += File.separator; + sipSubdir += File.separator; } - + return userhome + sipSubdir; } - /** - * Checks if a file exists and if it is writable or readable. If not - checks + /** + * Checks if a file exists and if it is writable or readable. If not - checks * if the user has a write privileges to the containing directory. - * - * If those conditions are met it returns a File in the directory with a - * fileName. If not - returns null. - * - * @param homedir - * @param fileName - * @return Returns null if the file does not exist and cannot be created. - * Otherwise - an object to this file - * @throws IOException Thrown if the home directory cannot be created - */ - private File accessibleFile(String homedir, String fileName) throws IOException { - File file = null; - - try { - logger.logEntry(); - - homedir = homedir.trim(); - if(!homedir.endsWith(File.separator)) { - homedir += File.separator; - } - - file = new File(homedir + fileName); - if (file.canRead() || file.canWrite()) { - return file; - } - - File homedirFile = new File(homedir); - - if(!homedirFile.exists()) { - logger.debug("Creating home directory : " + homedirFile.getAbsolutePath()); - if(!homedirFile.mkdirs()) { - String message = "Could not create the home directory : " - + homedirFile.getAbsolutePath(); - - logger.debug(message); - throw new IOException(message); - } - logger.debug("Home directory created : " + homedirFile.getAbsolutePath()); - } else if (!homedirFile.canWrite()) { - file = null; - } - - } finally { - logger.logExit(); - } - - return file; - } + * + * If those conditions are met it returns a File in the directory with a + * fileName. If not - returns null. + * + * @param homedir + * @param fileName + * @return Returns null if the file does not exist and cannot be created. + * Otherwise - an object to this file + * @throws IOException Thrown if the home directory cannot be created + */ + private File accessibleFile(String homedir, String fileName) throws IOException { + File file = null; + + try { + logger.logEntry(); + + homedir = homedir.trim(); + if(!homedir.endsWith(File.separator)) { + homedir += File.separator; + } + + file = new File(homedir + fileName); + if (file.canRead() || file.canWrite()) { + return file; + } + + File homedirFile = new File(homedir); + + if(!homedirFile.exists()) { + logger.debug("Creating home directory : " + homedirFile.getAbsolutePath()); + if(!homedirFile.mkdirs()) { + String message = "Could not create the home directory : " + + homedirFile.getAbsolutePath(); + + logger.debug(message); + throw new IOException(message); + } + logger.debug("Home directory created : " + homedirFile.getAbsolutePath()); + } else if (!homedirFile.canWrite()) { + file = null; + } + + } finally { + logger.logExit(); + } + + return file; + } } diff --git a/src/net/java/sip/communicator/service/fileaccess/FileAccessService.java b/src/net/java/sip/communicator/service/fileaccess/FileAccessService.java index e4c99d40e..4916bddc2 100644 --- a/src/net/java/sip/communicator/service/fileaccess/FileAccessService.java +++ b/src/net/java/sip/communicator/service/fileaccess/FileAccessService.java @@ -12,95 +12,95 @@ /** * A service used to provide the basic functionality required to access the * undelying file system. - * + * * Note: Never store unencrypted sensitive information, such as passwords, * personal data, credit card numbers, etc.. - * + * * @author Alexander Pelov */ public interface FileAccessService { - /** - * The key of the system property containing the user home dir - */ - public static final String SYSPROPERTYKEY_USER_HOME = "user.home"; + /** + * The key of the system property containing the user home dir + */ + public static final String SYSPROPERTYKEY_USER_HOME = "user.home"; - /** - * The key of the configuration property containing the user home dir - if - * it is not defined the system property is used - */ - public static final String CONFPROPERTYKEY_USER_HOME = - "net.java.sip.communicator.user.home"; + /** + * The key of the configuration property containing the user home dir - if + * it is not defined the system property is used + */ + public static final String CONFPROPERTYKEY_USER_HOME = + "net.java.sip.communicator.user.home"; - /** - * The subdirectory of USER_HOME in which all user files will be stored - */ - public static final String CONFPROPERTYKEY_SIP_DIRECTORY = - "net.java.sip.communicator.user.home.sip-communicator-home"; + /** + * The subdirectory of USER_HOME in which all user files will be stored + */ + public static final String CONFPROPERTYKEY_SIPCOMM_DIRECTORY = + "net.java.sip.communicator.user.home.sip-communicator-home"; - /** - * The default subdirectory - */ - public static final String DEFAULT_SIP_DIRECTORY = "sip-communicator"; + /** + * The default subdirectory + */ + public static final String DEFAULT_SIPCOMM_DIRECTORY = ".sip-communicator"; - /** - * This method returns a created temporary file. After you close this file - * it is not guaranteed that you will be able to open it again nor that it - * will contain any information. - * - * Note: DO NOT store unencrypted sensitive information in this file - * - * @return The created temporary file - * @throws IOException - * If the file cannot be created - */ - File getTemporaryFile() throws IOException; - /** - * This method returns a created temporary directory. Any file you create - * in it will be a temporary file. - * + * This method returns a created temporary file. After you close this file + * it is not guaranteed that you will be able to open it again nor that it + * will contain any information. + * + * Note: DO NOT store unencrypted sensitive information in this file + * + * @return The created temporary file + * @throws IOException + * If the file cannot be created + */ + File getTemporaryFile() throws IOException; + + /** + * This method returns a created temporary directory. Any file you create + * in it will be a temporary file. + * * Note: If there is no opened file in this directory it may be deleted * at any time. * Note: DO NOT store unencrypted sensitive information in this directory - * + * * @return The created directory * @throws IOException * If the directory cannot be created */ File getTemporaryDirectory() throws IOException; - /** - * This method returns a file specific to the current user. It may not - * exist, but it is guaranteed that you will have the sufficient rights to - * create it. - * - * This file should not be considered secure because the implementor may - * return a file accesible to everyone. Generaly it will reside in - * current user's homedir, but it may as well reside in a shared directory. - * - * Note: DO NOT store unencrypted sensitive information in this file - * - * @param fileName - * The name of the private file you wish to access - * @return The file - * @throws Exception - * Thrown if there is no suitable location for the persistent - * file - */ - File getPrivatePersistentFile(String fileName) throws Exception; - + /** + * This method returns a file specific to the current user. It may not + * exist, but it is guaranteed that you will have the sufficient rights to + * create it. + * + * This file should not be considered secure because the implementor may + * return a file accesible to everyone. Generaly it will reside in + * current user's homedir, but it may as well reside in a shared directory. + * + * Note: DO NOT store unencrypted sensitive information in this file + * + * @param fileName + * The name of the private file you wish to access + * @return The file + * @throws Exception + * Thrown if there is no suitable location for the persistent + * file + */ + File getPrivatePersistentFile(String fileName) throws Exception; + /** * This method creates a directory specific to the current user. - * + * * This directory should not be considered secure because the implementor may * return a directory accesible to everyone. Generaly it will reside in * current user's homedir, but it may as well reside in a shared directory. - * + * * It is guaranteed that you will be able to create files in it. - * + * * Note: DO NOT store unencrypted sensitive information in this file - * + * * @param dirName * The name of the private directory you wish to access. * @return The created directory. @@ -109,12 +109,12 @@ public interface FileAccessService { * directory. */ File getPrivatePersistentDirectory(String dirName) throws Exception; - + /** * This method creates a directory specific to the current user. - * + * * {@link #getPrivatePersistentDirectory(String)} - * + * * @param dirNames * The name of the private directory you wish to access. * @return The created directory.