Search in sources :

Example 11 with PropertyUtilsBean

use of org.apache.commons.beanutils.PropertyUtilsBean in project cobar by alibaba.

the class EditClusterPage method handleRequestInternal.

@SuppressWarnings("unchecked")
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    long clusterId = 0;
    try {
        clusterId = Long.parseLong(request.getParameter("clusterId").trim());
    } catch (Exception e) {
        throw new IllegalArgumentException("parameter 'clusterId' is invalid:" + request.getParameter("clusterId"));
    }
    ClusterDO cluster = xmlAccesser.getClusterDAO().getClusterById(clusterId);
    Map<String, Object> clusterMap = new PropertyUtilsBean().describe(cluster);
    clusterMap.remove("class");
    clusterMap.remove("name");
    clusterMap.remove("deployDesc");
    clusterMap.put("name", CobarStringUtil.htmlEscapedString(cluster.getName()));
    clusterMap.put("deployDesc", CobarStringUtil.htmlEscapedString(cluster.getDeployDesc()));
    return new ModelAndView("m_editCluster", new FluenceHashMap<String, Object>().putKeyValue("cluster", clusterMap));
}
Also used : ClusterDO(com.alibaba.cobar.manager.dataobject.xml.ClusterDO) PropertyUtilsBean(org.apache.commons.beanutils.PropertyUtilsBean) ModelAndView(org.springframework.web.servlet.ModelAndView) FluenceHashMap(com.alibaba.cobar.manager.util.FluenceHashMap)

Example 12 with PropertyUtilsBean

use of org.apache.commons.beanutils.PropertyUtilsBean in project cobar by alibaba.

the class MCobarListScreen method handleRequestInternal.

@SuppressWarnings({ "unchecked" })
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    UserDO user = (UserDO) request.getSession().getAttribute("user");
    long clusterId = Long.parseLong(request.getParameter("clusterId"));
    ClusterDO cluster = xmlAccesser.getClusterDAO().getClusterById(clusterId);
    List<CobarDO> cobarList = xmlAccesser.getCobarDAO().getCobarList(clusterId);
    List<Map<String, Object>> cobarViewList = null;
    if (null != cobarList) {
        ListSortUtil.sortCobarByName(cobarList);
        cobarViewList = new ArrayList<Map<String, Object>>();
        PropertyUtilsBean util = new PropertyUtilsBean();
        for (CobarDO c : cobarList) {
            Map<String, Object> map = util.describe(c);
            map.remove("class");
            map.remove("name");
            map.put("name", CobarStringUtil.htmlEscapedString(c.getName()));
            cobarViewList.add(map);
        }
    }
    Map<String, Object> clusterView = new HashMap<String, Object>();
    clusterView.put("id", cluster.getId());
    clusterView.put("name", CobarStringUtil.htmlEscapedString(cluster.getName()));
    return new ModelAndView("m_cobarList", new FluenceHashMap<String, Object>().putKeyValue("cobarList", cobarViewList).putKeyValue("user", user).putKeyValue("cluster", clusterView));
}
Also used : PropertyUtilsBean(org.apache.commons.beanutils.PropertyUtilsBean) HashMap(java.util.HashMap) FluenceHashMap(com.alibaba.cobar.manager.util.FluenceHashMap) UserDO(com.alibaba.cobar.manager.dataobject.xml.UserDO) ModelAndView(org.springframework.web.servlet.ModelAndView) FluenceHashMap(com.alibaba.cobar.manager.util.FluenceHashMap) CobarDO(com.alibaba.cobar.manager.dataobject.xml.CobarDO) ClusterDO(com.alibaba.cobar.manager.dataobject.xml.ClusterDO) HashMap(java.util.HashMap) FluenceHashMap(com.alibaba.cobar.manager.util.FluenceHashMap) Map(java.util.Map)

Example 13 with PropertyUtilsBean

use of org.apache.commons.beanutils.PropertyUtilsBean in project cobar by alibaba.

the class PropertyReloadScreen method handleRequestInternal.

