Java Code Examples for org.apache.commons.beanutils.BeanUtils#describe()

The following examples show how to use org.apache.commons.beanutils.BeanUtils#describe() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FSEventRecorder.java    From Bats with Apache License 2.0 6 votes vote down vote up
public void writeEvent(StramEvent event) throws Exception
{
  LOG.debug("Writing event {} to the storage", event.getType());
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  bos.write((event.getTimestamp() + ":").getBytes());
  bos.write((event.getType() + ":").getBytes());
  @SuppressWarnings("unchecked")
  Map<String, String> data = BeanUtils.describe(event);
  data.remove("timestamp");
  data.remove("class");
  data.remove("type");
  Slice f = streamCodec.toByteArray(data);
  bos.write(f.buffer, f.offset, f.length);
  bos.write("\n".getBytes());
  storage.writeDataItem(bos.toByteArray(), true);
  if (numSubscribers > 0) {
    LOG.debug("Publishing event {} through websocket to gateway", event.getType());
    EventsAgent.EventInfo eventInfo = new EventsAgent.EventInfo();
    eventInfo.id = event.getId();
    eventInfo.timestamp = event.getTimestamp();
    eventInfo.type = event.getType();
    eventInfo.data = data;
    eventInfo.data.remove("id");
    wsClient.publish(pubSubTopic, eventInfo);
  }
}
 
Example 2
Source File: FSEventRecorder.java    From attic-apex-core with Apache License 2.0 6 votes vote down vote up
public void writeEvent(StramEvent event) throws Exception
{
  LOG.debug("Writing event {} to the storage", event.getType());
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  bos.write((event.getTimestamp() + ":").getBytes());
  bos.write((event.getType() + ":").getBytes());
  @SuppressWarnings("unchecked")
  Map<String, String> data = BeanUtils.describe(event);
  data.remove("timestamp");
  data.remove("class");
  data.remove("type");
  Slice f = streamCodec.toByteArray(data);
  bos.write(f.buffer, f.offset, f.length);
  bos.write("\n".getBytes());
  storage.writeDataItem(bos.toByteArray(), true);
  if (numSubscribers > 0) {
    LOG.debug("Publishing event {} through websocket to gateway", event.getType());
    EventsAgent.EventInfo eventInfo = new EventsAgent.EventInfo();
    eventInfo.id = event.getId();
    eventInfo.timestamp = event.getTimestamp();
    eventInfo.type = event.getType();
    eventInfo.data = data;
    eventInfo.data.remove("id");
    wsClient.publish(pubSubTopic, eventInfo);
  }
}
 
Example 3
Source File: BeanSortComparator.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
 * protected utility method to wrap BeanUtils
 *
 * @param o DOCUMENTATION PENDING
 *
 * @return DOCUMENTATION PENDING
 *
 * @throws java.lang.UnsupportedOperationException DOCUMENTATION PENDING
 */
protected Map describeBean(Object o)
{
  Map m = null;

  try
  {
    m = BeanUtils.describe((Serializable) o);
  }
  catch(Throwable t)
  {
    log.debug("Caught error in BeanUtils.describe(): " + t.getMessage());
    throw new java.lang.UnsupportedOperationException(
      "Invalid describeBean. Objects may not be Java Beans.  " + t);
    
  }

  return m;
}
 
Example 4
Source File: PayUtils.java    From weixin-pay with MIT License 6 votes vote down vote up
public static String generatePayNativeReplyXML(PayPackage payPackage){
	try {
		
		Map<String, String> map = BeanUtils.describe(payPackage);
		map.remove("class");
		
		String sign = Signature.generateSign(map);
		payPackage.setSign(sign);
		
		XmlMapper xmlMapper = new XmlMapper();
		xmlMapper.setSerializationInclusion(Include.NON_EMPTY);
		
		String xmlContent = xmlMapper.writeValueAsString(payPackage);
		
		HttpsRequest httpsRequest = new HttpsRequest();
		String result = httpsRequest.sendPost(Configure.UNIFY_PAY_API, xmlContent);
		return result;
	} catch (Exception e) {
		logger.info("e:" + e);
	}
	
	return null;
}
 
