Search in sources :

Example 26 with LocalRepository

use of org.eclipse.aether.repository.LocalRepository in project fabric8 by jboss-fuse.

the class ProfileWatcherImpl method run.

public void run() {
    assertValid();
    LOG.debug("Profile watcher thread started");
    int oldCounter = -1;
    SortedSet<String> oldActiveProfiles = null;
    Map<File, Long> localChecksums = new HashMap<File, Long>();
    Map<File, Long> localModified = new HashMap<File, Long>();
    Set<Profile> refreshProfiles = new HashSet<Profile>();
    ProfileService profileService = fabricService.get().adapt(ProfileService.class);
    while (running.get() && watchURLs.size() > 0) {
        SortedSet<String> currentActiveProfiles = getCurrentActiveProfileVersions();
        if (profileArtifacts == null || oldCounter != counter.get() || oldActiveProfiles == null || !oldActiveProfiles.equals(currentActiveProfiles)) {
            oldCounter = counter.get();
            oldActiveProfiles = currentActiveProfiles;
            try {
                LOG.debug("Reloading the currently active profile artifacts");
                profileArtifacts = findProfileArifacts();
            } catch (Exception e) {
                LOG.error("Failed to get profiles artifacts: " + e, e);
            }
        }
        // lets refresh profiles on the next loop; so we've time to finish uploading/modifying files
        for (Profile profile : refreshProfiles) {
            LOG.info("Refreshing profile: " + profile);
            Profiles.refreshProfile(fabricService.get(), profile);
        }
        refreshProfiles.clear();
        if (profileArtifacts != null) {
            List<File> localRepositories = new LinkedList<>();
            if (mavenResolver.get().getLocalRepository() != null) {
                localRepositories.add(mavenResolver.get().getLocalRepository());
            }
            if (mavenResolver.get().getDefaultRepositories() != null) {
                for (LocalRepository repository : mavenResolver.get().getDefaultRepositories()) {
                    localRepositories.add(repository.getBasedir());
                }
            }
            Set<Map.Entry<ProfileVersionKey, Map<String, Parser>>> entries = profileArtifacts.entrySet();
            for (Map.Entry<ProfileVersionKey, Map<String, Parser>> entry : entries) {
                ProfileVersionKey key = entry.getKey();
                Map<String, Parser> artifactMap = entry.getValue();
                // lets find a container for the profile
                Profile profile = key.getProfile();
                Properties checksums = findProfileChecksums(fabricService.get(), profile);
                if (checksums != null) {
                    Set<Map.Entry<String, Parser>> artifactMapEntries = artifactMap.entrySet();
                    for (Map.Entry<String, Parser> artifactMapEntry : artifactMapEntries) {
                        String location = artifactMapEntry.getKey();
                        Parser parser = artifactMapEntry.getValue();
                        if (isSnapshot(parser) || wildCardMatch(location)) {
                            Object value = checksums.get(location);
                            if (value == null) {
                                value = checksums.get(JavaContainers.removeUriPrefixBeforeMaven(location));
                            }
                            Long checksum = null;
                            if (value instanceof Number) {
                                checksum = ((Number) value).longValue();
                            } else if (value instanceof String) {
                                checksum = Long.parseLong((String) value);
                            }
                            if (checksum == null) {
                                if (missingChecksums.add(location)) {
                                    LOG.warn("Could not find checksum for location " + location);
                                }
                            } else {
                                File file = null;
                                for (File localRepository : localRepositories) {
                                    File _file = new File(localRepository.getPath() + File.separator + parser.getArtifactPath());
                                    if (_file.isFile()) {
                                        file = _file;
                                        break;
                                    }
                                }
                                if (!file.exists()) {
                                    LOG.info("Ignoring file " + file.getPath() + " as it does not exist");
                                } else {
                                    // lets use a cache of last modified times to avoid having to continuously
                                    // recalculate the checksum on each file
                                    Long oldModfied = localModified.get(file);
                                    long modified = file.lastModified();
                                    if (oldModfied == null || modified != oldModfied) {
                                        localModified.put(file, modified);
                                        Long fileChecksum = getFileChecksum(file);
                                        if (fileChecksum != null && !fileChecksum.equals(checksum)) {
                                            // lets keep track of local checksums in case we've already started the upload process
                                            // and it takes the profile a little while to respond to uploaded jars and to
                                            // refreshed profiles
                                            Long localChecksum = localChecksums.get(file);
                                            if (localChecksum == null || !localChecksum.equals(fileChecksum)) {
                                                localChecksums.put(file, fileChecksum);
                                                LOG.info("Checksums don't match for " + location + ", container: " + checksum + " and local file: " + fileChecksum);
                                                LOG.info("Updated version of " + location + " detected in " + file);
                                                if (isUpload()) {
                                                    uploadFile(location, parser, file);
                                                }
                                                refreshProfiles.add(profile);
                                            }
                                        }
                                    }
                                }
                            }
                        } else {
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("Ignoring " + location);
                            }
                        }
                    }
                }
            }
        }
        try {
            Thread.sleep(interval);
        } catch (InterruptedException ex) {
            running.set(false);
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Profile watcher thread stopped");
    }
}
Also used : HashMap(java.util.HashMap) Properties(java.util.Properties) Profile(io.fabric8.api.Profile) HashSet(java.util.HashSet) LocalRepository(org.eclipse.aether.repository.LocalRepository) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) LinkedList(java.util.LinkedList) Parser(io.fabric8.maven.util.Parser) ProfileService(io.fabric8.api.ProfileService) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap)

