EMMA Coverage Report (generated Wed Oct 26 13:47:17 EDT 2011)
[all classes][org.jclouds.rest.config]

COVERAGE SUMMARY FOR SOURCE FILE [CredentialStoreModule.java]

nameclass, %method, %block, %line, %
CredentialStoreModule.java100% (10/10)100% (16/16)94%  (153/163)90%  (26/29)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CredentialStoreModule$CredentialsFromJsonInputStream100% (1/1)100% (2/2)73%  (27/37)67%  (6/9)
apply (InputStream): Credentials 100% (1/1)64%  (18/28)40%  (2/5)
CredentialStoreModule$CredentialsFromJsonInputStream (Json): void 100% (1/1)100% (9/9)100% (4/4)
     
class CredentialStoreModule100% (1/1)100% (5/5)100% (74/74)100% (14/14)
<static initializer> 100% (1/1)100% (5/5)100% (1/1)
CredentialStoreModule (): void 100% (1/1)100% (4/4)100% (2/2)
CredentialStoreModule (Map): void 100% (1/1)100% (6/6)100% (3/3)
configure (): void 100% (1/1)100% (52/52)100% (7/7)
provideCredentialStore (Map, Function, Function): Map 100% (1/1)100% (7/7)100% (1/1)
     
class CredentialStoreModule$1100% (1/1)100% (1/1)100% (6/6)100% (1/1)
CredentialStoreModule$1 (CredentialStoreModule): void 100% (1/1)100% (6/6)100% (1/1)
     
class CredentialStoreModule$2100% (1/1)100% (1/1)100% (6/6)100% (1/1)
CredentialStoreModule$2 (CredentialStoreModule): void 100% (1/1)100% (6/6)100% (1/1)
     
class CredentialStoreModule$3100% (1/1)100% (1/1)100% (6/6)100% (1/1)
CredentialStoreModule$3 (CredentialStoreModule): void 100% (1/1)100% (6/6)100% (1/1)
     
class CredentialStoreModule$4100% (1/1)100% (1/1)100% (6/6)100% (1/1)
CredentialStoreModule$4 (CredentialStoreModule): void 100% (1/1)100% (6/6)100% (1/1)
     
class CredentialStoreModule$5100% (1/1)100% (1/1)100% (6/6)100% (1/1)
CredentialStoreModule$5 (CredentialStoreModule): void 100% (1/1)100% (6/6)100% (1/1)
     
class CredentialStoreModule$6100% (1/1)100% (1/1)100% (6/6)100% (1/1)
CredentialStoreModule$6 (CredentialStoreModule): void 100% (1/1)100% (6/6)100% (1/1)
     
class CredentialStoreModule$CredentialsFromJsonInputStream$PrivateCredentials100% (1/1)100% (1/1)100% (3/3)100% (1/1)
CredentialStoreModule$CredentialsFromJsonInputStream$PrivateCredentials (): void 100% (1/1)100% (3/3)100% (1/1)
     
class CredentialStoreModule$CredentialsToJsonInputStream100% (1/1)100% (2/2)100% (13/13)100% (4/4)
CredentialStoreModule$CredentialsToJsonInputStream (Json): void 100% (1/1)100% (6/6)100% (3/3)
apply (Credentials): InputStream 100% (1/1)100% (7/7)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.rest.config;
20 
21import static com.google.common.base.Preconditions.checkNotNull;
22 
23import java.io.InputStream;
24import java.util.Map;
25import java.util.concurrent.ConcurrentHashMap;
26 
27import javax.annotation.Resource;
28import javax.inject.Inject;
29import javax.inject.Singleton;
30 
31import org.jclouds.collect.TransformingMap;
32import org.jclouds.domain.Credentials;
33import org.jclouds.io.CopyInputStreamInputSupplierMap;
34import org.jclouds.json.Json;
35import org.jclouds.logging.Logger;
36import org.jclouds.rest.ConfiguresCredentialStore;
37import org.jclouds.util.Strings2;
38 
39import com.google.common.annotations.Beta;
40import com.google.common.base.Function;
41import com.google.common.io.InputSupplier;
42import com.google.inject.AbstractModule;
43import com.google.inject.Provides;
44import com.google.inject.TypeLiteral;
45 
46/**
47 * 
48 * @author Adrian Cole
49 */
50@Beta
51@ConfiguresCredentialStore
52public class CredentialStoreModule extends AbstractModule {
53   private static final Map<String, InputSupplier<InputStream>> BACKING = new ConcurrentHashMap<String, InputSupplier<InputStream>>();
54   private final Map<String, InputStream> backing;
55 
56   public CredentialStoreModule(Map<String, InputStream> backing) {
57      this.backing = backing;
58   }
59 
60   public CredentialStoreModule() {
61      this(null);
62   }
63 
64   @Override
65   protected void configure() {
66      bind(new TypeLiteral<Function<Credentials, InputStream>>() {
67      }).to(CredentialsToJsonInputStream.class);
68      bind(new TypeLiteral<Function<InputStream, Credentials>>() {
69      }).to(CredentialsFromJsonInputStream.class);
70      if (backing != null) {
71         bind(new TypeLiteral<Map<String, InputStream>>() {
72         }).toInstance(backing);
73      } else {
74         bind(new TypeLiteral<Map<String, InputSupplier<InputStream>>>() {
75         }).toInstance(BACKING);
76         bind(new TypeLiteral<Map<String, InputStream>>() {
77         }).to(new TypeLiteral<CopyInputStreamInputSupplierMap>() {
78         });
79      }
80   }
81 
82   @Singleton
83   public static class CredentialsToJsonInputStream implements Function<Credentials, InputStream> {
84      private final Json json;
85 
86      @Inject
87      CredentialsToJsonInputStream(Json json) {
88         this.json = json;
89      }
90 
91      @Override
92      public InputStream apply(Credentials from) {
93         return Strings2.toInputStream(json.toJson(checkNotNull(from)));
94      }
95   }
96 
97   @Singleton
98   public static class CredentialsFromJsonInputStream implements Function<InputStream, Credentials> {
99      @Resource
100      protected Logger logger = Logger.NULL;
101 
102      private final Json json;
103 
104      @Inject
105      CredentialsFromJsonInputStream(Json json) {
106         this.json = json;
107      }
108 
109      private static class PrivateCredentials {
110         String identity;
111         String credential;
112      }
113 
114      @Override
115      public Credentials apply(InputStream from) {
116         try {
117            PrivateCredentials credentials = json.fromJson(Strings2.toStringAndClose(checkNotNull(from)),
118                  PrivateCredentials.class);
119            return new Credentials(credentials.identity, credentials.credential);
120         } catch (Exception e) {
121            logger.warn(e, "ignoring problem retrieving credentials");
122            return null;
123         }
124      }
125   }
126 
127   @Provides
128   @Singleton
129   protected Map<String, Credentials> provideCredentialStore(Map<String, InputStream> backing,
130         Function<Credentials, InputStream> credentialsSerializer,
131         Function<InputStream, Credentials> credentialsDeserializer) {
132      return new TransformingMap<String, InputStream, Credentials>(backing, credentialsDeserializer,
133            credentialsSerializer);
134   }
135}

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