| 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.config; |
| 20 | |
| 21 | import static com.google.common.base.Throwables.propagate; |
| 22 | import static org.jclouds.Constants.PROPERTY_SESSION_INTERVAL; |
| 23 | |
| 24 | import java.util.Map; |
| 25 | import java.util.concurrent.TimeUnit; |
| 26 | |
| 27 | import javax.inject.Inject; |
| 28 | import javax.inject.Named; |
| 29 | import javax.inject.Singleton; |
| 30 | |
| 31 | import org.jclouds.cim.xml.ResourceAllocationSettingDataHandler; |
| 32 | import org.jclouds.http.RequiresHttp; |
| 33 | import org.jclouds.rest.AsyncClientFactory; |
| 34 | import org.jclouds.rest.ConfiguresRestClient; |
| 35 | import org.jclouds.rest.suppliers.MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier; |
| 36 | import org.jclouds.vcloud.VCloudAsyncClient; |
| 37 | import org.jclouds.vcloud.VCloudClient; |
| 38 | import org.jclouds.vcloud.VCloudLoginAsyncClient; |
| 39 | import org.jclouds.vcloud.domain.CatalogItem; |
| 40 | import org.jclouds.vcloud.domain.ReferenceType; |
| 41 | import org.jclouds.vcloud.domain.VAppTemplate; |
| 42 | import org.jclouds.vcloud.domain.VCloudSession; |
| 43 | import org.jclouds.vcloud.functions.VAppTemplatesForCatalogItems; |
| 44 | import org.jclouds.vcloud.xml.ovf.VCloudResourceAllocationSettingDataHandler; |
| 45 | |
| 46 | import com.google.common.base.Function; |
| 47 | import com.google.common.base.Supplier; |
| 48 | import com.google.inject.Provides; |
| 49 | import com.google.inject.TypeLiteral; |
| 50 | |
| 51 | /** |
| 52 | * Configures the VCloud authentication service connection, including logging and http transport. |
| 53 | * |
| 54 | * @author Adrian Cole |
| 55 | */ |
| 56 | @RequiresHttp |
| 57 | @ConfiguresRestClient |
| 58 | public abstract class BaseVCloudRestClientModule<S extends VCloudClient, A extends VCloudAsyncClient> extends |
| 59 | CommonVCloudRestClientModule<S, A> { |
| 60 | |
| 61 | public BaseVCloudRestClientModule(Class<S> syncClientType, Class<A> asyncClientType) { |
| 62 | super(syncClientType, asyncClientType); |
| 63 | } |
| 64 | |
| 65 | public BaseVCloudRestClientModule(Class<S> syncClientType, Class<A> asyncClientType, |
| 66 | Map<Class<?>, Class<?>> delegateMap) { |
| 67 | super(syncClientType, asyncClientType, delegateMap); |
| 68 | } |
| 69 | |
| 70 | @Singleton |
| 71 | public static class VCloudWritableCatalog extends WriteableCatalog { |
| 72 | private final VCloudClient client; |
| 73 | |
| 74 | @Inject |
| 75 | public VCloudWritableCatalog(VCloudClient client) { |
| 76 | super(client); |
| 77 | this.client = client; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public boolean apply(ReferenceType arg0) { |
| 82 | return !client.getCatalogClient().getCatalog(arg0.getHref()).isReadOnly(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | protected void configure() { |
| 88 | bind(new TypeLiteral<Function<Iterable<? extends CatalogItem>, Iterable<? extends VAppTemplate>>>() { |
| 89 | }).to(new TypeLiteral<VAppTemplatesForCatalogItems>() { |
| 90 | }); |
| 91 | bind(ResourceAllocationSettingDataHandler.class).to(VCloudResourceAllocationSettingDataHandler.class); |
| 92 | bind(WriteableCatalog.class).to(VCloudWritableCatalog.class); |
| 93 | super.configure(); |
| 94 | } |
| 95 | |
| 96 | @Provides |
| 97 | @Singleton |
| 98 | protected VCloudLoginAsyncClient provideVCloudLogin(AsyncClientFactory factory) { |
| 99 | return factory.create(VCloudLoginAsyncClient.class); |
| 100 | } |
| 101 | |
| 102 | @Provides |
| 103 | @Singleton |
| 104 | protected Supplier<VCloudSession> provideVCloudTokenCache(@Named(PROPERTY_SESSION_INTERVAL) long seconds, |
| 105 | final VCloudLoginAsyncClient login) { |
| 106 | return new MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier<VCloudSession>(authException, seconds, |
| 107 | new Supplier<VCloudSession>() { |
| 108 | |
| 109 | @Override |
| 110 | public VCloudSession get() { |
| 111 | try { |
| 112 | return login.login().get(10, TimeUnit.SECONDS); |
| 113 | } catch (Exception e) { |
| 114 | propagate(e); |
| 115 | assert false : e; |
| 116 | return null; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | }); |
| 121 | } |
| 122 | } |