@SuppressWarnings("unchecked")
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    UserDO user = (UserDO) request.getSession().getAttribute("user");
    String id = request.getParameter("clusterId");
    long clusterId = -1;
    if (null != id) {
        clusterId = Long.parseLong(id);
    }
    List<ClusterDO> cList = xmlAccesser.getClusterDAO().listAllCluster();
    List<Map<String, Object>> clusterList = new ArrayList<Map<String, Object>>();
    ListSortUtil.sortClusterByName(cList);
    for (ClusterDO e : cList) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", e.getId());
        map.put("name", CobarStringUtil.htmlEscapedString(e.getName()));
        clusterList.add(map);
    }
    List<CobarDO> cobarList = null;
    if (null != cList && cList.size() > 0) {
        if (-1 == clusterId) {
            clusterId = cList.get(0).getId();
            cobarList = xmlAccesser.getCobarDAO().getCobarList(clusterId);
        } else {
            cobarList = xmlAccesser.getCobarDAO().getCobarList(clusterId);
        }
    }
    List<Map<String, Object>> cobarListMap = new ArrayList<Map<String, Object>>();
    PropertyUtilsBean util = new PropertyUtilsBean();
    if (null != cobarList) {
        ListSortUtil.sortCobarByName(cobarList);
        for (CobarDO c : cobarList) {
            CobarAdapterDAO perf = cobarAccesser.getAccesser(c.getId());
            Map<String, Object> map;
            try {
                map = util.describe(c);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
            map.remove("class");
            map.remove("name");
            map.put("name", CobarStringUtil.htmlEscapedString(c.getName()));
            if (ConstantDefine.ACTIVE.equals(c.getStatus())) {
                if (!perf.checkConnection()) {
                    map.remove("status");
                    map.put("status", ConstantDefine.ERROR);
                    map.put("reloadTime", "");
                    map.put("rollbackTime", "");
                } else {
                    ServerStatus ss = perf.getServerStatus();
                    String rollbackTime = "NO";
                    String reloadTime = FormatUtil.fromMilliseconds2String(ss.getReloadTime());
                    if (ss.getRollbackTime() != -1) {
                        rollbackTime = FormatUtil.fromMilliseconds2String(ss.getRollbackTime());
                    }
                    map.put("reloadTime", reloadTime);
                    map.put("rollbackTime", rollbackTime);
                }
            } else {
                map.put("reloadTime", "");
                map.put("rollbackTime", "");
            }
            cobarListMap.add(map);
        }
    }
    return new ModelAndView("c_propertyReload", new FluenceHashMap<String, Object>().putKeyValue("cList", clusterList).putKeyValue("cobarList", cobarListMap).putKeyValue("clusterId", clusterId).putKeyValue("user", user));
}
Also used : CobarAdapterDAO(com.alibaba.cobar.manager.dao.CobarAdapterDAO) HashMap(java.util.HashMap) FluenceHashMap(com.alibaba.cobar.manager.util.FluenceHashMap) PropertyUtilsBean(org.apache.commons.beanutils.PropertyUtilsBean) UserDO(com.alibaba.cobar.manager.dataobject.xml.UserDO) ArrayList(java.util.ArrayList) ModelAndView(org.springframework.web.servlet.ModelAndView) CobarDO(com.alibaba.cobar.manager.dataobject.xml.CobarDO) ClusterDO(com.alibaba.cobar.manager.dataobject.xml.ClusterDO) ServerStatus(com.alibaba.cobar.manager.dataobject.cobarnode.ServerStatus) HashMap(java.util.HashMap) FluenceHashMap(com.alibaba.cobar.manager.util.FluenceHashMap) Map(java.util.Map)

Example 14 with PropertyUtilsBean

use of org.apache.commons.beanutils.PropertyUtilsBean in project apex-core by apache.

the class InjectConfigTest method testBeanUtils.

