| 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.compute; |
| 20 | |
| 21 | import java.util.Properties; |
| 22 | |
| 23 | import org.jclouds.compute.config.ResolvesImages; |
| 24 | import org.jclouds.compute.internal.ComputeServiceContextImpl; |
| 25 | import org.jclouds.rest.RestContextBuilder; |
| 26 | |
| 27 | import com.google.common.base.Predicate; |
| 28 | import com.google.common.collect.Iterables; |
| 29 | import com.google.inject.Injector; |
| 30 | import com.google.inject.Key; |
| 31 | import com.google.inject.Module; |
| 32 | import com.google.inject.util.Types; |
| 33 | |
| 34 | /** |
| 35 | * @author Adrian Cole |
| 36 | */ |
| 37 | public abstract class ComputeServiceContextBuilder<S, A> extends RestContextBuilder<S, A> { |
| 38 | |
| 39 | public ComputeServiceContextBuilder(Class<S> syncClientType, Class<A> asyncClientType) { |
| 40 | this(syncClientType, asyncClientType, new Properties()); |
| 41 | } |
| 42 | |
| 43 | public ComputeServiceContextBuilder(Class<S> syncClientType, Class<A> asyncClientType, |
| 44 | Properties properties) { |
| 45 | super(syncClientType, asyncClientType, properties); |
| 46 | |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public Injector buildInjector() { |
| 51 | addImageResolutionModuleIfNotPresent(); |
| 52 | return super.buildInjector(); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * {@inheritDoc} |
| 57 | */ |
| 58 | @Override |
| 59 | public ComputeServiceContextBuilder<S, A> withModules(Iterable<Module> modules) { |
| 60 | return (ComputeServiceContextBuilder<S, A>) super.withModules(modules); |
| 61 | } |
| 62 | |
| 63 | public ComputeServiceContext buildComputeServiceContext() { |
| 64 | // need the generic type information |
| 65 | return (ComputeServiceContext) buildInjector().getInstance( |
| 66 | Key.get(Types.newParameterizedType(ComputeServiceContextImpl.class, syncClientType, |
| 67 | asyncClientType))); |
| 68 | } |
| 69 | |
| 70 | protected void addImageResolutionModuleIfNotPresent() { |
| 71 | if (!Iterables.any(modules, new Predicate<Module>() { |
| 72 | public boolean apply(Module input) { |
| 73 | return input.getClass().isAnnotationPresent(ResolvesImages.class); |
| 74 | } |
| 75 | |
| 76 | })) { |
| 77 | addImageResolutionModule(); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | protected void addImageResolutionModule() { |
| 82 | // do nothing; |
| 83 | // this is to be overridden when needed |
| 84 | } |
| 85 | } |