| 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.compute.config; | 
| 20 |   | 
| 21 | import java.util.Set; | 
| 22 |   | 
| 23 | import javax.inject.Singleton; | 
| 24 |   | 
| 25 | import org.jclouds.collect.TransformingSetSupplier; | 
| 26 | import org.jclouds.compute.ComputeServiceAdapter; | 
| 27 | import org.jclouds.compute.ComputeServiceContext; | 
| 28 | import org.jclouds.compute.domain.Hardware; | 
| 29 | import org.jclouds.compute.domain.Image; | 
| 30 | import org.jclouds.compute.internal.ComputeServiceContextImpl; | 
| 31 | import org.jclouds.compute.strategy.CreateNodeWithGroupEncodedIntoName; | 
| 32 | import org.jclouds.compute.strategy.DestroyNodeStrategy; | 
| 33 | import org.jclouds.compute.strategy.GetNodeMetadataStrategy; | 
| 34 | import org.jclouds.compute.strategy.ListNodesStrategy; | 
| 35 | import org.jclouds.compute.strategy.RebootNodeStrategy; | 
| 36 | import org.jclouds.compute.strategy.ResumeNodeStrategy; | 
| 37 | import org.jclouds.compute.strategy.SuspendNodeStrategy; | 
| 38 | import org.jclouds.compute.strategy.impl.AdaptingComputeServiceStrategies; | 
| 39 | import org.jclouds.domain.Location; | 
| 40 |   | 
| 41 | import com.google.common.base.Function; | 
| 42 | import com.google.common.base.Supplier; | 
| 43 | import com.google.inject.Provides; | 
| 44 | import com.google.inject.Scopes; | 
| 45 | import com.google.inject.TypeLiteral; | 
| 46 | import com.google.inject.util.Types; | 
| 47 |   | 
| 48 | /** | 
| 49 |  *  | 
| 50 |  * @author Adrian Cole | 
| 51 |  */ | 
| 52 | public class ComputeServiceAdapterContextModule<S, A, N, H, I, L> extends BaseComputeServiceContextModule { | 
| 53 |   | 
| 54 |    private Class<A> asyncClientType; | 
| 55 |    private Class<S> syncClientType; | 
| 56 |   | 
| 57 |    public ComputeServiceAdapterContextModule(Class<S> syncClientType, Class<A> asyncClientType) { | 
| 58 |       this.syncClientType = syncClientType; | 
| 59 |       this.asyncClientType = asyncClientType; | 
| 60 |    } | 
| 61 |   | 
| 62 |    @SuppressWarnings( { "unchecked", "rawtypes" }) | 
| 63 |    @Override | 
| 64 |    protected void configure() { | 
| 65 |       super.configure(); | 
| 66 |       bind(new TypeLiteral<ComputeServiceContext>() { | 
| 67 |       }).to( | 
| 68 |                (TypeLiteral) TypeLiteral.get(Types.newParameterizedType(ComputeServiceContextImpl.class, | 
| 69 |                         syncClientType, asyncClientType))).in(Scopes.SINGLETON); | 
| 70 |    } | 
| 71 |   | 
| 72 |    @Provides | 
| 73 |    @Singleton | 
| 74 |    protected Supplier<Set<? extends Location>> provideLocations(final ComputeServiceAdapter<N, H, I, L> adapter, | 
| 75 |             Function<L, Location> transformer) { | 
| 76 |       return new TransformingSetSupplier<L, Location>(new Supplier<Iterable<L>>() { | 
| 77 |   | 
| 78 |          @Override | 
| 79 |          public Iterable<L> get() { | 
| 80 |             return adapter.listLocations(); | 
| 81 |          } | 
| 82 |   | 
| 83 |       }, transformer); | 
| 84 |    } | 
| 85 |   | 
| 86 |    @Provides | 
| 87 |    @Singleton | 
| 88 |    protected Supplier<Set<? extends Hardware>> provideHardware(final ComputeServiceAdapter<N, H, I, L> adapter, | 
| 89 |             Function<H, Hardware> transformer) { | 
| 90 |       return new TransformingSetSupplier<H, Hardware>(new Supplier<Iterable<H>>() { | 
| 91 |   | 
| 92 |          @Override | 
| 93 |          public Iterable<H> get() { | 
| 94 |             return adapter.listHardwareProfiles(); | 
| 95 |          } | 
| 96 |   | 
| 97 |       }, transformer); | 
| 98 |    } | 
| 99 |   | 
| 100 |    @Provides | 
| 101 |    @Singleton | 
| 102 |    protected Supplier<Set<? extends Image>> provideImages(final ComputeServiceAdapter<N, H, I, L> adapter, | 
| 103 |             Function<I, Image> transformer) { | 
| 104 |       return new TransformingSetSupplier<I, Image>(new Supplier<Iterable<I>>() { | 
| 105 |   | 
| 106 |          @Override | 
| 107 |          public Iterable<I> get() { | 
| 108 |             return adapter.listImages(); | 
| 109 |          } | 
| 110 |   | 
| 111 |       }, transformer); | 
| 112 |    } | 
| 113 |   | 
| 114 |    @Provides | 
| 115 |    @Singleton | 
| 116 |    protected CreateNodeWithGroupEncodedIntoName defineAddNodeWithTagStrategy(AdaptingComputeServiceStrategies<N, H, I, L> in) { | 
| 117 |       return in; | 
| 118 |    } | 
| 119 |   | 
| 120 |    @Provides | 
| 121 |    @Singleton | 
| 122 |    protected DestroyNodeStrategy defineDestroyNodeStrategy(AdaptingComputeServiceStrategies<N, H, I, L> in) { | 
| 123 |       return in; | 
| 124 |    } | 
| 125 |   | 
| 126 |    @Provides | 
| 127 |    @Singleton | 
| 128 |    protected GetNodeMetadataStrategy defineGetNodeMetadataStrategy(AdaptingComputeServiceStrategies<N, H, I, L> in) { | 
| 129 |       return in; | 
| 130 |    } | 
| 131 |   | 
| 132 |    @Provides | 
| 133 |    @Singleton | 
| 134 |    protected ListNodesStrategy defineListNodesStrategy(AdaptingComputeServiceStrategies<N, H, I, L> in) { | 
| 135 |       return in; | 
| 136 |    } | 
| 137 |   | 
| 138 |    @Provides | 
| 139 |    @Singleton | 
| 140 |    protected RebootNodeStrategy defineRebootNodeStrategy(AdaptingComputeServiceStrategies<N, H, I, L> in) { | 
| 141 |       return in; | 
| 142 |    } | 
| 143 |   | 
| 144 |    @Provides | 
| 145 |    @Singleton | 
| 146 |    protected ResumeNodeStrategy defineStartNodeStrategy(AdaptingComputeServiceStrategies<N, H, I, L> in) { | 
| 147 |       return in; | 
| 148 |    } | 
| 149 |   | 
| 150 |    @Provides | 
| 151 |    @Singleton | 
| 152 |    protected SuspendNodeStrategy defineStopNodeStrategy(AdaptingComputeServiceStrategies<N, H, I, L> in) { | 
| 153 |       return in; | 
| 154 |    } | 
| 155 | } |