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));
}
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));
}
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));
}
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());
}
}
Aggregations