Example usage for org.apache.commons.beanutils PropertyUtils describe

List of usage examples for org.apache.commons.beanutils PropertyUtils describe

Introduction

In this page you can find the example usage for org.apache.commons.beanutils PropertyUtils describe.

Prototype

public static Map describe(Object bean)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException 

Source Link

Document

Return the entire set of properties for which the specified bean provides a read method.

For more details see PropertyUtilsBean.

Usage

From source file:de.hybris.platform.acceleratorservices.metainformation.HtmlMetaTag.java

protected Map<String, String> getAttributesMap(final MetaElementData metaElementData) {
    final Map<String, String> attributes = new LinkedHashMap<>();
    try {//from w w w .jav a2s .co m
        final Map<String, Object> fieldValues = PropertyUtils.describe(metaElementData);
        for (final Entry<String, Object> fieldData : fieldValues.entrySet()) {
            if ("class".equals(fieldData.getKey())) {
                continue;
            }
            final String name = metaElementAttributeNameResolver.resolveName(fieldData.getKey());
            String value = fieldData.getValue() == null ? null : fieldData.getValue().toString();
            if (value != null) {
                value = StringEscapeUtils.escapeHtml(value);
            }

            if (StringUtils.isNotEmpty(name) && StringUtils.isNotEmpty(value)) {
                attributes.put(name, value);
            }
        }
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
        LOG.error("Exception while running PropertyUtils.describe", e);
    }
    return attributes;
}

From source file:com.m2a.struts.M2ALoginAction.java

/**
 * Exposes result in a servlet context.// w  ww  .j a  va 2s .c om
 *
 * @param mapping the request being serviced
 * @param request the request being serviced
 * @param request the request being serviced
 * @param name The name to use in scope
 * @param scope The scope to set the attribute in
 * @param bean The attribute to be set
 */

