public InputStream checkout(boolean fail) throws IOException, IllegalArgumentException { m_mostRecentVersion = highestRemoteVersion(); if (m_mostRecentVersion == 0) { // If there is no remote version, then simply return an empty stream. if (fail) { throw new IOException("No version has yet been checked in to the repository."); } else { return new ByteArrayInputStream(new byte[0]); } } return checkout(m_mostRecentVersion); }
public boolean commit() throws IOException { if (m_mostRecentVersion < 0) { throw new IllegalStateException("A commit should be preceded by a checkout."); } return commit(m_mostRecentVersion++); }
/** * Helper method for login. * @throws IOException */ private CachedRepository getCachedRepositoryFromPreferences(User user, Repository repository, Preferences repositoryPrefs) throws IOException { long mostRecentVersion = repositoryPrefs.getLong("version", CachedRepositoryImpl.UNCOMMITTED_VERSION); return new CachedRepositoryImpl(user, repository, getBackupFromPreferences(repositoryPrefs), mostRecentVersion); }
public boolean isCurrent() throws IOException { return highestRemoteVersion() == m_mostRecentVersion; }
private long highestRemoteVersion() throws IOException { long result = 0; RangeIterator ri = getRange().iterator(); while (ri.hasNext()) { result = ri.next(); } return result; } }
public void updated(Dictionary dict) throws ConfigurationException { if (dict != null) { String locationString = (String) dict.get(KEY_REPOSITORY_LOCATION); if (locationString == null) { throw new ConfigurationException(KEY_REPOSITORY_LOCATION, "Property missing."); } URL location; try { location = new URL(locationString); } catch (MalformedURLException e) { throw new ConfigurationException(KEY_REPOSITORY_LOCATION, "Location " + locationString + " is not a valid URL."); } String customer = (String) dict.get(KEY_REPOSITORY_CUSTOMER); if (customer == null) { throw new ConfigurationException(KEY_REPOSITORY_CUSTOMER, "Property missing."); } String name = (String) dict.get(KEY_REPOSITORY_NAME); if (name == null) { throw new ConfigurationException(KEY_REPOSITORY_NAME, "Property missing."); } String fileRoot = FILE_ROOT + File.separator + location.getAuthority().replace(':', '-') + location.getPath().replace('/', '\\') + File.separator + customer + File.separator + name + File.separator; File local = getFile(fileRoot + "local"); File backup = getFile(fileRoot + "backup"); m_properties = getFile(fileRoot + "properties"); m_repo = new CachedRepositoryImpl(null, location, customer, name, local, backup, loadVersion(m_properties)); } }
@SuppressWarnings("unchecked") public void updated(Dictionary settings) throws ConfigurationException { if (settings != null) { String url = getNotNull(settings, URL, "DeploymentRepository URL not configured."); String name = getNotNull(settings, NAME, "RepositoryName not configured."); String customer = getNotNull(settings, CUSTOMER, "RepositoryCustomer not configured."); //create the remote repository and set it. try { BackupRepository backup = null; try { backup = new FilebasedBackupRepository(File.createTempFile("currentrepository", null), File.createTempFile("backuprepository", null)); } catch (Exception e) { m_log.log(LogService.LOG_WARNING, "Unable to create temporary files for FilebasedBackupRepository"); } // We always create the remote repository. If we can create a backup repository, we will wrap a CachedRepository // around it. m_directRepository = new RemoteRepository(new URL(url), customer, name); if (backup == null) { m_cachedRepository = null; } else { m_cachedRepository = new CachedRepositoryImpl(null, m_directRepository, backup, CachedRepositoryImpl.UNCOMMITTED_VERSION); } } catch (MalformedURLException mue) { throw new ConfigurationException(URL, mue.getMessage()); } } }
public boolean commit(InputStream data, long fromVersion) throws IOException, IllegalArgumentException { m_local.write(data); return commit(fromVersion); }
/** * There are two types of commit, one that takes an input stream, and one that simply flushes whatever is in the * current to the remote repository. After each commit, we should be able to renew the cached repository, and * checkout the data we put in before. */ @Test() public void testCommit() throws IllegalArgumentException, IOException { Repository m_repository = new MockRepository(); BackupRepository m_backupRepository = new MockBackupRepository(); CachedRepository m_cachedRepository = new CachedRepositoryImpl(m_repository, m_backupRepository, 0); byte[] testContent = new byte[] { 'i', 'n', 'i', 't', 'i', 'a', 'l' }; InputStream input = new ByteArrayInputStream(testContent); m_cachedRepository.commit(input, 0); m_cachedRepository = new CachedRepositoryImpl(m_repository, m_backupRepository, 0); input = m_cachedRepository.checkout(1); byte[] inputBytes = AdminTestUtil.copy(input); assert AdminTestUtil.byteArraysEqual(inputBytes, testContent) : "We got something different than 'initial' from checkout: " + new String(inputBytes); byte[] newTestContent = new byte[] { 'n', 'e', 'w' }; m_cachedRepository.writeLocal(new ByteArrayInputStream(newTestContent)); m_cachedRepository.commit(); m_cachedRepository = new CachedRepositoryImpl(m_repository, m_backupRepository, 0); input = m_cachedRepository.checkout(2); inputBytes = AdminTestUtil.copy(input); assert AdminTestUtil.byteArraysEqual(inputBytes, newTestContent) : "We got something different than 'new' from checkout: " + new String(inputBytes); }
BackupRepository m_backupRepository = new MockBackupRepository(); CachedRepository m_cachedRepository = new CachedRepositoryImpl(m_repository, m_backupRepository, 0); byte[] testContent = new byte[] { 'i', 'n', 'i', 't', 'i', 'a', 'l' }; m_cachedRepository.commit(input, 0); m_cachedRepository = new CachedRepositoryImpl(m_repository, m_backupRepository, 0); input = m_cachedRepository.checkout(1);
/** * Initial checkout: the remote repository contains some data for a given version, we make the cached repository do * a checkout, and check whether all data arrives at the right places: in getLocal, and in the backup repository. */ @Test() public void testInitialCheckout() throws IllegalArgumentException, IOException { Repository m_repository = new MockRepository(); byte[] testContent = new byte[] { 'i', 'n', 'i', 't', 'i', 'a', 'l' }; m_repository.commit(new ByteArrayInputStream(testContent), 0); BackupRepository m_backupRepository = new MockBackupRepository(); CachedRepository m_cachedRepository = new CachedRepositoryImpl(m_repository, m_backupRepository, 0); InputStream input = m_cachedRepository.checkout(1); byte[] inputBytes = AdminTestUtil.copy(input); assert AdminTestUtil.byteArraysEqual(inputBytes, testContent) : "We got something different than 'initial' from checkout: " + new String(inputBytes); input = m_cachedRepository.getLocal(false); inputBytes = AdminTestUtil.copy(input); assert AdminTestUtil.byteArraysEqual(inputBytes, testContent) : "We got something different than 'initial' from getLocal: " + new String(inputBytes); input = m_backupRepository.read(); inputBytes = AdminTestUtil.copy(input); assert AdminTestUtil.byteArraysEqual(inputBytes, testContent) : "We got something different than 'initial' from the backup repository: " + new String(inputBytes); }