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.compute.config; |
20 | |
21 | import java.util.Set; |
22 | |
23 | import javax.inject.Inject; |
24 | import javax.inject.Singleton; |
25 | |
26 | import org.jclouds.compute.domain.Hardware; |
27 | import org.jclouds.compute.domain.Image; |
28 | import org.jclouds.domain.Location; |
29 | import org.jclouds.location.suppliers.OnlyLocationOrFirstZone; |
30 | |
31 | import com.google.common.base.Supplier; |
32 | import com.google.inject.AbstractModule; |
33 | import com.google.inject.Scopes; |
34 | import com.google.inject.TypeLiteral; |
35 | |
36 | /** |
37 | * @author Adrian Cole |
38 | */ |
39 | public abstract class BindComputeSuppliersByClass extends AbstractModule { |
40 | |
41 | @Override |
42 | protected void configure() { |
43 | bindImageSupplier(defineImageSupplier()); |
44 | bindLocationSupplier(defineLocationSupplier()); |
45 | bindHardwareSupplier(defineHardwareSupplier()); |
46 | bindDefaultLocationSupplier(defineDefaultLocationSupplier()); |
47 | } |
48 | |
49 | protected abstract Class<? extends Supplier<Set<? extends Image>>> defineImageSupplier(); |
50 | |
51 | protected abstract Class<? extends Supplier<Set<? extends Hardware>>> defineHardwareSupplier(); |
52 | |
53 | protected Class<? extends Supplier<Set<? extends Location>>> defineLocationSupplier() { |
54 | return SupplierOfLocationSet.class; |
55 | } |
56 | |
57 | @Singleton |
58 | static class SupplierOfLocationSet implements Supplier<Set<? extends Location>> { |
59 | private final Set<? extends Location> locations; |
60 | |
61 | @Inject |
62 | SupplierOfLocationSet(Set<? extends Location> locations) { |
63 | this.locations = locations; |
64 | } |
65 | |
66 | @Override |
67 | public Set<? extends Location> get() { |
68 | return locations; |
69 | } |
70 | |
71 | } |
72 | |
73 | protected Class<? extends Supplier<Location>> defineDefaultLocationSupplier() { |
74 | return OnlyLocationOrFirstZone.class; |
75 | } |
76 | |
77 | protected void bindImageSupplier(Class<? extends Supplier<Set<? extends Image>>> clazz) { |
78 | bind(new TypeLiteral<Supplier<Set<? extends Image>>>() { |
79 | }).to(clazz).in(Scopes.SINGLETON); |
80 | } |
81 | |
82 | protected void bindLocationSupplier(Class<? extends Supplier<Set<? extends Location>>> clazz) { |
83 | bind(new TypeLiteral<Supplier<Set<? extends Location>>>() { |
84 | }).to(clazz).in(Scopes.SINGLETON); |
85 | } |
86 | |
87 | protected void bindDefaultLocationSupplier(Class<? extends Supplier<Location>> clazz) { |
88 | bind(new TypeLiteral<Supplier<Location>>() { |
89 | }).to(clazz).in(Scopes.SINGLETON); |
90 | } |
91 | |
92 | protected void bindHardwareSupplier(Class<? extends Supplier<Set<? extends Hardware>>> clazz) { |
93 | bind(new TypeLiteral<Supplier<Set<? extends Hardware>>>() { |
94 | }).to(clazz).in(Scopes.SINGLETON); |
95 | } |
96 | |
97 | } |