protected void exposeInScope(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response, String name, String scope, Object bean) {

    if (log.isDebugEnabled()) {
        log.debug("*** Executing exposeInScope()");
    }

    if (null == scope) {
        if (log.isDebugEnabled()) {
            log.debug("*** Scope is NULL");
        }
    } else {
        if (log.isDebugEnabled()) {
            log.debug("*** Scope is " + scope);
        }
    }

    if (null == bean) {
        if (log.isDebugEnabled()) {
            log.debug("*** Result bean is NULL");
        }
    } else if (Tokens.REQUEST.equals(scope)) {
        if (log.isDebugEnabled()) {
            log.debug("*** Setting result bean in Request scope");
        }
        request.setAttribute(name, bean);
    } else if (Tokens.SESSION.equals(scope)) {
        if (log.isDebugEnabled()) {
            log.debug("*** Setting result bean in Session scope");
        }
        request.getSession().setAttribute(name, bean);
    } else if (Tokens.APPLICATION.equals(scope)) {
        if (log.isDebugEnabled()) {
            log.debug("*** Setting result bean in Application scope");
        }
        servlet.getServletContext().setAttribute(name, bean);
    } else if (M2ATokens.CLIENT.equals(scope)) {
        if (log.isDebugEnabled()) {
            log.debug("*** Setting result bean in Client scope");
        }
        Map beanMap = null;
        Object attribObj = null;
        String customerNo = null;
        String loginKey = null;
        String sessionKey = null;
        String customerCountry = null;

        if (bean != null) {
            try {
                beanMap = PropertyUtils.describe(bean);
                attribObj = beanMap.get(M2ATokens.CUSTOMERNO);
                if (attribObj != null) {
                    customerNo = (String) attribObj;
                }

                attribObj = beanMap.get(M2ATokens.LOGINKEY);
                if (attribObj != null) {
                    loginKey = (String) attribObj;
                }

                attribObj = beanMap.get(M2ATokens.SESSIONKEY);
                if (attribObj != null) {
                    sessionKey = (String) attribObj;
                }

                attribObj = beanMap.get(M2ATokens.CUSTOMERCOUNTRY);
                if (attribObj != null) {
                    customerCountry = (String) attribObj;
                }

                if (log.isDebugEnabled()) {
                    log.debug("*** Cookie value is being set for " + M2ATokens.COOKIE_CUSTOMERNO + " "
                            + customerNo);
                    log.debug(
                            "*** Cookie value is being set for " + M2ATokens.COOKIE_LOGINKEY + " " + loginKey);
                    log.debug("*** Cookie value is being set for " + M2ATokens.COOKIE_SESSIONKEY + " "
                            + sessionKey);
                    log.debug("*** Cookie value is being set for " + M2ATokens.COOKIE_CUSTOMERCOUNTRY + " "
                            + customerCountry);
                }

                /*
                 ** Added on Sep 07 2003
                 ** contextPath included in the setCookie method
                 ** this is required for application which has sub applications
                 ** otherwise send empty string
                 */
                String contextPath = request.getContextPath();
                M2ACookies.setCookie(response, M2ATokens.COOKIE_CUSTOMERNO, customerNo, contextPath);
                M2ACookies.setCookie(response, M2ATokens.COOKIE_LOGINKEY, loginKey, contextPath);
                M2ACookies.setCookie(response, M2ATokens.COOKIE_SESSIONKEY, sessionKey, contextPath);
                M2ACookies.setCookie(response, M2ATokens.COOKIE_CUSTOMERCOUNTRY, customerCountry, contextPath);

                /**
                 * Values set into cookies can not be read in this same process
                 * Cookie value set into response object and can not be referred in
                 * before returning back to the client.
                 * When we have 2 process bean and result set returned by the first
                 * process bean used by the second bean then we need to set into form
                 * Set values into the form passed
                 */
                if (log.isDebugEnabled()) {
                    log.debug("*** Setting result bean into form");
                }

                M2AFormBase passedForm = (M2AFormBase) form;
                passedForm.set(bean);

            } catch (Exception e) {

            }
        }
    } else if (M2ATokens.FORM.equals(scope)) {
        if (log.isDebugEnabled()) {
            log.debug("*** Setting result bean into form");
        }
        M2AFormBase passedForm = (M2AFormBase) form;
        try {
            passedForm.set(bean);
        } catch (Exception e) {

        }
    } else if (M2ATokens.EMPTY.equals(scope)) {
        if (log.isDebugEnabled()) {
            log.debug("*** Setting result bean in Empty Scope");
        }
        // do nothing
    } else {
        StringBuffer sb = new StringBuffer("exposeInScope: ");
        sb.append(scope);
        sb.append(Tokens.INVALID_SCOPE);
        if (log.isDebugEnabled()) {
            log.debug("*** " + sb.toString());
        }
        throw new IllegalArgumentException(sb.toString());
    }

}

From source file:net.firejack.platform.generate.VelocityGenerator.java

