List of usage examples for org.apache.commons.beanutils PropertyUtils getSimpleProperty
public static Object getSimpleProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
Return the value of the specified simple property of the specified bean, with no type conversions.
For more details see PropertyUtilsBean
.
From source file:org.apache.struts.faces.systest1.LogonAction.java
/** * <p>Process an attempted logon.</p> */// w ww . j av a 2s. c om public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ActionErrors errors = new ActionErrors(); String username = (String) PropertyUtils.getSimpleProperty(form, "username"); if ((username == null) || ("".equals(username))) { errors.add("username", new ActionError("logon.username")); } String password = (String) PropertyUtils.getSimpleProperty(form, "password"); if ((password == null) || ("".equals(password))) { errors.add("password", new ActionError("logon.password")); } if (log.isTraceEnabled()) { log.trace("username=" + username + ",password=" + password); } if (errors.isEmpty() && (!"gooduser".equals(username) || !"goodpass".equals(password))) { errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("logon.mismatch")); } if (errors.isEmpty()) { if (log.isDebugEnabled()) { log.debug("Successful logon, forwarding to logon1"); } return (mapping.findForward("logon1")); } else { if (log.isDebugEnabled()) { log.debug("Unsuccessful logon, returning to input"); } saveErrors(request, errors); return (mapping.getInputForward()); } }
From source file:org.apache.struts.webapp.example.LogonAction.java
/** * Use "username" and "password" fields from ActionForm to retrieve a User * object from the database. If credentials are not valid, or database * has disappeared, post error messages and forward to input. * * @param mapping The ActionMapping used to select this instance * @param form The optional ActionForm bean for this request (if any) * @param request The HTTP request we are processing * @param response The HTTP response we are creating * * @exception Exception if the application business logic throws * an exception//from w w w .j a va 2s . c o m */ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // Local variables UserDatabase database = getUserDatabase(request); String username = (String) PropertyUtils.getSimpleProperty(form, USERNAME); String password = (String) PropertyUtils.getSimpleProperty(form, PASSWORD); ActionMessages errors = new ActionMessages(); // Retrieve user User user = getUser(database, username, password, errors); // Report back any errors, and exit if any if (!errors.isEmpty()) { this.saveErrors(request, errors); return (mapping.getInputForward()); } // Save user object SaveUser(request, user); // Otherwise, return "success" return (findSuccess(mapping)); }
From source file:org.apache.struts.webapp.example2.LogonAction.java
/** * Process the specified HTTP request, and create the corresponding HTTP * response (or forward to another web component that will create it). * Return an <code>ActionForward</code> instance describing where and how * control should be forwarded, or <code>null</code> if the response has * already been completed.// www .j a va2 s . c om * * @param mapping The ActionMapping used to select this instance * @param form The optional ActionForm bean for this request (if any) * @param request The HTTP request we are processing * @param response The HTTP response we are creating * * @exception Exception if business logic throws an exception */ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // Extract attributes we will need Locale locale = getLocale(request); MessageResources messages = getResources(request); User user = null; // Validate the request parameters specified by the user ActionErrors errors = new ActionErrors(); String username = (String) PropertyUtils.getSimpleProperty(form, "username"); String password = (String) PropertyUtils.getSimpleProperty(form, "password"); UserDatabase database = (UserDatabase) servlet.getServletContext().getAttribute(Constants.DATABASE_KEY); if (database == null) errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.database.missing")); else { user = getUser(database, username); if ((user != null) && !user.getPassword().equals(password)) user = null; if (user == null) errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.password.mismatch")); } // Report any errors we have discovered back to the original form if (!errors.isEmpty()) { saveErrors(request, errors); return (mapping.getInputForward()); } // Save our logged-in user in the session HttpSession session = request.getSession(); session.setAttribute(Constants.USER_KEY, user); if (log.isDebugEnabled()) { log.debug("LogonAction: User '" + user.getUsername() + "' logged on in session " + session.getId()); } // Remove the obsolete form bean if (mapping.getAttribute() != null) { if ("request".equals(mapping.getScope())) request.removeAttribute(mapping.getAttribute()); else session.removeAttribute(mapping.getAttribute()); } // Forward control to the specified success URI return (mapping.findForward("success")); }
From source file:org.apache.struts.webapp.validator.LocaleAction.java
/** * <p>//from w ww . j a v a 2s . co m * Change the user's {@link java.util.Locale} based on {@link ActionForm} * properties. * </p> * <p> * This <code>Action</code> looks for <code>language</code> and * <code>country</code> properties on the given form, constructs an * appropriate Locale object, and sets it as the Struts Locale for this * user's session. * Any <code>ActionForm, including a {@link DynaActionForm}, may be used. * </p> * <p> * If a <code>page</code> property is also provided, then after * setting the Locale, control is forwarded to that URI path. * Otherwise, control is forwarded to "success". * </p> * * @param mapping The ActionMapping used to select this instance * @param form The optional ActionForm bean for this request (if any) * @param request The HTTP request we are processing * @param response The HTTP response we are creating * * @return Action to forward to * @exception java.lang.Exception if an input/output error or servlet exception occurs */ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // Extract attributes we will need HttpSession session = request.getSession(); Locale locale = getLocale(request); String language = null; String country = null; String page = null; try { language = (String) PropertyUtils.getSimpleProperty(form, "language"); country = (String) PropertyUtils.getSimpleProperty(form, "country"); page = (String) PropertyUtils.getSimpleProperty(form, "page"); } catch (Exception e) { log.error(e.getMessage(), e); } if ((language != null && language.length() > 0) && (country != null && country.length() > 0)) { locale = new java.util.Locale(language, country); } else if (language != null && language.length() > 0) { locale = new java.util.Locale(language, ""); } session.setAttribute(Globals.LOCALE_KEY, locale); if (null == page) return mapping.findForward("success"); else return new ActionForward(page); }
From source file:org.apache.taverna.prov.Saver.java
protected Object getProperty(ExternalReferenceSPI ref, String propertyName) { try {/*from w ww . j a v a2 s. co m*/ return PropertyUtils.getSimpleProperty(ref, propertyName); } catch (Exception ex) { throw new IllegalArgumentException("Can't look up " + propertyName + " in bean " + ref, ex); } }
From source file:org.beangle.struts2.view.component.Select.java
public boolean isSelected(Object obj) { if (null == value) return false; else/*from w w w . j a v a2s.c o m*/ try { if (value instanceof Number) { Number v = (Number) value; return v.longValue() == new Long(PropertyUtils.getSimpleProperty(obj, keyName).toString()); } else if (value instanceof Collection) { @SuppressWarnings("rawtypes") Collection c = (Collection) value; for (Object o : c) { if (o.equals(obj)) { return true; } } return false; } else { try { if (obj instanceof Map) { return value.equals(((Map) obj).get(keyName)); } else { return value.equals(obj) || value.equals(PropertyUtils.getSimpleProperty(obj, keyName)); } } catch (Exception e) { return false; } } } catch (Exception e) { e.printStackTrace(); return false; } }
From source file:org.beangle.struts2.view.component245.Select.java
public boolean isSelected(Object obj) { if (null == value) return false; else/*from w ww. j a va2s . c om*/ try { if (value instanceof Number) { Number v = (Number) value; return v.longValue() == new Long(PropertyUtils.getSimpleProperty(obj, keyName).toString()); } else { try { return value.equals(obj) || value.equals(PropertyUtils.getSimpleProperty(obj, keyName)); } catch (Exception e) { return false; } } } catch (Exception e) { e.printStackTrace(); return false; } }
From source file:org.dcm4che3.conf.api.generic.ReflectiveConfig.java
/** * Walk through the <b>from</b> object and for each field annotated with * //w ww. j ava 2 s .c o m * @ConfigField, copy the value by using getter/setter to the <b>to</b> * object. * * @param from * @param to */ public static <T> void reconfigure(T from, T to) { // look through all fields of the config class, not including // superclass fields for (Field field : from.getClass().getDeclaredFields()) { // if field is not annotated, skip it ConfigField fieldAnno = (ConfigField) field.getAnnotation(ConfigField.class); if (fieldAnno == null) continue; try { PropertyUtils.setSimpleProperty(to, field.getName(), PropertyUtils.getSimpleProperty(from, field.getName())); } catch (Exception e) { throw new RuntimeException("Unable to reconfigure a device!", e); } } }
From source file:org.dcm4che3.conf.core.adapters.DefaultReferenceAdapter.java
@Override public Object toConfigNode(Object object, AnnotatedConfigurableProperty property, BeanVitalizer vitalizer) throws ConfigurationException { Map<String, Object> node = Configuration.NodeFactory.emptyNode(); AnnotatedConfigurableProperty uuidPropertyForClass = ConfigIterators .getUUIDPropertyForClass(property.getRawClass()); if (uuidPropertyForClass == null) throw new ConfigurationException("Class " + property.getRawClass().getName() + " cannot be referenced, because it lacks a UUID property"); String uuid;/*from w ww . ja va 2s. c om*/ try { uuid = (String) PropertyUtils.getSimpleProperty(object, uuidPropertyForClass.getName()); } catch (Exception e) { throw new ConfigurationException(e); } node.put(Configuration.REFERENCE_KEY, uuidReferencePath.set("uuid", uuid).path()); if (property.isWeakReference()) node.put(Configuration.WEAK_REFERENCE_KEY, true); return node; }
From source file:org.dcm4che3.conf.core.adapters.ReflectiveAdapter.java
@Override public Map<String, Object> toConfigNode(T object, AnnotatedConfigurableProperty property, BeanVitalizer vitalizer) throws ConfigurationException { if (object == null) return null; Class<T> clazz = (Class<T>) object.getClass(); Map<String, Object> configNode = new TreeMap<String, Object>(); // get data from all the configurable fields for (AnnotatedConfigurableProperty fieldProperty : ConfigIterators.getAllConfigurableFields(clazz)) { try {// w ww. j av a 2s.com Object value = PropertyUtils.getSimpleProperty(object, fieldProperty.getName()); DefaultConfigTypeAdapters.delegateChildToConfigNode(value, configNode, fieldProperty, vitalizer); } catch (Exception e) { throw new ConfigurationException("Error while serializing configuration field '" + fieldProperty.getName() + "' in class " + clazz.getSimpleName(), e); } } // there must be no setters for (ConfigIterators.AnnotatedSetter setter : ConfigIterators.getAllConfigurableSetters(clazz)) throw new ConfigurationUnserializableException( "Cannot infer properties which are setter parameters. This object has a setter (" + setter.getMethod().getName() + ")"); return configNode; }