public static Object newInstance(String clazzName, String value) { try { Class<?> type = forName(clazzName); Class<?> enclosing = type.getEnclosingClass(); if (enclosing == null) { Constructor<?> c = type.getConstructor(String.class); c.setAccessible(true); return type.cast(c.newInstance(value)); } Object o = enclosing.newInstance(); Constructor<?> c = type.getDeclaredConstructor(enclosing, String.class); c.setAccessible(true); return type.cast(c.newInstance(o, value)); } catch (Exception e) { throw new RuntimeException(e); } }
public Class<?> loadClass(String className) { ClassLoader org = Thread.currentThread().getContextClassLoader(); try { ClassLoader cl = loadJars(jar); Thread.currentThread().setContextClassLoader(cl); try { return forName(className); } catch (Exception e) { throw new IllegalArgumentException("Could not load stub [" + className + "] from jar [" + jar.getAbsolutePath() + "].", e); } } finally { Thread.currentThread().setContextClassLoader(org); } }
private Database(String dbProvider, List<String> installDdl, List<String> uninstallDdl) { this.dbProvider = dbProvider; this.installDdl = installDdl; this.uninstallDdl = uninstallDdl; this.username = System.getProperty("user.name"); this.password = System.getProperty("user.name"); this.host = PROPS.get(DB_HOST_TOOLS4J_CONFIG_PROPERTY); this.tablespace = System.getProperty("user.name"); if (DERBY.equals(dbProvider)) { driver = DERBY_DRIVER; forName(driver); url = "jdbc:derby:memory:" + tablespace + ";create=true"; } else if (MYSQL.equals(dbProvider)) { driver = MYSQL_DRIVER; url = "jdbc:mysql://" + host + ":3306/" + tablespace; } else if (POSTGRESQL.equals(dbProvider)) { driver = POSTGRESQL_DRIVER; url = "jdbc:postgresql://" + host + ":5432/" + tablespace; } else { throw new UnsupportedOperationException("DB provider not supported [" + dbProvider + "]"); } }
@SuppressWarnings({ "rawtypes", "unchecked" }) @Override public void validate(Collection<Bean> beans) throws AbortRuntimeException { if (validator == null) { validator = Validation.buildDefaultValidatorFactory().getValidator(); } ClassRepository repos = new ClassRepository(); ClassLoader cl = repos.getClassLoader(); ClassLoader org = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(cl); for (Bean bean : beans) { Class genclazz = forName(bean.getSchema().getType()); Object beanToValidate = conversion.convert(bean, genclazz); logger.debug("Validating {}", beanToValidate); Set<ConstraintViolation<Object>> violations = validator.validate(beanToValidate); String msg = ""; for (ConstraintViolation<Object> v : violations) { msg = msg + v.getPropertyPath() + " " + v.getMessage(); } if (msg != null && !"".equals(msg.trim())) { throw CFG309_VALIDATION_ERROR(msg); } } } finally { Thread.currentThread().setContextClassLoader(org); } }
private void convertPropertyList(Bean source, Map<String, Object> values) { for (SchemaPropertyList prop : source.getSchema().get(SchemaPropertyList.class)) { List<String> vals = source.getValues(prop.getName()); String field = prop.getFieldName(); if (vals == null) { continue; } Collection<Object> c = newCollection(forName(prop.getCollectionType())); for (String val : vals) { Object converted = conversion.convert(val, forName(prop.getType())); c.add(converted); } values.put(field, c); } }
private void convertPropertyRefMap(Bean source, Map<String, Object> values, Map<BeanId, Object> instanceCache) { for (SchemaPropertyRefMap prop : source.getSchema().get(SchemaPropertyRefMap.class)) { List<BeanId> beans = source.getReference(prop.getName()); if (beans == null) { continue; } Map<Object, Object> c = newMap(forName(prop.getMapType())); for (BeanId beanId : beans) { Bean b = beanId.getBean(); if (b != null) { Object beanInstance = instanceCache.get(beanId); if (beanInstance == null) { try { beanInstance = newInstance(forName(b.getSchema().getType())); } catch (Exception e) { throw new UnsupportedOperationException(e); } beanInstance = convert(b, beanInstance, new HashMap<String, Object>(), instanceCache); } c.put(beanId.getInstanceId(), beanInstance); } } values.put(prop.getFieldName(), c); } }
private void convertPropertyRefList(Bean source, Map<String, Object> values, Map<BeanId, Object> instanceCache) { for (SchemaPropertyRefList prop : source.getSchema().get(SchemaPropertyRefList.class)) { List<BeanId> references = source.getReference(prop.getName()); if (references == null) { continue; } Collection<Object> c = newCollection(forName(prop.getCollectionType())); for (BeanId beanId : references) { Bean b = beanId.getBean(); if (b != null) { Object beanInstance = instanceCache.get(beanId); if (beanInstance == null) { String type = b.getSchema().getType(); try { beanInstance = newInstance(forName(type)); } catch (Exception e) { throw new UnsupportedOperationException(e); } beanInstance = convert(b, beanInstance, new HashMap<String, Object>(), instanceCache); } c.add(beanInstance); } } values.put(prop.getFieldName(), c); } }
private void convertProperty(Bean source, Map<String, Object> values) { for (SchemaProperty prop : source.getSchema().get(SchemaProperty.class)) { String value = source.getSingleValue(prop.getName()); String field = prop.getFieldName(); Object converted = conversion.convert(value, forName(prop.getType())); values.put(field, converted); } }
private void convertPropertyRef(Bean source, Map<String, Object> values, Map<BeanId, Object> instanceCache) { for (SchemaPropertyRef prop : source.getSchema().get(SchemaPropertyRef.class)) { BeanId id = source.getFirstReference(prop.getName()); if (id == null) { continue; } Bean ref = id.getBean(); if (ref == null) { continue; } Schema refSchema = ref.getSchema(); SchemaPropertyRef schemaRef = source.getSchema().get(SchemaPropertyRef.class, prop.getName()); Object beanInstance = instanceCache.get(id); if (beanInstance == null) { try { beanInstance = newInstance(forName(refSchema.getType())); } catch (Exception e) { throw new UnsupportedOperationException(e); } beanInstance = convert(ref, beanInstance, new HashMap<String, Object>(), instanceCache); } values.put(schemaRef.getFieldName(), beanInstance); } }