public void copy()

in repository/service/src/main/java/org/apache/karaf/cave/repository/service/RepositoryServiceImpl.java [295:347]


    public void copy(String sourceRepositoryName, String destinationRepositoryName) throws Exception {
        if (repositories.get(sourceRepositoryName) == null) {
            throw new IllegalArgumentException("Repository " + sourceRepositoryName + " doesn't exist");
        }
        if (repositories.get(destinationRepositoryName) == null) {
            throw new IllegalArgumentException("Repository " + destinationRepositoryName + " doesn't exist");
        }
        Repository sourceRepository = repositories.get(sourceRepositoryName);
        Repository destinationRepository = repositories.get(destinationRepositoryName);
        if (sourceRepository.getLocation() == null || sourceRepository.getLocation().isEmpty()) {
            throw new IllegalStateException("Source repository " + sourceRepositoryName + " location is not defined");
        }
        if (destinationRepository.getLocation() == null || destinationRepository.getLocation().isEmpty()) {
            throw new IllegalStateException("Destination repository " + destinationRepositoryName + " location is not defined");
        }
        final Path source = Paths.get(sourceRepository.getLocation());
        final Path target = Paths.get(destinationRepository.getLocation());
        Files.walkFileTree(source, new FileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                Path newDir = target.resolve(source.relativize(dir));
                try {
                    Files.copy(dir, newDir, StandardCopyOption.COPY_ATTRIBUTES);
                } catch (FileAlreadyExistsException faee) {
                    // ignore
                } catch (IOException ioe) {
                    return FileVisitResult.SKIP_SUBTREE;
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Files.copy(file, target.resolve(source.relativize(file)), StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                if (exc == null) {
                    Path newDir = target.resolve(source.relativize(dir));
                    FileTime time = Files.getLastModifiedTime(dir);
                    Files.setLastModifiedTime(newDir, time);
                }
                return FileVisitResult.CONTINUE;
            }
        });
    }