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

COVERAGE SUMMARY FOR SOURCE FILE [EC2RestClientModule.java]

nameclass, %method, %block, %line, %
EC2RestClientModule.java67%  (2/3)44%  (4/9)66%  (123/185)56%  (22/39)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class EC2RestClientModule$RegionIdsToURI0%   (0/1)0%   (0/2)0%   (0/45)0%   (0/11)
EC2RestClientModule$RegionIdsToURI (EC2Client, Injector): void 0%   (0/1)0%   (0/10)0%   (0/4)
get (): Map 0%   (0/1)0%   (0/35)0%   (0/7)
     
class EC2RestClientModule100% (1/1)40%  (2/5)69%  (34/49)38%  (3/8)
bindRegionsToProvider (): void 0%   (0/1)0%   (0/4)0%   (0/2)
bindZonesToProvider (): void 0%   (0/1)0%   (0/4)0%   (0/2)
create (): EC2RestClientModule 0%   (0/1)0%   (0/7)0%   (0/1)
<static initializer> 100% (1/1)100% (28/28)100% (1/1)
EC2RestClientModule (Class, Class, Map): void 100% (1/1)100% (6/6)100% (2/2)
     
class EC2RestClientModule$RegionIdToZoneId100% (1/1)100% (2/2)98%  (89/91)95%  (19/20)
get (): Map 100% (1/1)97%  (76/78)93%  (14/15)
EC2RestClientModule$RegionIdToZoneId (EC2Client, Map): void 100% (1/1)100% (13/13)100% (5/5)

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.config;
20 
21import static org.jclouds.location.reference.LocationConstants.PROPERTY_REGIONS;
22 
23import java.net.URI;
24import java.util.Map;
25import java.util.Set;
26import java.util.Map.Entry;
27 
28import javax.annotation.Resource;
29import javax.inject.Inject;
30import javax.inject.Singleton;
31 
32import org.jclouds.aws.config.WithZonesFormSigningRestClientModule;
33import org.jclouds.ec2.EC2AsyncClient;
34import org.jclouds.ec2.EC2Client;
35import org.jclouds.ec2.domain.AvailabilityZoneInfo;
36import org.jclouds.ec2.services.AMIAsyncClient;
37import org.jclouds.ec2.services.AMIClient;
38import org.jclouds.ec2.services.AvailabilityZoneAndRegionAsyncClient;
39import org.jclouds.ec2.services.AvailabilityZoneAndRegionClient;
40import org.jclouds.ec2.services.ElasticBlockStoreAsyncClient;
41import org.jclouds.ec2.services.ElasticBlockStoreClient;
42import org.jclouds.ec2.services.ElasticIPAddressAsyncClient;
43import org.jclouds.ec2.services.ElasticIPAddressClient;
44import org.jclouds.ec2.services.InstanceAsyncClient;
45import org.jclouds.ec2.services.InstanceClient;
46import org.jclouds.ec2.services.KeyPairAsyncClient;
47import org.jclouds.ec2.services.KeyPairClient;
48import org.jclouds.ec2.services.SecurityGroupAsyncClient;
49import org.jclouds.ec2.services.SecurityGroupClient;
50import org.jclouds.ec2.services.WindowsAsyncClient;
51import org.jclouds.ec2.services.WindowsClient;
52import org.jclouds.http.HttpResponseException;
53import org.jclouds.http.RequiresHttp;
54import org.jclouds.location.Region;
55import org.jclouds.location.Zone;
56import org.jclouds.logging.Logger;
57import org.jclouds.rest.ConfiguresRestClient;
58 
59import com.google.common.base.Predicates;
60import com.google.common.base.Splitter;
61import com.google.common.collect.ImmutableMap;
62import com.google.common.collect.ImmutableSet;
63import com.google.common.collect.Maps;
64import com.google.common.collect.ImmutableMap.Builder;
65import com.google.inject.ConfigurationException;
66import com.google.inject.Injector;
67import com.google.inject.Key;
68import com.google.inject.name.Names;
69 
70/**
71 * Configures the EC2 connection.
72 * 
73 * @author Adrian Cole (EDIT: Nick Terry nterry@familysearch.org)
74 */
75@RequiresHttp
76@ConfiguresRestClient
77public class EC2RestClientModule<S extends EC2Client, A extends EC2AsyncClient> extends
78         WithZonesFormSigningRestClientModule<S, A> {
79 
80   public static final Map<Class<?>, Class<?>> DELEGATE_MAP = ImmutableMap.<Class<?>, Class<?>> builder()//
81            .put(AMIClient.class, AMIAsyncClient.class)//
82            .put(ElasticIPAddressClient.class, ElasticIPAddressAsyncClient.class)//
83            .put(InstanceClient.class, InstanceAsyncClient.class)//
84            .put(KeyPairClient.class, KeyPairAsyncClient.class)//
85            .put(SecurityGroupClient.class, SecurityGroupAsyncClient.class)//
86            .put(WindowsClient.class, WindowsAsyncClient.class)//
87            .put(AvailabilityZoneAndRegionClient.class, AvailabilityZoneAndRegionAsyncClient.class)//
88            .put(ElasticBlockStoreClient.class, ElasticBlockStoreAsyncClient.class)//
89            .build();
90 
91   public static EC2RestClientModule<EC2Client, EC2AsyncClient> create() {
92      return new EC2RestClientModule<EC2Client, EC2AsyncClient>(EC2Client.class, EC2AsyncClient.class, DELEGATE_MAP);
93   }
94 
95   public EC2RestClientModule(Class<S> sync, Class<A> async, Map<Class<?>, Class<?>> delegateMap) {
96      super(sync, async, delegateMap);
97   }
98 
99   @Override
100   protected void bindRegionsToProvider() {
101      bindRegionsToProvider(RegionIdsToURI.class);
102   }
103 
104   @Override
105   protected void bindZonesToProvider() {
106      bindZonesToProvider(RegionIdToZoneId.class);
107   }
108 
109   @Singleton
110   public static class RegionIdsToURI implements javax.inject.Provider<Map<String, URI>> {
111      private final AvailabilityZoneAndRegionClient client;
112      private final Injector injector;
113 
114      @Inject
115      public RegionIdsToURI(EC2Client client, Injector injector) {
116         this.client = client.getAvailabilityZoneAndRegionServices();
117         this.injector = injector;
118      }
119 
120      @Singleton
121      @Region
122      @Override
123      public Map<String, URI> get() {
124         try {
125            String regionString = injector.getInstance(Key.get(String.class, Names.named(PROPERTY_REGIONS)));
126            Set<String> regions = ImmutableSet.copyOf(Splitter.on(',').split(regionString));
127            if (regions.size() > 0)
128               return Maps.filterKeys(client.describeRegions(), Predicates.in(regions));
129         } catch (ConfigurationException e) {
130            // this happens if regions property isn't set
131            // services not run by AWS may not have regions, so this is ok.
132         }
133         return client.describeRegions();
134 
135      }
136   }
137 
138   @Singleton
139   public static class RegionIdToZoneId implements javax.inject.Provider<Map<String, String>> {
140      @Resource
141      protected Logger logger = Logger.NULL;
142      
143      private final AvailabilityZoneAndRegionClient client;
144      private final Map<String, URI> regions;
145 
146      @Inject
147      public RegionIdToZoneId(EC2Client client, @Region Map<String, URI> regions) {
148         this.client = client.getAvailabilityZoneAndRegionServices();
149         this.regions = regions;
150      }
151 
152      @Singleton
153      @Zone
154      @Override
155      public Map<String, String> get() {
156         Builder<String, String> map = ImmutableMap.builder();
157         HttpResponseException exception = null;
158         for (Entry<String, URI> region : regions.entrySet()) {
159            try {
160               for (AvailabilityZoneInfo zoneInfo : client.describeAvailabilityZonesInRegion(region.getKey())) {
161                  map.put(zoneInfo.getZone(), region.getKey());
162               }
163            } catch (HttpResponseException e) {
164               if (e.getMessage().contains("Unable to tunnel through proxy")) {
165                  exception = e;
166                  logger.error(e, "Could not describe availability zones in Region: %s", region.getKey());
167               } else {
168                  throw e;
169               }
170            }
171         }
172         ImmutableMap<String, String> result = map.build();
173         if (result.isEmpty() && exception != null) {
174            throw exception;
175         }
176         return result;
177      }
178 
179   }
180 
181}

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