Skip to content
This repository has been archived by the owner on Apr 5, 2019. It is now read-only.

Commit

Permalink
Merge 1cf7ec0 into c099b35
Browse files Browse the repository at this point in the history
  • Loading branch information
klieber committed Mar 26, 2017
2 parents c099b35 + 1cf7ec0 commit 2504a6b
Show file tree
Hide file tree
Showing 31 changed files with 570 additions and 439 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.github.klieber.phantomjs;

import com.github.klieber.phantomjs.locate.LocatorFactory;
import com.github.klieber.phantomjs.locate.PhantomJsLocatorOptions;
import com.github.klieber.phantomjs.locate.RepositoryDetails;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.repository.RemoteRepository;

import javax.inject.Inject;
import javax.inject.Named;
import java.util.ArrayList;
import java.util.List;

@Named
public class PhantomJs {

private final LocatorFactory locatorFactory;

@Inject
public PhantomJs(LocatorFactory locatorFactory) {
this.locatorFactory = locatorFactory;
}

public Builder options(PhantomJsLocatorOptions options) {
return new Builder(locatorFactory, options);
}

public static class Builder {

private final LocatorFactory locatorFactory;
private final PhantomJsLocatorOptions options;
private final List<RemoteRepository> remoteRepositories;

private RepositorySystem repositorySystem;
private RepositorySystemSession repositorySystemSession;

private Builder(LocatorFactory locatorFactory,
PhantomJsLocatorOptions options) {
this.locatorFactory = locatorFactory;
this.options = options;
this.remoteRepositories = new ArrayList<RemoteRepository>();
}

public Builder repositorySystem(RepositorySystem repositorySystem) {
this.repositorySystem = repositorySystem;
return this;
}

public Builder repositorySystemSession(RepositorySystemSession repositorySystemSession) {
this.repositorySystemSession = repositorySystemSession;
return this;
}

public Builder remoteRepository(RemoteRepository remoteRepository) {
this.remoteRepositories.add(remoteRepository);
return this;
}

public Builder remoteRepositories(List<RemoteRepository> remoteRepositories) {
this.remoteRepositories.addAll(remoteRepositories);
return this;
}

public String locate() {
RepositoryDetails repositoryDetails = new RepositoryDetails(
this.repositorySystem,
this.repositorySystemSession,
this.remoteRepositories
);

return locatorFactory.getLocator(options, repositoryDetails).locate();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,52 +25,69 @@
*/
package com.github.klieber.phantomjs.archive;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.github.klieber.phantomjs.archive.mapping.ArchiveMapping;
import com.github.klieber.phantomjs.archive.mapping.ArchiveMappings;
import com.github.klieber.phantomjs.locate.PhantomJsLocatorOptions;
import com.github.klieber.phantomjs.os.OperatingSystem;
import com.github.klieber.phantomjs.os.OperatingSystemFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.inject.Named;
import java.io.IOException;
import java.net.URL;

public class PhantomJSArchiveBuilder {
@Named
public class PhantomJSArchiveFactory {

private static final Logger LOGGER = LoggerFactory.getLogger(PhantomJSArchiveFactory.class);

private static final Logger LOGGER = LoggerFactory.getLogger(PhantomJSArchiveBuilder.class);
private final OperatingSystemFactory operatingSystemFactory;

private final OperatingSystem operatingSystem;
private final String version;
@Inject
public PhantomJSArchiveFactory(OperatingSystemFactory operatingSystemFactory) {
this.operatingSystemFactory = operatingSystemFactory;
}

public PhantomJSArchiveBuilder(OperatingSystem operatingSystem,
String version) {
this.operatingSystem = operatingSystem;
this.version = version;
public PhantomJSArchive create(PhantomJsLocatorOptions options) {
return create(options.getVersion(), options.getBaseUrl());
}

public PhantomJSArchive build() {
public PhantomJSArchive create(String version, String baseUrl) {
PhantomJSArchive archive = create(version);

if (baseUrl != null) {
archive = new UserProvidedBaseUrlPhantomJSArchive(archive, baseUrl);
}

return archive;
}

public PhantomJSArchive create(String version) {

OperatingSystem operatingSystem = this.operatingSystemFactory.create();

PhantomJSArchive archive = null;

ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
URL resource = PhantomJSArchiveBuilder.class.getResource("/archive-mapping.yaml");
URL resource = PhantomJSArchiveFactory.class.getResource("/archive-mapping.yaml");
try {
ArchiveMappings archiveMappings = mapper.readValue(resource, ArchiveMappings.class);
for (ArchiveMapping archiveMapping : archiveMappings.getMappings()) {
if (archiveMapping.getSpec().matches(this.version, this.operatingSystem)) {
archive = new PhantomJSArchiveImpl(archiveMapping.getFormat(), this.version);
if (archiveMapping.getSpec().matches(version, operatingSystem)) {
archive = new PhantomJSArchiveImpl(archiveMapping.getFormat(), version);
break;
}
}
} catch (IOException e) {
LOGGER.error("Unable to read archive-mapping.yaml", e);
}
if (archive == null) {
throw new UnsupportedPlatformException(this.operatingSystem);
throw new UnsupportedPlatformException(operatingSystem);
}
return archive;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.github.klieber.phantomjs.cache;

import com.github.klieber.phantomjs.archive.PhantomJSArchive;
import com.github.klieber.phantomjs.util.ArtifactBuilder;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.repository.LocalRepositoryManager;

import java.io.File;

public class ArchiveCache {

private final ArtifactBuilder artifactBuilder;
private final RepositorySystemSession repositorySystemSession;

public ArchiveCache(ArtifactBuilder artifactBuilder,
RepositorySystemSession repositorySystemSession) {
this.artifactBuilder = artifactBuilder;
this.repositorySystemSession = repositorySystemSession;
}

public File getFile(PhantomJSArchive phantomJSArchive) {
Artifact artifact = artifactBuilder.createArtifact(phantomJSArchive);
LocalRepositoryManager manager = repositorySystemSession.getLocalRepositoryManager();
return new File(manager.getRepository().getBasedir(), manager.getPathForLocalArtifact(artifact));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.github.klieber.phantomjs.cache;

import com.github.klieber.phantomjs.util.ArtifactBuilder;
import org.eclipse.aether.RepositorySystemSession;

import javax.inject.Inject;
import javax.inject.Named;

@Named
public class ArchiveCacheFactory {

private final ArtifactBuilder artifactBuilder;

@Inject
public ArchiveCacheFactory(ArtifactBuilder artifactBuilder) {
this.artifactBuilder = artifactBuilder;
}

public ArchiveCache create(RepositorySystemSession repositorySystemSession) {
return new ArchiveCache(artifactBuilder, repositorySystemSession);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.github.klieber.phantomjs.cache;

import com.github.klieber.phantomjs.archive.PhantomJSArchive;
import com.github.klieber.phantomjs.util.ArtifactBuilder;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.repository.LocalRepositoryManager;

import javax.inject.Inject;
import javax.inject.Named;
import java.io.File;

@Named
public class CachedFileFactory {

private final ArtifactBuilder artifactBuilder;

@Inject
public CachedFileFactory(ArtifactBuilder artifactBuilder) {
this.artifactBuilder = artifactBuilder;
}

public File create(PhantomJSArchive phantomJSArchive, RepositorySystemSession repositorySystemSession) {
Artifact artifact = artifactBuilder.createArtifact(phantomJSArchive);
LocalRepositoryManager manager = repositorySystemSession.getLocalRepositoryManager();
return new File(manager.getRepository().getBasedir(), manager.getPathForLocalArtifact(artifact));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.github.klieber.phantomjs.download;

import com.github.klieber.phantomjs.cache.ArchiveCache;
import com.github.klieber.phantomjs.cache.ArchiveCacheFactory;
import com.github.klieber.phantomjs.locate.PhantomJsLocatorOptions;
import com.github.klieber.phantomjs.locate.RepositoryDetails;
import com.github.klieber.phantomjs.util.ArtifactBuilder;

import javax.inject.Inject;
import javax.inject.Named;

@Named
public class DownloaderFactory {

private final ArtifactBuilder artifactBuilder;
private final ArchiveCacheFactory archiveCacheFactory;

@Inject
public DownloaderFactory(ArtifactBuilder artifactBuilder,
ArchiveCacheFactory archiveCacheFactory) {
this.archiveCacheFactory = archiveCacheFactory;
this.artifactBuilder = artifactBuilder;
}

public Downloader getDownloader(PhantomJsLocatorOptions options,
RepositoryDetails repositoryDetails) {
return isRepositorySource(options) ?
createRepositoryDownloader(repositoryDetails) : createWebDownloader(repositoryDetails);
}

private boolean isRepositorySource(PhantomJsLocatorOptions options) {
return PhantomJsLocatorOptions.Source.REPOSITORY.equals(options.getSource());
}

private Downloader createRepositoryDownloader(RepositoryDetails repositoryDetails) {
return new RepositoryDownloader(artifactBuilder, repositoryDetails);
}

private Downloader createWebDownloader(RepositoryDetails repositoryDetails) {
ArchiveCache archiveCache = archiveCacheFactory.create(repositoryDetails.getRepositorySystemSession());
return new WebDownloader(archiveCache);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public File download(PhantomJSArchive archive) throws DownloadException {
try {
ArtifactResult result = repositoryDetails.getRepositorySystem().resolveArtifact(
repositoryDetails.getRepositorySystemSession(),
request);
request
);
LOGGER.info(RESOLVED_ARTIFACT, result.getArtifact().getFile(), result.getRepository());
return result.getArtifact().getFile();
} catch(ArtifactResolutionException e) {
Expand Down

0 comments on commit 2504a6b

Please sign in to comment.