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

COVERAGE SUMMARY FOR SOURCE FILE [CredentialsForInstance.java]

nameclass, %method, %block, %line, %
CredentialsForInstance.java0%   (0/1)0%   (0/4)0%   (0/82)0%   (0/12)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CredentialsForInstance0%   (0/1)0%   (0/4)0%   (0/82)0%   (0/12)
CredentialsForInstance (ConcurrentMap, PopulateDefaultLoginCredentialsForImag... 0%   (0/1)0%   (0/18)0%   (0/5)
getLoginAccountFor (RunningInstance): String 0%   (0/1)0%   (0/28)0%   (0/1)
getPrivateKeyOrNull (RunningInstance): String 0%   (0/1)0%   (0/19)0%   (0/2)
load (RunningInstance): Credentials 0%   (0/1)0%   (0/17)0%   (0/4)

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.ec2.compute.functions;
20 
21import static com.google.common.base.Preconditions.checkNotNull;
22 
23import java.util.concurrent.ConcurrentMap;
24import java.util.concurrent.ExecutionException;
25 
26import javax.inject.Inject;
27import javax.inject.Singleton;
28 
29import org.jclouds.compute.domain.Image;
30import org.jclouds.compute.strategy.PopulateDefaultLoginCredentialsForImageStrategy;
31import org.jclouds.domain.Credentials;
32import org.jclouds.ec2.compute.domain.RegionAndName;
33import org.jclouds.ec2.domain.KeyPair;
34import org.jclouds.ec2.domain.RunningInstance;
35 
36import com.google.common.annotations.VisibleForTesting;
37import com.google.common.base.Supplier;
38import com.google.common.cache.Cache;
39import com.google.common.cache.CacheLoader;
40 
41/**
42 * 
43 * @author Adrian Cole
44 */
45@Singleton
46public class CredentialsForInstance extends CacheLoader<RunningInstance, Credentials> {
47   private final ConcurrentMap<RegionAndName, KeyPair> credentialsMap;
48   private final PopulateDefaultLoginCredentialsForImageStrategy credentialProvider;
49   private final Supplier<Cache<RegionAndName, ? extends Image>> imageMap;
50 
51   @Inject
52   CredentialsForInstance(ConcurrentMap<RegionAndName, KeyPair> credentialsMap,
53         PopulateDefaultLoginCredentialsForImageStrategy credentialProvider, Supplier<Cache<RegionAndName, ? extends Image>> imageMap) {
54      this.credentialsMap = checkNotNull(credentialsMap, "credentialsMap");
55      this.credentialProvider = checkNotNull(credentialProvider, "credentialProvider");
56      this.imageMap = imageMap;
57   }
58 
59   @Override
60   public Credentials load(RunningInstance instance) throws ExecutionException {
61      Credentials credentials = null;// default if no keypair exists
62      if (instance.getKeyName() != null) {
63         credentials = new Credentials(getLoginAccountFor(instance), getPrivateKeyOrNull(instance));
64      }
65      return credentials;
66   }
67 
68   @VisibleForTesting
69   String getPrivateKeyOrNull(RunningInstance instance) throws ExecutionException {
70      KeyPair keyPair = credentialsMap.get(new RegionAndName(instance.getRegion(), instance.getKeyName()));
71      return keyPair != null ? keyPair.getKeyMaterial() : null;
72   }
73 
74   @VisibleForTesting
75   String getLoginAccountFor(RunningInstance from) throws ExecutionException {
76      return checkNotNull(
77            credentialProvider.execute(imageMap.get().get(new RegionAndName(from.getRegion(), from.getImageId()))),
78            "login from image: " + from.getImageId()).identity;
79   }
80}

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