List of usage examples for org.apache.commons.beanutils PropertyUtils setProperty
public static void setProperty(Object bean, String name, Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
Set the value of the specified property of the specified bean, no matter which property reference format is used, with no type conversions.
For more details see PropertyUtilsBean
.
From source file:org.apache.ddlutils.platform.PlatformImplBase.java
/** * {@inheritDoc}//from www . j av a 2s . c om */ public void insert(Connection connection, Database model, DynaBean dynaBean) throws DatabaseOperationException { SqlDynaClass dynaClass = model.getDynaClassFor(dynaBean); SqlDynaProperty[] properties = getPropertiesForInsertion(model, dynaClass, dynaBean); Column[] autoIncrColumns = getRelevantIdentityColumns(model, dynaClass, dynaBean); if ((properties.length == 0) && (autoIncrColumns.length == 0)) { _log.warn("Cannot insert instances of type " + dynaClass + " because it has no usable properties"); return; } String insertSql = createInsertSql(model, dynaClass, properties, null); String queryIdentitySql = null; if (_log.isDebugEnabled()) { _log.debug("About to execute SQL: " + insertSql); } if (autoIncrColumns.length > 0) { if (!getPlatformInfo().isLastIdentityValueReadable()) { _log.warn("The database does not support querying for auto-generated column values"); } else { queryIdentitySql = createSelectLastInsertIdSql(model, dynaClass); } } boolean autoCommitMode = false; PreparedStatement statement = null; try { if (!getPlatformInfo().isAutoCommitModeForLastIdentityValueReading()) { autoCommitMode = connection.getAutoCommit(); connection.setAutoCommit(false); } beforeInsert(connection, dynaClass.getTable()); statement = connection.prepareStatement(insertSql); for (int idx = 0; idx < properties.length; idx++) { setObject(statement, idx + 1, dynaBean, properties[idx]); } int count = statement.executeUpdate(); afterInsert(connection, dynaClass.getTable()); if (count != 1) { _log.warn("Attempted to insert a single row " + dynaBean + " in table " + dynaClass.getTableName() + " but changed " + count + " row(s)"); } } catch (SQLException ex) { throw new DatabaseOperationException("Error while inserting into the database: " + ex.getMessage(), ex); } finally { closeStatement(statement); } if (queryIdentitySql != null) { Statement queryStmt = null; ResultSet lastInsertedIds = null; try { if (getPlatformInfo().isAutoCommitModeForLastIdentityValueReading()) { // we'll commit the statement(s) if no auto-commit is enabled because // otherwise it is possible that the auto increment hasn't happened yet // (the db didn't actually perform the insert yet so no triggering of // sequences did occur) if (!connection.getAutoCommit()) { connection.commit(); } } queryStmt = connection.createStatement(); lastInsertedIds = queryStmt.executeQuery(queryIdentitySql); lastInsertedIds.next(); for (int idx = 0; idx < autoIncrColumns.length; idx++) { // we're using the index rather than the name because we cannot know how // the SQL statement looks like; rather we assume that we get the values // back in the same order as the auto increment columns Object value = getObjectFromResultSet(lastInsertedIds, autoIncrColumns[idx], idx + 1); PropertyUtils.setProperty(dynaBean, autoIncrColumns[idx].getName(), value); } } catch (NoSuchMethodException ex) { // Can't happen because we're using dyna beans } catch (IllegalAccessException ex) { // Can't happen because we're using dyna beans } catch (InvocationTargetException ex) { // Can't happen because we're using dyna beans } catch (SQLException ex) { throw new DatabaseOperationException( "Error while retrieving the identity column value(s) from the database", ex); } finally { if (lastInsertedIds != null) { try { lastInsertedIds.close(); } catch (SQLException ex) { // we ignore this one } } closeStatement(statement); } } if (!getPlatformInfo().isAutoCommitModeForLastIdentityValueReading()) { try { // we need to do a manual commit now connection.commit(); connection.setAutoCommit(autoCommitMode); } catch (SQLException ex) { throw new DatabaseOperationException(ex); } } }
From source file:org.apache.empire.data.bean.BeanRecordProxy.java
protected void setBeanPropertyValue(Object bean, Column column, Object value) { // Check Params if (bean == null) throw new InvalidArgumentException("bean", bean); if (column == null) throw new InvalidArgumentException("column", column); // Get Property Name String property = column.getBeanPropertyName(); try { // Get Property Value if (ObjectUtils.isEmpty(value)) value = null;/* www . j av a2 s. com*/ // Set Property Value if (value != null) { // Bean utils will convert if necessary BeanUtils.setProperty(bean, property, value); } else { // Don't convert, just set PropertyUtils.setProperty(bean, property, null); } } catch (IllegalArgumentException e) { log.error(bean.getClass().getName() + ": invalid argument for property '" + property + "'"); throw new BeanPropertySetException(bean, property, e); } catch (IllegalAccessException e) { log.error(bean.getClass().getName() + ": unable to set property '" + property + "'"); throw new BeanPropertySetException(bean, property, e); } catch (InvocationTargetException e) { log.error(bean.getClass().getName() + ": unable to set property '" + property + "'"); throw new BeanPropertySetException(bean, property, e); } catch (NoSuchMethodException e) { log.error(bean.getClass().getName() + ": no setter available for property '" + property + "'"); throw new BeanPropertySetException(bean, property, e); } }
From source file:org.apache.empire.db.DBRecordData.java
/** * Set a single property value of a java bean object used by readProperties. *//*w w w .j a va 2s . com*/ protected void getBeanProperty(Object bean, String property, Object value) { try { if (bean == null) throw new InvalidArgumentException("bean", bean); if (StringUtils.isEmpty(property)) throw new InvalidArgumentException("property", property); /* if (log.isTraceEnabled()) log.trace(bean.getClass().getName() + ": setting property '" + property + "' to " + String.valueOf(value)); */ /* if (value instanceof Date) { // Patch for date bug in BeanUtils value = DateUtils.addDate((Date)value, 0, 0, 0); } */ // Set Property Value if (value != null) { // Bean utils will convert if necessary BeanUtils.setProperty(bean, property, value); } else { // Don't convert, just set PropertyUtils.setProperty(bean, property, null); } // IllegalAccessException } catch (IllegalAccessException e) { log.error(bean.getClass().getName() + ": unable to set property '" + property + "'"); throw new BeanPropertySetException(bean, property, e); // InvocationTargetException } catch (InvocationTargetException e) { log.error(bean.getClass().getName() + ": unable to set property '" + property + "'"); throw new BeanPropertySetException(bean, property, e); // NoSuchMethodException } catch (NoSuchMethodException e) { log.error(bean.getClass().getName() + ": unable to set property '" + property + "'"); throw new BeanPropertySetException(bean, property, e); } catch (NullPointerException e) { log.error(bean.getClass().getName() + ": unable to set property '" + property + "'"); throw new BeanPropertySetException(bean, property, e); } }
From source file:org.apache.empire.jsf2.utils.TagEncodingHelper.java
protected void setBeanPropertyValue(Object bean, String property, Object value) { // Get Property Name try { // Get Property Value if (ObjectUtils.isEmpty(value)) value = null;/*ww w . j a v a2s.c om*/ // Set Property Value if (value != null) { // Bean utils will convert if necessary BeanUtils.setProperty(bean, property, value); } else { // Don't convert, just set PropertyUtils.setProperty(bean, property, null); } } catch (IllegalArgumentException e) { log.error(bean.getClass().getName() + ": invalid argument for property '" + property + "'"); throw new BeanPropertySetException(bean, property, e); } catch (IllegalAccessException e) { log.error(bean.getClass().getName() + ": unable to set property '" + property + "'"); throw new BeanPropertySetException(bean, property, e); } catch (InvocationTargetException e) { log.error(bean.getClass().getName() + ": unable to set property '" + property + "'"); throw new BeanPropertySetException(bean, property, e); } catch (NoSuchMethodException e) { log.error(bean.getClass().getName() + ": no setter available for property '" + property + "'"); throw new BeanPropertySetException(bean, property, e); } }
From source file:org.apache.empire.spring.EmpireReader.java
@Override protected void getBeanProperty(Object bean, String property, Object value) { try {//from w ww . ja v a2 s . co m if (bean == null) throw new InvalidArgumentException("bean", bean); if (StringUtils.isEmpty(property)) throw new InvalidArgumentException("property", property); // Get descriptor PropertyDescriptor descriptor = BeanUtilsBean.getInstance().getPropertyUtils() .getPropertyDescriptor(bean, property); if (descriptor == null) { return; // Skip this property setter } // Check enum Class<?> type = descriptor.getPropertyType(); if (type.isEnum()) { // Enum<?> ev = Enum.valueOf(type, value); boolean found = false; Enum<?>[] items = (Enum[]) type.getEnumConstants(); for (int i = 0; i < items.length; i++) { Enum<?> item = items[i]; if (ObjectUtils.compareEqual(item.name(), value)) { value = item; found = true; break; } } // Enumeration value not found if (!found) throw new ItemNotFoundException(value); } // Set Property Value if (value != null) { // Bean utils will convert if necessary BeanUtils.setProperty(bean, property, value); } else { // Don't convert, just set PropertyUtils.setProperty(bean, property, null); } } catch (IllegalAccessException e) { log.error(bean.getClass().getName() + ": unable to set property '" + property + "'"); throw new BeanPropertySetException(bean, property, e); } catch (InvocationTargetException e) { log.error(bean.getClass().getName() + ": unable to set property '" + property + "'"); throw new BeanPropertySetException(bean, property, e); } catch (NoSuchMethodException e) { log.error(bean.getClass().getName() + ": unable to set property '" + property + "'"); throw new BeanPropertySetException(bean, property, e); } catch (NullPointerException e) { log.error(bean.getClass().getName() + ": unable to set property '" + property + "'"); throw new BeanPropertySetException(bean, property, e); } }
From source file:org.apache.struts.action.ActionServlet.java
/** * <p>Initialize the plug ins for the specified module.</p> * * @param config ModuleConfig information for this module * @throws ServletException if initialization cannot be performed * @since Struts 1.1/* w w w . j av a 2 s .c o m*/ */ protected void initModulePlugIns(ModuleConfig config) throws ServletException { if (log.isDebugEnabled()) { log.debug("Initializing module path '" + config.getPrefix() + "' plug ins"); } PlugInConfig[] plugInConfigs = config.findPlugInConfigs(); PlugIn[] plugIns = new PlugIn[plugInConfigs.length]; getServletContext().setAttribute(Globals.PLUG_INS_KEY + config.getPrefix(), plugIns); for (int i = 0; i < plugIns.length; i++) { try { plugIns[i] = (PlugIn) RequestUtils.applicationInstance(plugInConfigs[i].getClassName()); BeanUtils.populate(plugIns[i], plugInConfigs[i].getProperties()); // Pass the current plugIn config object to the PlugIn. // The property is set only if the plugin declares it. // This plugin config object is needed by Tiles try { PropertyUtils.setProperty(plugIns[i], "currentPlugInConfigObject", plugInConfigs[i]); } catch (Exception e) { ; // FIXME Whenever we fail silently, we must document a valid // reason for doing so. Why should we fail silently if a // property can't be set on the plugin? /** * Between version 1.138-1.140 cedric made these changes. * The exceptions are caught to deal with containers * applying strict security. This was in response to bug * #15736 * * Recommend that we make the currentPlugInConfigObject part * of the PlugIn Interface if we can, Rob */ } plugIns[i].init(this, config); } catch (ServletException e) { throw e; } catch (Exception e) { String errMsg = internal.getMessage("plugIn.init", plugInConfigs[i].getClassName()); log(errMsg, e); throw new UnavailableException(errMsg); } } }
From source file:org.apache.tapestry.parse.AbstractSpecificationRule.java
protected void setProperty(String propertyName, Object value) throws Exception { PropertyUtils.setProperty(digester.peek(), propertyName, value); }
From source file:org.apache.tapestry.parse.SetLimitedPropertiesRule.java
public void begin(String namespace, String name, Attributes attributes) throws Exception { Object top = digester.peek(); int count = attributes.getLength(); for (int i = 0; i < count; i++) { String attributeName = attributes.getLocalName(i); if (Tapestry.isBlank(attributeName)) attributeName = attributes.getQName(i); for (int x = 0; x < _attributeNames.length; x++) { if (_attributeNames[x].equals(attributeName)) { String value = attributes.getValue(i); String propertyName = _propertyNames[x]; PropertyUtils.setProperty(top, propertyName, value); // Terminate inner loop when attribute name is found. break; }/*from w w w . j a v a 2s.com*/ } } }
From source file:org.apache.unomi.persistence.spi.PropertyHelper.java
public static boolean setProperty(Object target, String propertyName, Object propertyValue, String setPropertyStrategy) { try {//from w w w .j a v a 2s .c om while (resolver.hasNested(propertyName)) { Object v = PropertyUtils.getProperty(target, resolver.next(propertyName)); if (v == null) { v = new LinkedHashMap<>(); PropertyUtils.setProperty(target, resolver.next(propertyName), v); } propertyName = resolver.remove(propertyName); target = v; } if (setPropertyStrategy != null && setPropertyStrategy.equals("addValue")) { Object previousValue = PropertyUtils.getProperty(target, propertyName); List<Object> values = new ArrayList<>(); if (previousValue != null && previousValue instanceof List) { values.addAll((List) previousValue); } else if (previousValue != null) { values.add(previousValue); } if (!values.contains(propertyValue)) { values.add(propertyValue); BeanUtils.setProperty(target, propertyName, values); return true; } } else if (propertyValue != null && !propertyValue.equals(BeanUtils.getProperty(target, propertyName))) { if (setPropertyStrategy == null || setPropertyStrategy.equals("alwaysSet") || (setPropertyStrategy.equals("setIfMissing") && BeanUtils.getProperty(target, propertyName) == null)) { BeanUtils.setProperty(target, propertyName, propertyValue); return true; } } } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { logger.error("Cannot set property", e); } return false; }
From source file:org.apache.velocity.tools.ToolInfo.java
protected void setProperty(Object tool, String name, Object value) throws Exception { if (PropertyUtils.isWriteable(tool, name)) { //TODO? support property conversion here? // heavy-handed way is BeanUtils.copyProperty(...) PropertyUtils.setProperty(tool, name, value); }/*from ww w . j a v a2 s . c om*/ }