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.base.Preconditions.checkNotNull; |
22 | import�static�com.google.common.base.Predicates.and; |
23 | import�static�com.google.common.base.Predicates.notNull; |
24 | import�static�com.google.common.collect.Iterables.concat; |
25 | import�static�com.google.common.collect.Iterables.filter; |
26 | import�static�com.google.common.collect.Iterables.transform; |
27 | import�static�org.jclouds.concurrent.FutureIterables.transformParallel; |
28 | � |
29 | import�java.util.Set; |
30 | import�java.util.concurrent.ExecutorService; |
31 | import�java.util.concurrent.Future; |
32 | � |
33 | import�javax.annotation.Resource; |
34 | import�javax.inject.Inject; |
35 | import�javax.inject.Named; |
36 | import�javax.inject.Singleton; |
37 | � |
38 | import�org.jclouds.Constants; |
39 | import�org.jclouds.compute.domain.ComputeMetadata; |
40 | import�org.jclouds.compute.domain.NodeMetadata; |
41 | import�org.jclouds.compute.predicates.NodePredicates; |
42 | import�org.jclouds.compute.reference.ComputeServiceConstants; |
43 | import�org.jclouds.compute.strategy.ListNodesStrategy; |
44 | import�org.jclouds.ec2.EC2AsyncClient; |
45 | import�org.jclouds.ec2.domain.Reservation; |
46 | import�org.jclouds.ec2.domain.RunningInstance; |
47 | import�org.jclouds.location.Region; |
48 | import�org.jclouds.logging.Logger; |
49 | � |
50 | import�com.google.common.base.Function; |
51 | import�com.google.common.base.Predicate; |
52 | import�com.google.common.base.Supplier; |
53 | import�com.google.common.collect.ImmutableSet; |
54 | � |
55 | /** |
56 | �*� |
57 | �*�@author�Adrian�Cole |
58 | �*/ |
59 | @Singleton |
60 | public�class�EC2ListNodesStrategy�implements�ListNodesStrategy�{ |
61 | ���@Resource |
62 | ���@Named(ComputeServiceConstants.COMPUTE_LOGGER) |
63 | ���protected�Logger�logger�=�Logger.NULL; |
64 | � |
65 | ���protected�final�EC2AsyncClient�client; |
66 | ���protected�final�Supplier<Set<String>>�regions; |
67 | ���protected�final�Function<RunningInstance,�NodeMetadata>�runningInstanceToNodeMetadata; |
68 | ���protected�final�ExecutorService�executor; |
69 | � |
70 | ���@Inject |
71 | ���protected�EC2ListNodesStrategy(EC2AsyncClient�client,�@Region�Supplier<Set<String>>�regions, |
72 | ������������Function<RunningInstance,�NodeMetadata>�runningInstanceToNodeMetadata, |
73 | ������������@Named(Constants.PROPERTY_USER_THREADS)�ExecutorService�executor)�{ |
74 | ������this.client�=��checkNotNull(client,�"client"); |
75 | ������this.regions�=��checkNotNull(regions,�"regions"); |
76 | ������this.runningInstanceToNodeMetadata�=�checkNotNull(runningInstanceToNodeMetadata,�"runningInstanceToNodeMetadata"); |
77 | ������this.executor�=��checkNotNull(executor,�"executor"); |
78 | ���} |
79 | � |
80 | ���@Override |
81 | ���public�Set<?�extends�ComputeMetadata>�listNodes()�{ |
82 | ������return�listDetailsOnNodesMatching(NodePredicates.all()); |
83 | ���} |
84 | � |
85 | ���@Override |
86 | ���public�Set<?�extends�NodeMetadata>�listDetailsOnNodesMatching(Predicate<ComputeMetadata>�filter)�{ |
87 | ������Iterable<?�extends�RunningInstance>�instances�=�pollRunningInstances(); |
88 | ������Iterable<?�extends�NodeMetadata>�nodes�=�filter(transform(filter(instances,�notNull()), |
89 | ���������������runningInstanceToNodeMetadata),�and(notNull(),�filter)); |
90 | ������return�ImmutableSet.copyOf(nodes); |
91 | ���} |
92 | � |
93 | ���protected�Iterable<?�extends�RunningInstance>�pollRunningInstances()�{ |
94 | ������Iterable<?�extends�Set<?�extends�Reservation<?�extends�RunningInstance>>>�reservations�=�transformParallel( |
95 | ���������������regions.get(),�new�Function<String,�Future<?�extends�Set<?�extends�Reservation<?�extends�RunningInstance>>>>()�{ |
96 | � |
97 | ������������������@Override |
98 | ������������������public�Future<Set<?�extends�Reservation<?�extends�RunningInstance>>>�apply(String�from)�{ |
99 | ���������������������//�see�http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7126754 |
100 | ���������������������return�castToSpecificTypedFuture(client.getInstanceServices().describeInstancesInRegion(from)); |
101 | ������������������} |
102 | � |
103 | ���������������},�executor,�null,�logger,�"reservations"); |
104 | � |
105 | ������return�concat(concat(reservations)); |
106 | ���} |
107 | � |
108 | ���@SuppressWarnings("unchecked") |
109 | ���private�static�<T>�Future<T>�castToSpecificTypedFuture(Future<?�extends�T>�input)�{ |
110 | �������return�(Future<T>)�input; |
111 | ���} |
112 | } |