use of java.util.function.BiConsumer in project DataX by alibaba.
the class DefaultGdbMapper method forElement.
private static BiConsumer<Record, GdbElement> forElement(final MappingRule rule) {
final boolean numPattern = rule.isNumPattern();
final List<BiConsumer<Record, GdbElement>> properties = new ArrayList<>();
for (final MappingRule.PropertyMappingRule propRule : rule.getProperties()) {
final Function<Record, String> keyFunc = forStrColumn(numPattern, propRule.getKey());
if (propRule.getValueType() == ValueType.STRING) {
final Function<Record, String> valueFunc = forStrColumn(numPattern, propRule.getValue());
properties.add((r, e) -> {
e.addProperty(keyFunc.apply(r), valueFunc.apply(r), propRule.getPType());
});
} else {
final Function<Record, Object> valueFunc = forObjColumn(numPattern, propRule.getValue(), propRule.getValueType());
properties.add((r, e) -> {
e.addProperty(keyFunc.apply(r), valueFunc.apply(r), propRule.getPType());
});
}
}
if (rule.getPropertiesJsonStr() != null) {
final Function<Record, String> jsonFunc = forStrColumn(numPattern, rule.getPropertiesJsonStr());
properties.add((r, e) -> {
final String propertiesStr = jsonFunc.apply(r);
final JSONObject root = (JSONObject) JSONObject.parse(propertiesStr);
final JSONArray propertiesList = root.getJSONArray("properties");
for (final Object object : propertiesList) {
final JSONObject jsonObject = (JSONObject) object;
final String key = jsonObject.getString("k");
final String name = jsonObject.getString("v");
final String type = jsonObject.getString("t");
final String card = jsonObject.getString("c");
if (key == null || name == null) {
continue;
}
addToProperties(e, key, name, type, card);
}
});
}
final BiConsumer<Record, GdbElement> ret = (r, e) -> {
final String label = forStrColumn(numPattern, rule.getLabel()).apply(r);
String id = forStrColumn(numPattern, rule.getId()).apply(r);
if (rule.getImportType() == Key.ImportType.EDGE) {
final String to = forStrColumn(numPattern, rule.getTo()).apply(r);
final String from = forStrColumn(numPattern, rule.getFrom()).apply(r);
if (to == null || from == null) {
log.error("invalid record to: {} , from: {}", to, from);
throw new IllegalArgumentException("to or from missed in edge");
}
((GdbEdge) e).setTo(to);
((GdbEdge) e).setFrom(from);
// generate UUID for edge
if (id == null) {
id = UUID.randomUUID().toString();
}
}
if (id == null || label == null) {
log.error("invalid record id: {} , label: {}", id, label);
throw new IllegalArgumentException("id or label missed");
}
e.setId(id);
e.setLabel(label);
properties.forEach(p -> p.accept(r, e));
};
return ret;
}
use of java.util.function.BiConsumer in project spring-boot by spring-projects.
the class PropertyDescriptorResolverTests method process.
private void process(Class<?> target, Collection<Class<?>> additionalClasses, BiConsumer<TypeElement, MetadataGenerationEnvironment> consumer) throws IOException {
BiConsumer<RoundEnvironmentTester, MetadataGenerationEnvironment> internalConsumer = (roundEnv, metadataEnv) -> {
TypeElement element = roundEnv.getRootElement(target);
consumer.accept(element, metadataEnv);
};
TestableAnnotationProcessor<MetadataGenerationEnvironment> processor = new TestableAnnotationProcessor<>(internalConsumer, new MetadataGenerationEnvironmentFactory());
TestCompiler compiler = new TestCompiler(this.tempDir);
ArrayList<Class<?>> allClasses = new ArrayList<>();
allClasses.add(target);
allClasses.addAll(additionalClasses);
compiler.getTask(allClasses.toArray(new Class<?>[0])).call(processor);
}
use of java.util.function.BiConsumer in project vert.x by eclipse.
the class WebSocketTest method testTLS.
private void testTLS(Cert<?> clientCert, Trust<?> clientTrust, Cert<?> serverCert, Trust<?> serverTrust, boolean requireClientAuth, boolean serverUsesCrl, boolean clientTrustAll, boolean clientUsesCrl, boolean shouldPass, boolean clientSsl, boolean serverSsl, boolean sni, String[] enabledCipherSuites, BiConsumer<HttpClient, Handler<AsyncResult<WebSocket>>> wsProvider) throws Exception {
HttpClientOptions options = new HttpClientOptions();
options.setSsl(clientSsl);
options.setTrustAll(clientTrustAll);
if (clientUsesCrl) {
options.addCrlPath("tls/root-ca/crl.pem");
}
options.setTrustOptions(clientTrust.get());
options.setKeyCertOptions(clientCert.get());
for (String suite : enabledCipherSuites) {
options.addEnabledCipherSuite(suite);
}
client = vertx.createHttpClient(options);
HttpServerOptions serverOptions = new HttpServerOptions();
serverOptions.setSsl(serverSsl);
serverOptions.setSni(sni);
serverOptions.setTrustOptions(serverTrust.get());
serverOptions.setKeyCertOptions(serverCert.get());
if (requireClientAuth) {
serverOptions.setClientAuth(ClientAuth.REQUIRED);
}
if (serverUsesCrl) {
serverOptions.addCrlPath("tls/root-ca/crl.pem");
}
for (String suite : enabledCipherSuites) {
serverOptions.addEnabledCipherSuite(suite);
}
server = vertx.createHttpServer(serverOptions.setPort(4043));
server.webSocketHandler(ws -> {
ws.handler(ws::write);
});
try {
server.listen(ar -> {
assertTrue(ar.succeeded());
Handler<AsyncResult<WebSocket>> handler = ar2 -> {
if (ar2.succeeded()) {
WebSocket ws = ar2.result();
if (clientSsl && sni) {
try {
Certificate clientPeerCert = ws.peerCertificates().get(0);
assertEquals("host2.com", TestUtils.cnOf(clientPeerCert));
} catch (Exception err) {
fail(err);
}
}
int size = 100;
Buffer received = Buffer.buffer();
ws.handler(data -> {
received.appendBuffer(data);
if (received.length() == size) {
ws.close();
testComplete();
}
});
Buffer buff = Buffer.buffer(TestUtils.randomByteArray(size));
ws.writeFrame(WebSocketFrame.binaryFrame(buff, true));
} else {
if (shouldPass) {
ar2.cause().printStackTrace();
fail("Should not throw exception");
} else {
testComplete();
}
}
};
wsProvider.accept(client, handler);
});
} catch (Exception e) {
e.printStackTrace();
}
await();
}
use of java.util.function.BiConsumer in project vert.x by eclipse.
the class CompositeFutureTest method testConcurrentCompletion.
private void testConcurrentCompletion(BiConsumer<Integer, Promise<String>> completer, Function<List<Future>, CompositeFuture> fact, Consumer<CompositeFuture> check) throws Exception {
disableThreadChecks();
List<Promise<String>> promises = IntStream.range(0, NUM_THREADS).mapToObj(i -> Promise.<String>promise()).collect(Collectors.toList());
List<Future> futures = promises.stream().map(Promise::future).collect(Collectors.toList());
CompositeFuture compositeFuture = fact.apply(futures);
ExecutorService executorService = Executors.newFixedThreadPool(NUM_THREADS);
CyclicBarrier barrier = new CyclicBarrier(NUM_THREADS);
for (int i = 0; i < NUM_THREADS; ++i) {
final int x = i;
executorService.submit(() -> {
Promise<String> promise = promises.get(x);
try {
barrier.await();
completer.accept(x, promise);
} catch (Throwable t) {
fail(t);
}
});
}
compositeFuture.onComplete(x -> {
check.accept(compositeFuture);
testComplete();
});
executorService.shutdown();
executorService.awaitTermination(30, TimeUnit.SECONDS);
await();
}
use of java.util.function.BiConsumer in project vert.x by eclipse.
the class Http2ServerTest method testPushPromise.
private void testPushPromise(Http2Headers requestHeaders, BiConsumer<HttpServerResponse, Handler<AsyncResult<HttpServerResponse>>> pusher, Consumer<Http2Headers> headerChecker) throws Exception {
Context ctx = vertx.getOrCreateContext();
server.requestHandler(req -> {
Handler<AsyncResult<HttpServerResponse>> handler = ar -> {
assertSameEventLoop(ctx, Vertx.currentContext());
assertTrue(ar.succeeded());
HttpServerResponse response = ar.result();
response.end("the_content");
assertIllegalStateException(() -> response.push(HttpMethod.GET, "/wibble2", resp -> {
}));
};
pusher.accept(req.response(), handler);
});
startServer(ctx);
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
int id = request.nextStreamId();
Http2ConnectionEncoder encoder = request.encoder;
encoder.writeHeaders(request.context, id, requestHeaders, 0, true, request.context.newPromise());
Map<Integer, Http2Headers> pushed = new HashMap<>();
request.decoder.frameListener(new Http2FrameAdapter() {
@Override
public void onPushPromiseRead(ChannelHandlerContext ctx, int streamId, int promisedStreamId, Http2Headers headers, int padding) throws Http2Exception {
pushed.put(promisedStreamId, headers);
}
@Override
public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception {
int delta = super.onDataRead(ctx, streamId, data, padding, endOfStream);
String content = data.toString(StandardCharsets.UTF_8);
vertx.runOnContext(v -> {
assertEquals(Collections.singleton(streamId), pushed.keySet());
assertEquals("the_content", content);
Http2Headers pushedHeaders = pushed.get(streamId);
headerChecker.accept(pushedHeaders);
testComplete();
});
return delta;
}
});
});
fut.sync();
await();
}
Aggregations