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

Commit

Permalink
Merge f3c8f4e into c099b35
Browse files Browse the repository at this point in the history
  • Loading branch information
klieber committed Mar 26, 2017
2 parents c099b35 + f3c8f4e commit f9fc467
Show file tree
Hide file tree
Showing 19 changed files with 389 additions and 400 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,52 +25,60 @@
*/
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.os.OperatingSystem;
import com.github.klieber.phantomjs.os.OperatingSystemFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URL;

public class PhantomJSArchiveBuilder {
public class PhantomJSArchiveFactory {

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

private final OperatingSystem operatingSystem;
private final String version;
private final OperatingSystemFactory operatingSystemFactory;

public PhantomJSArchiveBuilder(OperatingSystem operatingSystem,
String version) {
this.operatingSystem = operatingSystem;
this.version = version;
public PhantomJSArchiveFactory(OperatingSystemFactory operatingSystemFactory) {
this.operatingSystemFactory = operatingSystemFactory;
}

public PhantomJSArchive build() {
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;
}

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

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

return archive;
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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 CachedFileFactory {

private final ArtifactBuilder artifactBuilder;

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,39 @@
package com.github.klieber.phantomjs.download;

import com.github.klieber.phantomjs.archive.PhantomJSArchive;
import com.github.klieber.phantomjs.cache.CachedFileFactory;
import com.github.klieber.phantomjs.locate.PhantomJsLocatorOptions;
import com.github.klieber.phantomjs.locate.RepositoryDetails;
import com.github.klieber.phantomjs.util.ArtifactBuilder;

import java.io.File;

public class DownloaderFactory {

private final PhantomJsLocatorOptions options;
private final RepositoryDetails repositoryDetails;
private final ArtifactBuilder artifactBuilder;
private final CachedFileFactory cachedFileFactory;

public DownloaderFactory(PhantomJsLocatorOptions options,
RepositoryDetails repositoryDetails,
ArtifactBuilder artifactBuilder,
CachedFileFactory cachedFileFactory) {
this.options = options;
this.repositoryDetails = repositoryDetails;
this.artifactBuilder = artifactBuilder;
this.cachedFileFactory = cachedFileFactory;
}

public Downloader getDownloader(PhantomJSArchive phantomJSArchive) {
Downloader downloader;
if (PhantomJsLocatorOptions.Source.REPOSITORY.equals(options.getSource())) {
downloader = new RepositoryDownloader(artifactBuilder, repositoryDetails);
} else {
File cachedFile = cachedFileFactory.create(phantomJSArchive, repositoryDetails.getRepositorySystemSession());
downloader = new WebDownloader(cachedFile);
}
return downloader;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class PhantomJsProcessBuilder {
private static final Logger LOGGER = LoggerFactory.getLogger(PhantomJsProcessBuilder.class);

private static final String PHANTOMJS_COMMAND = "phantomjs command: {}";
private static final String UNABLE_TO_BUILD_CMD_LINE_OPTIONS = "Unable to build command line options.";
private static final String UNABLE_TO_BUILD_CMD_LINE_OPTIONS = "Unable to create command line options.";
private static final String UNABLE_TO_START = "Unable to start phantomjs process.";

private final String phantomJsBinary;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
*/
package com.github.klieber.phantomjs.extract;

import com.github.klieber.phantomjs.archive.PhantomJSArchive;
import de.schlichtherle.truezip.file.TFile;
import de.schlichtherle.truezip.file.TVFS;
import de.schlichtherle.truezip.fs.FsSyncException;
Expand All @@ -35,29 +34,20 @@
import java.io.File;
import java.io.IOException;

public class PhantomJsExtractor implements Extractor {
public class ArchiveExtractor {

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

private static final String UNABLE_TO_EXTRACT = "Unable to extract phantomjs binary from %s";
private static final String UNABLE_TO_EXTRACT = "Unable to extract member from %s";
private static final String UNABLE_TO_UNMOUNT = "Unable to unmount file system after extracting";
private static final String EXTRACTING = "Extracting {} to {}";


private final PhantomJSArchive phantomJSArchive;

public PhantomJsExtractor(PhantomJSArchive phantomJSArchive) {
this.phantomJSArchive = phantomJSArchive;
}

@Override
public void extract(File archive,File extractTo) throws ExtractionException {
public void extract(File archive, String member, File extractTo) throws ExtractionException {
try {
TFile tfile = new TFile(archive, phantomJSArchive.getPathToExecutable());
TFile tfile = new TFile(archive, member);
LOGGER.info(EXTRACTING, tfile.getAbsolutePath(), extractTo.getAbsolutePath());
if (extractTo.getParentFile().exists() || extractTo.getParentFile().mkdirs()) {
tfile.cp(extractTo);
extractTo.setExecutable(true);
}
} catch (IOException e) {
throw new ExtractionException(String.format(UNABLE_TO_EXTRACT, archive), e);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import com.github.klieber.phantomjs.download.DownloadException;
import com.github.klieber.phantomjs.download.Downloader;
import com.github.klieber.phantomjs.extract.ExtractionException;
import com.github.klieber.phantomjs.extract.Extractor;
import com.github.klieber.phantomjs.extract.ArchiveExtractor;

import java.io.File;

Expand All @@ -38,27 +38,27 @@ public class PhantomJsInstaller implements Installer {
private static final String UNABLE_TO_INSTALL = "Unable to install phantomjs.";

private final Downloader downloader;
private final Extractor extractor;
private final ArchiveExtractor extractor;
private final File outputDirectory;

public PhantomJsInstaller(Downloader downloader, Extractor extractor, File outputDirectory) {
public PhantomJsInstaller(Downloader downloader, ArchiveExtractor extractor, File outputDirectory) {
this.downloader = downloader;
this.extractor = extractor;
this.outputDirectory = outputDirectory;
}

@Override
public String install(PhantomJSArchive phantomJSArchive) throws InstallationException {
String executable = phantomJSArchive.getPathToExecutable();

File extractTo = new File(outputDirectory, phantomJSArchive.getPathToExecutable());
File extractTo = new File(outputDirectory, executable);

if (!extractTo.exists()) {
try {
File archive = downloader.download(phantomJSArchive);
extractor.extract(archive, extractTo);
} catch(DownloadException e) {
throw new InstallationException(UNABLE_TO_INSTALL, e);
} catch(ExtractionException e) {
extractor.extract(archive, executable, extractTo);
extractTo.setExecutable(true);
} catch(DownloadException | ExtractionException e) {
throw new InstallationException(UNABLE_TO_INSTALL, e);
}

Expand Down

0 comments on commit f9fc467

Please sign in to comment.