| 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.providers; |
| 20 | |
| 21 | import static com.google.common.collect.Iterables.filter; |
| 22 | import static com.google.common.collect.Iterables.find; |
| 23 | |
| 24 | import com.google.common.base.Predicates; |
| 25 | |
| 26 | import java.util.NoSuchElementException; |
| 27 | import java.util.ServiceLoader; |
| 28 | |
| 29 | /** |
| 30 | * The Providers class provides static methods for accessing providers. |
| 31 | * |
| 32 | * @author Jeremy Whitlock <jwhitlock@apache.org> |
| 33 | */ |
| 34 | public class Providers { |
| 35 | |
| 36 | /** |
| 37 | * Returns the providers located on the classpath via {@link java.util.ServiceLoader}. |
| 38 | * |
| 39 | * @return all available providers loaded from classpath via ServiceLoader |
| 40 | */ |
| 41 | private static Iterable<ProviderMetadata> fromServiceLoader() { |
| 42 | return ServiceLoader.load(ProviderMetadata.class); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Returns all available providers. |
| 47 | * |
| 48 | * @return all available providers |
| 49 | */ |
| 50 | public static Iterable<ProviderMetadata> all() { |
| 51 | return fromServiceLoader(); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Returns the first provider with the provided id |
| 56 | * |
| 57 | * @param id |
| 58 | * the id of the provider to return |
| 59 | * |
| 60 | * @return the provider with the given id |
| 61 | * |
| 62 | * @throws NoSuchElementException |
| 63 | * whenever there are no providers with the provided id |
| 64 | */ |
| 65 | public static ProviderMetadata withId(String id) throws NoSuchElementException { |
| 66 | return find(all(), ProviderPredicates.id(id)); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns the providers that are of type |
| 71 | * {@link org.jclouds.providers.ProviderMetadata#BLOBSTORE_TYPE}. |
| 72 | * |
| 73 | * @return the blobstore providers |
| 74 | */ |
| 75 | public static Iterable<ProviderMetadata> allBlobStore() { |
| 76 | return filter(all(), ProviderPredicates.type(ProviderMetadata.BLOBSTORE_TYPE)); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Returns the providers that are of type |
| 81 | * {@link org.jclouds.providers.ProviderMetadata#COMPUTE_TYPE}. |
| 82 | * |
| 83 | * @return the compute service providers |
| 84 | */ |
| 85 | public static Iterable<ProviderMetadata> allCompute() { |
| 86 | return filter(all(), ProviderPredicates.type(ProviderMetadata.COMPUTE_TYPE)); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Returns the providers that are of type |
| 91 | * {@link org.jclouds.providers.ProviderMetadata#QUEUE_TYPE}. |
| 92 | * |
| 93 | * @return the queue service providers |
| 94 | */ |
| 95 | public static Iterable<ProviderMetadata> allQueue() { |
| 96 | return filter(all(), ProviderPredicates.type(ProviderMetadata.QUEUE_TYPE)); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Returns the providers that are of type |
| 101 | * {@link org.jclouds.providers.ProviderMetadata#TABLE_TYPE}. |
| 102 | * |
| 103 | * @return the table service providers |
| 104 | */ |
| 105 | public static Iterable<ProviderMetadata> allTable() { |
| 106 | return filter(all(), ProviderPredicates.type(ProviderMetadata.TABLE_TYPE)); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Returns the providers that are of type |
| 111 | * {@link org.jclouds.providers.ProviderMetadata#LOADBALANCER_TYPE}. |
| 112 | * |
| 113 | * @return the load balancer service providers |
| 114 | */ |
| 115 | public static Iterable<ProviderMetadata> allLoadBalancer() { |
| 116 | return filter(all(), ProviderPredicates.type(ProviderMetadata.LOADBALANCER_TYPE)); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Returns the providers that are of the provided type. |
| 121 | * |
| 122 | * @param type |
| 123 | * the type to providers to return |
| 124 | * |
| 125 | * @return the providers of the provided type |
| 126 | */ |
| 127 | public static Iterable<ProviderMetadata> ofType(String type) { |
| 128 | return filter(all(), ProviderPredicates.type(type)); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Returns the providers that are bound to the same location as the given ISO 3166 code regardless of type. |
| 133 | * |
| 134 | * @param isoCode |
| 135 | * the ISO 3166 code to filter providers by |
| 136 | * |
| 137 | * @return the providers bound by the given ISO 3166 code |
| 138 | */ |
| 139 | public static Iterable<ProviderMetadata> boundedByIso3166Code(String iso3166Code) { |
| 140 | return filter(all(), ProviderPredicates.boundedByIso3166Code(iso3166Code)); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Returns the providers that are bound to the same location as the given ISO 3166 code and of the given type. |
| 145 | * |
| 146 | * @param iso3166Code |
| 147 | * the ISO 3166 code to filter providers by |
| 148 | * @param type |
| 149 | * the type to filter providers by |
| 150 | * |
| 151 | * @return the providers bound by the given ISO 3166 code and of the proper type |
| 152 | */ |
| 153 | public static Iterable<ProviderMetadata> boundedByIso3166Code(String iso3166Code, String type) { |
| 154 | return filter(all(), Predicates.and(ProviderPredicates.boundedByIso3166Code(iso3166Code), |
| 155 | ProviderPredicates.type(type))); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Returns the providers that have at least one common ISO 3166 code in common regardless of type. |
| 160 | * |
| 161 | * @param providerMetadata |
| 162 | * the provider metadata to use to filter providers by |
| 163 | * |
| 164 | * @return the providers that share at least one common ISO 3166 code |
| 165 | */ |
| 166 | public static Iterable<ProviderMetadata> collocatedWith(ProviderMetadata providerMetadata) { |
| 167 | return filter(all(), ProviderPredicates.intersectingIso3166Code(providerMetadata)); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Returns the providers that have at least one common ISO 3166 code and are of the given type. |
| 172 | * |
| 173 | * @param providerMetadata |
| 174 | * the provider metadata to use to filter providers by |
| 175 | * @param type |
| 176 | * the type to filter providers by |
| 177 | * |
| 178 | * @return the providers that share at least one common ISO 3166 code and of the given type |
| 179 | */ |
| 180 | public static Iterable<ProviderMetadata> collocatedWith(ProviderMetadata providerMetadata, String type) { |
| 181 | return filter(all(), Predicates.and(ProviderPredicates.intersectingIso3166Code(providerMetadata), |
| 182 | ProviderPredicates.type(type))); |
| 183 | } |
| 184 | } |