@Test public void testExtractThrift() throws Exception { final DateTime eventDateTime = new DateTime(); final String coreHostname = "hostname"; final TLoggingEvent event = new TLoggingEvent(); event.setCoreHostname(coreHostname); final String ip = "10.1.2.3"; event.setCoreIp(ip); final String type = "myType"; event.setCoreType(type); event.setEventDate(eventDateTime.getMillis()); final ArrayList<ThriftField> fields = new ArrayList<ThriftField>(); fields.add(ThriftField.createThriftField(coreHostname, (short) 15)); fields.add(ThriftField.createThriftField(ip, (short) 14)); fields.add(ThriftField.createThriftField(type, (short) 16)); fields.add(ThriftField.createThriftField(eventDateTime.getMillis(), (short) 1)); final ThriftEnvelope envelope = new ThriftEnvelope("TLoggingEvent", fields); final ThriftEnvelopeEvent envelopeEvent = new ThriftEnvelopeEvent(eventDateTime, envelope); final TLoggingEvent event2 = ThriftEnvelopeEventToThrift.extractThrift(TLoggingEvent.class, envelopeEvent); Assert.assertEquals(event2.getCoreHostname(), event.getCoreHostname()); Assert.assertEquals(event2.getCoreIp(), event.getCoreIp()); Assert.assertEquals(event2.getCoreType(), event.getCoreType()); Assert.assertEquals(event2.getEventDate(), event.getEventDate()); }
public static <T extends Serializable> T extractThrift(final Class<T> clazz, final ThriftEnvelopeEvent envelopeEvent) { final ThriftEnvelope envelope = (ThriftEnvelope) envelopeEvent.getData(); try { final T thriftObject = clazz.newInstance(); final Field[] fields = clazz.getFields(); for (final ThriftField tField : envelope.getPayload()) { if (tField.getId() >= fields.length) { continue; } final Field field = fields[tField.getId() - 1]; final Class<?> type = field.getType(); if (type.isAssignableFrom(Boolean.class)) { field.setBoolean(thriftObject, tField.getDataItem().getBoolean()); } else if (type.isAssignableFrom(boolean.class)) { field.setBoolean(thriftObject, tField.getDataItem().getBoolean()); } else if (type.isAssignableFrom(Byte.class)) { field.setByte(thriftObject, tField.getDataItem().getByte()); } else if (type.isAssignableFrom(byte.class)) { field.setByte(thriftObject, tField.getDataItem().getByte()); } else if (type.isAssignableFrom(Short.class)) { field.setShort(thriftObject, tField.getDataItem().getShort()); } else if (type.isAssignableFrom(short.class)) { field.setShort(thriftObject, tField.getDataItem().getShort()); } else if (type.isAssignableFrom(Integer.class)) { field.setInt(thriftObject, tField.getDataItem().getInteger()); } else if (type.isAssignableFrom(int.class)) { field.setInt(thriftObject, tField.getDataItem().getInteger()); } else if (type.isAssignableFrom(Long.class)) { field.setLong(thriftObject, tField.getDataItem().getLong()); } else if (type.isAssignableFrom(long.class)) { field.setLong(thriftObject, tField.getDataItem().getLong()); } else if (type.isAssignableFrom(Double.class)) { field.setDouble(thriftObject, tField.getDataItem().getDouble()); } else if (type.isAssignableFrom(double.class)) { field.setDouble(thriftObject, tField.getDataItem().getDouble()); } else if (type.isAssignableFrom(String.class)) { field.set(thriftObject, tField.getDataItem().getString()); } } return thriftObject; } catch (InstantiationException e) { return null; } catch (IllegalAccessException e) { return null; } }
ThriftEnvelopeEventToThrift { public static <T extends Serializable> T extractThrift(final Class<T> clazz, final ThriftEnvelopeEvent envelopeEvent) { final ThriftEnvelope envelope = (ThriftEnvelope) envelopeEvent.getData(); try { final T thriftObject = clazz.newInstance(); final Field[] fields = clazz.getFields(); for (final ThriftField tField : envelope.getPayload()) { if (tField.getId() >= fields.length) { continue; } final Field field = fields[tField.getId() - 1]; final Class<?> type = field.getType(); if (type.isAssignableFrom(Boolean.class)) { field.setBoolean(thriftObject, tField.getDataItem().getBoolean()); } else if (type.isAssignableFrom(boolean.class)) { field.setBoolean(thriftObject, tField.getDataItem().getBoolean()); } else if (type.isAssignableFrom(Byte.class)) { field.setByte(thriftObject, tField.getDataItem().getByte()); } else if (type.isAssignableFrom(byte.class)) { field.setByte(thriftObject, tField.getDataItem().getByte()); } else if (type.isAssignableFrom(Short.class)) { field.setShort(thriftObject, tField.getDataItem().getShort()); } else if (type.isAssignableFrom(short.class)) { field.setShort(thriftObject, tField.getDataItem().getShort()); } else if (type.isAssignableFrom(Integer.class)) { field.setInt(thriftObject, tField.getDataItem().getInteger()); } else if (type.isAssignableFrom(int.class)) { field.setInt(thriftObject, tField.getDataItem().getInteger()); } else if (type.isAssignableFrom(Long.class)) { field.setLong(thriftObject, tField.getDataItem().getLong()); } else if (type.isAssignableFrom(long.class)) { field.setLong(thriftObject, tField.getDataItem().getLong()); } else if (type.isAssignableFrom(Double.class)) { field.setDouble(thriftObject, tField.getDataItem().getDouble()); } else if (type.isAssignableFrom(double.class)) { field.setDouble(thriftObject, tField.getDataItem().getDouble()); } else if (type.isAssignableFrom(String.class)) { field.set(thriftObject, tField.getDataItem().getString()); } } return thriftObject; } catch (InstantiationException e) { return null; } catch (IllegalAccessException e) { return null; } } }
ThriftEnvelopeEventToThrift { public static <T extends Serializable> T extractThrift(final Class<T> clazz, final ThriftEnvelopeEvent envelopeEvent) { final ThriftEnvelope envelope = (ThriftEnvelope) envelopeEvent.getData(); try { final T thriftObject = clazz.newInstance(); final Field[] fields = clazz.getFields(); for (final ThriftField tField : envelope.getPayload()) { if (tField.getId() >= fields.length) { continue; } final Field field = fields[tField.getId() - 1]; final Class<?> type = field.getType(); if (type.isAssignableFrom(Boolean.class)) { field.setBoolean(thriftObject, tField.getDataItem().getBoolean()); } else if (type.isAssignableFrom(boolean.class)) { field.setBoolean(thriftObject, tField.getDataItem().getBoolean()); } else if (type.isAssignableFrom(Byte.class)) { field.setByte(thriftObject, tField.getDataItem().getByte()); } else if (type.isAssignableFrom(byte.class)) { field.setByte(thriftObject, tField.getDataItem().getByte()); } else if (type.isAssignableFrom(Short.class)) { field.setShort(thriftObject, tField.getDataItem().getShort()); } else if (type.isAssignableFrom(short.class)) { field.setShort(thriftObject, tField.getDataItem().getShort()); } else if (type.isAssignableFrom(Integer.class)) { field.setInt(thriftObject, tField.getDataItem().getInteger()); } else if (type.isAssignableFrom(int.class)) { field.setInt(thriftObject, tField.getDataItem().getInteger()); } else if (type.isAssignableFrom(Long.class)) { field.setLong(thriftObject, tField.getDataItem().getLong()); } else if (type.isAssignableFrom(long.class)) { field.setLong(thriftObject, tField.getDataItem().getLong()); } else if (type.isAssignableFrom(Double.class)) { field.setDouble(thriftObject, tField.getDataItem().getDouble()); } else if (type.isAssignableFrom(double.class)) { field.setDouble(thriftObject, tField.getDataItem().getDouble()); } else if (type.isAssignableFrom(String.class)) { field.set(thriftObject, tField.getDataItem().getString()); } } return thriftObject; } catch (InstantiationException e) { return null; } catch (IllegalAccessException e) { return null; } }  }
ThriftEnvelopeEventToThrift { public static <T extends Serializable> T extractThrift(final Class<T> clazz, final ThriftEnvelopeEvent envelopeEvent) { final ThriftEnvelope envelope = (ThriftEnvelope) envelopeEvent.getData(); try { final T thriftObject = clazz.newInstance(); final Field[] fields = clazz.getFields(); for (final ThriftField tField : envelope.getPayload()) { if (tField.getId() >= fields.length) { continue; } final Field field = fields[tField.getId() - 1]; final Class<?> type = field.getType(); if (type.isAssignableFrom(Boolean.class)) { field.setBoolean(thriftObject, tField.getDataItem().getBoolean()); } else if (type.isAssignableFrom(boolean.class)) { field.setBoolean(thriftObject, tField.getDataItem().getBoolean()); } else if (type.isAssignableFrom(Byte.class)) { field.setByte(thriftObject, tField.getDataItem().getByte()); } else if (type.isAssignableFrom(byte.class)) { field.setByte(thriftObject, tField.getDataItem().getByte()); } else if (type.isAssignableFrom(Short.class)) { field.setShort(thriftObject, tField.getDataItem().getShort()); } else if (type.isAssignableFrom(short.class)) { field.setShort(thriftObject, tField.getDataItem().getShort()); } else if (type.isAssignableFrom(Integer.class)) { field.setInt(thriftObject, tField.getDataItem().getInteger()); } else if (type.isAssignableFrom(int.class)) { field.setInt(thriftObject, tField.getDataItem().getInteger()); } else if (type.isAssignableFrom(Long.class)) { field.setLong(thriftObject, tField.getDataItem().getLong()); } else if (type.isAssignableFrom(long.class)) { field.setLong(thriftObject, tField.getDataItem().getLong()); } else if (type.isAssignableFrom(Double.class)) { field.setDouble(thriftObject, tField.getDataItem().getDouble()); } else if (type.isAssignableFrom(double.class)) { field.setDouble(thriftObject, tField.getDataItem().getDouble()); } else if (type.isAssignableFrom(String.class)) { field.set(thriftObject, tField.getDataItem().getString()); } } return thriftObject; } catch (InstantiationException e) { return null; } catch (IllegalAccessException e) { return null; } }  static T extractThrift(final Class<T> clazz, final ThriftEnvelopeEvent envelopeEvent); }
ThriftEnvelopeEventToThrift { public static <T extends Serializable> T extractThrift(final Class<T> clazz, final ThriftEnvelopeEvent envelopeEvent) { final ThriftEnvelope envelope = (ThriftEnvelope) envelopeEvent.getData(); try { final T thriftObject = clazz.newInstance(); final Field[] fields = clazz.getFields(); for (final ThriftField tField : envelope.getPayload()) { if (tField.getId() >= fields.length) { continue; } final Field field = fields[tField.getId() - 1]; final Class<?> type = field.getType(); if (type.isAssignableFrom(Boolean.class)) { field.setBoolean(thriftObject, tField.getDataItem().getBoolean()); } else if (type.isAssignableFrom(boolean.class)) { field.setBoolean(thriftObject, tField.getDataItem().getBoolean()); } else if (type.isAssignableFrom(Byte.class)) { field.setByte(thriftObject, tField.getDataItem().getByte()); } else if (type.isAssignableFrom(byte.class)) { field.setByte(thriftObject, tField.getDataItem().getByte()); } else if (type.isAssignableFrom(Short.class)) { field.setShort(thriftObject, tField.getDataItem().getShort()); } else if (type.isAssignableFrom(short.class)) { field.setShort(thriftObject, tField.getDataItem().getShort()); } else if (type.isAssignableFrom(Integer.class)) { field.setInt(thriftObject, tField.getDataItem().getInteger()); } else if (type.isAssignableFrom(int.class)) { field.setInt(thriftObject, tField.getDataItem().getInteger()); } else if (type.isAssignableFrom(Long.class)) { field.setLong(thriftObject, tField.getDataItem().getLong()); } else if (type.isAssignableFrom(long.class)) { field.setLong(thriftObject, tField.getDataItem().getLong()); } else if (type.isAssignableFrom(Double.class)) { field.setDouble(thriftObject, tField.getDataItem().getDouble()); } else if (type.isAssignableFrom(double.class)) { field.setDouble(thriftObject, tField.getDataItem().getDouble()); } else if (type.isAssignableFrom(String.class)) { field.set(thriftObject, tField.getDataItem().getString()); } } return thriftObject; } catch (InstantiationException e) { return null; } catch (IllegalAccessException e) { return null; } }  static T extractThrift(final Class<T> clazz, final ThriftEnvelopeEvent envelopeEvent);  }