Example 5
Source File: PlaceHoldersResolver.java    From n2o-framework with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private static Function<String, Object> function(Object data) {
    if (data instanceof Function) {
        return (Function<String, Object>) data;
    } else if (data instanceof PropertyResolver) {
        return ((PropertyResolver) data)::getProperty;
    } else if (data instanceof Map) {
        return ((Map) data)::get;
    } else if (data instanceof List) {
        return k -> ((List) data).get(Integer.parseInt(k));
    } else if (data != null && data.getClass().isArray()) {
        Object[] array = (Object[]) data;
        return k -> array[Integer.parseInt(k)];
    } else if (data instanceof String || data instanceof Number || data instanceof Date) {
        return k -> data;
    } else {
        try {
            Map<String, String> map = BeanUtils.describe(data);
            return map::get;
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            throw new IllegalArgumentException(e);
        }
    }
}
 
Example 6
Source File: BeanSortComparator.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
 * protected utility method to wrap BeanUtils
 *
 * @param o DOCUMENTATION PENDING
 *
 * @return DOCUMENTATION PENDING
 *
 * @throws java.lang.UnsupportedOperationException DOCUMENTATION PENDING
 */
protected Map describeBean(Object o)
{
  Map m = null;

  try
  {
    m = BeanUtils.describe((Serializable) o);
  }
  catch(Throwable t)
  {
    log.debug("Caught error in BeanUtils.describe(): " + t.getMessage());
    throw new java.lang.UnsupportedOperationException(
      "Invalid describeBean. Objects may not be Java Beans.  " + t);
    
  }

  return m;
}
 
Example 7
Source File: MapBuilderTest.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test the case where includes and excludes are null
 * @throws Exception if bean utils has trouble processing the bean.
 */
public void testBaseCase() throws Exception {
    Map map = builder.mapify(bean);
    Map properties = BeanUtils.describe(bean);
    assertEquals(properties.size(), map.size());
    assertMethod(map, "fieldWierdo", TestBean.DEFAULT_VALUE, true);
    assertMethod(map, "fieldNull", "", true);
}
 
Example 8
Source File: MapBuilder.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Basically you pass in a bean object to it
 * and it spits out a Map of name value pairs
 * where the name is the debeanified method name
 * and the value is what ever the method returns.
 *
 * Basic operation is (Master List ^ includes) - excludes
 * Where ^ = intersection
 * However there is on catch ...
 * If "includes" is empty, it includes everything..
 * ie... {@literal includes  = empty ==> Master List - excludes}
 *
 * @param bean the bean object to be mapified
 * @return a map containing the debeanified values.
 */
public Map mapify(Object bean) {
    Map retval = new HashMap();
    try {
        Map properties = BeanUtils.describe(bean);
        Iterator i = properties.keySet().iterator();
        while (i.hasNext()) {
            String key = (String) i.next();
            if (includes.isEmpty() || includes.contains(key)) {
                if (!excludes.contains(key)) {
                    if (properties.get(key) != null) {
                        String value = String.valueOf(properties.get(key));
                        retval.put(StringUtil.debeanify(key), value);
                    }
                    else {
                        retval.put(StringUtil.debeanify(key), "");
                    }
                }
            }
        }
    }
    catch (Exception e) {
        log.error(e);
        throw new RuntimeException("Caught error trying to describe a bean. ", e);
    }
    return retval;
}
 
Example 9
Source File: BigButtonSettings.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, String> toMap() throws Exception {
    @SuppressWarnings("unchecked")
    Map<String, String> description = BeanUtils.describe(this);

    Map<String, String> result = new HashMap<>();
    for(Entry<String, String> entry : description.entrySet()) {
        result.put(STORAGE_PREFIX + entry.getKey(), entry.getValue());
    }
    return result;
}
 
