EMMA Coverage Report (generated Fri Apr 27 15:03:37 EDT 2012)
[all classes][org.jclouds.ec2.compute.functions]

COVERAGE SUMMARY FOR SOURCE FILE [CreateUniqueKeyPair.java]

nameclass, %method, %block, %line, %
CreateUniqueKeyPair.java100% (1/1)75%  (3/4)93%  (89/96)94%  (17/18)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CreateUniqueKeyPair100% (1/1)75%  (3/4)93%  (89/96)94%  (17/18)
apply (RegionAndName): KeyPair 0%   (0/1)0%   (0/7)0%   (0/1)
CreateUniqueKeyPair (EC2Client, Supplier): void 100% (1/1)100% (15/15)100% (6/6)
createNewKeyPairInRegion (String, String): KeyPair 100% (1/1)100% (51/51)100% (10/10)
getNextName (String, String): String 100% (1/1)100% (23/23)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.ec2.compute.functions;
20 
21import static com.google.common.base.Preconditions.checkNotNull;
22import static org.jclouds.compute.config.ComputeServiceProperties.RESOURCENAME_DELIMITER;
23 
24import javax.annotation.Resource;
25import javax.inject.Named;
26import javax.inject.Singleton;
27 
28import org.jclouds.compute.reference.ComputeServiceConstants;
29import org.jclouds.ec2.EC2Client;
30import org.jclouds.ec2.compute.domain.RegionAndName;
31import org.jclouds.ec2.domain.KeyPair;
32import org.jclouds.logging.Logger;
33 
34import com.google.common.annotations.VisibleForTesting;
35import com.google.common.base.Function;
36import com.google.common.base.Supplier;
37import com.google.inject.Inject;
38 
39/**
40 * 
41 * @author Adrian Cole
42 */
43@Singleton
44public class CreateUniqueKeyPair implements Function<RegionAndName, KeyPair> {
45   @Resource
46   @Named(ComputeServiceConstants.COMPUTE_LOGGER)
47   protected Logger logger = Logger.NULL;
48   protected final EC2Client ec2Client;
49   protected final Supplier<String> randomSuffix;
50 
51   @Inject
52   public CreateUniqueKeyPair(EC2Client ec2Client, Supplier<String> randomSuffix) {
53      this.ec2Client = ec2Client;
54      this.randomSuffix = randomSuffix;
55   }
56 
57   @Override
58   public KeyPair apply(RegionAndName from) {
59      return createNewKeyPairInRegion(from.getRegion(), from.getName());
60   }
61 
62   @VisibleForTesting
63   KeyPair createNewKeyPairInRegion(String region, String group) {
64      checkNotNull(region, "region");
65      checkNotNull(group, "group");
66      logger.debug(">> creating keyPair region(%s) group(%s)", region, group);
67      KeyPair keyPair = null;
68      while (keyPair == null) {
69         try {
70            keyPair = ec2Client.getKeyPairServices().createKeyPairInRegion(region, getNextName(region, group));
71            logger.debug("<< created keyPair(%s)", keyPair);
72         } catch (IllegalStateException e) {
73 
74         }
75      }
76      return keyPair;
77   }
78   
79   @Inject(optional=true)
80   @Named(RESOURCENAME_DELIMITER) 
81   char delimiter = '#';
82   
83   private String getNextName(String region, String group) {
84      return String.format("jclouds#%s#%s#%s", group, region, randomSuffix.get()).replace('#', delimiter);
85   }
86}

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