| 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; | 
| 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 | } |