public boolean isCurrent() throws IOException { return highestRemoteVersion() == m_mostRecentVersion; }
/** * Creates a cached repository which uses <code>remote</code>, <code>customer</code> and * <code>name</code> to create a <code>RemoteRepository</code>, and uses the <code>Files</code>s * passed in as a for local storage and backup. * @param user A user object, which is allowed to access <code>remote</code>. * @param remote The location of the remote repository. * @param customer The customer name to be used with the remote repository. * @param name The name to be used with the remote repository. * @param local A local file to be used for storage of changes to the repository. * @param backup A local file to be used as a local backup of what was on the server. * @param mostRecentVersion The version from which <code>backup</code> was checked out or committed. * If no version has been committed yet, use <code>UNCOMMITTED_VERSION</code>. */ public CachedRepositoryImpl(User user, URL remote, String customer, String name, File local, File backup, long mostRecentVersion) { this(user, new RemoteRepository(remote, customer, name), new FilebasedBackupRepository(local, backup), mostRecentVersion); }
public boolean restore() throws IOException { if (!m_backup.exists()) { return false; } copy(m_backup, m_current); return true; }
@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()); } } }
backup.deleteOnExit(); FilebasedBackupRepository rep = new FilebasedBackupRepository(current, backup); rep.write(new ByteArrayInputStream(testContent)); InputStream input = rep.read(); byte[] inputBytes = AdminTestUtil.copy(input); assert AdminTestUtil.byteArraysEqual(inputBytes, testContent) : "We got something different than 'initial' from read: " + new String(inputBytes); rep.backup(); rep.write(new ByteArrayInputStream(newTestContent)); input = rep.read(); inputBytes = AdminTestUtil.copy(input); assert AdminTestUtil.byteArraysEqual(inputBytes, newTestContent) : "We got something different than 'new' from read: " + new String(inputBytes); rep.restore(); input = rep.read(); inputBytes = AdminTestUtil.copy(input); assert AdminTestUtil.byteArraysEqual(inputBytes, testContent) : "We got something different than 'initial' from read: " + new String(inputBytes);
/** * 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); }
/** * Helper method for login. * @throws IOException */ private BackupRepository getBackupFromPreferences(Preferences repositoryPrefs) throws IOException { File current = getFileFromPreferences(repositoryPrefs, PREFS_LOCAL_FILE_CURRENT); File backup = getFileFromPreferences(repositoryPrefs, PREFS_LOCAL_FILE_BACKUP); return new FilebasedBackupRepository(current, backup); }
/** * Helper method for login, which loads a set of repositories. * @param user A <code>User</code> object * @param rsd A RepositorySetDescriptor, defining the set to be created. * @param repos An array of <code>ObjectRepositoryImpl</code> which this set should manage. Each * @return The newly created repository set. * @throws IOException */ @SuppressWarnings("unchecked") public RepositorySet loadRepositorySet(User user, RepositorySetDescriptor rsd, ObjectRepositoryImpl[] repos) throws IOException { Repository repo = new RemoteRepository(rsd.m_location, rsd.m_customer, rsd.m_name); Preferences prefs = m_preferences.getUserPreferences(user.getName()); Preferences repoPrefs = getRepositoryPrefs(prefs, rsd.m_location, rsd.m_customer, rsd.m_name); return new RepositorySet(m_changeNotifier, m_log, user, repoPrefs, repos, getCachedRepositoryFromPreferences(user, repo, repoPrefs), rsd.m_name, rsd.m_writeAccess); }
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(InputStream data, long fromVersion) throws IOException, IllegalArgumentException { URL url = buildCommand(m_url, COMMAND_COMMIT, fromVersion); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setRequestProperty("Content-Type", MIME_APPLICATION_OCTET_STREAM); OutputStream out = connection.getOutputStream(); copy(data, out); out.flush(); out.close(); return connection.getResponseCode() == HttpServletResponse.SC_OK; }
public boolean commit() throws IOException { if (m_mostRecentVersion < 0) { throw new IllegalStateException("A commit should be preceded by a checkout."); } return commit(m_mostRecentVersion++); }
public InputStream checkout(long version) throws IOException, IllegalArgumentException { if (version <= 0) { throw new IllegalArgumentException("Version must be greater than 0."); } URL url = buildCommand(m_url, COMMAND_CHECKOUT, version); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); if (connection.getResponseCode() == HttpServletResponse.SC_NOT_FOUND) { throw new IllegalArgumentException("Requested version not found in remote repository. (" + connection.getResponseMessage() + ")"); } if (connection.getResponseCode() != HttpServletResponse.SC_OK) { throw new IOException("Connection error: " + connection.getResponseMessage()); } return connection.getInputStream(); }
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)); } }
public boolean backup() throws IOException { if (!m_current.exists()) { return false; } copy(m_current, m_backup); return true; }
public boolean commit(InputStream data, long fromVersion) throws IOException, IllegalArgumentException { m_local.write(data); return commit(fromVersion); }
public SortedRangeSet getRange() throws IOException { URL url = buildCommand(m_url, COMMAND_QUERY, 0); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); if (connection.getResponseCode() == HttpServletResponse.SC_OK) { BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line = reader.readLine(); if (line == null) { throw new IOException("Repository not found: customer=" + m_customer + ", name=" + m_name); } String representation = line.substring(line.lastIndexOf(',')); reader.close(); return new SortedRangeSet(representation); } throw new IOException("Connection error: " + connection.getResponseMessage()); }
/** * 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); }
/** * Helper function that writes the contents of one file to another. * @param source The source file. * @param destination The destination file. * @throws java.io.IOException Thrown when file IO goes wrong. */ private static void copy(File source, File destination) throws IOException { if (destination.exists()) { destination.delete(); } destination.createNewFile(); FileOutputStream out = new FileOutputStream(destination); FileInputStream in = new FileInputStream(source); copy(in, out); in.close(); out.close(); }
public void write(InputStream data) throws IOException { try { if (!m_current.exists()) { m_current.createNewFile(); } } catch (IOException e) { throw new IOException("Unable to create file:" + e.getMessage()); } try { FileOutputStream out = new FileOutputStream(m_current); copy(data, out); out.close(); } catch (FileNotFoundException e) { throw new IOException("Unable to open file:" + e.getMessage()); } }