| 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.deltacloud.config; |
| 20 | |
| 21 | import static org.jclouds.Constants.PROPERTY_SESSION_INTERVAL; |
| 22 | |
| 23 | import java.net.URI; |
| 24 | import java.util.NoSuchElementException; |
| 25 | import java.util.Set; |
| 26 | import java.util.concurrent.atomic.AtomicReference; |
| 27 | |
| 28 | import javax.inject.Named; |
| 29 | import javax.inject.Singleton; |
| 30 | |
| 31 | import org.jclouds.deltacloud.DeltacloudAsyncClient; |
| 32 | import org.jclouds.deltacloud.DeltacloudClient; |
| 33 | import org.jclouds.deltacloud.collections.HardwareProfiles; |
| 34 | import org.jclouds.deltacloud.collections.Images; |
| 35 | import org.jclouds.deltacloud.collections.InstanceStates; |
| 36 | import org.jclouds.deltacloud.collections.Instances; |
| 37 | import org.jclouds.deltacloud.collections.Realms; |
| 38 | import org.jclouds.deltacloud.domain.DeltacloudCollection; |
| 39 | import org.jclouds.deltacloud.handlers.DeltacloudErrorHandler; |
| 40 | import org.jclouds.deltacloud.handlers.DeltacloudRedirectionRetryHandler; |
| 41 | import org.jclouds.http.HttpErrorHandler; |
| 42 | import org.jclouds.http.HttpRetryHandler; |
| 43 | import org.jclouds.http.RequiresHttp; |
| 44 | import org.jclouds.http.annotation.ClientError; |
| 45 | import org.jclouds.http.annotation.Redirection; |
| 46 | import org.jclouds.http.annotation.ServerError; |
| 47 | import org.jclouds.rest.AuthorizationException; |
| 48 | import org.jclouds.rest.ConfiguresRestClient; |
| 49 | import org.jclouds.rest.config.RestClientModule; |
| 50 | import org.jclouds.rest.suppliers.MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier; |
| 51 | |
| 52 | import com.google.common.base.Predicate; |
| 53 | import com.google.common.base.Supplier; |
| 54 | import com.google.common.collect.Iterables; |
| 55 | import com.google.inject.Provides; |
| 56 | |
| 57 | /** |
| 58 | * Configures the deltacloud connection. |
| 59 | * |
| 60 | * @author Adrian Cole |
| 61 | */ |
| 62 | @RequiresHttp |
| 63 | @ConfiguresRestClient |
| 64 | public class DeltacloudRestClientModule extends RestClientModule<DeltacloudClient, DeltacloudAsyncClient> { |
| 65 | |
| 66 | public DeltacloudRestClientModule() { |
| 67 | super(DeltacloudClient.class, DeltacloudAsyncClient.class); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | protected void bindErrorHandlers() { |
| 72 | bind(HttpErrorHandler.class).annotatedWith(Redirection.class).to(DeltacloudErrorHandler.class); |
| 73 | bind(HttpErrorHandler.class).annotatedWith(ClientError.class).to(DeltacloudErrorHandler.class); |
| 74 | bind(HttpErrorHandler.class).annotatedWith(ServerError.class).to(DeltacloudErrorHandler.class); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | protected void bindRetryHandlers() { |
| 79 | bind(HttpRetryHandler.class).annotatedWith(Redirection.class).to(DeltacloudRedirectionRetryHandler.class); |
| 80 | } |
| 81 | |
| 82 | protected AtomicReference<AuthorizationException> authException = new AtomicReference<AuthorizationException>(); |
| 83 | |
| 84 | @Provides |
| 85 | @Singleton |
| 86 | protected Supplier<Set<? extends DeltacloudCollection>> provideCollections( |
| 87 | @Named(PROPERTY_SESSION_INTERVAL) long seconds, final DeltacloudClient client) { |
| 88 | return new MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier<Set<? extends DeltacloudCollection>>( |
| 89 | authException, seconds, new Supplier<Set<? extends DeltacloudCollection>>() { |
| 90 | @Override |
| 91 | public Set<? extends DeltacloudCollection> get() { |
| 92 | return client.getCollections(); |
| 93 | } |
| 94 | }); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * since the supplier is memoized, and there are no objects created here, this doesn't need to be |
| 99 | * singleton. |
| 100 | */ |
| 101 | @Provides |
| 102 | @Images |
| 103 | protected URI provideImageCollection(Supplier<Set<? extends DeltacloudCollection>> collectionSupplier) { |
| 104 | return findCollectionWithRel(collectionSupplier.get(), "images").getHref(); |
| 105 | } |
| 106 | |
| 107 | public static DeltacloudCollection findCollectionWithRel(Iterable<? extends DeltacloudCollection> iterable, |
| 108 | final String rel) { |
| 109 | try { |
| 110 | return Iterables.find(iterable, new Predicate<DeltacloudCollection>() { |
| 111 | |
| 112 | @Override |
| 113 | public boolean apply(DeltacloudCollection arg0) { |
| 114 | return arg0.getRel().equals(rel); |
| 115 | } |
| 116 | |
| 117 | }); |
| 118 | } catch (NoSuchElementException e) { |
| 119 | throw new NoSuchElementException("could not find rel " + rel + " in collections " + iterable); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | @Provides |
| 124 | @HardwareProfiles |
| 125 | protected URI provideHardwareProfileCollection(Supplier<Set<? extends DeltacloudCollection>> collectionSupplier) { |
| 126 | return findCollectionWithRel(collectionSupplier.get(), "hardware_profiles").getHref(); |
| 127 | } |
| 128 | |
| 129 | @Provides |
| 130 | @Instances |
| 131 | protected URI provideInstanceCollection(Supplier<Set<? extends DeltacloudCollection>> collectionSupplier) { |
| 132 | return findCollectionWithRel(collectionSupplier.get(), "instances").getHref(); |
| 133 | } |
| 134 | |
| 135 | @Provides |
| 136 | @Realms |
| 137 | protected URI provideRealmCollection(Supplier<Set<? extends DeltacloudCollection>> collectionSupplier) { |
| 138 | return findCollectionWithRel(collectionSupplier.get(), "realms").getHref(); |
| 139 | } |
| 140 | |
| 141 | @Provides |
| 142 | @InstanceStates |
| 143 | protected URI provideInstanceStateCollection(Supplier<Set<? extends DeltacloudCollection>> collectionSupplier) { |
| 144 | return findCollectionWithRel(collectionSupplier.get(), "instance_states").getHref(); |
| 145 | } |
| 146 | } |