| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * 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, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 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.Singleton; |
| 26 | |
| 27 | import org.jclouds.byon.Node; |
| 28 | import org.jclouds.byon.domain.YamlNode; |
| 29 | import org.jclouds.collect.TransformingMap; |
| 30 | import org.jclouds.io.CopyInputStreamInputSupplierMap; |
| 31 | |
| 32 | import com.google.common.annotations.Beta; |
| 33 | import com.google.common.base.Function; |
| 34 | import com.google.common.io.InputSupplier; |
| 35 | import com.google.inject.AbstractModule; |
| 36 | import com.google.inject.Provides; |
| 37 | import com.google.inject.TypeLiteral; |
| 38 | |
| 39 | /** |
| 40 | * |
| 41 | * @author Adrian Cole |
| 42 | */ |
| 43 | @ConfiguresNodeStore |
| 44 | @Beta |
| 45 | public class YamlNodeStoreModule extends AbstractModule { |
| 46 | private static final Map<String, InputSupplier<InputStream>> BACKING = new ConcurrentHashMap<String, InputSupplier<InputStream>>(); |
| 47 | private final Map<String, InputStream> backing; |
| 48 | |
| 49 | public YamlNodeStoreModule(Map<String, InputStream> backing) { |
| 50 | this.backing = backing; |
| 51 | } |
| 52 | |
| 53 | public YamlNodeStoreModule() { |
| 54 | this(null); |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | protected void configure() { |
| 59 | bind(new TypeLiteral<Function<YamlNode, InputStream>>() { |
| 60 | }).toInstance(org.jclouds.byon.domain.YamlNode.yamlNodeToInputStream); |
| 61 | bind(new TypeLiteral<Function<InputStream, YamlNode>>() { |
| 62 | }).toInstance(org.jclouds.byon.domain.YamlNode.inputStreamToYamlNode); |
| 63 | bind(new TypeLiteral<Function<Node, YamlNode>>() { |
| 64 | }).toInstance(org.jclouds.byon.domain.YamlNode.nodeToYamlNode); |
| 65 | bind(new TypeLiteral<Function<YamlNode, Node>>() { |
| 66 | }).toInstance(org.jclouds.byon.domain.YamlNode.toNode); |
| 67 | if (backing != null) { |
| 68 | bind(new TypeLiteral<Map<String, InputStream>>() { |
| 69 | }).toInstance(backing); |
| 70 | } else { |
| 71 | bind(new TypeLiteral<Map<String, InputSupplier<InputStream>>>() { |
| 72 | }).toInstance(BACKING); |
| 73 | bind(new TypeLiteral<Map<String, InputStream>>() { |
| 74 | }).to(new TypeLiteral<CopyInputStreamInputSupplierMap>() { |
| 75 | }); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | @Provides |
| 80 | @Singleton |
| 81 | protected Map<String, Node> provideNodeStore(Map<String, YamlNode> backing, |
| 82 | Function<Node, YamlNode> yamlSerializer, Function<YamlNode, Node> yamlDeserializer) { |
| 83 | return new TransformingMap<String, YamlNode, Node>(backing, yamlDeserializer, yamlSerializer); |
| 84 | } |
| 85 | |
| 86 | @Provides |
| 87 | @Singleton |
| 88 | protected Map<String, YamlNode> provideYamlStore(Map<String, InputStream> backing, |
| 89 | Function<YamlNode, InputStream> yamlSerializer, Function<InputStream, YamlNode> yamlDeserializer) { |
| 90 | return new TransformingMap<String, InputStream, YamlNode>(backing, yamlDeserializer, yamlSerializer); |
| 91 | } |
| 92 | } |