1 8 package org.ozoneDB.core.storage; 9 10 import java.lang.reflect.Constructor ; 11 import java.lang.reflect.InvocationTargetException ; 12 import java.util.Properties ; 13 import org.ozoneDB.core.ConfigurationException; 14 15 20 public class PropertyConfigurableFactory { 21 22 private PropertyConfigurableFactory() { 23 } 24 25 public static PropertyConfigurable create(Class assignableFrom, Properties properties, String propertyName) { 26 String className = properties.getProperty(propertyName); 27 if (className == null) { 28 throw new ConfigurationException("could not find property: '" + propertyName + "'"); 29 } 30 try { 31 Class classType = Class.forName(className); 32 if (!PropertyConfigurable.class.isAssignableFrom(classType)) { 33 throw new ConfigurationException(className + " should implement " + PropertyConfigurable.class); 34 } 35 if (!assignableFrom.isAssignableFrom(classType)) { 36 throw new ConfigurationException(className + " should implement " + assignableFrom); 37 } 38 Constructor constructor = classType.getConstructor(new Class [] {Properties .class, String .class}); 39 PropertyConfigurable result = (PropertyConfigurable) constructor.newInstance(new Object [] {properties, propertyName}); 40 return result; 41 } catch (ClassNotFoundException e) { 42 throw new ConfigurationException(e); 43 } catch (NoSuchMethodException e) { 44 throw new ConfigurationException(className + " does not follow PropertyConfigurable constructor contract", e); 45 } catch (InstantiationException e) { 46 throw new ConfigurationException(e); 47 } catch (IllegalAccessException e) { 48 throw new ConfigurationException(e); 49 } catch (InvocationTargetException e) { 50 throw new ConfigurationException(e); 51 } 52 } 53 54 } 55 56 | Popular Tags |