This project has retired. For details please refer to its Attic page.
JsonStringConfOption xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * JSON String configuration option
29   */
30  public class JsonStringConfOption extends AbstractConfOption {
31    /** Logger */
32    private static final Logger LOG =
33        Logger.getLogger(JsonStringConfOption.class);
34  
35    /**
36     * Constructor
37     *
38     * @param key String key name
39     * @param description String description of option
40     */
41    public JsonStringConfOption(String key, String description) {
42      super(key, description);
43    }
44  
45    /**
46     * Set JSON value
47     *
48     * @param conf Configuration
49     * @param value Json value
50     */
51    public void set(Configuration conf, Object value) {
52      ObjectMapper mapper = new ObjectMapper();
53      String jsonStr;
54      try {
55        jsonStr = mapper.writeValueAsString(value);
56        conf.set(getKey(), jsonStr);
57      } catch (IOException e) {
58        throw new IllegalStateException("Failed to set " + getKey() +
59            " with json value from " + value);
60      }
61    }
62  
63    /**
64     * Get raw JSON string
65     *
66     * @param conf Configuration
67     * @return raw JSON string value
68     */
69    public String getRaw(Configuration conf) {
70      return conf.get(getKey());
71    }
72  
73    /**
74     * Get JSON value
75     *
76     * @param <T> JSON type
77     * @param conf Configuration
78     * @param klass Class to read into
79     * @return JSON value
80     */
81    public <T> T get(Configuration conf, Class<T> klass) {
82      String jsonStr = getRaw(conf);
83      T value = null;
84      if (jsonStr != null) {
85        ObjectMapper mapper = new ObjectMapper();
86        try {
87          value = mapper.readValue(jsonStr, klass);
88        } catch (IOException e) {
89          throw new IllegalStateException("Failed to read json from key " +
90              getKey() + " with class " + klass);
91        }
92      }
93      return value;
94    }
95  
96    /**
97     * Get JSON value
98     *
99     * @param <T> JSON type
100    * @param conf Configuration
101    * @param typeReference TypeReference for JSON type
102    * @return JSON value
103    */
104   public <T> T get(Configuration conf, TypeReference<T> typeReference) {
105     String jsonStr = getRaw(conf);
106     T value = null;
107     if (jsonStr != null) {
108       ObjectMapper mapper = new ObjectMapper();
109       try {
110         value = mapper.<T>readValue(jsonStr, typeReference);
111       } catch (IOException e) {
112         throw new IllegalStateException("Failed to read json from key " +
113             getKey() + " with class " + typeReference);
114       }
115     }
116     return value;
117   }
118 
119   /**
120    * Get JSON value, or default if not present
121    *
122    * @param <T> JSON type
123    * @param klass Class to read into
124    * @param conf Configuration
125    * @param defaultValue Default value if not found
126    * @return JSON value
127    */
128   public <T> T getWithDefault(Configuration conf, Class<T> klass,
129       T defaultValue) {
130     if (contains(conf)) {
131       return get(conf, klass);
132     } else {
133       return defaultValue;
134     }
135   }
136 
137   @Override
138   public String getDefaultValueStr() {
139     return "null";
140   }
141 
142   @Override
143   public boolean isDefaultValue(Configuration conf) {
144     return !contains(conf);
145   }
146 
147   @Override
148   public ConfOptionType getType() {
149     return ConfOptionType.STRING;
150   }
151 }