Skip to content
This repository has been archived by the owner on May 18, 2021. It is now read-only.

Commit

Permalink
Update to airline release and guava 18.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Henning Schmiedehausen authored and dain committed Nov 21, 2014
1 parent aa2f7de commit a81374f
Show file tree
Hide file tree
Showing 21 changed files with 94 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public Deployment install(Installation installation)
// download the binary
File binary = new File(tempDir, "airship-binary.tar.gz");
try {
Files.copy(Resources.newInputStreamSupplier(installation.getBinaryFile().toURL()), binary);
Resources.asByteSource(installation.getBinaryFile().toURL()).copyTo(Files.asByteSink(binary));
}
catch (IOException e) {
throw new RuntimeException("Unable to download binary " + assignment.getBinary() + " from " + installation.getBinaryFile(), e);
Expand All @@ -156,7 +156,7 @@ public Deployment install(Installation installation)
// unpack config bundle
try {
URL url = installation.getConfigFile().toURL();
ConfigUtils.unpackConfig(Resources.newInputStreamSupplier(url), binaryRootDir);
ConfigUtils.unpackConfig(Resources.asByteSource(url), binaryRootDir);
}
catch (Exception e) {
throw new RuntimeException("Unable to extract config bundle " + assignment.getConfig() + ": " + e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import static com.google.common.io.Files.asByteSink;
import static com.google.common.io.Resources.getResource;
import static com.google.common.io.Resources.newInputStreamSupplier;
import static com.google.common.io.Resources.asByteSource;
import static io.airlift.airship.shared.AssignmentHelper.APPLE_ASSIGNMENT;
import static io.airlift.airship.shared.AssignmentHelper.BANANA_ASSIGNMENT;
import static io.airlift.airship.shared.FileUtils.deleteRecursively;
Expand Down Expand Up @@ -77,7 +78,7 @@ private Deployment createDeploymentDir(String name, Assignment assignment)

// copy launcher script
launcher.getParentFile().mkdirs();
Files.copy(newInputStreamSupplier(getResource(ArchiveHelper.class, "launcher")), launcher);
asByteSource(getResource(ArchiveHelper.class, "launcher")).copyTo(asByteSink(launcher));
launcher.setExecutable(true, true);

return new Deployment(UUID.randomUUID(), "location", deploymentDir, dataDir, assignment, ImmutableMap.<String, Integer>of("memory", 512));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.io.InputSupplier;
import com.google.common.io.ByteSource;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
Expand All @@ -20,7 +20,7 @@
import java.io.InputStream;
import java.util.Map;

import static com.google.common.base.Objects.firstNonNull;
import static com.google.common.base.MoreObjects.firstNonNull;

class GitUtils
{
Expand All @@ -44,14 +44,14 @@ public static RevCommit getCommit(Repository repository, Ref ref)
}
}

public static InputSupplier<InputStream> getBlob(final Repository repository, final ObjectId objectId)
public static ByteSource getBlob(final Repository repository, final ObjectId objectId)
{
Preconditions.checkArgument(repository.hasObject(objectId), "object id '%s' not found in git repository", objectId.getName());

return new InputSupplier<InputStream>()
return new ByteSource()
{
@Override
public InputStream getInput()
public InputStream openStream()
throws IOException
{
return repository.open(objectId).openStream();
Expand Down Expand Up @@ -92,11 +92,11 @@ public static ObjectId findFileObject(Repository repository, RevCommit commit, S
return null;
}

public static Function<? super ObjectId, InputSupplier<InputStream>> inputStreamSupplierFunction(final Repository repository)
public static Function<? super ObjectId, ByteSource> byteSourceFunction(final Repository repository)
{
return new Function<ObjectId, InputSupplier<InputStream>>()
return new Function<ObjectId, ByteSource>()
{
public InputSupplier<InputStream> apply(ObjectId input)
public ByteSource apply(ObjectId input)
{
return getBlob(repository, input);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
import com.google.common.base.Predicate;
import com.google.common.collect.Maps;
import com.google.common.collect.Ordering;
import com.google.common.io.CharStreams;
import com.google.common.io.ByteSource;
import com.google.common.io.Files;
import com.google.common.io.InputSupplier;
import com.google.common.primitives.Ints;
import io.airlift.json.JsonCodec;
import org.eclipse.jgit.api.Git;
Expand All @@ -27,15 +26,13 @@

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Comparator;
import java.util.Map;

import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.io.CharStreams.newReaderSupplier;
import static io.airlift.airship.configbundler.GitUtils.byteSourceFunction;
import static io.airlift.airship.configbundler.GitUtils.getBlob;
import static io.airlift.airship.configbundler.GitUtils.getBranch;
import static io.airlift.airship.configbundler.GitUtils.inputStreamSupplierFunction;

class Model
{
Expand All @@ -60,7 +57,7 @@ public Metadata readMetadata()

ObjectId objectId = GitUtils.findFileObject(repository, head, METADATA_FILE);

String json = CharStreams.toString(newReaderSupplier(getBlob(repository, objectId), UTF_8));
String json = getBlob(repository, objectId).asCharSource(UTF_8).read();
Metadata metadata = JsonCodec.jsonCodec(Metadata.class).fromJson(json);

Preconditions.checkNotNull(metadata, ".metadata file not found in master branch");
Expand Down Expand Up @@ -151,7 +148,7 @@ public void activateBundle(Bundle bundle)
git.checkout().setName(bundle.getName()).call();
}

public Map<String, InputSupplier<InputStream>> getEntries(Bundle bundle)
public Map<String, ByteSource> getEntries(Bundle bundle)
throws IOException
{
Ref ref;
Expand All @@ -166,7 +163,7 @@ public Map<String, InputSupplier<InputStream>> getEntries(Bundle bundle)

RevCommit commit = GitUtils.getCommit(git.getRepository(), ref);

return Maps.transformValues(GitUtils.getEntries(git.getRepository(), commit.getTree()), inputStreamSupplierFunction(git.getRepository()));
return Maps.transformValues(GitUtils.getEntries(git.getRepository(), commit.getTree()), byteSourceFunction(git.getRepository()));
}

public Bundle createNewVersion(Bundle bundle)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package io.airlift.airship.configbundler;

import com.google.common.base.Preconditions;
import com.google.common.io.InputSupplier;
import com.google.common.io.ByteSource;
import io.airlift.airline.Arguments;
import io.airlift.airline.Command;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.RepositoryBuilder;
import org.eclipse.jgit.util.FS;

import java.io.InputStream;
import java.util.Map;
import java.util.concurrent.Callable;

Expand Down Expand Up @@ -61,7 +60,7 @@ public Void call()
}

// get entries from tag
final Map<String, InputSupplier<InputStream>> entries = model.getEntries(bundle);
final Map<String, ByteSource> entries = model.getEntries(bundle);

if (entries.isEmpty()) {
throw new RuntimeException("Cannot build an empty config package");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package io.airlift.airship.configbundler;

import com.google.common.base.Preconditions;
import com.google.common.io.InputSupplier;
import com.google.common.io.ByteSource;
import io.airlift.airline.Arguments;
import io.airlift.airline.Command;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.RepositoryBuilder;
import org.eclipse.jgit.util.FS;

import java.io.InputStream;
import java.util.Map;
import java.util.concurrent.Callable;

Expand Down Expand Up @@ -45,7 +44,7 @@ public Void call()
Preconditions.checkState(bundle.isSnapshot(), "There are not pending changes for bundle %s. Use released version %s:%s instead",
bundle.getName(), bundle.getName(), bundle.getVersionString());

final Map<String, InputSupplier<InputStream>> entries = model.getEntries(bundle);
final Map<String, ByteSource> entries = model.getEntries(bundle);

if (entries.isEmpty()) {
throw new RuntimeException("Cannot build an empty config package");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package io.airlift.airship.configbundler;

import com.google.common.io.InputSupplier;
import com.google.common.io.ByteSource;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

class ZipGenerator
implements Generator
{
private final Map<String, InputSupplier<InputStream>> entries;
private final Map<String, ByteSource> entries;

public ZipGenerator(Map<String, InputSupplier<InputStream>> entries)
public ZipGenerator(Map<String, ByteSource> entries)
{
this.entries = entries;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
package io.airlift.airship.configbundler;

import com.google.common.io.ByteStreams;
import com.google.common.io.InputSupplier;
import com.google.common.io.ByteSource;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

class ZipPackager
{
public static void packageEntries(OutputStream output, Map<String, InputSupplier<InputStream>> entries)
public static void packageEntries(OutputStream output, Map<String, ByteSource> entries)
throws IOException
{
ZipOutputStream out = new ZipOutputStream(output);

for (Map.Entry<String, InputSupplier<InputStream>> entry : entries.entrySet()) {
for (Map.Entry<String, ByteSource> entry : entries.entrySet()) {
String path = entry.getKey();
ZipEntry fileEntry = new ZipEntry(path);
out.putNextEntry(fileEntry);
ByteStreams.copy(entry.getValue(), out);
entry.getValue().copyTo(out);
}
out.finish();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ public Response getBinary(@PathParam("groupId") String groupId,
return Response.status(Status.NOT_FOUND).build();
}

return Response.ok(new InputSupplierStreamingOutput(Resources.newInputStreamSupplier(binaryUrl))).build();
return Response.ok(new InputSupplierStreamingOutput(Resources.asByteSource(binaryUrl))).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.io.ByteProcessor;
import com.google.common.io.ByteStreams;
import com.google.common.io.InputSupplier;
import com.google.common.io.ByteSource;
import com.google.common.io.Resources;

import io.airlift.airship.shared.Repository;

import javax.annotation.Nullable;
import javax.inject.Inject;

import java.io.InputStream;
import java.net.URI;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -299,8 +298,8 @@ private URI toHttpUri(String path, String defaultExtension)
private boolean isValidLocation(URI uri)
{
try {
InputSupplier<InputStream> inputSupplier = Resources.newInputStreamSupplier(uri.toURL());
ByteStreams.readBytes(inputSupplier, new ByteProcessor<Void>()
ByteSource byteSource = Resources.asByteSource(uri.toURL());
byteSource.read(new ByteProcessor<Void>()
{
private int count;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.CharStreams;
import com.google.common.io.ByteSource;
import com.google.common.io.Files;
import com.google.common.io.InputSupplier;
import com.google.inject.Inject;

import io.airlift.airship.shared.Assignment;
import io.airlift.airship.shared.ConfigUtils;
import io.airlift.airship.shared.DigestUtils;
Expand All @@ -34,7 +34,6 @@

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -119,7 +118,7 @@ private List<ServiceDescriptor> getServiceInventory(SlotStatus slotStatus)
File cacheFile = getCacheFile(config);
if (cacheFile.canRead()) {
try {
String json = CharStreams.toString(Files.newReaderSupplier(cacheFile, Charsets.UTF_8));
String json = Files.asCharSource(cacheFile, Charsets.UTF_8).read();
List<ServiceDescriptor> descriptors = descriptorsJsonCodec.fromJson(json);
invalidServiceInventory.remove(config);
return descriptors;
Expand All @@ -130,15 +129,15 @@ private List<ServiceDescriptor> getServiceInventory(SlotStatus slotStatus)
}
}

InputSupplier<? extends InputStream> configFile = ConfigUtils.newConfigEntrySupplier(repository, config, "airship-service-inventory.json");
ByteSource configFile = ConfigUtils.newConfigEntrySupplier(repository, config, "airship-service-inventory.json");
if (configFile == null) {
return null;
}

try {
String json;
try {
json = CharStreams.toString(CharStreams.newReaderSupplier(configFile, Charsets.UTF_8));
json = configFile.asCharSource(Charsets.UTF_8).read();
}
catch (FileNotFoundException e) {
// no service inventory in the config, so replace with json null so caching works
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
package io.airlift.airship.coordinator;

import com.google.common.io.ByteStreams;
import com.google.common.io.InputSupplier;
import com.google.common.io.ByteSource;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.StreamingOutput;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

class InputSupplierStreamingOutput
implements StreamingOutput
{
private final InputSupplier<? extends InputStream> inputSupplier;
private final ByteSource byteSource;

public InputSupplierStreamingOutput(InputSupplier<? extends InputStream> inputSupplier)
public InputSupplierStreamingOutput(ByteSource byteSource)
{
this.inputSupplier = inputSupplier;
this.byteSource = byteSource;
}

public void write(OutputStream output)
throws IOException, WebApplicationException
{
ByteStreams.copy(inputSupplier, output);
byteSource.copyTo(output);
}
}
Loading

0 comments on commit a81374f

Please sign in to comment.