Example 27 with LocalRepository

use of org.eclipse.aether.repository.LocalRepository in project fabric8 by jboss-fuse.

the class AetherBasedResolver method releaseSession.

private void releaseSession(DefaultRepositorySystemSession session) {
    LocalRepository repo = session.getLocalRepository();
    Deque<DefaultRepositorySystemSession> deque = sessions.get(repo);
    if (deque == null) {
        sessions.putIfAbsent(repo, new ConcurrentLinkedDeque<DefaultRepositorySystemSession>());
        deque = sessions.get(repo);
    }
    deque.add(session);
}
Also used : DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) LocalRepository(org.eclipse.aether.repository.LocalRepository)

Example 28 with LocalRepository

use of org.eclipse.aether.repository.LocalRepository in project fabric8 by jboss-fuse.

the class AetherBasedResolver method resolveFile.

/**
 * Resolve maven artifact as file in repository.
 */
public File resolveFile(Artifact artifact, MavenRepositoryURL repositoryURL, Exception previousException) throws IOException {
    List<LocalRepository> defaultRepos = selectDefaultRepositories();
    List<RemoteRepository> remoteRepos = selectRepositories();
    if (repositoryURL != null) {
        addRepo(remoteRepos, repositoryURL);
    }
    if (previousException != null) {
        // we'll try using previous repositories, without these that will fail again anyway
        List<RemoteRepository> altered = new LinkedList<>();
        RepositoryException repositoryException = findAetherException(previousException);
        if (repositoryException instanceof ArtifactResolutionException) {
            // check only this aggregate exception and assume it's related to current artifact
            ArtifactResult result = ((ArtifactResolutionException) repositoryException).getResult();
            if (result != null && result.getRequest() != null && result.getRequest().getArtifact().equals(artifact)) {
                // - these exceptions contain repository that was checked
                for (Exception exception : result.getExceptions()) {
                    RepositoryException singleException = findAetherException(exception);
                    if (singleException instanceof ArtifactTransferException) {
                        RemoteRepository repository = ((ArtifactTransferException) singleException).getRepository();
                        if (repository != null) {
                            RetryChance chance = isRetryableException(singleException);
                            if (chance == RetryChance.NEVER) {
                                LOG.debug("Removing " + repository + " from list of repositories, previous exception: " + singleException.getClass().getName() + ": " + singleException.getMessage());
                            } else {
                                altered.add(repository);
                            }
                        }
                    }
                }
                // swap list of repos now
                remoteRepos = altered;
            }
        }
    }
    assignProxyAndMirrors(remoteRepos);
    File resolved = resolve(defaultRepos, remoteRepos, artifact);
    LOG.debug("Resolved ({}) as {}", artifact.toString(), resolved.getAbsolutePath());
    return resolved;
}
Also used : ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) ArtifactTransferException(org.eclipse.aether.transfer.ArtifactTransferException) LocalRepository(org.eclipse.aether.repository.LocalRepository) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) RepositoryException(org.eclipse.aether.RepositoryException) File(java.io.File) JarFile(java.util.jar.JarFile) LinkedList(java.util.LinkedList) DependencyCollectionException(org.eclipse.aether.collection.DependencyCollectionException) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) ArtifactNotFoundException(org.eclipse.aether.transfer.ArtifactNotFoundException) RepositoryException(org.eclipse.aether.RepositoryException) VersionRangeResolutionException(org.eclipse.aether.resolution.VersionRangeResolutionException) MetadataTransferException(org.eclipse.aether.transfer.MetadataTransferException) NoRouteToHostException(java.net.NoRouteToHostException) ArtifactTransferException(org.eclipse.aether.transfer.ArtifactTransferException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) DependencyResolutionException(org.eclipse.aether.resolution.DependencyResolutionException) MalformedURLException(java.net.MalformedURLException) MetadataNotFoundException(org.eclipse.aether.transfer.MetadataNotFoundException) InvalidVersionSpecificationException(org.eclipse.aether.version.InvalidVersionSpecificationException) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult)

