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 | */ |
19 | package org.jclouds.ec2.compute.strategy; |
20 | |
21 | import static com.google.common.collect.Iterables.concat; |
22 | import static org.jclouds.concurrent.FutureIterables.transformParallel; |
23 | |
24 | import java.util.Map.Entry; |
25 | import java.util.Set; |
26 | import java.util.concurrent.ExecutorService; |
27 | import java.util.concurrent.Future; |
28 | |
29 | import javax.annotation.Resource; |
30 | import javax.inject.Inject; |
31 | import javax.inject.Named; |
32 | import javax.inject.Singleton; |
33 | |
34 | import org.jclouds.Constants; |
35 | import org.jclouds.compute.reference.ComputeServiceConstants; |
36 | import org.jclouds.ec2.EC2AsyncClient; |
37 | import org.jclouds.ec2.options.DescribeImagesOptions; |
38 | import org.jclouds.logging.Logger; |
39 | |
40 | import com.google.common.base.Function; |
41 | |
42 | /** |
43 | * |
44 | * @author Adrian Cole |
45 | */ |
46 | @Singleton |
47 | public class DescribeImagesParallel implements |
48 | Function<Iterable<Entry<String, DescribeImagesOptions>>, Iterable<? extends org.jclouds.ec2.domain.Image>> { |
49 | @Resource |
50 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
51 | protected Logger logger = Logger.NULL; |
52 | |
53 | protected final EC2AsyncClient async; |
54 | final ExecutorService executor; |
55 | |
56 | @Inject |
57 | public DescribeImagesParallel(EC2AsyncClient async, @Named(Constants.PROPERTY_USER_THREADS) ExecutorService executor) { |
58 | super(); |
59 | this.async = async; |
60 | this.executor = executor; |
61 | } |
62 | |
63 | @Override |
64 | public Iterable<? extends org.jclouds.ec2.domain.Image> apply( |
65 | Iterable<Entry<String, DescribeImagesOptions>> queries) { |
66 | return concat(transformParallel( |
67 | queries, |
68 | new Function<Entry<String, DescribeImagesOptions>, Future<? extends Set<? extends org.jclouds.ec2.domain.Image>>>() { |
69 | |
70 | @Override |
71 | public Future<Set<? extends org.jclouds.ec2.domain.Image>> apply( |
72 | Entry<String, DescribeImagesOptions> from) { |
73 | return async.getAMIServices().describeImagesInRegion(from.getKey(), from.getValue()); |
74 | } |
75 | |
76 | }, executor, null, logger, "amis")); |
77 | } |
78 | |
79 | } |