Example 10
Source File: MapBuilder.java    From spacewalk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Basically you pass in a bean object to it
 * and it spits out a Map of name value pairs
 * where the name is the debeanified method name
 * and the value is what ever the method returns.
 *
 * Basic operation is (Master List ^ includes) - excludes
 * Where ^ = intersection
 * However there is on catch ...
 * If "includes" is empty, it includes everything..
 * ie... {@literal includes  = empty ==> Master List - excludes}
 *
 * @param bean the bean object to be mapified
 * @return a map containing the debeanified values.
 */
public Map mapify(Object bean) {
    Map retval = new HashMap();
    try {
        Map properties = BeanUtils.describe(bean);
        Iterator i = properties.keySet().iterator();
        while (i.hasNext()) {
            String key = (String) i.next();
            if (includes.isEmpty() || includes.contains(key)) {
                if (!excludes.contains(key)) {
                    if (properties.get(key) != null) {
                        String value = String.valueOf(properties.get(key));
                        retval.put(StringUtil.debeanify(key), value);
                    }
                    else {
                        retval.put(StringUtil.debeanify(key), "");
                    }
                }
            }
        }
    }
    catch (Exception e) {
        log.error(e);
        throw new RuntimeException("Caught error trying to describe a bean. ", e);
    }
    return retval;
}
 
Example 11
Source File: EMailServiceSettings.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, String> toMap() throws Exception {
    @SuppressWarnings("unchecked")
    Map<String, String> description = BeanUtils.describe(this);

    Map<String, String> result = new HashMap<>();
    for(Entry<String, String> entry : description.entrySet()) {
        result.put(STORAGE_PREFIX + entry.getKey(), entry.getValue());
    }
    result.put(ENABLED_KEY, String.valueOf(serviceEnabled));
    return result;
}
 
Example 12
Source File: WithContentFormFlow.java    From commercetools-sunrise-java with Apache License 2.0 5 votes vote down vote up
default Form<? extends F> createFilledForm(final I input, final F formData) {
    preFillFormData(input, formData);
    try {
        final Map<String, String> classFieldValues = BeanUtils.describe(formData);
        final Form<? extends F> filledForm = createForm().bind(classFieldValues);
        filledForm.discardErrors();
        return filledForm;
    } catch (ReflectiveOperationException e) {
        throw new RuntimeException("Form cannot be populated for class " + getFormDataClass().getCanonicalName(), e);
    }
}
 
Example 13
Source File: NetDumperOptions.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public Map<String, String> toMap() throws Exception {

	Map<String, String> description = BeanUtils.describe(this);
       Map<String, String> result = new HashMap<>();

       for(Entry<String, String> entry : description.entrySet()) {
           if(!"class".equals(entry.getKey())) {
			result.put(STORAGE_PREFIX + entry.getKey(), entry.getValue());
		}
	}
	
	return result;
}
 
Example 14
Source File: T.java    From seezoon-framework-all with Apache License 2.0 5 votes vote down vote up
@Test
public void t10() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
	TreeMap<String,Object> treeMap = new TreeMap<>();
	UnifiedOrder order = new UnifiedOrder();
	Map<String, String> describe = BeanUtils.describe(order);
	System.out.println(JSON.toJSONString(describe));
	
}
 
Example 15
Source File: PayUtils.java    From weixin-pay with MIT License 5 votes vote down vote up
public static boolean validateAppSignature(PayNativeInput payNativeInput){
	try {
		Map<String, String> map = BeanUtils.describe(payNativeInput);
		map.remove("class");
		map.put("sign", "");
		
		String sign = Signature.generateSign(map);
		return payNativeInput.getSign().equals(sign) ? true : false;
	} catch (Exception e) {
	}
	
	return false;
}
 
