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 static org.jclouds.rest.RestContextFactory.createContextBuilder; |
22 | import static org.jclouds.util.Throwables2.propagateAuthorizationOrOriginalException; |
23 | |
24 | import java.util.Properties; |
25 | |
26 | import org.jclouds.javax.annotation.Nullable; |
27 | |
28 | import org.jclouds.rest.RestContextFactory; |
29 | import org.jclouds.rest.RestContextSpec; |
30 | |
31 | import com.google.inject.Module; |
32 | |
33 | /** |
34 | * Helper class to instantiate {@code ComputeServiceContext} instances. |
35 | * |
36 | * @author Adrian Cole |
37 | */ |
38 | public class ComputeServiceContextFactory { |
39 | |
40 | private final RestContextFactory contextFactory; |
41 | |
42 | /** |
43 | * Initializes with the default properties built-in to jclouds. This is typically stored in the |
44 | * classpath resource {@code rest.properties} |
45 | * |
46 | * @see RestContextFactory#getPropertiesFromResource |
47 | */ |
48 | public ComputeServiceContextFactory() { |
49 | this(new RestContextFactory()); |
50 | } |
51 | |
52 | /** |
53 | * Finds definitions in the specified properties. |
54 | */ |
55 | public ComputeServiceContextFactory(Properties properties) { |
56 | this(new RestContextFactory(properties)); |
57 | } |
58 | |
59 | /** |
60 | * |
61 | * Uses the supplied RestContextFactory to create {@link ComputeServiceContext}s |
62 | */ |
63 | public ComputeServiceContextFactory(RestContextFactory restContextFactory) { |
64 | this.contextFactory = restContextFactory; |
65 | } |
66 | |
67 | public static <S, A> ComputeServiceContext buildContextUnwrappingExceptions( |
68 | ComputeServiceContextBuilder<S, A> builder) { |
69 | try { |
70 | return builder.buildComputeServiceContext(); |
71 | } catch (Exception e) { |
72 | return propagateAuthorizationOrOriginalException(e); |
73 | } |
74 | } |
75 | |
76 | /** |
77 | * @see RestContextFactory#createContextBuilder(String, String, String) |
78 | */ |
79 | public ComputeServiceContext createContext(String provider, String identity, String credential) { |
80 | ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class.cast(contextFactory |
81 | .createContextBuilder(provider, identity, credential)); |
82 | return buildContextUnwrappingExceptions(builder); |
83 | } |
84 | |
85 | /** |
86 | * @see RestContextFactory#createContextBuilder(String, Properties) |
87 | */ |
88 | public ComputeServiceContext createContext(String provider, Properties overrides) { |
89 | ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class.cast(contextFactory |
90 | .createContextBuilder(provider, overrides)); |
91 | return buildContextUnwrappingExceptions(builder); |
92 | } |
93 | |
94 | /** |
95 | * @see RestContextFactory#createContextBuilder(String, Iterable) |
96 | */ |
97 | public ComputeServiceContext createContext(String provider, Iterable<? extends Module> modules, Properties overrides) { |
98 | ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class.cast(contextFactory |
99 | .createContextBuilder(provider, modules, overrides)); |
100 | return buildContextUnwrappingExceptions(builder); |
101 | |
102 | } |
103 | |
104 | /** |
105 | * @see RestContextFactory#createContextBuilder(String, String,String, Iterable) |
106 | */ |
107 | public ComputeServiceContext createContext(String provider, @Nullable String identity, @Nullable String credential, |
108 | Iterable<? extends Module> modules) { |
109 | ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class.cast(contextFactory |
110 | .createContextBuilder(provider, identity, credential, modules)); |
111 | return buildContextUnwrappingExceptions(builder); |
112 | } |
113 | |
114 | /** |
115 | * @see RestContextFactory#createContextBuilder(String, String,String, Iterable, Properties) |
116 | */ |
117 | public ComputeServiceContext createContext(String provider, @Nullable String identity, @Nullable String credential, |
118 | Iterable<? extends Module> modules, Properties overrides) { |
119 | ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class.cast(contextFactory |
120 | .createContextBuilder(provider, identity, credential, modules, overrides)); |
121 | return buildContextUnwrappingExceptions(builder); |
122 | } |
123 | |
124 | /** |
125 | * @see RestContextFactory#createContextBuilder(RestContextSpec) |
126 | */ |
127 | public <S, A> ComputeServiceContext createContext(RestContextSpec<S, A> contextSpec) { |
128 | ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class |
129 | .cast(createContextBuilder(contextSpec)); |
130 | return buildContextUnwrappingExceptions(builder); |
131 | |
132 | } |
133 | |
134 | /** |
135 | * @see RestContextFactory#createContextBuilder(RestContextSpec, Properties) |
136 | */ |
137 | public <S, A> ComputeServiceContext createContext(RestContextSpec<S, A> contextSpec, Properties overrides) { |
138 | ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class.cast(createContextBuilder( |
139 | contextSpec, overrides)); |
140 | return buildContextUnwrappingExceptions(builder); |
141 | } |
142 | |
143 | /** |
144 | * @see RestContextFactory#createContextBuilder(RestContextSpec, Iterable, Properties) |
145 | */ |
146 | public <S, A> ComputeServiceContext createContext(RestContextSpec<S, A> contextSpec, Iterable<Module> modules, |
147 | Properties overrides) { |
148 | ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class.cast(createContextBuilder( |
149 | contextSpec, modules, overrides)); |
150 | return buildContextUnwrappingExceptions(builder); |
151 | } |
152 | |
153 | } |