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.rest.config; |
20 | |
21 | import static com.google.common.base.Preconditions.checkState; |
22 | |
23 | import java.util.Map; |
24 | import java.util.concurrent.ExecutionException; |
25 | |
26 | import javax.inject.Inject; |
27 | import javax.inject.Named; |
28 | import javax.inject.Provider; |
29 | |
30 | import com.google.inject.Injector; |
31 | import com.google.inject.Key; |
32 | import com.google.inject.TypeLiteral; |
33 | import com.google.inject.name.Names; |
34 | import org.jclouds.concurrent.internal.SyncProxy; |
35 | import org.jclouds.internal.ClassMethodArgs; |
36 | |
37 | import com.google.common.base.Throwables; |
38 | import com.google.common.cache.Cache; |
39 | import com.google.common.cache.CacheLoader; |
40 | |
41 | /** |
42 | * CreateClientForCaller is parameterized, so clients it creates aren't singletons. For example, |
43 | * CreateClientForCaller satisfies a call like this: |
44 | * {@code context.getProviderSpecificContext().getApi().getOrgClientForName(name)} |
45 | * |
46 | * @author Adrian Cole |
47 | */ |
48 | public class CreateClientForCaller extends CacheLoader<ClassMethodArgs, Object> { |
49 | @Inject |
50 | Injector injector; |
51 | private final Cache<ClassMethodArgs, Object> asyncMap; |
52 | private final Provider<Cache<ClassMethodArgs, Object>> delegateMap; |
53 | Map<Class<?>, Class<?>> sync2Async; |
54 | |
55 | @Inject |
56 | CreateClientForCaller(@Named("async") Cache<ClassMethodArgs, Object> asyncMap, |
57 | @Named("sync") Provider<Cache<ClassMethodArgs, Object>> delegateMap) { |
58 | this.asyncMap = asyncMap; |
59 | this.delegateMap = delegateMap; |
60 | } |
61 | |
62 | @Override |
63 | public Object load(ClassMethodArgs from) throws ExecutionException { |
64 | Class<?> syncClass = from.getMethod().getReturnType(); |
65 | Class<?> asyncClass = sync2Async.get(syncClass); |
66 | checkState(asyncClass != null, "configuration error, sync class " + syncClass + " not mapped to an async class"); |
67 | Object asyncClient = asyncMap.get(from); |
68 | checkState(asyncClient != null, "configuration error, sync client for " + from + " not found"); |
69 | try { |
70 | return SyncProxy.proxy(syncClass, asyncClient, delegateMap.get(), sync2Async, |
71 | injector.getInstance(Key.get(new TypeLiteral<Map<String, Long>>() { |
72 | }, Names.named("TIMEOUTS")))); |
73 | } catch (Exception e) { |
74 | Throwables.propagate(e); |
75 | assert false : "should have propagated"; |
76 | return null; |
77 | } |
78 | |
79 | } |
80 | } |