| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.byon.config; |
| 20 | |
| 21 | import java.io.InputStream; |
| 22 | import java.util.Map; |
| 23 | import java.util.concurrent.ConcurrentHashMap; |
| 24 | |
| 25 | import javax.inject.Inject; |
| 26 | import javax.inject.Named; |
| 27 | import javax.inject.Singleton; |
| 28 | |
| 29 | import org.jclouds.byon.Node; |
| 30 | import org.jclouds.byon.domain.YamlNode; |
| 31 | import org.jclouds.byon.functions.NodesFromYamlStream; |
| 32 | import org.jclouds.byon.suppliers.NodesParsedFromSupplier; |
| 33 | import org.jclouds.collect.TransformingMap; |
| 34 | import org.jclouds.io.CopyInputStreamInputSupplierMap; |
| 35 | import org.jclouds.io.CopyInputStreamIntoSupplier; |
| 36 | |
| 37 | import com.google.common.annotations.Beta; |
| 38 | import com.google.common.base.Function; |
| 39 | import com.google.common.base.Functions; |
| 40 | import com.google.common.base.Supplier; |
| 41 | import com.google.common.cache.Cache; |
| 42 | import com.google.common.cache.CacheBuilder; |
| 43 | import com.google.common.cache.CacheLoader; |
| 44 | import com.google.common.io.InputSupplier; |
| 45 | import com.google.inject.AbstractModule; |
| 46 | import com.google.inject.Provides; |
| 47 | import com.google.inject.TypeLiteral; |
| 48 | import com.google.inject.name.Names; |
| 49 | |
| 50 | /** |
| 51 | * |
| 52 | * @author Adrian Cole |
| 53 | */ |
| 54 | @ConfiguresNodeStore |
| 55 | @Beta |
| 56 | public class YamlNodeStoreModule extends AbstractModule { |
| 57 | private static final Map<String, InputSupplier<InputStream>> BACKING = new ConcurrentHashMap<String, InputSupplier<InputStream>>(); |
| 58 | private final Map<String, InputStream> backing; |
| 59 | |
| 60 | public YamlNodeStoreModule(Map<String, InputStream> backing) { |
| 61 | this.backing = backing; |
| 62 | } |
| 63 | |
| 64 | public YamlNodeStoreModule() { |
| 65 | this(null); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | protected void configure() { |
| 70 | bind(new TypeLiteral<Supplier<Cache<String, Node>>>() { |
| 71 | }).to(NodesParsedFromSupplier.class); |
| 72 | bind(new TypeLiteral<Function<InputStream, Cache<String, Node>>>() { |
| 73 | }).to(NodesFromYamlStream.class); |
| 74 | bind(new TypeLiteral<Function<YamlNode, InputStream>>() { |
| 75 | }).toInstance(org.jclouds.byon.domain.YamlNode.yamlNodeToInputStream); |
| 76 | bind(new TypeLiteral<Function<InputStream, YamlNode>>() { |
| 77 | }).toInstance(org.jclouds.byon.domain.YamlNode.inputStreamToYamlNode); |
| 78 | bind(new TypeLiteral<Function<Node, YamlNode>>() { |
| 79 | }).toInstance(org.jclouds.byon.domain.YamlNode.nodeToYamlNode); |
| 80 | bind(new TypeLiteral<Function<YamlNode, Node>>() { |
| 81 | }).toInstance(org.jclouds.byon.domain.YamlNode.toNode); |
| 82 | if (backing != null) { |
| 83 | bind(new TypeLiteral<Map<String, InputStream>>() { |
| 84 | }).annotatedWith(Names.named("yaml")).toInstance(backing); |
| 85 | } else { |
| 86 | bind(new TypeLiteral<Map<String, InputSupplier<InputStream>>>() { |
| 87 | }).annotatedWith(Names.named("yaml")).toInstance(BACKING); |
| 88 | bind(new TypeLiteral<Map<String, InputStream>>() { |
| 89 | }).annotatedWith(Names.named("yaml")).to(new TypeLiteral<YAMLCopyInputStreamInputSupplierMap>() { |
| 90 | }); |
| 91 | } |
| 92 | |
| 93 | } |
| 94 | |
| 95 | @Singleton |
| 96 | public static class YAMLCopyInputStreamInputSupplierMap extends CopyInputStreamInputSupplierMap { |
| 97 | @Inject |
| 98 | public YAMLCopyInputStreamInputSupplierMap(@Named("yaml") Map<String, InputSupplier<InputStream>> toMap, |
| 99 | CopyInputStreamIntoSupplier putFunction) { |
| 100 | super(toMap, putFunction); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | @Provides |
| 105 | @Singleton |
| 106 | protected Cache<String, Node> provideNodeStore(Map<String, YamlNode> backing, Function<Node, YamlNode> yamlSerializer, |
| 107 | Function<YamlNode, Node> yamlDeserializer) { |
| 108 | return CacheBuilder.newBuilder().build(CacheLoader.from(Functions.forMap(new TransformingMap<String, YamlNode, Node>(backing, yamlDeserializer, yamlSerializer)))); |
| 109 | } |
| 110 | |
| 111 | @Provides |
| 112 | @Singleton |
| 113 | protected Map<String, YamlNode> provideYamlStore(@Named("yaml") Map<String, InputStream> backing, |
| 114 | Function<YamlNode, InputStream> yamlSerializer, Function<InputStream, YamlNode> yamlDeserializer) { |
| 115 | return new TransformingMap<String, InputStream, YamlNode>(backing, yamlDeserializer, yamlSerializer); |
| 116 | } |
| 117 | } |