/** * Handle all the logic leading to the decoding of a Protobuf-encoded binary given a schema file path. * @param schema Schema used to decode the binary data * @param messageType Type of Protobuf Message * @param encodedData Encoded data source * @return A JSON representation of the data, contained in a Java String * @throws InvalidProtocolBufferException Thrown when an error occurs during the encoding of the decoded data into JSON * @throws Descriptors.DescriptorValidationException Thrown when the schema is invalid * @throws UnknownMessageTypeException Thrown when the given message type is not contained in the schema * @throws MessageDecodingException Thrown when an error occurs during the binary decoding * @throws SchemaLoadingException Thrown when an error occurs while reading the schema file */ public static String decodeProtobuf(DynamicSchema schema, String messageType, InputStream encodedData) throws InvalidProtocolBufferException, Descriptors.DescriptorValidationException, UnknownMessageTypeException, MessageDecodingException, SchemaLoadingException { Descriptors.Descriptor descriptor; DynamicMessage message; descriptor = schema.getMessageDescriptor(messageType); if (descriptor == null) { throw new UnknownMessageTypeException(messageType); } try { message = DynamicMessage.parseFrom(descriptor, encodedData); } catch (IOException e) { throw new MessageDecodingException(e); } return JSONMapper.toJSON(message); }
@Override public boolean registerService(Service instance) { /* * No stacking of instances is allowed for a single service name */ Descriptors.ServiceDescriptor serviceDesc = instance.getDescriptorForType(); if (coprocessorServiceHandlers.containsKey(serviceDesc.getFullName())) { LOG.error("Coprocessor service "+serviceDesc.getFullName()+ " already registered, rejecting request from "+instance ); return false; } coprocessorServiceHandlers.put(serviceDesc.getFullName(), instance); if (LOG.isDebugEnabled()) { LOG.debug("Registered master coprocessor service: service="+serviceDesc.getFullName()); } return true; }
@Override public boolean registerService(Service instance) { /* * No stacking of instances is allowed for a single service name */ Descriptors.ServiceDescriptor serviceDesc = instance.getDescriptorForType(); if (coprocessorServiceHandlers.containsKey(serviceDesc.getFullName())) { LOG.error("Coprocessor service " + serviceDesc.getFullName() + " already registered, rejecting request from " + instance); return false; } coprocessorServiceHandlers.put(serviceDesc.getFullName(), instance); if (LOG.isDebugEnabled()) { LOG.debug( "Registered regionserver coprocessor service: service=" + serviceDesc.getFullName()); } return true; }
@Override public boolean registerService(Service instance) { /* * No stacking of instances is allowed for a single service name */ Descriptors.ServiceDescriptor serviceDesc = instance.getDescriptorForType(); if (coprocessorServiceHandlers.containsKey(serviceDesc.getFullName())) { LOG.error("Coprocessor service " + serviceDesc.getFullName() + " already registered, rejecting request from " + instance); return false; } coprocessorServiceHandlers.put(serviceDesc.getFullName(), instance); if (LOG.isDebugEnabled()) { LOG.debug("Registered coprocessor service: region=" + Bytes .toStringBinary(getRegionInfo().getRegionName()) + " service=" + serviceDesc .getFullName()); } return true; }
/** * {@inheritDoc} */ @Override public <R extends Message> Map<byte[], R> batchCoprocessorService( Descriptors.MethodDescriptor methodDescriptor, Message request, byte[] startKey, byte[] endKey, R responsePrototype) throws ServiceException, Throwable { final Map<byte[], R> results = Collections.synchronizedMap(new TreeMap<byte[], R>( Bytes.BYTES_COMPARATOR)); batchCoprocessorService(methodDescriptor, request, startKey, endKey, responsePrototype, new Callback<R>() { @Override public void update(byte[] region, byte[] row, R result) { if (region != null) { results.put(region, result); } } }); return results; }
/** * Constructor * * @param eventLoop for call * @param connectId connection id * @param md the method descriptor * @param param parameters to send to Server * @param controller controller for response * @param responseDefaultType the default response type */ public AsyncCall(EventLoop eventLoop, int connectId, Descriptors.MethodDescriptor md, Message param, PayloadCarryingRpcController controller, Message responseDefaultType, MetricsConnection.CallStats callStats) { super(eventLoop); this.id = connectId; this.method = md; this.param = param; this.controller = controller; this.responseDefaultType = responseDefaultType; this.startTime = EnvironmentEdgeManager.currentTime(); this.rpcTimeout = controller.hasCallTimeout() ? controller.getCallTimeout() : 0; this.callStats = callStats; }
@Override public void callMethod(Descriptors.MethodDescriptor md, RpcController controller, Message param, Message returnType, RpcCallback<Message> done) { PayloadCarryingRpcController pcrc; if (controller != null) { pcrc = (PayloadCarryingRpcController) controller; if (!pcrc.hasCallTimeout()) { pcrc.setCallTimeout(channelOperationTimeout); } } else { pcrc = new PayloadCarryingRpcController(); pcrc.setCallTimeout(channelOperationTimeout); } this.rpcClient.callMethod(md, pcrc, param, returnType, this.ticket, this.isa, done); }
@Override public Message callBlockingMethod(Descriptors.MethodDescriptor md, RpcController controller, Message param, Message returnType) throws ServiceException { PayloadCarryingRpcController pcrc; if (controller != null && controller instanceof PayloadCarryingRpcController) { pcrc = (PayloadCarryingRpcController) controller; if (!pcrc.hasCallTimeout()) { pcrc.setCallTimeout(channelOperationTimeout); } } else { pcrc = new PayloadCarryingRpcController(); pcrc.setCallTimeout(channelOperationTimeout); } return this.rpcClient.callBlockingMethod(md, pcrc, param, returnType, this.ticket, this.isa); }
@Override @InterfaceAudience.Private public void callMethod(Descriptors.MethodDescriptor method, RpcController controller, Message request, Message responsePrototype, RpcCallback<Message> callback) { Message response = null; try { response = callExecService(controller, method, request, responsePrototype); } catch (IOException ioe) { LOG.warn("Call failed on IOException", ioe); ResponseConverter.setControllerException(controller, ioe); } if (callback != null) { callback.run(response); } }
@Test public void testUnpack() throws InvalidProtocolBufferException { for (Descriptors.ServiceDescriptor serviceDescriptor : FyChessSi.getDescriptor().getServices()) { MethodDescriptor methodByName = serviceDescriptor.findMethodByName("onEnterRoom"); if (methodByName != null) { GpbMessageDesc method = new GpbMessageDesc(methodByName); // VoEnterRoom message = VoEnterRoom.newBuilder() .setRoomId(999) .setSeat(8) .build(); Message unpack = method.unpack(message.toByteString()); Message pack = method.pack(new Object[]{ 999, 8, 10001L, "xx" }); System.out.println(); } } }
private static void requireAllFieldsExcept(AbstractMessage message, int... fieldNumbersNotRequired) { Collection<Descriptors.FieldDescriptor> required = new ArrayList<>(message.getDescriptorForType().getFields()); Collection<Descriptors.FieldDescriptor> actual = message.getAllFields().keySet(); required.removeAll(actual); if(fieldNumbersNotRequired != null) { for(int fieldNumber : fieldNumbersNotRequired) { required.remove(message.getDescriptorForType().findFieldByNumber(fieldNumber)); } } if(!required.isEmpty()) { Collection<String> names = new ArrayList<>(required.size()); for(Descriptors.FieldDescriptor desc : required) { names.add(desc.getName()); } throw new ProtobufReadException(message.getDescriptorForType().getFullName(), "Missing required fields: " + names.toString()); } }
private static <M extends Message, B extends Message.Builder> M messageForFilter( ProtocolStringList filter, Constructor<B> builderConstructor, Message wholeMessage) throws InstantiationException, IllegalAccessException, InvocationTargetException { final B builder = builderConstructor.newInstance(); final List<Descriptors.FieldDescriptor> fields = wholeMessage.getDescriptorForType() .getFields(); for (Descriptors.FieldDescriptor field : fields) { if (filter.contains(field.getFullName())) { builder.setField(field, wholeMessage.getField(field)); } } @SuppressWarnings("unchecked") // It's fine as the constructor is of {@code MessageCls.Builder} type. final M result = (M) builder.build(); return result; }
@Test public void testDefaultValues() throws Exception { Assert.assertEquals(9, defaultValueMap.size()); Assert.assertEquals( "HOME", ((Descriptors.EnumValueDescriptor)defaultValueMap.get("util.Person.PhoneNumber.type")).getName() ); Assert.assertEquals("engineering", defaultValueMap.get("util.Engineer.depName")); Assert.assertEquals("NY", defaultValueMap.get("util.Employee.stringField")); Assert.assertEquals(43243, defaultValueMap.get("util.Employee.intField")); Assert.assertEquals(3534.234, defaultValueMap.get("util.Employee.doubleField")); Assert.assertEquals(true, defaultValueMap.get("util.Employee.boolField")); Assert.assertEquals(343.34f, defaultValueMap.get("util.Employee.floatField")); Assert.assertEquals(2343254354L, defaultValueMap.get("util.Employee.longField")); Assert.assertTrue( Arrays.equals( "NewYork".getBytes(), ((ByteString) defaultValueMap.get("util.Employee.bytesField")).toByteArray() ) ); }
@SuppressWarnings("ConstantConditions") // Converter nullability issues @Test public void read_single_record_with_mask() { final I id = newId(); final EntityRecord record = newStorageRecord(id); final RecordStorage<I> storage = getStorage(); storage.write(id, record); final Descriptors.Descriptor descriptor = newState(id).getDescriptorForType(); final FieldMask idMask = FieldMasks.maskOf(descriptor, 1); final RecordReadRequest<I> readRequest = new RecordReadRequest<>(id); final Optional<EntityRecord> optional = storage.read(readRequest, idMask); assertTrue(optional.isPresent()); final EntityRecord entityRecord = optional.get(); final Message unpacked = unpack(entityRecord.getState()); assertFalse(isDefault(unpacked)); }
@Test public void register_aggregate_repositories() { final BoundedContext boundedContext = BoundedContext.newBuilder() .build(); final Stand stand = boundedContext.getStand(); checkTypesEmpty(stand); final CustomerAggregateRepository customerAggregateRepo = new CustomerAggregateRepository(); stand.registerTypeSupplier(customerAggregateRepo); final Descriptors.Descriptor customerEntityDescriptor = Customer.getDescriptor(); checkHasExactlyOne(stand.getExposedTypes(), customerEntityDescriptor); checkHasExactlyOne(stand.getExposedAggregateTypes(), customerEntityDescriptor); @SuppressWarnings("LocalVariableNamingConvention") final CustomerAggregateRepository anotherCustomerAggregateRepo = new CustomerAggregateRepository(); stand.registerTypeSupplier(anotherCustomerAggregateRepo); checkHasExactlyOne(stand.getExposedTypes(), customerEntityDescriptor); checkHasExactlyOne(stand.getExposedAggregateTypes(), customerEntityDescriptor); }
@Override public boolean registerService(Service instance) { /* * No stacking of instances is allowed for a single service name */ Descriptors.ServiceDescriptor serviceDesc = instance.getDescriptorForType(); if (coprocessorServiceHandlers.containsKey(serviceDesc.getFullName())) { LOG.error("Coprocessor service " + serviceDesc.getFullName() + " already registered, rejecting request from " + instance ); return false; } coprocessorServiceHandlers.put(serviceDesc.getFullName(), instance); if (LOG.isDebugEnabled()) { LOG.debug("Registered master coprocessor service: service=" + serviceDesc.getFullName()); } return true; }
@Override public boolean registerService(Service instance) { /* * No stacking of instances is allowed for a single service name */ Descriptors.ServiceDescriptor serviceDesc = instance.getDescriptorForType(); if (coprocessorServiceHandlers.containsKey(serviceDesc.getFullName())) { LOG.error("Coprocessor service " + serviceDesc.getFullName() + " already registered, rejecting request from " + instance); return false; } coprocessorServiceHandlers.put(serviceDesc.getFullName(), instance); if (LOG.isDebugEnabled()) { LOG.debug("Registered regionserver coprocessor service: service=" + serviceDesc.getFullName()); } return true; }
/** * Registers a new protocol buffer {@link Service} subclass as a coprocessor endpoint to * be available for handling * {@link HRegion#execService(com.google.protobuf.RpcController, * org.apache.hadoop.hbase.protobuf.generated.ClientProtos.CoprocessorServiceCall)}} calls. * <p/> * <p> * Only a single instance may be registered per region for a given {@link Service} subclass (the * instances are keyed on {@link com.google.protobuf.Descriptors.ServiceDescriptor#getFullName()}. * After the first registration, subsequent calls with the same service name will fail with * a return value of {@code false}. * </p> * * @param instance the {@code Service} subclass instance to expose as a coprocessor endpoint * @return {@code true} if the registration was successful, {@code false} * otherwise */ public boolean registerService(Service instance) { /* * No stacking of instances is allowed for a single service name */ Descriptors.ServiceDescriptor serviceDesc = instance.getDescriptorForType(); if (coprocessorServiceHandlers.containsKey(serviceDesc.getFullName())) { LOG.error("Coprocessor service " + serviceDesc.getFullName() + " already registered, rejecting request from " + instance ); return false; } coprocessorServiceHandlers.put(serviceDesc.getFullName(), instance); if (LOG.isDebugEnabled()) { LOG.debug("Registered coprocessor service: region=" + Bytes.toStringBinary(getRegionName()) + " service=" + serviceDesc.getFullName()); } return true; }
@Override protected Message callExecService(Descriptors.MethodDescriptor method, Message request, Message responsePrototype) throws IOException { if (LOG.isTraceEnabled()) { LOG.trace("Call: " + method.getName() + ", " + request.toString()); } final ClientProtos.CoprocessorServiceCall call = ClientProtos.CoprocessorServiceCall.newBuilder() .setRow(ByteStringer.wrap(HConstants.EMPTY_BYTE_ARRAY)) .setServiceName(method.getService().getFullName()).setMethodName(method.getName()) .setRequest(request.toByteString()).build(); CoprocessorServiceResponse result = ProtobufUtil.execRegionServerService(connection.getClient(serverName), call); Message response = null; if (result.getValue().hasValue()) { response = responsePrototype.newBuilderForType().mergeFrom(result.getValue().getValue()).build(); } else { response = responsePrototype.getDefaultInstanceForType(); } if (LOG.isTraceEnabled()) { LOG.trace("Result is value=" + response); } return response; }
@Override public Message callBlockingMethod(Descriptors.MethodDescriptor md, RpcController controller, Message param, Message returnType) throws ServiceException { PayloadCarryingRpcController pcrc; if (controller != null) { pcrc = (PayloadCarryingRpcController) controller; if (!pcrc.hasCallTimeout()){ pcrc.setCallTimeout(defaultOperationTimeout); } } else { pcrc = new PayloadCarryingRpcController(); pcrc.setCallTimeout(defaultOperationTimeout); } return this.rpcClient.callBlockingMethod(md, pcrc, param, returnType, this.ticket, this.isa); }
@Test public void testEmptyRepeated() throws DataGeneratorException { Record r = RecordCreator.create(); Map<String, Field> repeated = new HashMap<>(); repeated.put("samples", Field.create(Field.Type.LIST, new ArrayList<>())); r.set(Field.create(repeated)); Descriptors.Descriptor descriptor = RepeatedProto.getDescriptor().findMessageTypeByName("Repeated"); // repeated field samples is null and ignored DynamicMessage dynamicMessage = ProtobufTypeUtil.sdcFieldToProtobufMsg( r, descriptor, typeToExtensionMap, defaultValueMap ); // null repeated fields are treated as empty arrays Object samples = dynamicMessage.getField(descriptor.findFieldByName("samples")); Assert.assertNotNull(samples); Assert.assertTrue(samples instanceof List); Assert.assertEquals(0, ((List)samples).size()); }
@Override @InterfaceAudience.Private public void callMethod(Descriptors.MethodDescriptor method, RpcController controller, Message request, Message responsePrototype, RpcCallback<Message> callback) { Message response = null; try { response = callExecService(method, request, responsePrototype); } catch (IOException ioe) { LOG.warn("Call failed on IOException", ioe); ResponseConverter.setControllerException(controller, ioe); } if (callback != null) { callback.run(response); } }
/** * Registers a new protocol buffer {@link Service} subclass as a coprocessor endpoint to * be available for handling * {@link HRegion#execService(com.google.protobuf.RpcController, * org.apache.hadoop.hbase.protobuf.generated.ClientProtos.CoprocessorServiceCall)}} calls. * * <p> * Only a single instance may be registered per region for a given {@link Service} subclass (the * instances are keyed on {@link com.google.protobuf.Descriptors.ServiceDescriptor#getFullName()}. * After the first registration, subsequent calls with the same service name will fail with * a return value of {@code false}. * </p> * @param instance the {@code Service} subclass instance to expose as a coprocessor endpoint * @return {@code true} if the registration was successful, {@code false} * otherwise */ public boolean registerService(Service instance) { /* * No stacking of instances is allowed for a single service name */ Descriptors.ServiceDescriptor serviceDesc = instance.getDescriptorForType(); if (coprocessorServiceHandlers.containsKey(serviceDesc.getFullName())) { LOG.error("Coprocessor service "+serviceDesc.getFullName()+ " already registered, rejecting request from "+instance ); return false; } coprocessorServiceHandlers.put(serviceDesc.getFullName(), instance); if (LOG.isDebugEnabled()) { LOG.debug("Registered coprocessor service: region="+ Bytes.toStringBinary(getRegionName())+" service="+serviceDesc.getFullName()); } return true; }
@Override public void callMethod(Descriptors.MethodDescriptor method, RpcController controller, Message request, Message responsePrototype, RpcCallback<Message> callback) { Message response = null; try { response = callExecService(method, request, responsePrototype); } catch (IOException ioe) { LOG.warn("Call failed on IOException", ioe); ResponseConverter.setControllerException(controller, ioe); } if (callback != null) { callback.run(response); } }
public ProtobufDataParser( ProtoConfigurableEntity.Context context, String messageId, Descriptors.Descriptor descriptor, Map<String, Set<Descriptors.FieldDescriptor>> messageTypeToExtensionMap, ExtensionRegistry extensionRegistry, InputStream inputStream, String readerOffset, int maxObjectLength, boolean isDelimited ) throws IOException, Descriptors.DescriptorValidationException, DataParserException { this.context = context; this.inputStream = new OverrunInputStream(inputStream, maxObjectLength, true); this.messageId = messageId; this.messageTypeToExtensionMap = messageTypeToExtensionMap; this.extensionRegistry = extensionRegistry; this.descriptor = descriptor; this.builder = DynamicMessage.newBuilder(descriptor); this.isDelimited = isDelimited; // skip to the required location if (readerOffset != null && !readerOffset.isEmpty() && !readerOffset.equals("0")) { int offset = Integer.parseInt(readerOffset); this.inputStream.skip(offset); } }
public ProtobufDataParserFactory(Settings settings) throws StageException { super(settings); this.protoDescriptorFile = settings.getConfig(ProtobufConstants.PROTO_DESCRIPTOR_FILE_KEY); this.messageType = settings.getConfig(ProtobufConstants.MESSAGE_TYPE_KEY); this.isDelimited = settings.getConfig(ProtobufConstants.DELIMITED_KEY); messageTypeToExtensionMap = new HashMap<>(); defaultValueMap = new HashMap<>(); // Get the descriptor for the expected message type descriptor = ProtobufTypeUtil.getDescriptor( settings.getContext(), protoDescriptorFile, messageType, messageTypeToExtensionMap, defaultValueMap ); // Build the extension registry based on the cached extension map extensionRegistry = ExtensionRegistry.newInstance(); for(Map.Entry<String, Set<Descriptors.FieldDescriptor>> e : messageTypeToExtensionMap.entrySet()) { Set<Descriptors.FieldDescriptor> value = e.getValue(); for (Descriptors.FieldDescriptor f : value) { extensionRegistry.add(f); } } }
@Override public DataParser getParser(String id, InputStream is, String offset) throws DataParserException { try { return new ProtobufDataParser( getSettings().getContext(), id, descriptor, messageTypeToExtensionMap, extensionRegistry, is, offset, getSettings().getOverRunLimit(), isDelimited ); } catch (IOException | Descriptors.DescriptorValidationException e) { throw new DataParserException(Errors.DATA_PARSER_01, e.toString(), e); } }
/** * Populates a map of protobuf extensions and map with the default values for * each message field from a map of file descriptors. * * @param fileDescriptorMap Map of file descriptors * @param typeToExtensionMap Map of extensions to populate * @param defaultValueMap Map of default values to populate */ public static void populateDefaultsAndExtensions( Map<String, Descriptors.FileDescriptor> fileDescriptorMap, Map<String, Set<Descriptors.FieldDescriptor>> typeToExtensionMap, Map<String, Object> defaultValueMap ) { for (Descriptors.FileDescriptor f : fileDescriptorMap.values()) { // go over every file descriptor and look for extensions and default values of those extensions for (Descriptors.FieldDescriptor fieldDescriptor : f.getExtensions()) { String containingType = fieldDescriptor.getContainingType().getFullName(); Set<Descriptors.FieldDescriptor> fieldDescriptors = typeToExtensionMap.get(containingType); if (fieldDescriptors == null) { fieldDescriptors = new LinkedHashSet<>(); typeToExtensionMap.put(containingType, fieldDescriptors); } fieldDescriptors.add(fieldDescriptor); if (fieldDescriptor.hasDefaultValue()) { defaultValueMap.put(containingType + "." + fieldDescriptor.getName(), fieldDescriptor.getDefaultValue()); } } // go over messages within file descriptor and look for all fields and extensions and their defaults for (Descriptors.Descriptor d : f.getMessageTypes()) { addDefaultsAndExtensions(typeToExtensionMap, defaultValueMap, d); } } }
public void register(Service service) { // TODO: Support registering multiple local services? Needs "local 2PC" effectively. Yuck. Descriptors.ServiceDescriptor descriptor = service.getDescriptorForType(); for (MethodDescriptor i : descriptor.getMethods()) { if (methods.containsKey(i.getFullName())) { throw new IllegalStateException( "method " + i.getFullName() + " is already registered"); } methods.put(i.getFullName(), new ProtoMethodInvoker(service, i)); } }
public void callMethod(Descriptors.MethodDescriptor method, RpcController controller, Message request, Message responsePrototype, RpcCallback<Message> done) { ProtoRpcController rpc = (ProtoRpcController) controller; rpc.startRpc(eventLoop, responsePrototype.newBuilderForType(), done); if (connection == null) { // closed connection: fail the RPC rpc.finishRpcFailure(Protocol.Status.ERROR_COMMUNICATION, "Connection closed"); return; } // Package up the request and send it final boolean debug = LOG.isDebugEnabled(); synchronized (this) { pendingRpcs.put(sequence, rpc); // System.err.println("Sending RPC sequence " + sequence); RpcRequest rpcRequest = makeRpcRequest(sequence, method, request); sequence += 1; boolean blocked = connection.tryWrite(rpcRequest); if (blocked) { // the write blocked: wait for write callbacks if (debug) LOG.debug("registering write with eventLoop: " + eventLoop); eventLoop.registerWrite(connection.getChannel(), this); } if (debug) LOG.debug(String.format("%d: Sending RPC %s sequence %d blocked = %b", hashCode(), method.getFullName(), sequence, blocked)); } }
public static RpcRequest makeRpcRequest( int sequence, Descriptors.MethodDescriptor method, Message request) { RpcRequest.Builder requestBuilder = RpcRequest.newBuilder(); requestBuilder.setSequenceNumber(sequence); requestBuilder.setMethodName(method.getFullName()); requestBuilder.setRequest(request.toByteString()); return requestBuilder.build(); }
private void print(final Message message, final JavaPropsGenerator generator) throws IOException { for (final Map.Entry<Descriptors.FieldDescriptor, Object> field : message.getAllFields().entrySet()) { printField(field.getKey(), field.getValue(), generator); } printUnknownFields(message.getUnknownFields(), generator); }
public void printField(final Descriptors.FieldDescriptor field, final Object value, final Appendable output) throws IOException { final JavaPropsGenerator generator = new JavaPropsGenerator(output); printField(field, value, generator); }
public String printFieldToString(final Descriptors.FieldDescriptor field, final Object value) { try { final StringBuilder text = new StringBuilder(); printField(field, value, text); return text.toString(); } catch (IOException e) { throw new RuntimeException( "Writing to a StringBuilder threw an IOException (should never " + "happen).", e); } }
private void printField(final Descriptors.FieldDescriptor field, final Object value, final JavaPropsGenerator generator) throws IOException { if (field.isRepeated()) { // Repeated field. Print each element. List<?> list = (List<?>) value; for (int i = 0; i < list.size(); i++) { printSingleField(field, list.get(i), i, generator); } } else { printSingleField(field, value, null, generator); } }
private Descriptors.Descriptor getDescriptor(Class<M> messageClass) { try { return (Descriptors.Descriptor) MethodUtils.invokeStaticMethod( messageClass, "getDescriptor"); } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) { throw new RuntimeException(ex); } }
/** * set preparedstatement params * * @param ps * @param args * @return * @throws SQLException */ private void populate(PreparedStatement ps, List<?> args) throws SQLException { for (int i = 0; i < args.size(); i++) { Object o = args.get(i); if (o instanceof Integer) { ps.setInt(i + 1, (int) o); } else if (o instanceof Long) { ps.setLong(i + 1, (long) o); } else if (o instanceof String) { ps.setString(i + 1, (String) o); } else if (o instanceof Date) { ps.setDate(i + 1, (Date) o); } else if (o instanceof Float) { ps.setFloat(i + 1, (Float) o); } else if (o instanceof Double) { ps.setDouble(i + 1, (Double) o); } else if (o instanceof Date) { ps.setDate(i + 1, (Date) o); } else if (o instanceof Timestamp) { ps.setTimestamp(i + 1, (Timestamp) o); } else if (o instanceof Descriptors.EnumValueDescriptor) { ps.setInt(i + 1, ((Descriptors.EnumValueDescriptor) o).getNumber()); } else if(o instanceof Boolean){ ps.setBoolean(i+1, (Boolean)o); } else { ps.setObject(i+1, o); } } }
/** * * @param rs * @param builder * @throws SQLException */ private void populate(ResultSet rs, Message.Builder builder) throws SQLException { ResultSetMetaData metaData = rs.getMetaData(); int columnCount = metaData.getColumnCount();// 列个数 String columnLabel = null;// 列名 Object columnValue = null;// 列值 Descriptors.FieldDescriptor fieldDescriptor = null; for (int i = 1; i <= columnCount; i++) { columnLabel = metaData.getColumnLabel(i); columnValue = rs.getObject(i); if (columnValue == null) continue;// 如果为空,继续下一个 fieldDescriptor = descriptor.findFieldByName(columnLabel); if (fieldDescriptor == null) continue;// 如果为空,继续下一个 // 转换为相应的类型 ,会自动将 date 类型转换为long if (fieldDescriptor.getType().equals(FieldDescriptor.Type.ENUM)) { columnValue = fieldDescriptor.getEnumType().findValueByNumber( (int) columnValue); } else { columnValue = ConvertUtils.convert(columnValue, fieldDescriptor .getDefaultValue().getClass()); } builder.setField(fieldDescriptor, columnValue); } }