1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.apache.giraph.conf; |
19 | |
|
20 | |
import org.apache.hadoop.conf.Configuration; |
21 | |
import org.apache.log4j.Logger; |
22 | |
import org.codehaus.jackson.map.ObjectMapper; |
23 | |
import org.codehaus.jackson.type.TypeReference; |
24 | |
|
25 | |
import java.io.IOException; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
public class JsonStringConfOption extends AbstractConfOption { |
31 | |
|
32 | 0 | private static final Logger LOG = |
33 | 0 | Logger.getLogger(JsonStringConfOption.class); |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public JsonStringConfOption(String key, String description) { |
42 | 0 | super(key, description); |
43 | 0 | } |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
public void set(Configuration conf, Object value) { |
52 | 0 | ObjectMapper mapper = new ObjectMapper(); |
53 | |
String jsonStr; |
54 | |
try { |
55 | 0 | jsonStr = mapper.writeValueAsString(value); |
56 | 0 | conf.set(getKey(), jsonStr); |
57 | 0 | } catch (IOException e) { |
58 | 0 | throw new IllegalStateException("Failed to set " + getKey() + |
59 | |
" with json value from " + value); |
60 | 0 | } |
61 | 0 | } |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
public String getRaw(Configuration conf) { |
70 | 0 | return conf.get(getKey()); |
71 | |
} |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
public <T> T get(Configuration conf, Class<T> klass) { |
82 | 0 | String jsonStr = getRaw(conf); |
83 | 0 | T value = null; |
84 | 0 | if (jsonStr != null) { |
85 | 0 | ObjectMapper mapper = new ObjectMapper(); |
86 | |
try { |
87 | 0 | value = mapper.readValue(jsonStr, klass); |
88 | 0 | } catch (IOException e) { |
89 | 0 | throw new IllegalStateException("Failed to read json from key " + |
90 | 0 | getKey() + " with class " + klass); |
91 | 0 | } |
92 | |
} |
93 | 0 | return value; |
94 | |
} |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
public <T> T get(Configuration conf, TypeReference<T> typeReference) { |
105 | 0 | String jsonStr = getRaw(conf); |
106 | 0 | T value = null; |
107 | 0 | if (jsonStr != null) { |
108 | 0 | ObjectMapper mapper = new ObjectMapper(); |
109 | |
try { |
110 | 0 | value = mapper.<T>readValue(jsonStr, typeReference); |
111 | 0 | } catch (IOException e) { |
112 | 0 | throw new IllegalStateException("Failed to read json from key " + |
113 | 0 | getKey() + " with class " + typeReference); |
114 | 0 | } |
115 | |
} |
116 | 0 | return value; |
117 | |
} |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
public <T> T getWithDefault(Configuration conf, Class<T> klass, |
129 | |
T defaultValue) { |
130 | 0 | if (contains(conf)) { |
131 | 0 | return get(conf, klass); |
132 | |
} else { |
133 | 0 | return defaultValue; |
134 | |
} |
135 | |
} |
136 | |
|
137 | |
@Override |
138 | |
public String getDefaultValueStr() { |
139 | 0 | return "null"; |
140 | |
} |
141 | |
|
142 | |
@Override |
143 | |
public boolean isDefaultValue(Configuration conf) { |
144 | 0 | return !contains(conf); |
145 | |
} |
146 | |
|
147 | |
@Override |
148 | |
public ConfOptionType getType() { |
149 | 0 | return ConfOptionType.STRING; |
150 | |
} |
151 | |
} |