mirror of https://github.com/apache/jclouds.git
Replace InputSupplierMap with Map<K, ByteSource>
A future version of Guava will remove InputSupplier.
This commit is contained in:
parent
d9d8d421cb
commit
baddf8fe80
|
@ -30,7 +30,7 @@ import org.jclouds.domain.Location;
|
|||
import org.jclouds.location.Provider;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Supplier;
|
||||
import com.google.common.io.ByteSource;
|
||||
import com.google.inject.TypeLiteral;
|
||||
|
||||
/**
|
||||
|
@ -49,8 +49,7 @@ public class BYONComputeServiceContextModule extends JCloudsNativeComputeService
|
|||
super.configure();
|
||||
bind(new TypeLiteral<Function<URI, InputStream>>() {
|
||||
}).to(SupplyFromProviderURIOrNodesProperty.class);
|
||||
bind(new TypeLiteral<Supplier<InputStream>>() {
|
||||
}).annotatedWith(Provider.class).to(SupplyFromProviderURIOrNodesProperty.class);
|
||||
bind(ByteSource.class).annotatedWith(Provider.class).to(SupplyFromProviderURIOrNodesProperty.class);
|
||||
bind(new TypeLiteral<Function<URI, InputStream>>() {
|
||||
}).to(SupplyFromProviderURIOrNodesProperty.class);
|
||||
install(new LocationsFromComputeServiceAdapterModule<NodeMetadata, Hardware, Image, Location>() {
|
||||
|
|
|
@ -16,11 +16,9 @@
|
|||
*/
|
||||
package org.jclouds.byon.config;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
|
@ -29,8 +27,6 @@ import org.jclouds.byon.domain.YamlNode;
|
|||
import org.jclouds.byon.functions.NodesFromYamlStream;
|
||||
import org.jclouds.byon.suppliers.NodesParsedFromSupplier;
|
||||
import org.jclouds.collect.TransformingMap;
|
||||
import org.jclouds.io.CopyInputStreamInputSupplierMap;
|
||||
import org.jclouds.io.CopyInputStreamIntoSupplier;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
import com.google.common.base.Function;
|
||||
|
@ -39,7 +35,7 @@ import com.google.common.base.Supplier;
|
|||
import com.google.common.cache.CacheBuilder;
|
||||
import com.google.common.cache.CacheLoader;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
import com.google.common.io.InputSupplier;
|
||||
import com.google.common.io.ByteSource;
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Provides;
|
||||
import com.google.inject.TypeLiteral;
|
||||
|
@ -52,10 +48,10 @@ import com.google.inject.name.Names;
|
|||
@ConfiguresNodeStore
|
||||
@Beta
|
||||
public class YamlNodeStoreModule extends AbstractModule {
|
||||
private static final Map<String, InputSupplier<InputStream>> BACKING = new ConcurrentHashMap<String, InputSupplier<InputStream>>();
|
||||
private final Map<String, InputStream> backing;
|
||||
private static final Map<String, ByteSource> BACKING = new ConcurrentHashMap<String, ByteSource>();
|
||||
private final Map<String, ByteSource> backing;
|
||||
|
||||
public YamlNodeStoreModule(Map<String, InputStream> backing) {
|
||||
public YamlNodeStoreModule(Map<String, ByteSource> backing) {
|
||||
this.backing = backing;
|
||||
}
|
||||
|
||||
|
@ -67,38 +63,26 @@ public class YamlNodeStoreModule extends AbstractModule {
|
|||
protected void configure() {
|
||||
bind(new TypeLiteral<Supplier<LoadingCache<String, Node>>>() {
|
||||
}).to(NodesParsedFromSupplier.class);
|
||||
bind(new TypeLiteral<Function<InputStream, LoadingCache<String, Node>>>() {
|
||||
bind(new TypeLiteral<Function<ByteSource, LoadingCache<String, Node>>>() {
|
||||
}).to(NodesFromYamlStream.class);
|
||||
bind(new TypeLiteral<Function<YamlNode, InputStream>>() {
|
||||
}).toInstance(org.jclouds.byon.domain.YamlNode.yamlNodeToInputStream);
|
||||
bind(new TypeLiteral<Function<InputStream, YamlNode>>() {
|
||||
}).toInstance(org.jclouds.byon.domain.YamlNode.inputStreamToYamlNode);
|
||||
bind(new TypeLiteral<Function<YamlNode, ByteSource>>() {
|
||||
}).toInstance(org.jclouds.byon.domain.YamlNode.yamlNodeToByteSource);
|
||||
bind(new TypeLiteral<Function<ByteSource, YamlNode>>() {
|
||||
}).toInstance(org.jclouds.byon.domain.YamlNode.byteSourceToYamlNode);
|
||||
bind(new TypeLiteral<Function<Node, YamlNode>>() {
|
||||
}).toInstance(org.jclouds.byon.domain.YamlNode.nodeToYamlNode);
|
||||
bind(new TypeLiteral<Function<YamlNode, Node>>() {
|
||||
}).toInstance(org.jclouds.byon.domain.YamlNode.toNode);
|
||||
if (backing != null) {
|
||||
bind(new TypeLiteral<Map<String, InputStream>>() {
|
||||
bind(new TypeLiteral<Map<String, ByteSource>>() {
|
||||
}).annotatedWith(Names.named("yaml")).toInstance(backing);
|
||||
} else {
|
||||
bind(new TypeLiteral<Map<String, InputSupplier<InputStream>>>() {
|
||||
bind(new TypeLiteral<Map<String, ByteSource>>() {
|
||||
}).annotatedWith(Names.named("yaml")).toInstance(BACKING);
|
||||
bind(new TypeLiteral<Map<String, InputStream>>() {
|
||||
}).annotatedWith(Names.named("yaml")).to(new TypeLiteral<YAMLCopyInputStreamInputSupplierMap>() {
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Singleton
|
||||
public static class YAMLCopyInputStreamInputSupplierMap extends CopyInputStreamInputSupplierMap {
|
||||
@Inject
|
||||
public YAMLCopyInputStreamInputSupplierMap(@Named("yaml") Map<String, InputSupplier<InputStream>> toMap,
|
||||
CopyInputStreamIntoSupplier putFunction) {
|
||||
super(toMap, putFunction);
|
||||
}
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
protected LoadingCache<String, Node> provideNodeStore(Map<String, YamlNode> backing, Function<Node, YamlNode> yamlSerializer,
|
||||
|
@ -108,8 +92,8 @@ public class YamlNodeStoreModule extends AbstractModule {
|
|||
|
||||
@Provides
|
||||
@Singleton
|
||||
protected Map<String, YamlNode> provideYamlStore(@Named("yaml") Map<String, InputStream> backing,
|
||||
Function<YamlNode, InputStream> yamlSerializer, Function<InputStream, YamlNode> yamlDeserializer) {
|
||||
return new TransformingMap<String, InputStream, YamlNode>(backing, yamlDeserializer, yamlSerializer);
|
||||
protected Map<String, YamlNode> provideYamlStore(@Named("yaml") Map<String, ByteSource> backing,
|
||||
Function<YamlNode, ByteSource> yamlSerializer, Function<ByteSource, YamlNode> yamlDeserializer) {
|
||||
return new TransformingMap<String, ByteSource, YamlNode>(backing, yamlDeserializer, yamlSerializer);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
*/
|
||||
package org.jclouds.byon.domain;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
|
@ -23,18 +24,20 @@ import java.util.Map;
|
|||
|
||||
import org.jclouds.byon.Node;
|
||||
import org.jclouds.util.Closeables2;
|
||||
import org.jclouds.util.Strings2;
|
||||
import org.yaml.snakeyaml.DumperOptions;
|
||||
import org.yaml.snakeyaml.Loader;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
import org.yaml.snakeyaml.constructor.Constructor;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Throwables;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.collect.ImmutableMap.Builder;
|
||||
import com.google.common.io.ByteSource;
|
||||
|
||||
/**
|
||||
* Serializes to the following
|
||||
|
@ -104,28 +107,32 @@ public class YamlNode {
|
|||
return toNode.apply(this);
|
||||
}
|
||||
|
||||
public static final Function<InputStream, YamlNode> inputStreamToYamlNode = new Function<InputStream, YamlNode>() {
|
||||
public static final Function<ByteSource, YamlNode> byteSourceToYamlNode = new Function<ByteSource, YamlNode>() {
|
||||
@Override
|
||||
public YamlNode apply(InputStream in) {
|
||||
if (in == null)
|
||||
public YamlNode apply(ByteSource byteSource) {
|
||||
if (byteSource == null)
|
||||
return null;
|
||||
// note that snakeyaml also throws nosuchmethod error when you use the non-deprecated
|
||||
// constructor
|
||||
InputStream in = null;
|
||||
try {
|
||||
in = byteSource.openStream();
|
||||
return (YamlNode) new Yaml(new Loader(new Constructor(YamlNode.class))).load(in);
|
||||
} catch (IOException ioe) {
|
||||
throw Throwables.propagate(ioe);
|
||||
} finally {
|
||||
Closeables2.closeQuietly(in);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public static YamlNode fromYaml(InputStream in) {
|
||||
return inputStreamToYamlNode.apply(in);
|
||||
public static YamlNode fromYaml(ByteSource in) {
|
||||
return byteSourceToYamlNode.apply(in);
|
||||
}
|
||||
|
||||
public static final Function<YamlNode, InputStream> yamlNodeToInputStream = new Function<YamlNode, InputStream>() {
|
||||
public static final Function<YamlNode, ByteSource> yamlNodeToByteSource = new Function<YamlNode, ByteSource>() {
|
||||
@Override
|
||||
public InputStream apply(YamlNode in) {
|
||||
public ByteSource apply(YamlNode in) {
|
||||
if (in == null)
|
||||
return null;
|
||||
Builder<String, Object> prettier = ImmutableMap.builder();
|
||||
|
@ -167,12 +174,12 @@ public class YamlNode {
|
|||
prettier.put("sudo_password", in.sudo_password);
|
||||
DumperOptions options = new DumperOptions();
|
||||
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
||||
return Strings2.toInputStream(new Yaml(options).dump(prettier.build()));
|
||||
return ByteSource.wrap(new Yaml(options).dump(prettier.build()).getBytes(Charsets.UTF_8));
|
||||
}
|
||||
};
|
||||
|
||||
public InputStream toYaml() {
|
||||
return yamlNodeToInputStream.apply(this);
|
||||
public ByteSource toYaml() {
|
||||
return yamlNodeToByteSource.apply(this);
|
||||
}
|
||||
|
||||
public static YamlNode fromNode(Node in) {
|
||||
|
|
|
@ -19,6 +19,7 @@ package org.jclouds.byon.functions;
|
|||
import static com.google.common.base.Preconditions.checkState;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -33,11 +34,14 @@ import org.yaml.snakeyaml.constructor.Constructor;
|
|||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Functions;
|
||||
import com.google.common.base.Throwables;
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.google.common.cache.CacheLoader;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.io.ByteSource;
|
||||
import com.google.common.io.Closeables;
|
||||
|
||||
/**
|
||||
* Parses the following syntax.
|
||||
|
@ -67,7 +71,7 @@ import com.google.common.collect.Maps;
|
|||
* @author Adrian Cole
|
||||
*/
|
||||
@Singleton
|
||||
public class NodesFromYamlStream implements Function<InputStream, LoadingCache<String, Node>> {
|
||||
public class NodesFromYamlStream implements Function<ByteSource, LoadingCache<String, Node>> {
|
||||
|
||||
/**
|
||||
* Type-safe config class for YAML
|
||||
|
@ -78,7 +82,7 @@ public class NodesFromYamlStream implements Function<InputStream, LoadingCache<S
|
|||
}
|
||||
|
||||
@Override
|
||||
public LoadingCache<String, Node> apply(InputStream source) {
|
||||
public LoadingCache<String, Node> apply(ByteSource source) {
|
||||
|
||||
Constructor constructor = new Constructor(Config.class);
|
||||
|
||||
|
@ -93,7 +97,16 @@ public class NodesFromYamlStream implements Function<InputStream, LoadingCache<S
|
|||
// non-deprecated
|
||||
// constructor
|
||||
Yaml yaml = new Yaml(new Loader(constructor));
|
||||
Config config = (Config) yaml.load(source);
|
||||
Config config;
|
||||
InputStream in = null;
|
||||
try {
|
||||
in = source.openStream();
|
||||
config = (Config) yaml.load(in);
|
||||
} catch (IOException ioe) {
|
||||
throw Throwables.propagate(ioe);
|
||||
} finally {
|
||||
Closeables.closeQuietly(in);
|
||||
}
|
||||
checkState(config != null, "missing config: class");
|
||||
checkState(config.nodes != null, "missing nodes: collection");
|
||||
|
||||
|
|
|
@ -19,8 +19,6 @@ package org.jclouds.byon.suppliers;
|
|||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static com.google.common.base.Preconditions.checkState;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
|
@ -32,6 +30,7 @@ import org.jclouds.logging.Logger;
|
|||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Supplier;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
import com.google.common.io.ByteSource;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -42,18 +41,18 @@ public class NodesParsedFromSupplier implements Supplier<LoadingCache<String, No
|
|||
@Resource
|
||||
protected Logger logger = Logger.NULL;
|
||||
|
||||
private final Supplier<InputStream> supplier;
|
||||
private final Function<InputStream, LoadingCache<String, Node>> parser;
|
||||
private final ByteSource supplier;
|
||||
private final Function<ByteSource, LoadingCache<String, Node>> parser;
|
||||
|
||||
@Inject
|
||||
NodesParsedFromSupplier(@Provider Supplier<InputStream> supplier, Function<InputStream, LoadingCache<String, Node>> parser) {
|
||||
NodesParsedFromSupplier(@Provider ByteSource supplier, Function<ByteSource, LoadingCache<String, Node>> parser) {
|
||||
this.supplier = checkNotNull(supplier, "supplier");
|
||||
this.parser = checkNotNull(parser, "parser");
|
||||
}
|
||||
|
||||
@Override
|
||||
public LoadingCache<String, Node> get() {
|
||||
LoadingCache<String, Node> nodes = parser.apply(supplier.get());
|
||||
LoadingCache<String, Node> nodes = parser.apply(supplier);
|
||||
checkState(nodes != null && nodes.size() > 0, "no nodes parsed from supplier: %s", supplier);
|
||||
return nodes;
|
||||
}
|
||||
|
|
|
@ -33,13 +33,14 @@ import com.google.common.base.Function;
|
|||
import com.google.common.base.Supplier;
|
||||
import com.google.common.base.Suppliers;
|
||||
import com.google.common.base.Throwables;
|
||||
import com.google.common.io.ByteSource;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.name.Named;
|
||||
|
||||
/**
|
||||
* @author Adrian Cole
|
||||
*/
|
||||
public class SupplyFromProviderURIOrNodesProperty implements Supplier<InputStream>, Function<URI, InputStream> {
|
||||
public class SupplyFromProviderURIOrNodesProperty extends ByteSource implements Function<URI, InputStream> {
|
||||
@Resource
|
||||
protected Logger logger = Logger.NULL;
|
||||
private final Supplier<URI> url;
|
||||
|
@ -60,7 +61,7 @@ public class SupplyFromProviderURIOrNodesProperty implements Supplier<InputStrea
|
|||
}
|
||||
|
||||
@Override
|
||||
public InputStream get() {
|
||||
public InputStream openStream() {
|
||||
if (nodes != null)
|
||||
return Strings2.toInputStream(nodes);
|
||||
return apply(url.get());
|
||||
|
|
|
@ -20,7 +20,6 @@ import static org.testng.Assert.assertEquals;
|
|||
import static org.testng.Assert.fail;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
|
||||
import org.jclouds.byon.Node;
|
||||
|
@ -29,18 +28,18 @@ import org.testng.annotations.DataProvider;
|
|||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.common.base.Functions;
|
||||
import com.google.common.base.Supplier;
|
||||
import com.google.common.base.Suppliers;
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.google.common.cache.CacheLoader;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.io.ByteSource;
|
||||
import com.google.common.util.concurrent.UncheckedExecutionException;
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
import com.google.inject.Key;
|
||||
import com.google.inject.TypeLiteral;
|
||||
import com.google.inject.util.Providers;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -110,8 +109,7 @@ public class CacheNodeStoreModuleTest {
|
|||
|
||||
@Override
|
||||
public void configure() {
|
||||
bind(new TypeLiteral<Supplier<InputStream>>() {
|
||||
}).annotatedWith(Provider.class).toInstance(Suppliers.<InputStream> ofInstance(null));
|
||||
bind(ByteSource.class).annotatedWith(Provider.class).toProvider(Providers.<ByteSource>of(null));
|
||||
}
|
||||
|
||||
});
|
||||
|
@ -122,8 +120,7 @@ public class CacheNodeStoreModuleTest {
|
|||
|
||||
@Override
|
||||
public void configure() {
|
||||
bind(new TypeLiteral<Supplier<InputStream>>() {
|
||||
}).annotatedWith(Provider.class).toInstance(Suppliers.<InputStream> ofInstance(null));
|
||||
bind(ByteSource.class).annotatedWith(Provider.class).toProvider(Providers.<ByteSource>of(null));
|
||||
}
|
||||
|
||||
});
|
||||
|
|
|
@ -19,24 +19,19 @@ package org.jclouds.byon.config;
|
|||
import static org.testng.Assert.assertEquals;
|
||||
import static org.testng.Assert.fail;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.jclouds.byon.Node;
|
||||
import org.jclouds.io.CopyInputStreamInputSupplierMap;
|
||||
import org.jclouds.location.Provider;
|
||||
import org.jclouds.util.Strings2;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import com.google.common.base.Supplier;
|
||||
import com.google.common.base.Suppliers;
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
import com.google.common.io.InputSupplier;
|
||||
import com.google.common.io.ByteSource;
|
||||
import com.google.common.util.concurrent.UncheckedExecutionException;
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Guice;
|
||||
|
@ -44,6 +39,7 @@ import com.google.inject.Injector;
|
|||
import com.google.inject.Key;
|
||||
import com.google.inject.TypeLiteral;
|
||||
import com.google.inject.name.Names;
|
||||
import com.google.inject.util.Providers;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -62,15 +58,15 @@ public class YamlNodeStoreModuleTest {
|
|||
@Test(dataProvider = "names")
|
||||
public void deleteObject(String id, String name) throws InterruptedException, IOException {
|
||||
Injector injector = createInjector();
|
||||
Map<String, InputStream> map = getMap(injector);
|
||||
Map<String, ByteSource> map = getMap(injector);
|
||||
check(map, getStore(injector), "i-20312", id, name);
|
||||
}
|
||||
|
||||
public void testProvidedMapWithValue() throws IOException {
|
||||
Map<String, InputStream> map = new CopyInputStreamInputSupplierMap(
|
||||
new ConcurrentHashMap<String, InputSupplier<InputStream>>());
|
||||
Map<String, ByteSource> map =
|
||||
new ConcurrentHashMap<String, ByteSource>();
|
||||
|
||||
map.put("test", new ByteArrayInputStream("id: instance1\nname: instancename\n".getBytes()));
|
||||
map.put("test", ByteSource.wrap("id: instance1\nname: instancename\n".getBytes()));
|
||||
checkConsistent(map, getStore(createInjectorWithProvidedMap(map)), "test", "instance1", "instancename");
|
||||
checkConsistent(map, getStore(createInjectorWithProvidedMap(map)), "test", "instance1", "instancename");
|
||||
remove(map, getStore(createInjectorWithProvidedMap(map)), "test");
|
||||
|
@ -78,11 +74,11 @@ public class YamlNodeStoreModuleTest {
|
|||
}
|
||||
|
||||
public void testProvidedConsistentAcrossRepeatedWrites() throws IOException {
|
||||
Map<String, InputStream> map = new CopyInputStreamInputSupplierMap(
|
||||
new ConcurrentHashMap<String, InputSupplier<InputStream>>());
|
||||
Map<String, ByteSource> map =
|
||||
new ConcurrentHashMap<String, ByteSource>();
|
||||
|
||||
Injector injector = createInjectorWithProvidedMap(map);
|
||||
assertEquals(injector.getInstance(Key.get(new TypeLiteral<Map<String, InputStream>>() {
|
||||
assertEquals(injector.getInstance(Key.get(new TypeLiteral<Map<String, ByteSource>>() {
|
||||
}, Names.named("yaml"))), map);
|
||||
LoadingCache<String, Node> store = getStore(injector);
|
||||
|
||||
|
@ -92,8 +88,8 @@ public class YamlNodeStoreModuleTest {
|
|||
}
|
||||
|
||||
public void testProvidedConsistentAcrossMultipleInjectors() throws IOException {
|
||||
Map<String, InputStream> map = new CopyInputStreamInputSupplierMap(
|
||||
new ConcurrentHashMap<String, InputSupplier<InputStream>>());
|
||||
Map<String, ByteSource> map =
|
||||
new ConcurrentHashMap<String, ByteSource>();
|
||||
|
||||
put(map, getStore(createInjectorWithProvidedMap(map)), "test", "instance1", "instancename");
|
||||
checkConsistent(map, getStore(createInjectorWithProvidedMap(map)), "test", "instance1", "instancename");
|
||||
|
@ -103,7 +99,7 @@ public class YamlNodeStoreModuleTest {
|
|||
}
|
||||
|
||||
public void testDefaultConsistentAcrossMultipleInjectors() throws IOException {
|
||||
Map<String, InputStream> map = getMap(createInjector());
|
||||
Map<String, ByteSource> map = getMap(createInjector());
|
||||
|
||||
put(map, getStore(createInjector()), "test", "instance1", "instancename");
|
||||
|
||||
|
@ -118,18 +114,17 @@ public class YamlNodeStoreModuleTest {
|
|||
}));
|
||||
}
|
||||
|
||||
protected Map<String, InputStream> getMap(Injector injector) {
|
||||
return injector.getInstance(Key.get(new TypeLiteral<Map<String, InputStream>>() {
|
||||
protected Map<String, ByteSource> getMap(Injector injector) {
|
||||
return injector.getInstance(Key.get(new TypeLiteral<Map<String, ByteSource>>() {
|
||||
}, Names.named("yaml")));
|
||||
}
|
||||
|
||||
protected Injector createInjectorWithProvidedMap(Map<String, InputStream> map) {
|
||||
protected Injector createInjectorWithProvidedMap(Map<String, ByteSource> map) {
|
||||
return Guice.createInjector(new YamlNodeStoreModule(map), new AbstractModule() {
|
||||
|
||||
@Override
|
||||
protected void configure() {
|
||||
bind(new TypeLiteral<Supplier<InputStream>>() {
|
||||
}).annotatedWith(Provider.class).toInstance(Suppliers.<InputStream> ofInstance(null));
|
||||
bind(ByteSource.class).annotatedWith(Provider.class).toProvider(Providers.<ByteSource>of(null));
|
||||
}
|
||||
|
||||
});
|
||||
|
@ -140,21 +135,20 @@ public class YamlNodeStoreModuleTest {
|
|||
|
||||
@Override
|
||||
protected void configure() {
|
||||
bind(new TypeLiteral<Supplier<InputStream>>() {
|
||||
}).annotatedWith(Provider.class).toInstance(Suppliers.<InputStream> ofInstance(null));
|
||||
bind(ByteSource.class).annotatedWith(Provider.class).toProvider(Providers.<ByteSource>of(null));
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
protected void check(Map<String, InputStream> map, LoadingCache<String, Node> store, String key, String id, String name)
|
||||
protected void check(Map<String, ByteSource> map, LoadingCache<String, Node> store, String key, String id, String name)
|
||||
throws IOException {
|
||||
put(map, store, key, id, name);
|
||||
checkConsistent(map, store, key, id, name);
|
||||
remove(map, store, key);
|
||||
}
|
||||
|
||||
protected void remove(Map<String, InputStream> map, LoadingCache<String, Node> store, String key) {
|
||||
protected void remove(Map<String, ByteSource> map, LoadingCache<String, Node> store, String key) {
|
||||
store.invalidate(key);
|
||||
assertEquals(store.size(), 0);
|
||||
map.remove(key);
|
||||
|
@ -168,7 +162,7 @@ public class YamlNodeStoreModuleTest {
|
|||
assertEquals(map.get(key), null);
|
||||
}
|
||||
|
||||
protected void checkConsistent(Map<String, InputStream> map, LoadingCache<String, Node> store, String key, String id,
|
||||
protected void checkConsistent(Map<String, ByteSource> map, LoadingCache<String, Node> store, String key, String id,
|
||||
String name) throws IOException {
|
||||
assertEquals(map.size(), 1);
|
||||
if (store.size() == 0)
|
||||
|
@ -182,14 +176,14 @@ public class YamlNodeStoreModuleTest {
|
|||
checkToYaml(map, key, id, name);
|
||||
}
|
||||
|
||||
protected void checkToYaml(Map<String, InputStream> map, String key, String id, String name) throws IOException {
|
||||
assertEquals(Strings2.toStringAndClose(map.get(key)), String.format("id: %s\nname: %s\n", id, name));
|
||||
protected void checkToYaml(Map<String, ByteSource> map, String key, String id, String name) throws IOException {
|
||||
assertEquals(map.get(key).asCharSource(Charsets.UTF_8).read(), String.format("id: %s\nname: %s\n", id, name));
|
||||
}
|
||||
|
||||
protected void put(Map<String, InputStream> map, LoadingCache<String, Node> store, String key, String id, String name) {
|
||||
protected void put(Map<String, ByteSource> map, LoadingCache<String, Node> store, String key, String id, String name) {
|
||||
assertEquals(store.size(), 0);
|
||||
assertEquals(map.size(), 0);
|
||||
map.put(key, new ByteArrayInputStream(String.format("id: %s\nname: %s\n", id, name).getBytes()));
|
||||
map.put(key, ByteSource.wrap(String.format("id: %s\nname: %s\n", id, name).getBytes()));
|
||||
store.getUnchecked(key);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,14 +18,13 @@ package org.jclouds.byon.functions;
|
|||
|
||||
import static org.testng.Assert.assertEquals;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.jclouds.byon.Node;
|
||||
import org.jclouds.util.Strings2;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.io.ByteSource;
|
||||
import com.google.common.io.Resources;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -53,33 +52,33 @@ public class NodesFromYamlTest {
|
|||
@Test
|
||||
public void testNodesParse() throws Exception {
|
||||
|
||||
InputStream is = getClass().getResourceAsStream("/test1.yaml");
|
||||
ByteSource byteSource = Resources.asByteSource(Resources.getResource("test1.yaml"));
|
||||
NodesFromYamlStream parser = new NodesFromYamlStream();
|
||||
|
||||
assertEquals(parser.apply(is).asMap(), ImmutableMap.of(TEST1.getId(), TEST1));
|
||||
assertEquals(parser.apply(byteSource).asMap(), ImmutableMap.of(TEST1.getId(), TEST1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNodesParseLocation() throws Exception {
|
||||
|
||||
InputStream is = getClass().getResourceAsStream("/test_location.yaml");
|
||||
ByteSource byteSource = Resources.asByteSource(Resources.getResource("test_location.yaml"));
|
||||
NodesFromYamlStream parser = new NodesFromYamlStream();
|
||||
|
||||
assertEquals(parser.apply(is).asMap(), ImmutableMap.of(TEST2.getId(), TEST2, TEST3.getId(), TEST3));
|
||||
assertEquals(parser.apply(byteSource).asMap(), ImmutableMap.of(TEST2.getId(), TEST2, TEST3.getId(), TEST3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNodesParseWhenCredentialInUrl() throws Exception {
|
||||
|
||||
InputStream is = getClass().getResourceAsStream("/test_with_url.yaml");
|
||||
ByteSource byteSource = Resources.asByteSource(Resources.getResource("test_with_url.yaml"));
|
||||
NodesFromYamlStream parser = new NodesFromYamlStream();
|
||||
|
||||
assertEquals(parser.apply(is).asMap(), ImmutableMap.of(TEST1.getId(), TEST1));
|
||||
assertEquals(parser.apply(byteSource).asMap(), ImmutableMap.of(TEST1.getId(), TEST1));
|
||||
}
|
||||
|
||||
@Test(expectedExceptions = IllegalStateException.class)
|
||||
public void testMustParseSomething() throws Exception {
|
||||
new NodesFromYamlStream().apply(Strings2.toInputStream(""));
|
||||
new NodesFromYamlStream().apply(ByteSource.empty());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -17,10 +17,10 @@
|
|||
package org.jclouds.byon.suppliers;
|
||||
|
||||
import org.jclouds.byon.functions.NodesFromYamlStream;
|
||||
import org.jclouds.util.Strings2;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.common.base.Suppliers;
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.io.ByteSource;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -31,7 +31,7 @@ public class NodesParsedFromSupplierTest {
|
|||
@Test(expectedExceptions = IllegalStateException.class)
|
||||
public void testMustParseSomething() throws Exception {
|
||||
|
||||
new NodesParsedFromSupplier(Suppliers.ofInstance(Strings2.toInputStream("nodes:\n")), new NodesFromYamlStream()).get();
|
||||
new NodesParsedFromSupplier(ByteSource.wrap("nodes:\n".getBytes(Charsets.UTF_8)), new NodesFromYamlStream()).get();
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,6 +20,8 @@ import static org.testng.Assert.assertEquals;
|
|||
|
||||
import java.net.URI;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
|
||||
import org.jclouds.util.Strings2;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
|
@ -36,7 +38,7 @@ public class SupplyFromProviderURIOrNodesPropertyTest {
|
|||
SupplyFromProviderURIOrNodesProperty supplier = new SupplyFromProviderURIOrNodesProperty(URI.create("file://"
|
||||
+ path));
|
||||
|
||||
assertEquals(Strings2.toStringAndClose(supplier.get()), Strings2.toStringAndClose(getClass().getResourceAsStream(
|
||||
assertEquals(supplier.asCharSource(Charsets.UTF_8).read(), Strings2.toStringAndClose(getClass().getResourceAsStream(
|
||||
"/test1.yaml")));
|
||||
|
||||
}
|
||||
|
@ -47,7 +49,7 @@ public class SupplyFromProviderURIOrNodesPropertyTest {
|
|||
SupplyFromProviderURIOrNodesProperty supplier = new SupplyFromProviderURIOrNodesProperty(URI
|
||||
.create("classpath:///test1.yaml"));
|
||||
|
||||
assertEquals(Strings2.toStringAndClose(supplier.get()), Strings2.toStringAndClose(getClass().getResourceAsStream(
|
||||
assertEquals(supplier.asCharSource(Charsets.UTF_8).read(), Strings2.toStringAndClose(getClass().getResourceAsStream(
|
||||
"/test1.yaml")));
|
||||
|
||||
}
|
||||
|
@ -58,7 +60,7 @@ public class SupplyFromProviderURIOrNodesPropertyTest {
|
|||
SupplyFromProviderURIOrNodesProperty supplier = new SupplyFromProviderURIOrNodesProperty(URI.create("file://bar"));
|
||||
supplier.nodes = Strings2.toStringAndClose(getClass().getResourceAsStream("/test1.yaml"));
|
||||
|
||||
assertEquals(Strings2.toStringAndClose(supplier.get()), Strings2.toStringAndClose(getClass().getResourceAsStream(
|
||||
assertEquals(supplier.asCharSource(Charsets.UTF_8).read(), Strings2.toStringAndClose(getClass().getResourceAsStream(
|
||||
"/test1.yaml")));
|
||||
|
||||
}
|
||||
|
@ -69,7 +71,7 @@ public class SupplyFromProviderURIOrNodesPropertyTest {
|
|||
SupplyFromProviderURIOrNodesProperty supplier = new SupplyFromProviderURIOrNodesProperty(URI.create("file://"
|
||||
+ path));
|
||||
for (int i = 0; i < 5; i++)
|
||||
assertEquals(Strings2.toStringAndClose(supplier.get()), Strings2.toStringAndClose(getClass()
|
||||
assertEquals(supplier.asCharSource(Charsets.UTF_8).read(), Strings2.toStringAndClose(getClass()
|
||||
.getResourceAsStream("/test1.yaml")));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,7 +16,6 @@
|
|||
*/
|
||||
package org.jclouds.compute.internal;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
|
@ -25,13 +24,12 @@ import org.jclouds.compute.ComputeServiceContext;
|
|||
import org.jclouds.compute.domain.TemplateBuilderSpec;
|
||||
import org.jclouds.domain.LoginCredentials;
|
||||
import org.jclouds.domain.LoginCredentials.Builder;
|
||||
import org.jclouds.io.CopyInputStreamInputSupplierMap;
|
||||
import org.jclouds.rest.config.CredentialStoreModule;
|
||||
|
||||
import com.google.common.base.Splitter;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.io.InputSupplier;
|
||||
import com.google.common.io.ByteSource;
|
||||
import com.google.inject.Module;
|
||||
import com.google.inject.util.Modules;
|
||||
|
||||
|
@ -44,8 +42,8 @@ public abstract class BaseGenericComputeServiceContextLiveTest<W extends Compute
|
|||
protected LoginCredentials loginCredentials = LoginCredentials.builder().user("root").build();
|
||||
|
||||
// isolate tests from eachother, as default credentialStore is static
|
||||
protected Module credentialStoreModule = new CredentialStoreModule(new CopyInputStreamInputSupplierMap(
|
||||
new ConcurrentHashMap<String, InputSupplier<InputStream>>()));
|
||||
protected Module credentialStoreModule = new CredentialStoreModule(
|
||||
new ConcurrentHashMap<String, ByteSource>());
|
||||
|
||||
@Override
|
||||
protected Properties setupProperties() {
|
||||
|
|
|
@ -20,7 +20,6 @@ import static org.jclouds.compute.util.ComputeServiceUtils.getCores;
|
|||
import static org.testng.Assert.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.NoSuchElementException;
|
||||
|
@ -39,7 +38,6 @@ import org.jclouds.compute.reference.ComputeServiceConstants;
|
|||
import org.jclouds.domain.Location;
|
||||
import org.jclouds.domain.LocationScope;
|
||||
import org.jclouds.domain.LoginCredentials;
|
||||
import org.jclouds.io.CopyInputStreamInputSupplierMap;
|
||||
import org.jclouds.json.Json;
|
||||
import org.jclouds.json.config.GsonModule;
|
||||
import org.jclouds.rest.config.CredentialStoreModule;
|
||||
|
@ -52,7 +50,7 @@ import com.google.common.base.Splitter;
|
|||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Sets;
|
||||
import com.google.common.io.InputSupplier;
|
||||
import com.google.common.io.ByteSource;
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Module;
|
||||
|
||||
|
@ -269,8 +267,8 @@ public abstract class BaseTemplateBuilderLiveTest extends BaseComputeServiceCont
|
|||
|
||||
protected void tryOverrideUsingPropertyKey(String propertyKey) {
|
||||
// isolate tests from eachother, as default credentialStore is static
|
||||
Module credentialStoreModule = new CredentialStoreModule(new CopyInputStreamInputSupplierMap(
|
||||
new ConcurrentHashMap<String, InputSupplier<InputStream>>()));
|
||||
Module credentialStoreModule = new CredentialStoreModule(
|
||||
new ConcurrentHashMap<String, ByteSource>());
|
||||
|
||||
ComputeServiceContext context = null;
|
||||
try {
|
||||
|
|
|
@ -1,164 +0,0 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jclouds.collect;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.AbstractMap;
|
||||
import java.util.AbstractSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Throwables;
|
||||
import com.google.common.io.InputSupplier;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author Adrian Cole
|
||||
*
|
||||
*/
|
||||
public class InputSupplierMap<K, V> extends AbstractMap<K, V> {
|
||||
private final Map<K, InputSupplier<V>> toMap;
|
||||
private final Function<V, InputSupplier<V>> putFunction;
|
||||
|
||||
public InputSupplierMap(Map<K, InputSupplier<V>> toMap, Function<V, InputSupplier<V>> putFunction) {
|
||||
this.toMap = checkNotNull(toMap);
|
||||
this.putFunction = checkNotNull(putFunction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return toMap.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public V put(K key, V value) {
|
||||
V old = get(key);
|
||||
toMap.put(key, value != null ? putFunction.apply(value) : null);
|
||||
return old;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsKey(Object key) {
|
||||
return toMap.containsKey(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V get(Object key) {
|
||||
InputSupplier<V> value = toMap.get(key);
|
||||
try {
|
||||
return value != null ? value.getInput() : null;
|
||||
} catch (IOException e) {
|
||||
throw Throwables.propagate(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public V remove(Object key) {
|
||||
try {
|
||||
return toMap.containsKey(key) ? toMap.remove(key).getInput() : null;
|
||||
} catch (IOException e) {
|
||||
throw Throwables.propagate(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
toMap.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Entry<K, V>> entrySet() {
|
||||
return new EntrySet();
|
||||
}
|
||||
|
||||
private class EntrySet extends AbstractSet<Entry<K, V>> {
|
||||
@Override
|
||||
public int size() {
|
||||
return InputSupplierMap.this.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Entry<K, V>> iterator() {
|
||||
final Iterator<java.util.Map.Entry<K, InputSupplier<V>>> mapIterator = toMap.entrySet().iterator();
|
||||
|
||||
return new Iterator<Entry<K, V>>() {
|
||||
public boolean hasNext() {
|
||||
return mapIterator.hasNext();
|
||||
}
|
||||
|
||||
public Entry<K, V> next() {
|
||||
final java.util.Map.Entry<K, InputSupplier<V>> entry = mapIterator.next();
|
||||
return new AbstractMapEntry<K, V>() {
|
||||
@Override
|
||||
public K getKey() {
|
||||
return entry.getKey();
|
||||
}
|
||||
|
||||
@Override
|
||||
public V getValue() {
|
||||
try {
|
||||
return entry.getValue().getInput();
|
||||
} catch (IOException e) {
|
||||
throw Throwables.propagate(e);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public void remove() {
|
||||
mapIterator.remove();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
toMap.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object o) {
|
||||
if (!(o instanceof Entry)) {
|
||||
return false;
|
||||
}
|
||||
Entry<?, ?> entry = (Entry<?, ?>) o;
|
||||
Object entryKey = entry.getKey();
|
||||
Object entryValue = entry.getValue();
|
||||
V mapValue = InputSupplierMap.this.get(entryKey);
|
||||
if (mapValue != null) {
|
||||
return mapValue.equals(entryValue);
|
||||
}
|
||||
return entryValue == null && containsKey(entryKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(Object o) {
|
||||
if (contains(o)) {
|
||||
Entry<?, ?> entry = (Entry<?, ?>) o;
|
||||
Object key = entry.getKey();
|
||||
toMap.remove(key);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jclouds.io;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import org.jclouds.collect.InputSupplierMap;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
import com.google.common.io.InputSupplier;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Adrian Cole
|
||||
*/
|
||||
@Beta
|
||||
public class CopyInputStreamInputSupplierMap extends InputSupplierMap<String, InputStream> {
|
||||
@Inject
|
||||
public CopyInputStreamInputSupplierMap(Map<String, InputSupplier<InputStream>> toMap,
|
||||
CopyInputStreamIntoSupplier putFunction) {
|
||||
super(toMap, putFunction);
|
||||
}
|
||||
|
||||
public CopyInputStreamInputSupplierMap(Map<String, InputSupplier<InputStream>> toMap) {
|
||||
super(toMap, new CopyInputStreamIntoSupplier());
|
||||
}
|
||||
|
||||
}
|
|
@ -1,62 +0,0 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jclouds.io;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.jclouds.logging.Logger;
|
||||
import org.jclouds.util.Closeables2;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import com.google.common.io.InputSupplier;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Adrian Cole
|
||||
*/
|
||||
@Beta
|
||||
public class CopyInputStreamIntoSupplier implements Function<InputStream, InputSupplier<InputStream>> {
|
||||
@Resource
|
||||
protected Logger logger = Logger.NULL;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public InputSupplier<InputStream> apply(InputStream from) {
|
||||
if (from == null)
|
||||
return new InputSupplier<InputStream>() {
|
||||
|
||||
@Override
|
||||
public InputStream getInput() throws IOException {
|
||||
return null;
|
||||
}
|
||||
|
||||
};
|
||||
try {
|
||||
return InputSupplier.class.cast(ByteStreams.newInputStreamSupplier(ByteStreams.toByteArray(from)));
|
||||
} catch (Exception e) {
|
||||
logger.warn(e, "ignoring problem retrieving credentials");
|
||||
return null;
|
||||
} finally {
|
||||
Closeables2.closeQuietly(from);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -18,7 +18,6 @@ package org.jclouds.rest.config;
|
|||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
|
@ -29,15 +28,14 @@ import javax.inject.Singleton;
|
|||
import org.jclouds.collect.TransformingMap;
|
||||
import org.jclouds.domain.Credentials;
|
||||
import org.jclouds.domain.LoginCredentials;
|
||||
import org.jclouds.io.CopyInputStreamInputSupplierMap;
|
||||
import org.jclouds.json.Json;
|
||||
import org.jclouds.logging.Logger;
|
||||
import org.jclouds.rest.ConfiguresCredentialStore;
|
||||
import org.jclouds.util.Strings2;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.io.InputSupplier;
|
||||
import com.google.common.io.ByteSource;
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Provides;
|
||||
import com.google.inject.TypeLiteral;
|
||||
|
@ -49,10 +47,10 @@ import com.google.inject.TypeLiteral;
|
|||
@Beta
|
||||
@ConfiguresCredentialStore
|
||||
public class CredentialStoreModule extends AbstractModule {
|
||||
private static final Map<String, InputSupplier<InputStream>> BACKING = new ConcurrentHashMap<String, InputSupplier<InputStream>>();
|
||||
private final Map<String, InputStream> backing;
|
||||
private static final Map<String, ByteSource> BACKING = new ConcurrentHashMap<String, ByteSource>();
|
||||
private final Map<String, ByteSource> backing;
|
||||
|
||||
public CredentialStoreModule(Map<String, InputStream> backing) {
|
||||
public CredentialStoreModule(Map<String, ByteSource> backing) {
|
||||
this.backing = backing;
|
||||
}
|
||||
|
||||
|
@ -62,33 +60,30 @@ public class CredentialStoreModule extends AbstractModule {
|
|||
|
||||
@Override
|
||||
protected void configure() {
|
||||
bind(new TypeLiteral<Function<Credentials, InputStream>>() {
|
||||
}).to(CredentialsToJsonInputStream.class);
|
||||
bind(new TypeLiteral<Function<InputStream, Credentials>>() {
|
||||
}).to(CredentialsFromJsonInputStream.class);
|
||||
bind(new TypeLiteral<Function<Credentials, ByteSource>>() {
|
||||
}).to(CredentialsToJsonByteSource.class);
|
||||
bind(new TypeLiteral<Function<ByteSource, Credentials>>() {
|
||||
}).to(CredentialsFromJsonByteSource.class);
|
||||
if (backing != null) {
|
||||
bind(new TypeLiteral<Map<String, InputStream>>() {
|
||||
bind(new TypeLiteral<Map<String, ByteSource>>() {
|
||||
}).toInstance(backing);
|
||||
} else {
|
||||
bind(new TypeLiteral<Map<String, InputSupplier<InputStream>>>() {
|
||||
bind(new TypeLiteral<Map<String, ByteSource>>() {
|
||||
}).toInstance(BACKING);
|
||||
bind(new TypeLiteral<Map<String, InputStream>>() {
|
||||
}).to(new TypeLiteral<CopyInputStreamInputSupplierMap>() {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Singleton
|
||||
public static class CredentialsToJsonInputStream implements Function<Credentials, InputStream> {
|
||||
public static class CredentialsToJsonByteSource implements Function<Credentials, ByteSource> {
|
||||
private final Json json;
|
||||
|
||||
@Inject
|
||||
CredentialsToJsonInputStream(Json json) {
|
||||
CredentialsToJsonByteSource(Json json) {
|
||||
this.json = json;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream apply(Credentials from) {
|
||||
public ByteSource apply(Credentials from) {
|
||||
checkNotNull(from, "inputCredentials");
|
||||
if (from instanceof LoginCredentials) {
|
||||
LoginCredentials login = LoginCredentials.class.cast(from);
|
||||
|
@ -98,9 +93,9 @@ public class CredentialStoreModule extends AbstractModule {
|
|||
val.privateKey = login.getPrivateKey();
|
||||
if (login.shouldAuthenticateSudo())
|
||||
val.authenticateSudo = login.shouldAuthenticateSudo();
|
||||
return Strings2.toInputStream(json.toJson(val));
|
||||
return ByteSource.wrap(json.toJson(val).getBytes(Charsets.UTF_8));
|
||||
}
|
||||
return Strings2.toInputStream(json.toJson(from));
|
||||
return ByteSource.wrap(json.toJson(from).getBytes(Charsets.UTF_8));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -112,21 +107,21 @@ public class CredentialStoreModule extends AbstractModule {
|
|||
}
|
||||
|
||||
@Singleton
|
||||
public static class CredentialsFromJsonInputStream implements Function<InputStream, Credentials> {
|
||||
public static class CredentialsFromJsonByteSource implements Function<ByteSource, Credentials> {
|
||||
@Resource
|
||||
protected Logger logger = Logger.NULL;
|
||||
|
||||
private final Json json;
|
||||
|
||||
@Inject
|
||||
CredentialsFromJsonInputStream(Json json) {
|
||||
CredentialsFromJsonByteSource(Json json) {
|
||||
this.json = json;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Credentials apply(InputStream from) {
|
||||
public Credentials apply(ByteSource from) {
|
||||
try {
|
||||
String creds = Strings2.toStringAndClose(checkNotNull(from));
|
||||
String creds = (checkNotNull(from)).asCharSource(Charsets.UTF_8).read();
|
||||
if (creds.indexOf("\"user\":") == -1) {
|
||||
return json.fromJson(creds, Credentials.class);
|
||||
} else {
|
||||
|
@ -143,10 +138,10 @@ public class CredentialStoreModule extends AbstractModule {
|
|||
|
||||
@Provides
|
||||
@Singleton
|
||||
protected Map<String, Credentials> provideCredentialStore(Map<String, InputStream> backing,
|
||||
Function<Credentials, InputStream> credentialsSerializer,
|
||||
Function<InputStream, Credentials> credentialsDeserializer) {
|
||||
return new TransformingMap<String, InputStream, Credentials>(backing, credentialsDeserializer,
|
||||
protected Map<String, Credentials> provideCredentialStore(Map<String, ByteSource> backing,
|
||||
Function<Credentials, ByteSource> credentialsSerializer,
|
||||
Function<ByteSource, Credentials> credentialsDeserializer) {
|
||||
return new TransformingMap<String, ByteSource, Credentials>(backing, credentialsDeserializer,
|
||||
credentialsSerializer);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,24 +20,21 @@ import static org.testng.Assert.assertEquals;
|
|||
import static org.testng.Assert.assertFalse;
|
||||
import static org.testng.Assert.assertTrue;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.jclouds.crypto.PemsTest;
|
||||
import org.jclouds.domain.Credentials;
|
||||
import org.jclouds.domain.LoginCredentials;
|
||||
import org.jclouds.io.CopyInputStreamInputSupplierMap;
|
||||
import org.jclouds.json.Json;
|
||||
import org.jclouds.json.config.GsonModule;
|
||||
import org.jclouds.rest.config.CredentialStoreModule;
|
||||
import org.jclouds.util.Strings2;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.common.io.InputSupplier;
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.io.ByteSource;
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
import com.google.inject.Key;
|
||||
|
@ -64,15 +61,14 @@ public class CredentialStoreModuleTest {
|
|||
@Test(dataProvider = "credentials")
|
||||
public void deleteObject(String identity, String credential) throws InterruptedException, IOException {
|
||||
Injector injector = createInjector();
|
||||
Map<String, InputStream> map = getMap(injector);
|
||||
Map<String, ByteSource> map = getMap(injector);
|
||||
check(map, getStore(injector), "i-20312", new Credentials(identity, credential));
|
||||
}
|
||||
|
||||
public void testProvidedMapWithValue() throws IOException {
|
||||
Map<String, InputStream> map = new CopyInputStreamInputSupplierMap(
|
||||
new ConcurrentHashMap<String, InputSupplier<InputStream>>());
|
||||
Map<String, ByteSource> map = new ConcurrentHashMap<String, ByteSource>();
|
||||
|
||||
map.put("test", new ByteArrayInputStream(json.toJson(new Credentials("user", "pass")).getBytes()));
|
||||
map.put("test", ByteSource.wrap(json.toJson(new Credentials("user", "pass")).getBytes()));
|
||||
checkConsistent(map, getStore(createInjectorWithProvidedMap(map)), "test", new Credentials("user", "pass"));
|
||||
checkConsistent(map, getStore(createInjectorWithProvidedMap(map)), "test", new Credentials("user", "pass"));
|
||||
remove(map, getStore(createInjectorWithProvidedMap(map)), "test");
|
||||
|
@ -80,11 +76,10 @@ public class CredentialStoreModuleTest {
|
|||
}
|
||||
|
||||
public void testProvidedConsistentAcrossRepeatedWrites() throws IOException {
|
||||
Map<String, InputStream> map = new CopyInputStreamInputSupplierMap(
|
||||
new ConcurrentHashMap<String, InputSupplier<InputStream>>());
|
||||
Map<String, ByteSource> map = new ConcurrentHashMap<String, ByteSource>();
|
||||
|
||||
Injector injector = createInjectorWithProvidedMap(map);
|
||||
assertEquals(injector.getInstance(Key.get(new TypeLiteral<Map<String, InputStream>>() {
|
||||
assertEquals(injector.getInstance(Key.get(new TypeLiteral<Map<String, ByteSource>>() {
|
||||
})), map);
|
||||
Map<String, Credentials> store = getStore(injector);
|
||||
|
||||
|
@ -94,8 +89,7 @@ public class CredentialStoreModuleTest {
|
|||
}
|
||||
|
||||
public void testProvidedConsistentAcrossMultipleInjectors() throws IOException {
|
||||
Map<String, InputStream> map = new CopyInputStreamInputSupplierMap(
|
||||
new ConcurrentHashMap<String, InputSupplier<InputStream>>());
|
||||
Map<String, ByteSource> map = new ConcurrentHashMap<String, ByteSource>();
|
||||
|
||||
put(map, getStore(createInjectorWithProvidedMap(map)), "test", new Credentials("user", "pass"));
|
||||
checkConsistent(map, getStore(createInjectorWithProvidedMap(map)), "test", new Credentials("user", "pass"));
|
||||
|
@ -105,7 +99,7 @@ public class CredentialStoreModuleTest {
|
|||
}
|
||||
|
||||
public void testDefaultConsistentAcrossMultipleInjectors() throws IOException {
|
||||
Map<String, InputStream> map = getMap(createInjector());
|
||||
Map<String, ByteSource> map = getMap(createInjector());
|
||||
|
||||
put(map, getStore(createInjector()), "test", new Credentials("user", "pass"));
|
||||
checkConsistent(map, getStore(createInjector()), "test", new Credentials("user", "pass"));
|
||||
|
@ -115,7 +109,7 @@ public class CredentialStoreModuleTest {
|
|||
}
|
||||
|
||||
public void testLoginConsistentAcrossMultipleInjectorsAndLooksNice() throws IOException {
|
||||
Map<String, InputStream> map = getMap(createInjector());
|
||||
Map<String, ByteSource> map = getMap(createInjector());
|
||||
LoginCredentials creds = LoginCredentials.builder().user("user").password("pass").build();
|
||||
put(map, getStore(createInjector()), "test", creds);
|
||||
checkConsistent(map, getStore(createInjector()), "test", creds, "{\"user\":\"user\",\"password\":\"pass\"}");
|
||||
|
@ -124,7 +118,7 @@ public class CredentialStoreModuleTest {
|
|||
}
|
||||
|
||||
public void testLoginConsistentAcrossMultipleInjectorsAndLooksNiceWithSudo() throws IOException {
|
||||
Map<String, InputStream> map = getMap(createInjector());
|
||||
Map<String, ByteSource> map = getMap(createInjector());
|
||||
LoginCredentials creds = LoginCredentials.builder().user("user").password("pass").authenticateSudo(true).build();
|
||||
put(map, getStore(createInjector()), "test", creds);
|
||||
checkConsistent(map, getStore(createInjector()), "test", creds,
|
||||
|
@ -139,12 +133,12 @@ public class CredentialStoreModuleTest {
|
|||
}));
|
||||
}
|
||||
|
||||
protected Map<String, InputStream> getMap(Injector injector) {
|
||||
return injector.getInstance(Key.get(new TypeLiteral<Map<String, InputStream>>() {
|
||||
protected Map<String, ByteSource> getMap(Injector injector) {
|
||||
return injector.getInstance(Key.get(new TypeLiteral<Map<String, ByteSource>>() {
|
||||
}));
|
||||
}
|
||||
|
||||
protected Injector createInjectorWithProvidedMap(Map<String, InputStream> map) {
|
||||
protected Injector createInjectorWithProvidedMap(Map<String, ByteSource> map) {
|
||||
return Guice.createInjector(new CredentialStoreModule(map), new GsonModule());
|
||||
}
|
||||
|
||||
|
@ -152,14 +146,14 @@ public class CredentialStoreModuleTest {
|
|||
return Guice.createInjector(new CredentialStoreModule(), new GsonModule());
|
||||
}
|
||||
|
||||
protected void check(Map<String, InputStream> map, Map<String, Credentials> store, String key, Credentials creds)
|
||||
protected void check(Map<String, ByteSource> map, Map<String, Credentials> store, String key, Credentials creds)
|
||||
throws IOException {
|
||||
put(map, store, key, creds);
|
||||
checkConsistent(map, store, key, creds);
|
||||
remove(map, store, key);
|
||||
}
|
||||
|
||||
protected void remove(Map<String, InputStream> map, Map<String, Credentials> store, String key) {
|
||||
protected void remove(Map<String, ByteSource> map, Map<String, Credentials> store, String key) {
|
||||
store.remove(key);
|
||||
assertEquals(store.size(), 0);
|
||||
assertEquals(map.size(), 0);
|
||||
|
@ -167,12 +161,12 @@ public class CredentialStoreModuleTest {
|
|||
assertEquals(map.get(key), null);
|
||||
}
|
||||
|
||||
protected void checkConsistent(Map<String, InputStream> map, Map<String, Credentials> store, String key,
|
||||
protected void checkConsistent(Map<String, ByteSource> map, Map<String, Credentials> store, String key,
|
||||
Credentials creds) throws IOException {
|
||||
checkConsistent(map, store, key, creds, json.toJson(creds));
|
||||
}
|
||||
|
||||
protected void checkConsistent(Map<String, InputStream> map, Map<String, Credentials> store, String key,
|
||||
protected void checkConsistent(Map<String, ByteSource> map, Map<String, Credentials> store, String key,
|
||||
Credentials creds, String expected) throws IOException {
|
||||
assertEquals(store.size(), 1);
|
||||
assertEquals(map.size(), 1);
|
||||
|
@ -188,11 +182,11 @@ public class CredentialStoreModuleTest {
|
|||
checkToJson(map, key, expected);
|
||||
}
|
||||
|
||||
protected void checkToJson(Map<String, InputStream> map, String key, String expected) throws IOException {
|
||||
assertEquals(Strings2.toStringAndClose(map.get(key)), expected);
|
||||
protected void checkToJson(Map<String, ByteSource> map, String key, String expected) throws IOException {
|
||||
assertEquals(map.get(key).asCharSource(Charsets.UTF_8).read(), expected);
|
||||
}
|
||||
|
||||
protected void put(Map<String, InputStream> map, Map<String, Credentials> store, String key, Credentials creds) {
|
||||
protected void put(Map<String, ByteSource> map, Map<String, Credentials> store, String key, Credentials creds) {
|
||||
assertEquals(store.size(), 0);
|
||||
assertEquals(map.size(), 0);
|
||||
assertFalse(store.containsKey(key));
|
||||
|
|
|
@ -25,7 +25,6 @@ import static org.jclouds.Constants.PROPERTY_USER_THREADS;
|
|||
import static org.testng.Assert.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
@ -63,7 +62,6 @@ import org.jclouds.http.internal.BaseHttpCommandExecutorService;
|
|||
import org.jclouds.http.internal.HttpWire;
|
||||
import org.jclouds.io.ContentMetadataCodec;
|
||||
import org.jclouds.io.ContentMetadataCodec.DefaultContentMetadataCodec;
|
||||
import org.jclouds.io.CopyInputStreamInputSupplierMap;
|
||||
import org.jclouds.io.Payload;
|
||||
import org.jclouds.io.Payloads;
|
||||
import org.jclouds.logging.config.NullLoggingModule;
|
||||
|
@ -80,7 +78,7 @@ import com.google.common.base.Throwables;
|
|||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.io.InputSupplier;
|
||||
import com.google.common.io.ByteSource;
|
||||
import com.google.common.util.concurrent.ListeningExecutorService;
|
||||
import com.google.common.util.concurrent.SimpleTimeLimiter;
|
||||
import com.google.common.util.concurrent.TimeLimiter;
|
||||
|
@ -566,9 +564,9 @@ public abstract class BaseRestApiExpectTest<S> {
|
|||
throw new UnsupportedOperationException("unsupported base type: " + am);
|
||||
}
|
||||
// isolate tests from eachother, as default credentialStore is static
|
||||
return builder.credentials(identity, credential).modules(
|
||||
ImmutableSet.of(new ExpectModule(fn), new NullLoggingModule(), new CredentialStoreModule(new CopyInputStreamInputSupplierMap(
|
||||
new ConcurrentHashMap<String, InputSupplier<InputStream>>())), module)).overrides(props)
|
||||
return builder.credentials(identity, credential)
|
||||
.modules(ImmutableSet.of(new ExpectModule(fn), new NullLoggingModule(), new CredentialStoreModule(new ConcurrentHashMap<String, ByteSource>()), module))
|
||||
.overrides(props)
|
||||
.buildInjector();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue