Generically load enum mapping via properties file

  public static void main(String[] args) {
    final Properties props = loadProperties("some.properties");
    loadMap(props, SomeEnum.class, someMap, "some.properties");
  }

  public > void loadMap(final Properties props, Class enumType,
      Map m, final String resourceName)
  {
    for (Object o: props.keySet())
    {
      String key = null;
      String value = null;
      try
      {
        key = (String) o;
        value = (String) props.get(key);

        m.put(key, Enum.valueOf(enumType, value));
      }
      catch (Exception ex)
      {
        log.error(String.format("Error loading %s key %s, value %s", resourceName, key, value), ex);
      }
    }
  }

  public Properties loadProperties(String resourceName)
  {
    Properties props = new Properties();
    try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(resourceName))
    {
      props.load(is);
      return props;
    }
    catch (IOException e)
    {
      e.printStackTrace();
      throw new RuntimeException("Error loading " + resourceName, e);
    }
  }

Comments

Popular posts from this blog

Oracle JDBC ReadTimeout QueryTimeout

ActiveMQ https