Example 16
Source File: IrisAbstractApplication.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
private static Map<String,String> mapFromArgs(Arguments args) {
	Map<String, String> properties = new HashMap<String,String>();
	try {
		properties = BeanUtils.describe(args);
		if (properties.containsKey("class")) {
			properties.remove("class");
		}
	} catch (Exception e) {}
	return ImmutableMap.copyOf(properties);
}
 
Example 17
Source File: MapBuilderTest.java    From spacewalk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test the case where includes and excludes are null
 * @throws Exception if bean utils has trouble processing the bean.
 */
public void testBaseCase() throws Exception {
    Map map = builder.mapify(bean);
    Map properties = BeanUtils.describe(bean);
    assertEquals(properties.size(), map.size());
    assertMethod(map, "fieldWierdo", TestBean.DEFAULT_VALUE, true);
    assertMethod(map, "fieldNull", "", true);
}
 
Example 18
Source File: HibernateStorageSettings.java    From sailfish-core with Apache License 2.0 4 votes vote down vote up
public Map<String, String> toMap() throws Exception {
    
    @SuppressWarnings("unchecked")
    Map<String, String> description = BeanUtils.describe(this);
    
    Map<String, String> result = new HashMap<String, String>();

    for(Entry<String, String> entry : description.entrySet()) {

        if(!"class".equals(entry.getKey())) {
        
            result.put(storagePrefix + entry.getKey(), entry.getValue());
        
        }
        
    }
    
    return result;
    
}
 
Example 19
Source File: FlightRecorderOptions.java    From sailfish-core with Apache License 2.0 4 votes vote down vote up
public Map<String, String> toMap() throws Exception {
	
	@SuppressWarnings("unchecked")
	Map<String, String> description = BeanUtils.describe(this);
	
	Map<String, String> result = new HashMap<String, String>();
	
	for(Entry<String, String> entry : description.entrySet()) {

           if(!"class".equals(entry.getKey())) {
		
			result.put(STORAGE_PREFIX + entry.getKey(), entry.getValue());
		
		}
		
	}
	
	return result;
	
}
 
Example 20
Source File: StramWebServices.java    From attic-apex-core with Apache License 2.0 4 votes vote down vote up
@POST // not supported by WebAppProxyServlet, can only be called directly
@Path(PATH_LOGICAL_PLAN)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public JSONObject logicalPlanModification(JSONObject request)
{
  init();
  JSONObject response = new JSONObject();
  try {
    JSONArray jsonArray = request.getJSONArray("requests");
    List<LogicalPlanRequest> requests = new ArrayList<>();
    for (int i = 0; i < jsonArray.length(); i++) {
      JSONObject jsonObj = (JSONObject)jsonArray.get(i);
      LogicalPlanRequest requestObj = (LogicalPlanRequest)Class.forName(LogicalPlanRequest.class.getPackage().getName() + "." + jsonObj.getString("requestType")).newInstance();
      @SuppressWarnings("unchecked")
      Map<String, String> properties = BeanUtils.describe(requestObj);
      @SuppressWarnings("unchecked")
      Iterator<String> keys = jsonObj.keys();

      while (keys.hasNext()) {
        String key = keys.next();
        if (!key.equals("requestType")) {
          properties.put(key, jsonObj.get(key).toString());
        }
      }
      BeanUtils.populate(requestObj, properties);
      requests.add(requestObj);
    }
    Future<?> fr = dagManager.logicalPlanModification(requests);
    fr.get(3000, TimeUnit.MILLISECONDS);
  } catch (Exception ex) {
    LOG.error("Error processing plan change", ex);
    try {
      if (ex instanceof ExecutionException) {
        response.put("error", ex.getCause().toString());
      } else {
        response.put("error", ex.toString());
      }
    } catch (Exception e) {
      // ignore
    }
  }

  return response;
}