public String compose(String name, Object model) {
    try {// w w  w . j av a2s . co m
        Map describe = PropertyUtils.describe(model);
        return compose(name, describe);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.stratio.decision.functions.engine.DroolsEngineAction.java

private List<Map<String, Object>> formatDroolsResults(Results results) throws Exception {

    List<Map<String, Object>> outputList = new ArrayList<>();

    for (Object singleResult : results.getResults()) {

        Map<String, Object> outputMap = null;

        try {//from   w w  w.  j  a v  a2 s. c  o m

            Object propertyObject = PropertyUtils.getProperty(singleResult, "result");
            //outputMap = PropertyUtils.describe(singleResult);
            outputMap = PropertyUtils.describe(propertyObject);

        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            throw new Exception(e);
        }

        outputList.add(outputMap);

    }

    return outputList;

}

From source file:com.utest.webservice.builders.Builder.java

protected void populateLocators(Ti result, UriBuilder ub)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    Map<?, ?> resultProperties = PropertyUtils.describe(result);
    for (Object property : resultProperties.keySet()) {
        if (((String) property).endsWith("Locator")) {
            String resourcePath = null;
            Integer resourceId = null;
            String resourceName = ((String) property).substring(0, ((String) property).indexOf("Locator"));
            if (resultProperties.containsKey(resourceName + "Id")) {
                resourceId = (Integer) PropertyUtils.getProperty(result, resourceName + "Id");
                if (resourceId != null) {
                    resourcePath = getResourcePath(resourceName.toLowerCase());
                }/*  ww  w  .j  a v a 2s . c  om*/
            }
            // represents a user who performed an operation
            else if (resourceName.endsWith("By")) {
                resourceId = (Integer) PropertyUtils.getProperty(result, resourceName);
                if (resourceId != null) {
                    resourcePath = getResourcePath("user");
                }
            }
            if (resourcePath != null) {
                String className = getResourceNamedClass(resourceName.toLowerCase());
                String name = "";
                if (className != null) {
                    try {
                        Class<?> clazz = Class.forName(className);
                        Object namedEntity = factory.getStaticDataService().getEntity(clazz, resourceId);
                        if (namedEntity != null && namedEntity instanceof Named) {
                            name = ((Named) namedEntity).getName();
                        }
                    } catch (ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                ResourceLocator resourceLocator = new ResourceLocator(resourceId,
                        ub.clone().path(resourcePath).build(resourceId).toString(), name);
                PropertyUtils.setProperty(result, (String) property, resourceLocator);
            }
        }
    }
}

From source file:com.m2a.struts.M2AProcessBridgeAction.java

/**
 * Exposes result in a servlet context./*from   w  w w .j  ava2 s. co m*/
 *
 * @param mapping the request being serviced
 * @param request the request being serviced
 * @param request the request being serviced
 * @param name The name to use in scope
 * @param scope The scope to set the attribute in
 * @param bean The attribute to be set
 */
protected void exposeInScope(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response, String name, String scope, Object bean) {

    if (log.isDebugEnabled()) {
        log.debug("*** Executing exposeInScope()");
    }

    if (null == scope) {
        if (log.isDebugEnabled()) {
            log.debug("*** Scope is NULL");
        }
    } else {
        if (log.isDebugEnabled()) {
            log.debug("*** Scope is " + scope);
        }
    }

    if (null == bean) {
        if (log.isDebugEnabled()) {
            log.debug("*** Result bean is NULL");
        }
    } else if (Tokens.REQUEST.equals(scope)) {
        if (log.isDebugEnabled()) {
            log.debug("*** Setting result bean in Request scope");
        }
        request.setAttribute(name, bean);
    } else if (Tokens.SESSION.equals(scope)) {
        if (log.isDebugEnabled()) {
            log.debug("*** Setting result bean in Session scope");
        }
        request.getSession().setAttribute(name, bean);
    } else if (Tokens.APPLICATION.equals(scope)) {
        if (log.isDebugEnabled()) {
            log.debug("*** Setting result bean in Application scope");
        }
        servlet.getServletContext().setAttribute(name, bean);
    } else if (M2ATokens.CLIENT.equals(scope)) {
        if (log.isDebugEnabled()) {
            log.debug("*** Setting result bean in Client scope");
        }
        Map beanMap = null;
        Object attribObj = null;
        String customerNo = null;
        String loginKey = null;
        String sessionKey = null;
        String customerCountry = null;

        if (bean != null) {
            try {
                beanMap = PropertyUtils.describe(bean);
                attribObj = beanMap.get(M2ATokens.CUSTOMERNO);
                if (attribObj != null) {
                    customerNo = (String) attribObj;
                }

                attribObj = beanMap.get(M2ATokens.LOGINKEY);
                if (attribObj != null) {
                    loginKey = (String) attribObj;
                }

                attribObj = beanMap.get(M2ATokens.SESSIONKEY);
                if (attribObj != null) {
                    sessionKey = (String) attribObj;
                }

                attribObj = beanMap.get(M2ATokens.CUSTOMERCOUNTRY);
                if (attribObj != null) {
                    customerCountry = (String) attribObj;
                }

                if (log.isDebugEnabled()) {
                    log.debug("*** Cookie value is being set for " + M2ATokens.COOKIE_CUSTOMERNO + " "
                            + customerNo);
                    log.debug(
                            "*** Cookie value is being set for " + M2ATokens.COOKIE_LOGINKEY + " " + loginKey);
                    log.debug("*** Cookie value is being set for " + M2ATokens.COOKIE_SESSIONKEY + " "
                            + sessionKey);
                    log.debug("*** Cookie value is being set for " + M2ATokens.COOKIE_CUSTOMERCOUNTRY + " "
                            + customerCountry);
                }

                System.out.println(" CLIENT --> " + customerCountry);

                String contextPath = request.getContextPath();
                M2ACookies.setCookie(response, M2ATokens.COOKIE_CUSTOMERNO, customerNo, contextPath);
                M2ACookies.setCookie(response, M2ATokens.COOKIE_LOGINKEY, loginKey, contextPath);
                M2ACookies.setCookie(response, M2ATokens.COOKIE_SESSIONKEY, sessionKey, contextPath);
                M2ACookies.setCookie(response, M2ATokens.COOKIE_CUSTOMERCOUNTRY, customerCountry, contextPath);
                /**
                 * Values set into cookies can be read in this same process
                 * When we have 2 process bean and result set returned by the first
                 * process bean used by the second bean then we need to set into form
                 * Set values into the form passed
                 */
                if (log.isDebugEnabled()) {
                    log.debug("*** Setting result bean into form");
                }
                M2AFormBase passedForm = (M2AFormBase) form;
                passedForm.set(bean);

            } catch (Exception e) {

            }
        }
    } else if (M2ATokens.FORM.equals(scope)) {
        if (log.isDebugEnabled()) {
            log.debug("*** Setting result bean into form");
        }
        M2AFormBase passedForm = (M2AFormBase) form;
        try {
            passedForm.set(bean);
        } catch (Exception e) {

        }
    } else if (M2ATokens.FORM_LIST.equals(scope)) { // 20111212:1
        if (log.isDebugEnabled()) {
            log.debug("*** Setting result bean into form list");
        }
        M2AFormBase passedForm = (M2AFormBase) form;
        try {
            passedForm.set(bean, name);
        } catch (Exception e) {

        }
    } else if (M2ATokens.EMPTY.equals(scope)) {
        if (log.isDebugEnabled()) {
            log.debug("*** Setting result bean in Empty Scope");
        }
        // do nothing
    } else {
        StringBuffer sb = new StringBuffer("exposeInScope: ");
        sb.append(scope);
        sb.append(Tokens.INVALID_SCOPE);
        if (log.isDebugEnabled()) {
            log.debug("*** " + sb.toString());
        }
        throw new IllegalArgumentException(sb.toString());
    }

}

From source file:com.sun.faces.mock.MockResult.java

public SortedMap[] getRows() {

    TreeMap results[] = new TreeMap[beans.length];
    for (int i = 0; i < results.length; i++) {
        try {/*from www .j a  va 2  s .  co  m*/
            results[i] = new TreeMap(PropertyUtils.describe(beans[i]));
        } catch (Exception e) {
            throw new FacesException(e);
        }
    }
    return (results);

}

From source file:io.pelle.mango.db.dao.BaseEntityDAO.java

@SuppressWarnings("unchecked")
private void populateClients(IBaseClientEntity clientEntity, List<IBaseClientEntity> visited) {
    try {/*from www  . java 2  s .c o m*/
        clientEntity.setClient(getCurrentUser().getClient());
        visited.add(clientEntity);

        for (Map.Entry<String, Object> entry : ((Map<String, Object>) PropertyUtils.describe(clientEntity))
                .entrySet()) {

            String propertyName = entry.getKey();
            PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(clientEntity,
                    propertyName);

            if (propertyDescriptor != null) {
                Object attribute = PropertyUtils.getSimpleProperty(clientEntity, propertyName);

                if (attribute != null && IBaseClientEntity.class.isAssignableFrom(attribute.getClass())) {
                    if (!visited.contains(attribute)) {
                        populateClients((IBaseClientEntity) attribute, visited);
                    }
                }

                if (attribute != null && List.class.isAssignableFrom(attribute.getClass())) {
                    List<?> list = (List<?>) attribute;

                    for (Object listElement : list) {
                        if (IBaseClientEntity.class.isAssignableFrom(listElement.getClass())) {
                            if (!visited.contains(attribute)) {
                                populateClients((IBaseClientEntity) listElement, visited);
                            }
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("error setting client", e);
    }
}

From source file:com.sunchenbin.store.feilong.core.bean.PropertyUtil.java

/**
 * <code>bean</code> <span style="color:green">?</span>,??/Map.
 * //from w ww  .  ja  va2s.c o  m
 * <p>
 * ???class,Object??,classjava.lang.Object
 * </p>
 * 
 * <h3>?:</h3>
 * 
 * <blockquote>
 * <ol>
 * <li>?bean class {@link java.beans.PropertyDescriptor}</li>
 * <li>, {@link java.beans.PropertyDescriptor#getReadMethod()}</li>
 * <li> name and {@link org.apache.commons.beanutils.PropertyUtilsBean#getProperty(Object, String)} map</li>
 * </ol>
 * </blockquote>
 *
 * @param bean
 *            Bean whose properties are to be extracted
 * @return The set of properties for the bean
 * @see org.apache.commons.beanutils.BeanUtils#describe(Object)
 * @see org.apache.commons.beanutils.PropertyUtils#describe(Object)
 * @see com.sunchenbin.store.feilong.core.bean.BeanUtil#describe(Object)
 */
public static Map<String, Object> describe(Object bean) {
    try {
        //Return the entire set of properties for which the specified bean provides a read method.
        return PropertyUtils.describe(bean);
    } catch (Exception e) {
        LOGGER.error(e.getClass().getName(), e);
        throw new BeanUtilException(e);
    }
}

From source file:hermes.browser.dialog.EditNamingConfigDialog.java

@SuppressWarnings("unchecked")
public void doSelectionChanged() {
    try {//from w  ww.  jav a  2  s  . co  m
        final String selectedConfig = (String) comboBox.getSelectedItem();
        final NamingConfig config = (NamingConfig) namingConfigsByName.get(selectedConfig);
        final PropertySetConfig propertySet = config.getProperties();

        if (currentSelection == null || !currentSelection.equals(selectedConfig)) {
            currentSelection = selectedConfig;

            bean = new JNDIContextFactory();

            LoaderSupport.populateBean(bean, propertySet);

            final Map properties = PropertyUtils.describe(bean);
            final List list = new ArrayList();

            classpathIdProperty = new Property("loader", "Classpath Loader to use.", String.class) {
                /**
                * 
                */
                private static final long serialVersionUID = -3071689960943636606L;
                private String classpathId = config.getClasspathId();

                public void setValue(Object value) {
                    classpathId = value.toString();
                }

                public Object getValue() {
                    return classpathId;
                }

                public boolean hasValue() {
                    return true;
                }
            };

            classpathIdProperty.setEditorContext(ClasspathIdCellEdtitor.CONTEXT);

            list.add(classpathIdProperty);

            for (Iterator iter = properties.entrySet().iterator(); iter.hasNext();) {
                final Map.Entry entry = (Map.Entry) iter.next();
                final String propertyName = (String) entry.getKey();
                final Object propertyValue = entry.getValue() != null ? entry.getValue() : "";

                if (!propertyName.equals("class") && !propertyName.equals("name")) {
                    Property displayProperty = new Property(propertyName, propertyName,
                            PropertyUtils.getPropertyType(bean, propertyName)) {
                        /**
                        * 
                        */
                        private static final long serialVersionUID = 1751773758147906036L;

                        public void setValue(Object value) {
                            try {
                                PropertyUtils.setProperty(bean, propertyName, value);
                            } catch (Exception e) {
                                log.error(e.getMessage(), e);
                            }
                        }

                        public Object getValue() {
                            try {
                                return PropertyUtils.getProperty(bean, propertyName);
                            } catch (Exception e) {
                                log.error(e.getMessage(), e);
                            }

                            return null;
                        }

                        public boolean hasValue() {
                            return true;
                        }
                    };

                    list.add(displayProperty);
                }
            }

            final PropertyTableModel model = new PropertyTableModel(list);
            final PropertyTable table = new PropertyTable(model);

            table.setAutoResizeMode(PropertyTable.AUTO_RESIZE_ALL_COLUMNS);

            PropertyPane pane = new PropertyPane(table);

            pane.addPropertyChangeListener(new PropertyChangeListener() {
                public void propertyChange(PropertyChangeEvent evt) {

                }
            });

            model.expandAll();

            scrollPane.setViewportView(pane);
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);

        HermesBrowser.getBrowser().showErrorDialog(e);
    }
}