@Test
public void testBeanUtils() throws Exception {
    // http://www.cowtowncoder.com/blog/archives/2011/02/entry_440.html
    BeanUtilsTestBean testBean = new BeanUtilsTestBean();
    testBean.url = new URL("http://localhost:12345/context");
    ObjectMapper mapper = new ObjectMapper();
    Map<String, Object> properties = mapper.convertValue(testBean, Map.class);
    LOG.debug("testBean source: {}", properties);
    BeanUtilsTestBean testBean2 = new BeanUtilsTestBean();
    testBean2.string2 = "testBean2";
    Assert.assertFalse("contains transientProperty", properties.containsKey("transientProperty"));
    Assert.assertTrue("contains string2", properties.containsKey("string2"));
    // remove null
    properties.remove("string2");
    //properties.put("string3", "");
    BeanUtilsBean bub = new BeanUtilsBean();
    try {
        bub.getProperty(testBean, "invalidProperty");
        Assert.fail("exception expected");
    } catch (Exception e) {
        Assert.assertTrue(e.getMessage().contains("Unknown property 'invalidProperty'"));
    }
    bub.setProperty(properties, "mapProperty.someKey", "someValue");
    JsonNode sourceTree = mapper.convertValue(testBean2, JsonNode.class);
    JsonNode updateTree = mapper.convertValue(properties, JsonNode.class);
    merge(sourceTree, updateTree);
    //   mapper.readerForUpdating(testBean2).readValue(sourceTree);
    //   Assert.assertEquals("preserve existing value", "testBean2", testBean2.string2);
    //   Assert.assertEquals("map property", "someValue", testBean2.mapProperty.get("someKey"));
    //   LOG.debug("testBean cloned: {}", mapper.convertValue(testBean2, Map.class));
    PropertyUtilsBean propertyUtilsBean = BeanUtilsBean.getInstance().getPropertyUtils();
    // set value on non-existing property
    try {
        propertyUtilsBean.setProperty(testBean, "nonExistingProperty.someProperty", "ddd");
        Assert.fail("should throw exception");
    } catch (NoSuchMethodException e) {
        Assert.assertTrue("" + e, e.getMessage().contains("Unknown property 'nonExistingProperty'"));
    }
    // set value on read-only property
    try {
        testBean.getMapProperty().put("s", "s1Val");
        PropertyDescriptor pd = propertyUtilsBean.getPropertyDescriptor(testBean, "mapProperty");
        Class<?> type = propertyUtilsBean.getPropertyType(testBean, "mapProperty.s");
        propertyUtilsBean.setProperty(testBean, "mapProperty", Integer.valueOf(1));
        Assert.fail("should throw exception");
    } catch (Exception e) {
        Assert.assertTrue("" + e, e.getMessage().contains("Property 'mapProperty' has no setter method"));
    }
    // type mismatch
    try {
        propertyUtilsBean.setProperty(testBean, "intProp", "s1");
        Assert.fail("should throw exception");
    } catch (Exception e) {
        Assert.assertEquals(e.getClass(), IllegalArgumentException.class);
    }
    try {
        propertyUtilsBean.setProperty(testBean, "intProp", "1");
    } catch (IllegalArgumentException e) {
        // BeanUtils does not report invalid properties, but it handles type conversion, which above doesn't
        Assert.assertEquals("", 0, testBean.getIntProp());
        // by default beanutils ignores conversion error
        bub.setProperty(testBean, "intProp", "1");
        Assert.assertEquals("", 1, testBean.getIntProp());
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) PropertyUtilsBean(org.apache.commons.beanutils.PropertyUtilsBean) JsonNode(org.codehaus.jackson.JsonNode) URL(java.net.URL) BeanUtilsBean(org.apache.commons.beanutils.BeanUtilsBean) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Test(org.junit.Test)

Aggregations

PropertyUtilsBean (org.apache.commons.beanutils.PropertyUtilsBean)14 HashMap (java.util.HashMap)10 Map (java.util.Map)10 ClusterDO (com.alibaba.cobar.manager.dataobject.xml.ClusterDO)7 FluenceHashMap (com.alibaba.cobar.manager.util.FluenceHashMap)7 ModelAndView (org.springframework.web.servlet.ModelAndView)7 CobarDO (com.alibaba.cobar.manager.dataobject.xml.CobarDO)6 ArrayList (java.util.ArrayList)6 UserDO (com.alibaba.cobar.manager.dataobject.xml.UserDO)5 CobarAdapterDAO (com.alibaba.cobar.manager.dao.CobarAdapterDAO)3 IOException (java.io.IOException)3 ServletException (javax.servlet.ServletException)3 JSONObject (net.sf.json.JSONObject)3 JSONArray (net.sf.json.JSONArray)2 BeanUtilsBean (org.apache.commons.beanutils.BeanUtilsBean)2 CommandStatus (com.alibaba.cobar.manager.dataobject.cobarnode.CommandStatus)1 DataNodesStatus (com.alibaba.cobar.manager.dataobject.cobarnode.DataNodesStatus)1 ProcessorStatus (com.alibaba.cobar.manager.dataobject.cobarnode.ProcessorStatus)1 ServerStatus (com.alibaba.cobar.manager.dataobject.cobarnode.ServerStatus)1 Pair (com.alibaba.cobar.manager.util.Pair)1