All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.librec.util.DriverClassUtil Maven / Gradle / Ivy

/**
 * Copyright (C) 2016 LibRec
 * 

* This file is part of LibRec. * LibRec is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. *

* LibRec is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. *

* You should have received a copy of the GNU General Public License * along with LibRec. If not, see . */ package net.librec.util; import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; import net.librec.recommender.Recommender; import org.apache.commons.lang.StringUtils; import java.io.IOException; import java.io.InputStream; import java.util.Iterator; import java.util.Map.Entry; import java.util.Properties; /** * Driver Class Util * * @author WangYuFeng */ public class DriverClassUtil { /** * driver Class BiMap matches configuration of driver.classes.props */ private static BiMap driverClassBiMap; /** * inverse configuration of driver.classes.props */ private static BiMap driverClassInverseBiMap; static { driverClassBiMap = HashBiMap.create(); Properties prop = new Properties(); InputStream is = null; try { is = DriverClassUtil.class.getClassLoader().getResourceAsStream("driver.classes.props"); prop.load(is); } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } Iterator> propIte = prop.entrySet().iterator(); while (propIte.hasNext()) { Entry entry = propIte.next(); String key = (String) entry.getKey(); String value = (String) entry.getValue(); driverClassBiMap.put(key, value); } driverClassInverseBiMap = driverClassBiMap.inverse(); } /** * get Class by driver name. * * @param driver driver name * @return Class object * @throws ClassNotFoundException if can't find the Class */ public static Class getClass(String driver) throws ClassNotFoundException { if (StringUtils.isBlank(driver)) { return null; } else if (StringUtils.contains(driver, ".")) { return Class.forName(driver); } else { String fullName = driverClassBiMap.get(driver); return Class.forName(fullName); } } /** * get Driver Name by clazz * * @param clazz clazz name * @return driver name * @throws ClassNotFoundException if can't find the Class */ public static String getDriverName(String clazz) throws ClassNotFoundException { if (StringUtils.isBlank(clazz)) { return null; } else { return driverClassInverseBiMap.get(clazz); } } /** * get Driver Name by clazz * * @param clazz clazz name * @return driver name * @throws ClassNotFoundException if can't find the Class */ public static String getDriverName(Class clazz) throws ClassNotFoundException { if (clazz == null) { return null; } else { String driverName = driverClassInverseBiMap.get(clazz.getName()); if (StringUtils.isNotBlank(driverName)) { return driverName; } else { return clazz.getSimpleName().toLowerCase().replace("recommender", ""); } } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy