EMMA Coverage Report (generated Wed Jun 22 19:47:49 EDT 2011)
[all classes][org.jclouds.blobstore.internal]

COVERAGE SUMMARY FOR SOURCE FILE [InputStreamMapImpl.java]

nameclass, %method, %block, %line, %
InputStreamMapImpl.java33%  (1/3)5%   (1/21)6%   (13/222)7%   (3/42)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class InputStreamMapImpl$10%   (0/1)0%   (0/2)0%   (0/11)0%   (0/2)
InputStreamMapImpl$1 (InputStreamMapImpl): void 0%   (0/1)0%   (0/6)0%   (0/1)
apply (Blob): InputStream 0%   (0/1)0%   (0/5)0%   (0/1)
     
class InputStreamMapImpl$20%   (0/1)0%   (0/2)0%   (0/19)0%   (0/4)
InputStreamMapImpl$2 (InputStreamMapImpl): void 0%   (0/1)0%   (0/6)0%   (0/1)
apply (Map$Entry): Blob 0%   (0/1)0%   (0/13)0%   (0/3)
     
class InputStreamMapImpl100% (1/1)6%   (1/17)7%   (13/192)8%   (3/37)
access$000 (InputStreamMapImpl, Blob): InputStream 0%   (0/1)0%   (0/4)0%   (0/1)
get (Object): InputStream 0%   (0/1)0%   (0/18)0%   (0/3)
getInputStreamOrNull (Blob): InputStream 0%   (0/1)0%   (0/13)0%   (0/1)
newBlobWithMD5 (String, Object): Blob 0%   (0/1)0%   (0/26)0%   (0/6)
put (String, InputStream): InputStream 0%   (0/1)0%   (0/8)0%   (0/1)
putAll (Map): void 0%   (0/1)0%   (0/4)0%   (0/2)
putAllBytes (Map): void 0%   (0/1)0%   (0/4)0%   (0/2)
putAllFiles (Map): void 0%   (0/1)0%   (0/4)0%   (0/2)
putAllInternal (Map): void 0%   (0/1)0%   (0/13)0%   (0/2)
putAllStrings (Map): void 0%   (0/1)0%   (0/4)0%   (0/2)
putBytes (String, byte []): InputStream 0%   (0/1)0%   (0/8)0%   (0/1)
putFile (String, File): InputStream 0%   (0/1)0%   (0/8)0%   (0/1)
putInternal (String, Payload): InputStream 0%   (0/1)0%   (0/24)0%   (0/4)
putString (String, String): InputStream 0%   (0/1)0%   (0/8)0%   (0/1)
remove (Object): InputStream 0%   (0/1)0%   (0/19)0%   (0/4)
values (): Collection 0%   (0/1)0%   (0/14)0%   (0/1)
InputStreamMapImpl (BlobStore, Provider, GetBlobsInListStrategy, ListContaine... 100% (1/1)100% (13/13)100% (3/3)

1/**
2 *
3 * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
4 *
5 * ====================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * 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, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ====================================================================
18 */
19package org.jclouds.blobstore.internal;
20 
21import static com.google.common.collect.Iterables.transform;
22import static com.google.common.collect.Lists.newArrayList;
23import static org.jclouds.io.Payloads.newPayload;
24 
25import java.io.File;
26import java.io.IOException;
27import java.io.InputStream;
28import java.util.Collection;
29import java.util.Map;
30 
31import javax.inject.Inject;
32import javax.inject.Provider;
33 
34import org.jclouds.blobstore.BlobMap;
35import org.jclouds.blobstore.BlobStore;
36import org.jclouds.blobstore.InputStreamMap;
37import org.jclouds.blobstore.domain.Blob;
38import org.jclouds.blobstore.domain.BlobBuilder;
39import org.jclouds.blobstore.options.ListContainerOptions;
40import org.jclouds.blobstore.strategy.ContainsValueInListStrategy;
41import org.jclouds.blobstore.strategy.GetBlobsInListStrategy;
42import org.jclouds.blobstore.strategy.PutBlobsStrategy;
43import org.jclouds.blobstore.strategy.internal.ListContainerAndRecurseThroughFolders;
44import org.jclouds.crypto.Crypto;
45import org.jclouds.io.Payload;
46import org.jclouds.io.Payloads;
47import org.jclouds.io.payloads.ByteArrayPayload;
48import org.jclouds.io.payloads.FilePayload;
49import org.jclouds.io.payloads.InputStreamPayload;
50import org.jclouds.io.payloads.StringPayload;
51 
52import com.google.common.annotations.VisibleForTesting;
53import com.google.common.base.Function;
54import com.google.common.base.Throwables;
55 
56/**
57 * Map representation of a live connection to a BlobStore. All put operations will result in ETag
58 * calculation. If this is not desired, use {@link BlobMap} instead.
59 * 
60 * @author Adrian Cole
61 * 
62 * @see BlobStore
63 * @see InputStreamMap
64 * @see BaseBlobMap
65 */
66public class InputStreamMapImpl extends BaseBlobMap<InputStream> implements InputStreamMap {
67   protected final Crypto crypto;
68 
69   @Inject
70   public InputStreamMapImpl(BlobStore connection, Provider<BlobBuilder> blobBuilders,
71         GetBlobsInListStrategy getAllBlobs, ListContainerAndRecurseThroughFolders listStrategy,
72         ContainsValueInListStrategy containsValueStrategy, PutBlobsStrategy putBlobsStrategy, String containerName,
73         ListContainerOptions options, Crypto crypto) {
74      super(connection, getAllBlobs, containsValueStrategy, putBlobsStrategy, listStrategy, containerName, options);
75      this.crypto = crypto;
76   }
77 
78   @Override
79   public InputStream get(Object o) {
80      String realKey = prefixer.apply(o.toString());
81      Blob blob = blobstore.getBlob(containerName, realKey);
82      return getInputStreamOrNull(blob);
83   }
84 
85   private InputStream getInputStreamOrNull(Blob blob) {
86      return blob != null ? blob.getPayload() != null ? blob.getPayload().getInput() : null : null;
87   }
88 
89   @Override
90   public InputStream remove(Object o) {
91      InputStream old = get(o);
92      String realKey = prefixer.apply(o.toString());
93      blobstore.removeBlob(containerName, realKey);
94      return old;
95   }
96 
97   @Override
98   public Collection<InputStream> values() {
99      return newArrayList(transform(getAllBlobs.execute(containerName, options), new Function<Blob, InputStream>() {
100         public InputStream apply(Blob from) {
101            return getInputStreamOrNull(from);
102         }
103      }));
104   }
105 
106   @Override
107   public void putAll(Map<? extends String, ? extends InputStream> map) {
108      putAllInternal(map);
109   }
110 
111   @Override
112   public void putAllBytes(Map<? extends String, ? extends byte[]> map) {
113      putAllInternal(map);
114   }
115 
116   @Override
117   public void putAllFiles(Map<? extends String, ? extends File> map) {
118      putAllInternal(map);
119   }
120 
121   @Override
122   public void putAllStrings(Map<? extends String, ? extends String> map) {
123      putAllInternal(map);
124   }
125 
126   /**
127    * submits requests to add all objects and collects the results later. All values will have eTag
128    * calculated first. As a side-effect of this, the content will be copied into a byte [].
129    * 
130    * @see S3Client#put(String, Blob)
131    */
132   @VisibleForTesting
133   void putAllInternal(Map<? extends String, ? extends Object> map) {
134      putBlobsStrategy.execute(containerName,
135            transform(map.entrySet(), new Function<Map.Entry<? extends String, ? extends Object>, Blob>() {
136               @Override
137               public Blob apply(Map.Entry<? extends String, ? extends Object> from) {
138                  String name = from.getKey();
139                  Object value = from.getValue();
140                  return newBlobWithMD5(name, value);
141               }
142 
143            }));
144   }
145 
146   @VisibleForTesting
147   Blob newBlobWithMD5(String name, Object value) {
148      Blob blob = blobstore.blobBuilder(prefixer.apply(name)).payload(newPayload(value)).build();
149      try {
150         Payloads.calculateMD5(blob, crypto.md5());
151      } catch (IOException e) {
152         Throwables.propagate(e);
153      }
154      return blob;
155   }
156 
157   @Override
158   public InputStream putString(String key, String value) {
159      return putInternal(key, new StringPayload(value));
160   }
161 
162   @Override
163   public InputStream putFile(String key, File value) {
164      return putInternal(key, new FilePayload(value));
165   }
166 
167   @Override
168   public InputStream putBytes(String key, byte[] value) {
169      return putInternal(key, new ByteArrayPayload(value));
170   }
171 
172   @Override
173   public InputStream put(String key, InputStream value) {
174      return putInternal(key, new InputStreamPayload(value));
175   }
176 
177   /**
178    * calculates eTag before adding the object to s3. As a side-effect of this, the content will be
179    * copied into a byte []. *
180    * 
181    * @see S3Client#put(String, Blob)
182    */
183   @VisibleForTesting
184   InputStream putInternal(String name, Payload payload) {
185      InputStream returnVal = containsKey(name) ? get(name) : null;
186      Blob blob = newBlobWithMD5(name, payload);
187      blobstore.putBlob(containerName, blob);
188      return returnVal;
189   }
190 
191}

[all classes][org.jclouds.blobstore.internal]
EMMA 2.0.5312 (C) Vladimir Roubtsov