EMMA Coverage Report (generated Mon Oct 17 05:41:20 EDT 2011)
[all classes][org.jclouds.byon.functions]

COVERAGE SUMMARY FOR SOURCE FILE [NodesFromYamlStream.java]

nameclass, %method, %block, %line, %
NodesFromYamlStream.java100% (3/3)100% (5/5)100% (108/108)100% (20/20)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class NodesFromYamlStream100% (1/1)100% (2/2)100% (96/96)100% (18/18)
NodesFromYamlStream (): void 100% (1/1)100% (3/3)100% (2/2)
apply (InputStream): Cache 100% (1/1)100% (93/93)100% (16/16)
     
class NodesFromYamlStream$1100% (1/1)100% (2/2)100% (9/9)100% (2/2)
NodesFromYamlStream$1 (NodesFromYamlStream): void 100% (1/1)100% (6/6)100% (1/1)
apply (Node): String 100% (1/1)100% (3/3)100% (1/1)
     
class NodesFromYamlStream$Config100% (1/1)100% (1/1)100% (3/3)100% (1/1)
NodesFromYamlStream$Config (): void 100% (1/1)100% (3/3)100% (1/1)

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 */
19package org.jclouds.byon.functions;
20 
21import static com.google.common.base.Preconditions.checkState;
22 
23import java.io.InputStream;
24import java.util.List;
25import java.util.Map;
26 
27import javax.inject.Singleton;
28 
29import org.jclouds.byon.Node;
30import org.jclouds.byon.domain.YamlNode;
31import org.yaml.snakeyaml.Loader;
32import org.yaml.snakeyaml.TypeDescription;
33import org.yaml.snakeyaml.Yaml;
34import org.yaml.snakeyaml.constructor.Constructor;
35 
36import com.google.common.base.Function;
37import com.google.common.base.Functions;
38import com.google.common.cache.Cache;
39import com.google.common.cache.CacheBuilder;
40import com.google.common.cache.CacheLoader;
41import com.google.common.collect.Iterables;
42import com.google.common.collect.Maps;
43 
44/**
45 * Parses the following syntax.
46 * 
47 * <pre>
48 * nodes:
49 *     - id: cluster-1:
50 *       name: cluster-1
51 *       description: xyz
52 *       hostname: cluster-1.mydomain.com
53 *       location_id: virginia
54 *       os_arch: x86
55 *       os_family: linux
56 *       os_description: redhat
57 *       os_version: 5.3
58 *       group: hadoop
59 *       tags:
60 *           - vanilla
61 *       username: kelvin
62 *       credential: password_or_rsa
63 *         or
64 *       credential_url: password_or_rsa_file ex. resource:///id_rsa will get the classpath /id_rsa; file://path/to/id_rsa
65 *       sudo_password: password
66 * </pre>
67 * 
68 * @author Kelvin Kakugawa
69 * @author Adrian Cole
70 */
71@Singleton
72public class NodesFromYamlStream implements Function<InputStream, Cache<String, Node>> {
73 
74   /**
75    * Type-safe config class for YAML
76    * 
77    */
78   public static class Config {
79      public List<YamlNode> nodes;
80   }
81 
82   @Override
83   public Cache<String, Node> apply(InputStream source) {
84 
85      Constructor constructor = new Constructor(Config.class);
86 
87      TypeDescription nodeDesc = new TypeDescription(YamlNode.class);
88      nodeDesc.putListPropertyType("tags", String.class);
89      constructor.addTypeDescription(nodeDesc);
90 
91      TypeDescription configDesc = new TypeDescription(Config.class);
92      configDesc.putListPropertyType("nodes", YamlNode.class);
93      constructor.addTypeDescription(configDesc);
94      // note that snakeyaml also throws nosuchmethod error when you use the
95      // non-deprecated
96      // constructor
97      Yaml yaml = new Yaml(new Loader(constructor));
98      Config config = (Config) yaml.load(source);
99      checkState(config != null, "missing config: class");
100      checkState(config.nodes != null, "missing nodes: collection");
101 
102      Map<String, Node> backingMap = Maps.uniqueIndex(Iterables.transform(config.nodes, YamlNode.toNode),
103            new Function<Node, String>() {
104               public String apply(Node node) {
105                  return node.getId();
106               }
107            });
108      Cache<String, Node> cache = CacheBuilder.newBuilder().build(CacheLoader.from(Functions.forMap(backingMap)));
109      for (String node : backingMap.keySet())
110         cache.getUnchecked(node);
111      return cache;
112   }
113}

[all classes][org.jclouds.byon.functions]
EMMA 2.0.5312 (C) Vladimir Roubtsov