| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * 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, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | package org.jclouds.vcloud.terremark.config; |
| 20 | |
| 21 | import static org.jclouds.Constants.PROPERTY_SESSION_INTERVAL; |
| 22 | |
| 23 | import java.io.IOException; |
| 24 | import java.util.Map; |
| 25 | |
| 26 | import javax.inject.Inject; |
| 27 | import javax.inject.Named; |
| 28 | import javax.inject.Singleton; |
| 29 | |
| 30 | import org.jclouds.http.HttpErrorHandler; |
| 31 | import org.jclouds.http.annotation.ClientError; |
| 32 | import org.jclouds.http.annotation.Redirection; |
| 33 | import org.jclouds.http.annotation.ServerError; |
| 34 | import org.jclouds.rest.suppliers.MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier; |
| 35 | import org.jclouds.util.Strings2; |
| 36 | import org.jclouds.vcloud.config.BaseVCloudExpressRestClientModule; |
| 37 | import org.jclouds.vcloud.domain.ReferenceType; |
| 38 | import org.jclouds.vcloud.domain.VCloudSession; |
| 39 | import org.jclouds.vcloud.terremark.TerremarkVCloudAsyncClient; |
| 40 | import org.jclouds.vcloud.terremark.TerremarkVCloudClient; |
| 41 | import org.jclouds.vcloud.terremark.endpoints.KeysList; |
| 42 | import org.jclouds.vcloud.terremark.handlers.ParseTerremarkVCloudErrorFromHttpResponse; |
| 43 | |
| 44 | import com.google.common.base.Function; |
| 45 | import com.google.common.base.Supplier; |
| 46 | import com.google.common.collect.Maps; |
| 47 | import com.google.inject.Provides; |
| 48 | |
| 49 | public abstract class TerremarkRestClientModule<S extends TerremarkVCloudClient, A extends TerremarkVCloudAsyncClient> |
| 50 | extends BaseVCloudExpressRestClientModule<S, A> { |
| 51 | |
| 52 | public TerremarkRestClientModule(Class<S> syncClientType, Class<A> asyncClientType) { |
| 53 | super(syncClientType, asyncClientType); |
| 54 | } |
| 55 | |
| 56 | @Singleton |
| 57 | @Provides |
| 58 | @Named("CreateInternetService") |
| 59 | String provideCreateInternetService() throws IOException { |
| 60 | return Strings2.toStringAndClose(getClass().getResourceAsStream( |
| 61 | "/terremark/CreateInternetService.xml")); |
| 62 | } |
| 63 | |
| 64 | @Singleton |
| 65 | @Provides |
| 66 | @Named("CreateNodeService") |
| 67 | String provideCreateNodeService() throws IOException { |
| 68 | return Strings2.toStringAndClose(getClass().getResourceAsStream( |
| 69 | "/terremark/CreateNodeService.xml")); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | protected void bindErrorHandlers() { |
| 74 | bind(HttpErrorHandler.class).annotatedWith(Redirection.class).to( |
| 75 | ParseTerremarkVCloudErrorFromHttpResponse.class); |
| 76 | bind(HttpErrorHandler.class).annotatedWith(ClientError.class).to( |
| 77 | ParseTerremarkVCloudErrorFromHttpResponse.class); |
| 78 | bind(HttpErrorHandler.class).annotatedWith(ServerError.class).to( |
| 79 | ParseTerremarkVCloudErrorFromHttpResponse.class); |
| 80 | } |
| 81 | |
| 82 | @Singleton |
| 83 | public static class OrgNameToKeysListSupplier implements Supplier<Map<String, ReferenceType>> { |
| 84 | protected final Supplier<VCloudSession> sessionSupplier; |
| 85 | private final TerremarkVCloudClient client; |
| 86 | |
| 87 | @Inject |
| 88 | protected OrgNameToKeysListSupplier(Supplier<VCloudSession> sessionSupplier, TerremarkVCloudClient client) { |
| 89 | this.sessionSupplier = sessionSupplier; |
| 90 | this.client = client; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public Map<String, ReferenceType> get() { |
| 95 | return Maps.transformValues(sessionSupplier.get().getOrgs(), new Function<ReferenceType, ReferenceType>() { |
| 96 | |
| 97 | @Override |
| 98 | public ReferenceType apply(ReferenceType from) { |
| 99 | return client.findOrgNamed(from.getName()).getKeysList(); |
| 100 | } |
| 101 | |
| 102 | }); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | @Provides |
| 107 | @Singleton |
| 108 | @KeysList |
| 109 | protected Supplier<Map<String, ReferenceType>> provideOrgToKeysListCache( |
| 110 | @Named(PROPERTY_SESSION_INTERVAL) long seconds, final OrgNameToKeysListSupplier supplier) { |
| 111 | return new MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier<Map<String, ReferenceType>>(authException, |
| 112 | seconds, new Supplier<Map<String, ReferenceType>>() { |
| 113 | @Override |
| 114 | public Map<String, ReferenceType> get() { |
| 115 | return supplier.get(); |
| 116 | } |
| 117 | }); |
| 118 | } |
| 119 | |
| 120 | @Singleton |
| 121 | @Provides |
| 122 | @Named("CreateKey") |
| 123 | String provideCreateKey() throws IOException { |
| 124 | return Strings2.toStringAndClose(getClass().getResourceAsStream("/terremark/CreateKey.xml")); |
| 125 | } |
| 126 | } |