forked from dtinit/data-transfer-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApiExtensionContext.java
100 lines (86 loc) · 3.01 KB
/
ApiExtensionContext.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* Copyright 2018 The Data Transfer Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.datatransferproject.api;
import com.google.common.base.Preconditions;
import java.util.HashMap;
import java.util.Map;
import org.datatransferproject.api.launcher.Constants.Environment;
import org.datatransferproject.api.launcher.ExtensionContext;
import org.datatransferproject.api.launcher.Flag;
import org.datatransferproject.api.launcher.TypeManager;
import org.datatransferproject.config.extension.SettingsExtension;
/**
* Provides a context for initializing extensions.
*
* <p>
*/
public class ApiExtensionContext implements ExtensionContext {
private final Map<Class<?>, Object> registered = new HashMap<>();
private final TypeManager typeManager;
private final SettingsExtension settingsExtension;
// Required settings
private final String cloud;
private final Environment environment;
private final String baseUrl;
private final String baseApiUrl;
public ApiExtensionContext(TypeManager typeManager, SettingsExtension settingsExtension) {
this.typeManager = typeManager;
this.settingsExtension = settingsExtension;
registered.put(TypeManager.class, typeManager);
cloud = settingsExtension.getSetting("cloud", null);
Preconditions.checkNotNull(cloud, "Required setting 'cloud' is missing");
environment = Environment.valueOf(settingsExtension.getSetting("environment", null));
Preconditions.checkNotNull(environment, "Required setting 'environment' is missing");
baseUrl = settingsExtension.getSetting("baseUrl", null);
Preconditions.checkNotNull(baseUrl, "Required setting 'baseUrl' is missing");
baseApiUrl = settingsExtension.getSetting("baseApiUrl", null);
Preconditions.checkNotNull(baseApiUrl, "Required setting 'baseApiUrl' is missing");
}
@Override
public TypeManager getTypeManager() {
return typeManager;
}
@Override
public <T> T getService(Class<T> type) {
return type.cast(registered.get(type));
}
@Override
public <T> void registerService(Class<T> type, T service) {
registered.put(type, service);
}
@Override
public <T> T getSetting(String setting, T defaultValue) {
return settingsExtension.getSetting(setting, defaultValue);
}
@Override
@Flag
public String cloud() {
return cloud;
}
@Override
@Flag
public Environment environment() {
return environment;
}
@Flag
public String baseUrl() {
return baseUrl;
}
@Flag
public String baseApiUrl() {
return baseApiUrl;
}
}