001/* 002 * This file is part of the Kompics component model runtime. 003 * 004 * Copyright (C) 2009 Swedish Institute of Computer Science (SICS) 005 * Copyright (C) 2009 Royal Institute of Technology (KTH) 006 * 007 * This program is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU General Public License 009 * as published by the Free Software Foundation; either version 2 010 * of the License, or (at your option) any later version. 011 * 012 * This program is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 015 * GNU General Public License for more details. 016 * 017 * You should have received a copy of the GNU General Public License 018 * along with this program; if not, write to the Free Software 019 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 020 */ 021package se.sics.kompics.config; 022 023import com.typesafe.config.ConfigException; 024import com.typesafe.config.ConfigFactory; 025import com.typesafe.config.ConfigList; 026import java.util.LinkedList; 027import java.util.List; 028 029/** 030 * 031 * @author Lars Kroll {@literal <[email protected]>} 032 */ 033public class TypesafeConfig implements BaselineConfig { 034 035 private final com.typesafe.config.Config config; 036 037 public static Config load() { 038 com.typesafe.config.Config conf = ConfigFactory.load(); 039 return load(conf); 040 } 041 042 public static Config load(com.typesafe.config.Config conf) { 043 TypesafeConfig instance = new TypesafeConfig(conf); 044 return Config.Factory.load(instance); 045 } 046 047 private TypesafeConfig(com.typesafe.config.Config config) { 048 this.config = config; 049 } 050 051 @Override 052 public ConfigValue getValue(String key) { 053 try { 054 com.typesafe.config.ConfigValue cv = config.getValue(key); 055 return new TypesafeValue(cv); 056 } catch (ConfigException.Missing ex) { 057 return null; 058 } 059 } 060 061 @Override 062 public List<? extends ConfigValue> getValues(String path) { 063 ConfigList cl = config.getList(path); 064 if (cl != null) { 065 List<ConfigValue> l = new LinkedList<>(); 066 for (com.typesafe.config.ConfigValue cv : cl) { 067 l.add(new TypesafeValue(cv)); 068 } 069 return l; 070 } else { 071 return null; 072 } 073 } 074 075 private static class TypesafeValue implements ConfigValue { 076 077 public final com.typesafe.config.ConfigValue cv; 078 079 private TypesafeValue(com.typesafe.config.ConfigValue cv) { 080 this.cv = cv; 081 } 082 083 @Override 084 public Object unwrap() { 085 return cv.unwrapped(); 086 } 087 088 @Override 089 public Class<?> type() { 090 return cv.unwrapped().getClass(); 091 } 092 093 @Override 094 public long version() { 095 return 0; // this is always a baseline value 096 } 097 098 @Override 099 public ValueOptions options() { 100 return ValueOptions.DEFAULT; 101 } 102 103 } 104}