001package co.codewizards.cloudstore.local; 002 003import static co.codewizards.cloudstore.core.util.Util.*; 004 005import java.io.File; 006import java.io.IOException; 007import java.util.HashMap; 008import java.util.Map; 009import java.util.Properties; 010 011import co.codewizards.cloudstore.core.repo.local.LocalRepoManager; 012import co.codewizards.cloudstore.core.repo.local.RepositoryCorruptException; 013import co.codewizards.cloudstore.core.util.PropertiesUtil; 014 015public class PersistencePropertiesProvider { 016 017 private final File localRoot; 018 019 public PersistencePropertiesProvider(File localRoot) { 020 this.localRoot = assertNotNull("localRoot", localRoot); 021 if (!localRoot.isDirectory()) 022 throw new IllegalArgumentException("The given localRoot is not an existing directory: " + localRoot.getAbsolutePath()); 023 } 024 private File getMetaDir() { 025 return new File(localRoot, LocalRepoManager.META_DIR_NAME); 026 } 027 028 public Map<String, String> getPersistenceProperties(boolean createRepository) { 029 final File metaDirectory = getMetaDir(); 030 if (!metaDirectory.isDirectory()) 031 throw new IllegalStateException("The localRoot does not contain the meta-directory: " + metaDirectory.getAbsolutePath()); 032 033 final File persistencePropertiesFile = new File(metaDirectory, LocalRepoManager.PERSISTENCE_PROPERTIES_FILE_NAME); 034 if (!persistencePropertiesFile.isFile()) 035 throw new IllegalStateException("The persistencePropertiesFile does not exist or is not a file: " + persistencePropertiesFile.getAbsolutePath()); 036 037 Map<String, String> variablesMap = new HashMap<String, String>(); 038 variablesMap.put(LocalRepoManager.VAR_LOCAL_ROOT, localRoot.getPath()); 039 variablesMap.put(LocalRepoManager.VAR_META_DIR, getMetaDir().getPath()); 040 041 Properties rawProperties; 042 try { 043 rawProperties = PropertiesUtil.load(persistencePropertiesFile); 044 } catch (IOException e) { 045 throw new RuntimeException(e); 046 } 047 Map<String, String> persistenceProperties = PropertiesUtil.filterProperties(rawProperties, variablesMap); 048 String connectionURL = persistenceProperties.get(PersistencePropertiesEnum.CONNECTION_URL.key); 049 persistenceProperties.put(PersistencePropertiesEnum.CONNECTION_URL_ORIGINAL.key, connectionURL); 050 051 if (createRepository) { 052 modifyConnectionURLForCreate(persistenceProperties); 053 } 054 return persistenceProperties; 055 } 056 057 private void modifyConnectionURLForCreate(Map<String, String> persistenceProperties) { 058 String value = persistenceProperties.get(PersistencePropertiesEnum.CONNECTION_URL.key); 059 if (value == null || value.trim().isEmpty()) { 060 throw new RepositoryCorruptException(localRoot, 061 String.format("Property '%s' missing in '%s'.", PersistencePropertiesEnum.CONNECTION_URL.key, LocalRepoManager.PERSISTENCE_PROPERTIES_FILE_NAME)); 062 } 063 064 String newValue = value.trim() + ";create=true"; 065 persistenceProperties.put(PersistencePropertiesEnum.CONNECTION_URL.key, newValue); 066 } 067}