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

COVERAGE SUMMARY FOR SOURCE FILE [CreateUniqueKeyPair.java]

nameclass, %method, %block, %line, %
CreateUniqueKeyPair.java100% (1/1)0%   (0/4)0%   (0/104)0%   (0/20)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CreateUniqueKeyPair100% (1/1)0%   (0/4)0%   (0/104)0%   (0/20)
CreateUniqueKeyPair (TerremarkVCloudClient, Supplier): void 0%   (0/1)0%   (0/12)0%   (0/5)
apply (OrgAndName): KeyPair 0%   (0/1)0%   (0/7)0%   (0/1)
createNewKeyPairInOrg (URI, String): KeyPair 0%   (0/1)0%   (0/69)0%   (0/13)
getNextName (String): String 0%   (0/1)0%   (0/16)0%   (0/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.trmk.vcloud_0_8.compute.functions;
20 
21import static com.google.common.base.Preconditions.checkNotNull;
22 
23import java.net.URI;
24 
25import javax.annotation.Resource;
26import javax.inject.Inject;
27import javax.inject.Named;
28import javax.inject.Singleton;
29 
30import org.jclouds.compute.reference.ComputeServiceConstants;
31import org.jclouds.http.HttpResponseException;
32import org.jclouds.logging.Logger;
33import org.jclouds.trmk.vcloud_0_8.TerremarkVCloudClient;
34import org.jclouds.trmk.vcloud_0_8.compute.domain.OrgAndName;
35import org.jclouds.trmk.vcloud_0_8.domain.KeyPair;
36import org.jclouds.util.Throwables2;
37 
38import com.google.common.base.Function;
39import com.google.common.base.Supplier;
40 
41/**
42 * 
43 * @author Adrian Cole
44 */
45@Singleton
46public class CreateUniqueKeyPair implements Function<OrgAndName, KeyPair> {
47   @Resource
48   @Named(ComputeServiceConstants.COMPUTE_LOGGER)
49   protected Logger logger = Logger.NULL;
50   protected final TerremarkVCloudClient trmkClient;
51   protected Supplier<String> randomSuffix;
52 
53   @Inject
54   CreateUniqueKeyPair(TerremarkVCloudClient trmkClient, Supplier<String> randomSuffix) {
55      this.trmkClient = trmkClient;
56      this.randomSuffix = randomSuffix;
57   }
58 
59   @Override
60   public KeyPair apply(OrgAndName from) {
61      return createNewKeyPairInOrg(from.getOrg(), from.getName());
62   }
63 
64   private KeyPair createNewKeyPairInOrg(URI org, String keyPairName) {
65      checkNotNull(org, "org");
66      checkNotNull(keyPairName, "keyPairName");
67      logger.debug(">> creating keyPair org(%s) name(%s)", org, keyPairName);
68      KeyPair keyPair = null;
69      while (keyPair == null) {
70         try {
71            keyPair = trmkClient.generateKeyPairInOrg(org, getNextName(keyPairName), false);
72            logger.debug("<< created keyPair(%s)", keyPair.getName());
73         } catch (RuntimeException e) {
74            HttpResponseException ht = Throwables2.getFirstThrowableOfType(e, HttpResponseException.class);
75            if (ht == null || ht.getContent() == null
76                  || ht.getContent().indexOf("Security key with same name exists") == -1)
77               throw e;
78         }
79      }
80      return keyPair;
81   }
82 
83   private String getNextName(String keyPairName) {
84      return "jclouds#" + keyPairName + "#" + randomSuffix.get();
85   }
86}

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