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.omid;
19  
20  import com.google.common.base.Preconditions;
21  import com.google.common.io.Resources;
22  import org.apache.commons.beanutils.BeanUtils;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  import org.yaml.snakeyaml.Yaml;
26  
27  import java.io.IOException;
28  import java.lang.reflect.InvocationTargetException;
29  import java.nio.charset.Charset;
30  import java.util.HashMap;
31  import java.util.Map;
32  
33  public class YAMLUtils {
34  
35      private static final Logger LOG = LoggerFactory.getLogger(YAMLUtils.class);
36  
37      public void loadSettings(String resourcePath, String defaultResourcePath, Object bean) {
38          try {
39              Map properties = loadSettings(resourcePath, defaultResourcePath);
40              BeanUtils.populate(bean, properties);
41          } catch (IllegalAccessException | InvocationTargetException | IOException e) {
42              throw new IllegalStateException(e);
43          }
44      }
45  
46      public void loadSettings(String resourcePath, Object bean) {
47          try {
48              Map properties = loadSettings(null, resourcePath);
49              BeanUtils.populate(bean, properties);
50          } catch (IllegalAccessException | InvocationTargetException | IOException e) {
51              throw new IllegalStateException(e);
52          }
53      }
54  
55      @SuppressWarnings("unchecked")
56      private Map loadSettings(String resourcePath, String defaultResourcePath) throws IOException {
57          Map defaultSetting = loadAsMap(defaultResourcePath);
58          Preconditions.checkState(defaultSetting.size() > 0, String.format("Failed to load file '%s' from classpath", defaultResourcePath));
59          if (resourcePath != null) {
60              Map userSetting = loadAsMap(resourcePath);
61              defaultSetting.putAll(userSetting);
62          }
63          return defaultSetting;
64      }
65  
66      @SuppressWarnings("unchecked")
67      private Map loadAsMap(String path) throws IOException {
68          try {
69              String content = Resources.toString(Resources.getResource(path), Charset.forName("UTF-8"));
70              LOG.debug("Loaded resource file '{}'\n{}", path, content);
71              Map settings = new Yaml().loadAs(content, Map.class);
72              if (settings == null) {
73                  settings = new HashMap(0);
74              }
75              return settings;
76          } catch (IllegalArgumentException e) {
77              return new HashMap();
78          }
79      }
80  
81  }