public void generateFile(String protoPath) { try { if (pojoTypes == null) { pojoTypes = Maps.newHashMap(); } } finally { if (!new File(protoPath).exists()) { logger.warn("protoPath:" + protoPath + " not exist, it may be in the third party jars, so it can't be generate"); return; } FileDescriptorSet fileDescriptorSet = commondProtoc.invoke(protoPath); for (FileDescriptorProto fdp : fileDescriptorSet.getFileList()) { Pair<String, String> packageClassName = this.packageClassName(fdp.getOptions()); if (packageClassName == null) { continue; } ProtocolStringList dependencyList = fdp.getDependencyList(); for (Iterator<String> it = dependencyList.iterator(); it.hasNext();) { String dependencyPath = discoveryRoot + "/" + it.next(); generateFile(dependencyPath); } doPrint(fdp, packageClassName.getLeft(), packageClassName.getRight()); } } }
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; }
public static void tryRemoveFromGroup(UnitConfigType.UnitConfig group, String userId) throws CouldNotPerformException, InterruptedException { UnitConfigType.UnitConfig.Builder unitConfig = Registries.getUserRegistry().getAuthorizationGroupConfigById(group.getId()).toBuilder(); AuthorizationGroupConfigType.AuthorizationGroupConfig.Builder authorizationGroupConfig = unitConfig.getAuthorizationGroupConfigBuilder(); ProtocolStringList members = authorizationGroupConfig.getMemberIdList(); authorizationGroupConfig.clearMemberId(); for (String member : members) { if (!member.equals(userId)) { authorizationGroupConfig.addMemberId(member); } } Registries.getUserRegistry().updateAuthorizationGroupConfig(unitConfig.build()); }
@Override public void batchRegister(NetworkAddresses request, StreamObserver<NetworkAddressMappings> responseObserver) { logger.debug("register application"); ProtocolStringList addressesList = request.getAddressesList(); NetworkAddressMappings.Builder builder = NetworkAddressMappings.newBuilder(); for (int i = 0; i < addressesList.size(); i++) { String networkAddress = addressesList.get(i); int addressId = networkAddressIDService.getOrCreate(networkAddress); if (addressId != 0) { KeyWithIntegerValue value = KeyWithIntegerValue.newBuilder().setKey(networkAddress).setValue(addressId).build(); builder.addAddressIds(value); } } responseObserver.onNext(builder.build()); responseObserver.onCompleted(); }
/** * Cleans the currently built commandHandlers from the duplicates. * * <p>Calling this method will cause the {@linkplain #commandHandlers current commandHandlers} * not to contain duplicate entries in any {@code repeated} field. */ private void removeDuplicates() { final ProtocolStringList handlingTypesList = commandHandlers.getCommandHandlingTypesList(); final Set<String> commandHandlingTypes = newTreeSet(handlingTypesList); commandHandlers.clearCommandHandlingTypes() .addAllCommandHandlingTypes(commandHandlingTypes); }
private static void verifyMultiplePathsInQuery(String[] paths, Query readAllWithPathFilteringQuery) { final FieldMask fieldMask = readAllWithPathFilteringQuery.getFieldMask(); assertEquals(paths.length, fieldMask.getPathsCount()); final ProtocolStringList pathsList = fieldMask.getPathsList(); for (String expectedPath : paths) { assertTrue(pathsList.contains(expectedPath)); } }
/** * Applies the given {@code FieldMask} to given collection of {@link Message}s. * Does not change the {@link Collection} itself. * * <p>In case the {@code FieldMask} instance contains invalid field declarations, they are * ignored and do not affect the execution result. * * @param mask {@code FieldMask} to apply to each item of the input {@link Collection}. * @param messages {@link Message}s to filter. * @param type type of the {@link Message}s. * @return messages with the {@code FieldMask} applied */ @Nonnull public static <M extends Message, B extends Message.Builder> Collection<M> applyMask(FieldMask mask, Collection<M> messages, TypeUrl type) { checkNotNull(mask); checkNotNull(messages); checkNotNull(type); final List<M> filtered = new LinkedList<>(); final ProtocolStringList filter = mask.getPathsList(); final Class<B> builderClass = getBuilderForType(type); if (filter.isEmpty() || builderClass == null) { return Collections.unmodifiableCollection(messages); } try { final Constructor<B> builderConstructor = builderClass.getDeclaredConstructor(); builderConstructor.setAccessible(true); for (Message wholeMessage : messages) { final M message = messageForFilter(filter, builderConstructor, wholeMessage); filtered.add(message); } } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | InstantiationException e) { // If any reflection failure happens, return all the data without any mask applied. log().warn(format(CONSTRUCTOR_INVOCATION_ERROR_LOGGING_PATTERN, builderClass.getCanonicalName()), e); return Collections.unmodifiableCollection(messages); } return Collections.unmodifiableList(filtered); }
private static <M extends Message, B extends Message.Builder> M doApply(FieldMask mask, M message, TypeUrl type) { checkNotNull(mask); checkNotNull(message); checkNotNull(type); final ProtocolStringList filter = mask.getPathsList(); final Class<B> builderClass = getBuilderForType(type); if (builderClass == null) { return message; } try { final Constructor<B> builderConstructor = builderClass.getDeclaredConstructor(); builderConstructor.setAccessible(true); final M result = messageForFilter(filter, builderConstructor, message); return result; } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | InstantiationException e) { log().warn(format(CONSTRUCTOR_INVOCATION_ERROR_LOGGING_PATTERN, builderClass.getCanonicalName()), e); return message; } }
@Override public void processElement(DoFn<WorkPacketConfig, Map<Integer, List<WorkPacketKey>>>.ProcessContext c) throws Exception { long keyCount = 0; // Now we build the cartesian join and emit each pair forward, we ignore // self joins and transitive joins {a,b,c} become {a-b,a-c,b-c} Set<String> transList = new HashSet<String>(); Map<Integer, List<WorkPacketKey>> keysForPartition = new HashMap<>(); ProtocolStringList keyList = c.element().getKeysList(); for (String outer : keyList) { for (String inner : keyList) { String key = WorkPacketUtils.createKey(outer, inner); if (!outer.equals(inner) && !transList.contains(key)) { int partition = WorkPacketUtils.getMyPartitions(c.element(), key); ++keyCount; if (!keysForPartition.containsKey(partition)) { keysForPartition.put(partition, new ArrayList<WorkPacketKey>()); } keysForPartition.get(partition) .add(WorkPacketKey.newBuilder().setKey1(inner).setKey2(outer).build()); transList.add(key); } } } // TODO: Check for empty list c.output(keysForPartition); LOG.info(String.format("Number of Keys was %s Number of partitions are %s", keyCount, c.element().getPartitionLength())); }