Example 29 with LocalRepository

use of org.eclipse.aether.repository.LocalRepository in project fabric8 by jboss-fuse.

the class AetherBasedResolver method addLocalRepo.

private void addLocalRepo(List<LocalRepository> list, MavenRepositoryURL repo) {
    if (repo.getFile() != null) {
        LocalRepository local = new LocalRepository(repo.getFile(), "simple");
        list.add(local);
        if (repo.isSnapshotsEnabled()) {
            localRepositoriesWithSnapshots.add(local);
        }
        List<RemoteRepository> remotes = new ArrayList<>(1);
        addRepo(remotes, repo);
        // remember the remote equivalent of local/default repository
        defaultRepositories.put(local.getBasedir(), remotes.get(0));
    }
}
Also used : LocalRepository(org.eclipse.aether.repository.LocalRepository) ArrayList(java.util.ArrayList) RemoteRepository(org.eclipse.aether.repository.RemoteRepository)

Example 30 with LocalRepository

use of org.eclipse.aether.repository.LocalRepository in project spring-cloud-deployer by spring-cloud.

the class MavenArtifactResolver method newRepositorySystemSession.

/*
	 * Create a session to manage remote and local synchronization.
	 */
private DefaultRepositorySystemSession newRepositorySystemSession(RepositorySystem system, String localRepoPath) {
    DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
    LocalRepository localRepo = new LocalRepository(localRepoPath);
    session.setLocalRepositoryManager(system.newLocalRepositoryManager(session, localRepo));
    session.setOffline(this.properties.isOffline());
    if (this.properties.getConnectTimeout() != null) {
        session.setConfigProperty(ConfigurationProperties.CONNECT_TIMEOUT, this.properties.getConnectTimeout());
    }
    if (this.properties.getRequestTimeout() != null) {
        session.setConfigProperty(ConfigurationProperties.REQUEST_TIMEOUT, this.properties.getRequestTimeout());
    }
    if (isProxyEnabled()) {
        DefaultProxySelector proxySelector = new DefaultProxySelector();
        Proxy proxy = new Proxy(this.properties.getProxy().getProtocol(), this.properties.getProxy().getHost(), this.properties.getProxy().getPort(), this.authentication);
        proxySelector.add(proxy, this.properties.getProxy().getNonProxyHosts());
        session.setProxySelector(proxySelector);
    }
    return session;
}
Also used : Proxy(org.eclipse.aether.repository.Proxy) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) DefaultProxySelector(org.eclipse.aether.util.repository.DefaultProxySelector) LocalRepository(org.eclipse.aether.repository.LocalRepository)

Aggregations

LocalRepository (org.eclipse.aether.repository.LocalRepository)42 DefaultRepositorySystemSession (org.eclipse.aether.DefaultRepositorySystemSession)34 File (java.io.File)15 RemoteRepository (org.eclipse.aether.repository.RemoteRepository)8 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 RepositorySystem (org.eclipse.aether.RepositorySystem)6 JarFile (java.util.jar.JarFile)5 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)5 SimpleLocalRepositoryManagerFactory (org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory)5 LocalRepositoryManager (org.eclipse.aether.repository.LocalRepositoryManager)5 MalformedURLException (java.net.MalformedURLException)4 LinkedList (java.util.LinkedList)4 Artifact (org.eclipse.aether.artifact.Artifact)4 DefaultServiceLocator (org.eclipse.aether.impl.DefaultServiceLocator)4 URL (java.net.URL)3 DefaultMavenExecutionRequest (org.apache.maven.execution.DefaultMavenExecutionRequest)3 MavenExecutionRequest (org.apache.maven.execution.MavenExecutionRequest)3 ArtifactResult (org.eclipse.aether.resolution.ArtifactResult)3 VersionConstraint (org.eclipse.aether.version.VersionConstraint)3