public boolean backup() throws IOException { if (!m_current.exists()) { return false; } copy(m_current, m_backup); return true; }
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);
/** * 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); }
/** * 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); }
@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 restore() throws IOException { if (!m_backup.exists()) { return false; } copy(m_backup, m_current); return true; }
/** * 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()); } }