forked from aws/aws-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelLoaderUtils.java
More file actions
79 lines (71 loc) · 2.8 KB
/
ModelLoaderUtils.java
File metadata and controls
79 lines (71 loc) · 2.8 KB
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
/*
* Copyright 2011-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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 com.amazonaws.codegen.utils;
import com.amazonaws.codegen.internal.Jackson;
import com.amazonaws.codegen.internal.Utils;
import com.amazonaws.codegen.model.service.ServiceModel;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;
public class ModelLoaderUtils {
public static ServiceModel loadModel(String modelLocation) {
return loadConfigurationModel(ServiceModel.class, modelLocation);
}
/**
* Deserialize the contents of a given configuration file.
*
* @param clzz Class to deserialize into
* @param configurationFileLocation Location of config file to load
* @return Marshalled configuration class
*/
public static <T> T loadConfigurationModel(Class<T> clzz, String configurationFileLocation) {
System.out.println("Loading config file " + configurationFileLocation);
InputStream fileContents = null;
try {
fileContents = getRequiredResourceAsStream(configurationFileLocation);
return Jackson.load(clzz, fileContents);
} catch (IOException e) {
System.err.println("Failed to read the configuration file " + configurationFileLocation);
throw new RuntimeException(e);
} finally {
if (fileContents != null) {
Utils.closeQuietly(fileContents);
}
}
}
/**
* Return an InputStream of the specified resource, failing if it can't be found.
*
* @param location Location of resource
*/
public static InputStream getRequiredResourceAsStream(String location) {
return Utils.getRequiredResourceAsStream(ModelLoaderUtils.class, location);
}
public static <T> T loadModel(Class<T> clzz, File file) {
try {
return Jackson.load(clzz, file);
} catch (IOException e) {
System.err.println("Failed to read the configuration file " + file.getAbsolutePath());
throw new RuntimeException(e);
}
}
public static <T> Optional<T> loadOptionalModel(Class<T> clzz, File file) {
if (!file.exists()) {
return Optional.empty();
}
return Optional.of(loadModel(clzz, file));
}
}