List of usage examples for org.apache.commons.beanutils BeanMap BeanMap
public BeanMap(Object bean)
BeanMap
�作指定的bean, 如果给定的bean是null
, 那么 这个map将会是empty。 From source file:org.pentaho.big.data.impl.cluster.NamedClusterImpl.java
public String toXmlForEmbed(String rootTag) { BeanMap m = new BeanMap(this); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = null; Document doc = null;/* w w w . ja va 2s . c o m*/ try { builder = dbf.newDocumentBuilder(); doc = builder.newDocument(); Element rootNode = doc.createElement(rootTag); doc.appendChild(rootNode); Iterator<Map.Entry<Object, Object>> i = m.entryIterator(); while (i.hasNext()) { Map.Entry<Object, Object> entry = i.next(); String elementName = (String) entry.getKey(); if (!"class".equals(elementName) && !"parentVariableSpace".equals(elementName)) { String value = ""; String type = "String"; Object o = entry.getValue(); if (o != null) { if (o instanceof Long) { value = Long.toString((Long) o); } else if (o instanceof Boolean) { value = Boolean.toString((Boolean) o); } else { try { value = (String) entry.getValue(); if (elementName.toLowerCase().contains("password")) { value = passwordEncoder.encode(value); } } catch (Exception e) { e.printStackTrace(); } } } rootNode.appendChild(createChildElement(doc, elementName, type, value)); } } DOMSource domSource = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.transform(domSource, result); String s = writer.toString(); // Remove header from the XML s = s.substring(s.indexOf(">") + 1); return s; } catch (ParserConfigurationException | TransformerException e1) { LOGGER.error("Could not parse embedded cluster xml" + e1.toString()); return ""; } }
From source file:org.red5.io.AbstractIOTest.java
@Test @SuppressWarnings({ "rawtypes" }) public void testJavaBean() { log.debug("\ntestJavaBean"); TestJavaBean beanIn = new TestJavaBean(); beanIn.setTestString("test string here"); beanIn.setTestBoolean((System.currentTimeMillis() % 2 == 0) ? true : false); beanIn.setTestBooleanObject((System.currentTimeMillis() % 2 == 0) ? Boolean.TRUE : Boolean.FALSE); beanIn.setTestNumberObject(Integer.valueOf((int) System.currentTimeMillis() / 1000)); Serializer.serialize(out, beanIn);//from ww w. j a v a2 s.c o m dumpOutput(); Object mapOrBean = Deserializer.deserialize(in, Object.class); assertEquals(beanIn.getClass().getName(), mapOrBean.getClass().getName()); Map<?, ?> map = (mapOrBean instanceof Map) ? (Map<?, ?>) mapOrBean : new BeanMap(mapOrBean); Set<?> entrySet = map.entrySet(); Iterator<?> it = entrySet.iterator(); Map beanInMap = new BeanMap(beanIn); assertEquals(beanInMap.size(), map.size()); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); String propOut = (String) entry.getKey(); Object valueOut = entry.getValue(); assertTrue(beanInMap.containsKey(propOut)); assertEquals(valueOut, beanInMap.get(propOut)); } resetOutput(); }
From source file:org.red5.io.utils.ConversionUtils.java
/** * Convert bean to map//from w ww .j a v a 2 s . c om * * @param source * Source bean * @return Converted map */ public static Map<?, ?> convertBeanToMap(Object source) { return new BeanMap(source); }
From source file:org.red5.server.io.AbstractIOTest.java
@SuppressWarnings({ "rawtypes" }) public void testJavaBean() { log.debug("Testing list"); TestJavaBean beanIn = new TestJavaBean(); beanIn.setTestString("test string here"); beanIn.setTestBoolean((System.currentTimeMillis() % 2 == 0) ? true : false); beanIn.setTestBooleanObject((System.currentTimeMillis() % 2 == 0) ? Boolean.TRUE : Boolean.FALSE); beanIn.setTestNumberObject(Integer.valueOf((int) System.currentTimeMillis() / 1000)); serializer.serialize(out, beanIn);// www . j a v a 2 s.com dumpOutput(); Object mapOrBean = deserializer.deserialize(in, Object.class); Assert.assertEquals(beanIn.getClass().getName(), mapOrBean.getClass().getName()); Map<?, ?> map = (mapOrBean instanceof Map) ? (Map<?, ?>) mapOrBean : new BeanMap(mapOrBean); Set<?> entrySet = map.entrySet(); Iterator<?> it = entrySet.iterator(); Map beanInMap = new BeanMap(beanIn); Assert.assertEquals(beanInMap.size(), map.size()); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); String propOut = (String) entry.getKey(); Object valueOut = entry.getValue(); Assert.assertTrue(beanInMap.containsKey(propOut)); Assert.assertEquals(valueOut, beanInMap.get(propOut)); } resetOutput(); }
From source file:org.seedstack.seed.core.utils.SeedBeanUtils.java
/** * Set properties derived from configuration on a bean. * * <ul>//from w w w .ja va 2 s . com * <li>[prefix].property.* gives the properties to set.</li> * </ul> * * @param bean the bean to set properties on. * @param configuration the configuration to derive properties from. * @param prefix the property prefix. */ public static void setPropertiesFromConfiguration(Object bean, Configuration configuration, String prefix) { BeanMap beanMap = new BeanMap(bean); Properties properties = SeedConfigurationUtils.buildPropertiesFromConfiguration(configuration, prefix); for (String key : properties.stringPropertyNames()) { String value = properties.getProperty(key); try { PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(bean, key); if (propertyDescriptor == null) { throw SeedException.createNew(CoreUtilsErrorCode.PROPERTY_NOT_FOUND).put("property", key) .put("class", bean.getClass().getCanonicalName()); } beanMap.put(key, value); } catch (Exception e) { throw SeedException.wrap(e, CoreUtilsErrorCode.UNABLE_TO_SET_PROPERTY).put("property", key) .put("class", bean.getClass().getCanonicalName()).put("value", value); } } }
From source file:org.sindice.rdfcommons.beanmapper.BeanDeserializer.java
@SuppressWarnings("unchecked") public <T> T deserialize(DeserializationContext context, Class<T> clazz, Annotation[] annotations, Identifier identifier, QueryEndpoint endPoint) throws DeserializationException { final T bean; try {/*from w ww . j av a 2s.c o m*/ bean = clazz.newInstance(); } catch (Exception e) { throw new DeserializationException("Error while instantiating object.", e); } context.registerInstance(identifier, bean); final String classUrl = getClassURL(bean.getClass()); final BeanMap beanMap = new BeanMap(bean); String propertyURL; for (Map.Entry<String, Object> entry : (Set<Map.Entry<String, Object>>) beanMap.entrySet()) { // Skipping self description. if ("class".equals(entry.getKey())) { continue; } final String propertyName = entry.getKey(); final Method propertyWriteMethod = beanMap.getWriteMethod(propertyName); if (propertyWriteMethod == null) { throw new DeserializationException( String.format("Cannot find setter for property '%s' in bean %s", propertyName, bean)); } final Class propertyType = propertyWriteMethod.getParameterTypes()[0]; // Skipping properties marked as ignored. if (isIgnored(propertyWriteMethod)) { continue; } final Object value = retrieveObject(classUrl, propertyName, propertyWriteMethod, identifier, endPoint, context); // Skipping missing values. if (value == null) { continue; } Object deserializedValue = context.deserialize(context, propertyType, propertyWriteMethod.getAnnotations(), new Identifier(value, Identifier.Type.resource), endPoint); try { propertyWriteMethod.invoke(bean, deserializedValue); } catch (Exception e) { throw new DeserializationException( String.format("Error while setting value [%s] on bean [%s] using setter [%s].", value, bean, propertyWriteMethod), e); } } return bean; }
From source file:org.sindice.rdfcommons.beanmapper.BeanSerializer.java
@SuppressWarnings("unchecked") public Identifier serialize(SerializationContext context, Object bean, Annotation[] annotations, TripleSet buffer) throws SerializationException { // Handling adapted object. Object adapted = getAdapted(bean); if (adapted != null) { return context.serialize(context, adapted, annotations, buffer); }//from w ww . j a va 2 s. c om // Handling common bean. try { BeanMap beanMap = new BeanMap(bean); final String classUrl = getClassURL(bean.getClass()); final String instanceUrl = getObjectURL(bean); buffer.addTriple(instanceUrl, RDFVocabulary.TYPE, classUrl); for (Map.Entry<String, Object> entry : (Set<Map.Entry<String, Object>>) beanMap.entrySet()) { final String propertyName = entry.getKey(); final Method propertyReadMethod = beanMap.getReadMethod(propertyName); // Skipping self description. if ("class".equals(entry.getKey())) { continue; } // Skipping properties marked as ignored. if (isIgnored(propertyReadMethod)) { continue; } // Avoid serialization of null values. final Object propertyValue = entry.getValue(); if (propertyValue == null) { continue; } Identifier identifierEntry = context.serialize(context, propertyValue, getAnnotations(propertyReadMethod), buffer); buffer.addTriple(instanceUrl, getPropertyURL(classUrl, propertyName, propertyReadMethod), identifierEntry.getId(), identifierEntry.isLiteral() ? ObjectType.literal : ObjectType.uri); } return null; } catch (Exception e) { throw new SerializationException(e); } }
From source file:org.sindice.rdfcommons.beanmapper.StaticBeanDeserializer.java
public <T> T deserialize(DeserializationContext context, Class<T> clazz, Annotation[] annotations, Identifier identifier, QueryEndpoint endPoint) throws DeserializationException { if (identifier != null) { throw new IllegalArgumentException("for static deserialization the identifier is expected to be null."); }//from ww w. ja v a2 s . c o m final Identifier staticIdentifier = getIdentifier(clazz, annotations); // Create the bean instance. final T instance; try { instance = clazz.newInstance(); } catch (Exception e) { throw new DeserializationException(String .format("Error while creating instance of class %s: defualt constructor required.", clazz)); } context.registerInstance(staticIdentifier, instance); // Define the bean map. final BeanMap beanMap = new BeanMap(instance); // Extracts the class property URLs. final Map<String, Method> propertyURLs = new HashMap<String, Method>(); final String classURL = getClassURL(clazz); String propertyName; Method propertyWriteMethod; for (Map.Entry<String, Object> entry : (Set<Map.Entry<String, Object>>) beanMap.entrySet()) { // Skipping self description. if ("class".equals(entry.getKey())) { continue; } propertyName = entry.getKey(); propertyWriteMethod = beanMap.getWriteMethod(propertyName); propertyURLs.put(getPropertyURL(classURL, propertyName, propertyWriteMethod), propertyWriteMethod); } // Retrieve the class triples. endPoint.addQuery((String) staticIdentifier.getId(), "?prop", "?value"); ResultSet rs = endPoint.execute(); // Coupling object properties with actual ones. Object property; Object value; Class propertyType; while (rs.hasNext()) { property = rs.getVariableValue("?prop"); value = rs.getVariableValue("?value"); rs.next(); propertyWriteMethod = propertyURLs.get(property); if (propertyWriteMethod == null) { context.reportIssue(String.format("Cannot find write method for property URL %s", property)); continue; } propertyType = propertyWriteMethod.getParameterTypes()[0]; // Deserializing recursively. Object deserialized = context.deserialize(context, propertyType, propertyType.getAnnotations(), new Identifier(value, Identifier.Type.resource), endPoint); try { propertyWriteMethod.invoke(instance, deserialized); } catch (Exception e) { throw new DeserializationException( String.format("Error while invoking write method %s of instance %s on value %s[%s]", propertyWriteMethod, instance, deserialized, deserialized.getClass())); } } return instance; }
From source file:org.sindice.rdfcommons.beanmapper.StaticBeanSerializer.java
public Identifier serialize(SerializationContext context, Object bean, Annotation[] annotations, TripleSet buffer) throws SerializationException { try {//from www . jav a2 s. c om BeanMap beanMap = new BeanMap(bean); final String classURL = getClassURL(bean.getClass()); for (Map.Entry<String, Object> entry : (Set<Map.Entry<String, Object>>) beanMap.entrySet()) { final String propertyName = entry.getKey(); final Method propertyReadMethod = beanMap.getReadMethod(propertyName); // Skipping self description. if ("class".equals(propertyName)) { continue; } // Skipping properties marked as ignored. if (isIgnored(propertyReadMethod)) { continue; } Object propertyValue = entry.getValue(); Identifier identifierEntry = context.serialize(context, propertyValue, getAnnotations(propertyReadMethod), buffer); buffer.addTriple(classURL, getPropertyURL(classURL, propertyName, propertyReadMethod), identifierEntry.getId(), identifierEntry.isLiteral() ? ObjectType.literal : ObjectType.uri); } return null; } catch (Exception e) { e.printStackTrace(); throw new SerializationException(e); } }
From source file:plum.mybatis.PagingParametersFinder.java
/** * In the object to find whether contains <code>PaginationCriteria</code> objects. * * @param object parameter object./*w ww . j a va 2s . c om*/ * @return PaginationCriteria */ private PageQuery findCriteriaFromObject(Object object) { //???NULL if (search_map.containsKey(object)) { return null; } //object class Class<?> obj_class = object.getClass(); PageQuery pc; //primitive if (Classs.isPrimitiveType(obj_class)) { pc = null; } else if (obj_class.isAssignableFrom(PageQuery.class)) { pc = (PageQuery) object; } else if (object instanceof Map) { pc = findCriteriaFromMap((Map) object); } else if (object instanceof Collection) { pc = findCriteriaFromCollection((Collection) object); } else if (obj_class.isArray()) { pc = findCriteriaFromArray(object); } else { BeanMap map = new BeanMap(object); return findCriteriaFromMap(map); } search_map.put(object, StringUtils.EMPTY); return pc; }