public static <T extends Config> T getConfig() { if (config == null) { throw new ConfigError(ConfigError.MISSING_CONFIG); } return (T) config; }
protected void setProperty(String propName, String value) { // first check if it really exists Field field = null; try { field = getField(this.getClass(), propName); } catch (ConfigError e) { // we choose to ignore unknown config properties, but we do give a loud warning logger.warn(e.getMessage()); logger.debug(e.getMessage(), e); return; } // note that lazy evaluation is used on purpose! boolean success = attemptSetPropertyUsingMethod(getSetterFor(this.getClass(), propName + SETTER_FROM_STRING), value) || attemptSetPropertyUsingField(field, value); if (!success) { throw new ConfigError(ConfigError.COULD_NOT_SET_PROPERTY, propName); } }
synchronized (INIT_LOCK) { if (initialized) { throw new ConfigError(ConfigError.ALREADY_INITIALIZED); throw new ConfigError("A configuration must be provided"); } catch (Exception ex) { ex.printStackTrace(); throw new ConfigError(ex); } catch (ConfigError e) { logger.error("Could not initialize Fenix Framework", e); e.printStackTrace(); throw e;
protected static void missingRequired(String fieldName) { throw new ConfigError(ConfigError.MISSING_REQUIRED_FIELD, "'" + fieldName + "'"); }
private void checkForMultipleDomainModelUrlsDefinition() { if (domainModelURLs != null) { // means that it was already set logger.error(ConfigError.DUPLICATE_DEFINITION_OF_DOMAIN_MODEL_URLS); throw new ConfigError(ConfigError.DUPLICATE_DEFINITION_OF_DOMAIN_MODEL_URLS); } }
private boolean attemptSetPropertyUsingMethod(Method setter, String value) { if (setter == null) { return false; } setter.setAccessible(true); try { setter.invoke(this, value); } catch (Exception e) { throw new ConfigError(e); } return true; }
protected void expectedInitialNodesFromString(String value) { try { expectedInitialNodes = Integer.parseInt(value.trim()); } catch (NumberFormatException e) { throw new ConfigError(e); } }
private Field getField(Class<? extends Config> clazz, String fieldName) { try { return clazz.getDeclaredField(fieldName); } catch (NoSuchFieldException e) { // climb the hierarchy, but only up to Config class Class<?> superclass = clazz.getSuperclass(); if (Config.class.isAssignableFrom(superclass)) { return getField((Class<? extends Config>) superclass, fieldName); } else { throw new ConfigError(ConfigError.UNKNOWN_PROPERTY + fieldName, e); } } }
/** * Lookup via reflection the {@link pt.ist.fenixframework.backend.CurrentBackEndId} class and * return an instance of it. * * @throws ConfigError if the expected class does not exit, or it does not extend the BackEndId * class */ public static final BackEndId getBackEndId() throws ConfigError { Exception ex = null; try { Class<CurrentBackEndId> currentBackEndIdClass = (Class<CurrentBackEndId>) Class.forName(CurrentBackEndId.class.getName()); BackEndId beId = currentBackEndIdClass.newInstance(); return beId; } catch (ClassNotFoundException e) { ex = e; } catch (InstantiationException e) { ex = e; } catch (IllegalAccessException e) { ex = e; } String message = "Missing required BackEndId: " + Constants.BACKEND_PACKAGE + "." + Constants.CURRENT_BACKEND_ID_CLASS; logger.error(message); throw new ConfigError(message); } }
@Override protected void init() { try { this.backEnd.configInfinispan(this); } catch (Exception e) { throw new ConfigError(FAILED_INIT, e); } // DomainClassInfo.initializeClassInfos(FenixFramework.getDomainModel(), 0); super.init(); }
private static Config createConfigFromProperties(Properties props) { // get the config instance Config config = null; try { Class<? extends Config> configClass = null; // first check for possible overriding in the config file String configClassName = props.getProperty(Config.PROPERTY_CONFIG_CLASS); if (configClassName != null) { try { configClass = (Class<? extends Config>) Class.forName(configClassName); } catch (ClassNotFoundException e) { // here, we could ignore and attempt the default config class, but it's best if // the programmer understands that the configuration is flawed logger.error(ConfigError.CONFIG_CLASS_NOT_FOUND + configClassName, e); throw new ConfigError(ConfigError.CONFIG_CLASS_NOT_FOUND, configClassName); } } else { // fallback to the current backend's default configClass = BackEndId.getBackEndId().getDefaultConfigClass(); } config = configClass.newInstance(); } catch (InstantiationException e) { throw new ConfigError(e); } catch (IllegalAccessException e) { throw new ConfigError(e); } // populate config from properties config.populate(props); return config; }
@Override protected void init() { try { this.backEnd.configOgm(this); } catch (Exception e) { throw new ConfigError(FAILED_INIT, e); } super.init(); // do this only after having set up transaction manager }
/** * Subclasses of this config should set their own backEnd before calling this init. Otherwise, this method will set its own * backEnd (JVSTMBackEnd) */ @Override protected void init() { // any sub-BackEnd with a concrete repository should have set it up by now // (and invoked super.init() only after doing so). If a backend is not // yet set, then we use the default JVSTMBackEnd, which has its own Repository. // // We do not use the Config's constructor to pass the BackEnd instance, // because the config instance is created **before** being populated with // the configuration parameters, thus it may not be possible to create the // BackEnd instance. if (backEnd == null) { this.backEnd = new JVSTMBackEnd(); } try { this.backEnd.init(this); } catch (Exception e) { throw new ConfigError(FAILED_INIT, e); } super.init(); }