SPI Factory cleanup - fixes #3109

This commit is contained in:
Julien Viet
2019-09-16 15:04:36 +02:00
parent b1c91fd3b8
commit ba5fd53583
17 changed files with 79 additions and 299 deletions

View File

@@ -16,13 +16,12 @@ import io.netty.buffer.ByteBuf;
import io.vertx.codegen.annotations.Fluent;
import io.vertx.codegen.annotations.GenIgnore;
import io.vertx.codegen.annotations.VertxGen;
import io.vertx.core.ServiceHelper;
import io.vertx.core.buffer.impl.BufferImpl;
import io.vertx.core.json.Json;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.core.shareddata.Shareable;
import io.vertx.core.shareddata.impl.ClusterSerializable;
import io.vertx.core.spi.BufferFactory;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
@@ -46,7 +45,7 @@ public interface Buffer extends ClusterSerializable, Shareable {
* @return the buffer
*/
static Buffer buffer() {
return factory.buffer();
return BufferImpl.buffer();
}
/**
@@ -59,7 +58,7 @@ public interface Buffer extends ClusterSerializable, Shareable {
* @return the buffer
*/
static Buffer buffer(int initialSizeHint) {
return factory.buffer(initialSizeHint);
return BufferImpl.buffer(initialSizeHint);
}
/**
@@ -69,7 +68,7 @@ public interface Buffer extends ClusterSerializable, Shareable {
* @return the buffer
*/
static Buffer buffer(String string) {
return factory.buffer(string);
return BufferImpl.buffer(string);
}
/**
@@ -80,7 +79,7 @@ public interface Buffer extends ClusterSerializable, Shareable {
* @return the buffer
*/
static Buffer buffer(String string, String enc) {
return factory.buffer(string, enc);
return BufferImpl.buffer(string, enc);
}
/**
@@ -91,7 +90,7 @@ public interface Buffer extends ClusterSerializable, Shareable {
*/
@GenIgnore(GenIgnore.PERMITTED_TYPE)
static Buffer buffer(byte[] bytes) {
return factory.buffer(bytes);
return BufferImpl.buffer(bytes);
}
/**
@@ -113,7 +112,7 @@ public interface Buffer extends ClusterSerializable, Shareable {
*/
@GenIgnore(GenIgnore.PERMITTED_TYPE)
static Buffer buffer(ByteBuf byteBuf) {
return factory.buffer(byteBuf);
return BufferImpl.buffer(byteBuf);
}
/**
@@ -709,7 +708,4 @@ public interface Buffer extends ClusterSerializable, Shareable {
@GenIgnore(GenIgnore.PERMITTED_TYPE)
ByteBuf getByteBuf();
@GenIgnore
BufferFactory factory = ServiceHelper.loadFactory(BufferFactory.class);
}

View File

@@ -1,68 +0,0 @@
/*
* Copyright (c) 2014 Red Hat, Inc. and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package io.vertx.core.buffer.impl;
import io.netty.buffer.ByteBuf;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.net.impl.PartialPooledByteBufAllocator;
import io.vertx.core.spi.BufferFactory;
import java.nio.charset.Charset;
import java.util.Objects;
/**
* @author <a href="http://tfox.org">Tim Fox</a>
*/
public class BufferFactoryImpl implements BufferFactory {
@Override
public Buffer buffer(int initialSizeHint) {
return new BufferImpl(initialSizeHint);
}
@Override
public Buffer buffer() {
return new BufferImpl();
}
@Override
public Buffer buffer(String str) {
return new BufferImpl(str);
}
@Override
public Buffer buffer(String str, String enc) {
return new BufferImpl(str, enc);
}
@Override
public Buffer buffer(byte[] bytes) {
return new BufferImpl(bytes);
}
@Override
public Buffer buffer(ByteBuf byteBuffer) {
return new BufferImpl(byteBuffer);
}
@Override
public Buffer directBuffer(String str, String enc) {
return directBuffer(str.getBytes(Charset.forName(Objects.requireNonNull(enc))));
}
@Override
public Buffer directBuffer(byte[] bytes) {
ByteBuf buff = PartialPooledByteBufAllocator.UNPOOLED.directBuffer(bytes.length);
buff.writeBytes(bytes);
return new BufferImpl(buff);
}
}

View File

@@ -19,6 +19,7 @@ import io.vertx.core.buffer.Buffer;
import io.vertx.core.impl.Arguments;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.core.net.impl.PartialPooledByteBufAllocator;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
@@ -30,6 +31,40 @@ import java.util.Objects;
*/
public class BufferImpl implements Buffer {
public static Buffer buffer(int initialSizeHint) {
return new BufferImpl(initialSizeHint);
}
public static Buffer buffer() {
return new BufferImpl();
}
public static Buffer buffer(String str) {
return new BufferImpl(str);
}
public static Buffer buffer(String str, String enc) {
return new BufferImpl(str, enc);
}
public static Buffer buffer(byte[] bytes) {
return new BufferImpl(bytes);
}
public static Buffer buffer(ByteBuf byteBuffer) {
return new BufferImpl(byteBuffer);
}
public static Buffer directBuffer(String str, String enc) {
return directBuffer(str.getBytes(Charset.forName(Objects.requireNonNull(enc))));
}
public static Buffer directBuffer(byte[] bytes) {
ByteBuf buff = PartialPooledByteBufAllocator.UNPOOLED.directBuffer(bytes.length);
buff.writeBytes(bytes);
return new BufferImpl(buff);
}
private ByteBuf buffer;
public BufferImpl() {

View File

@@ -12,11 +12,9 @@
package io.vertx.core.http;
import io.vertx.codegen.annotations.CacheReturn;
import io.vertx.codegen.annotations.GenIgnore;
import io.vertx.codegen.annotations.VertxGen;
import io.vertx.core.ServiceHelper;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.spi.WebSocketFrameFactory;
import io.vertx.core.http.impl.ws.WebSocketFrameImpl;
/**
* A WebSocket frame that represents either text or binary data.
@@ -44,7 +42,7 @@ public interface WebSocketFrame {
* @return the frame
*/
static WebSocketFrame binaryFrame(Buffer data, boolean isFinal) {
return factory.binaryFrame(data, isFinal);
return WebSocketFrameImpl.binaryFrame(data, isFinal);
}
/**
@@ -55,7 +53,7 @@ public interface WebSocketFrame {
* @return the frame
*/
static WebSocketFrame textFrame(String str, boolean isFinal) {
return factory.textFrame(str, isFinal);
return WebSocketFrameImpl.textFrame(str, isFinal);
}
/**
@@ -65,7 +63,7 @@ public interface WebSocketFrame {
* @return the frame
*/
static WebSocketFrame pingFrame(Buffer data) {
return factory.pingFrame(data);
return WebSocketFrameImpl.pingFrame(data);
}
/**
@@ -75,7 +73,7 @@ public interface WebSocketFrame {
* @return the frame
*/
static WebSocketFrame pongFrame(Buffer data) {
return factory.pongFrame(data);
return WebSocketFrameImpl.pongFrame(data);
}
/**
@@ -86,7 +84,7 @@ public interface WebSocketFrame {
* @return the frame
*/
static WebSocketFrame continuationFrame(Buffer data, boolean isFinal) {
return factory.continuationFrame(data, isFinal);
return WebSocketFrameImpl.continuationFrame(data, isFinal);
}
/**
@@ -137,6 +135,4 @@ public interface WebSocketFrame {
*/
String closeReason();
@GenIgnore
WebSocketFrameFactory factory = ServiceHelper.loadFactory(WebSocketFrameFactory.class);
}

View File

@@ -1,49 +0,0 @@
/*
* Copyright (c) 2011-2017 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package io.vertx.core.http.impl;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.WebSocketFrame;
import io.vertx.core.http.impl.ws.WebSocketFrameImpl;
import io.vertx.core.spi.WebSocketFrameFactory;
/**
* @author <a href="http://tfox.org">Tim Fox</a>
*/
public class WebSocketFrameFactoryImpl implements WebSocketFrameFactory {
@Override
public WebSocketFrame binaryFrame(Buffer data, boolean isFinal) {
return new WebSocketFrameImpl(FrameType.BINARY, data.getByteBuf(), isFinal);
}
@Override
public WebSocketFrame textFrame(String str, boolean isFinal) {
return new WebSocketFrameImpl(str, isFinal);
}
@Override
public WebSocketFrame continuationFrame(Buffer data, boolean isFinal) {
return new WebSocketFrameImpl(FrameType.CONTINUATION, data.getByteBuf(), isFinal);
}
@Override
public WebSocketFrame pingFrame(Buffer data) {
return new WebSocketFrameImpl(FrameType.PING, data.getByteBuf(), true);
}
@Override
public WebSocketFrame pongFrame(Buffer data) {
return new WebSocketFrameImpl(FrameType.PONG, data.getByteBuf(), true);
}
}

View File

@@ -16,6 +16,7 @@ import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCounted;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.WebSocketFrame;
import io.vertx.core.http.impl.FrameType;
import java.nio.charset.StandardCharsets;
@@ -29,6 +30,25 @@ import java.nio.charset.StandardCharsets;
*/
public class WebSocketFrameImpl implements WebSocketFrameInternal, ReferenceCounted {
public static WebSocketFrame binaryFrame(Buffer data, boolean isFinal) {
return new WebSocketFrameImpl(FrameType.BINARY, data.getByteBuf(), isFinal);
}
public static WebSocketFrame textFrame(String str, boolean isFinal) {
return new WebSocketFrameImpl(str, isFinal);
}
public static WebSocketFrame continuationFrame(Buffer data, boolean isFinal) {
return new WebSocketFrameImpl(FrameType.CONTINUATION, data.getByteBuf(), isFinal);
}
public static WebSocketFrame pingFrame(Buffer data) {
return new WebSocketFrameImpl(FrameType.PING, data.getByteBuf(), true);
}
public static WebSocketFrame pongFrame(Buffer data) {
return new WebSocketFrameImpl(FrameType.PONG, data.getByteBuf(), true);
}
private final FrameType type;
private final boolean isFinalFrame;

View File

@@ -1,45 +0,0 @@
/*
* Copyright (c) 2014 Red Hat, Inc. and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package io.vertx.core.spi;
import io.netty.buffer.ByteBuf;
import io.vertx.core.buffer.Buffer;
/**
* @author <a href="http://tfox.org">Tim Fox</a>
*/
public interface BufferFactory {
Buffer buffer(int initialSizeHint);
Buffer buffer();
Buffer buffer(String str);
Buffer buffer(String str, String enc);
Buffer buffer(byte[] bytes);
Buffer buffer(ByteBuf byteBuffer);
Buffer directBuffer(String str, String enc);
/**
* Create a direct buffer, use this with care as Vert.x buffers are not releasable (unpooled) and you would need
* to release the underlying Netty wrapped {@code ByteBuf}.
*
* @param bytes the bytes
* @return the buffer
*/
Buffer directBuffer(byte[] bytes);
}

View File

@@ -1,26 +0,0 @@
/*
* Copyright (c) 2014 Red Hat, Inc. and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package io.vertx.core.spi;
import io.vertx.core.streams.Pump;
import io.vertx.core.streams.ReadStream;
import io.vertx.core.streams.WriteStream;
/**
* @author <a href="http://tfox.org">Tim Fox</a>
*/
public interface PumpFactory {
<T> Pump pump(ReadStream<T> rs, WriteStream<T> ws);
<T> Pump pump(ReadStream<T> rs, WriteStream<T> ws, int writeQueueMaxSize);
}

View File

@@ -1,32 +0,0 @@
/*
* Copyright (c) 2011-2017 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package io.vertx.core.spi;
import io.netty.buffer.ByteBuf;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.WebSocketFrame;
/**
* @author <a href="http://tfox.org">Tim Fox</a>
*/
public interface WebSocketFrameFactory {
WebSocketFrame binaryFrame(Buffer data, boolean isFinal);
WebSocketFrame textFrame(String str, boolean isFinal);
WebSocketFrame continuationFrame(Buffer data, boolean isFinal);
WebSocketFrame pingFrame(Buffer data);
WebSocketFrame pongFrame(Buffer data);
}

View File

@@ -12,10 +12,8 @@
package io.vertx.core.streams;
import io.vertx.codegen.annotations.Fluent;
import io.vertx.codegen.annotations.GenIgnore;
import io.vertx.codegen.annotations.VertxGen;
import io.vertx.core.ServiceHelper;
import io.vertx.core.spi.PumpFactory;
import io.vertx.core.streams.impl.PumpImpl;
/**
* Pumps data from a {@link ReadStream} to a {@link WriteStream} and performs flow control where necessary to
@@ -51,7 +49,7 @@ public interface Pump {
* @return the pump
*/
static <T> Pump pump(ReadStream<T> rs, WriteStream<T> ws) {
return factory.pump(rs, ws);
return new PumpImpl<>(rs, ws);
}
/**
@@ -64,7 +62,7 @@ public interface Pump {
* @return the pump
*/
static <T> Pump pump(ReadStream<T> rs, WriteStream<T> ws, int writeQueueMaxSize) {
return factory.pump(rs, ws, writeQueueMaxSize);
return new PumpImpl<>(rs, ws, writeQueueMaxSize);
}
/**
@@ -97,8 +95,4 @@ public interface Pump {
*/
int numberPumped();
@GenIgnore
PumpFactory factory = ServiceHelper.loadFactory(PumpFactory.class);
}

View File

@@ -1,38 +0,0 @@
/*
* Copyright (c) 2014 Red Hat, Inc. and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package io.vertx.core.streams.impl;
import java.util.Objects;
import io.vertx.core.spi.PumpFactory;
import io.vertx.core.streams.Pump;
import io.vertx.core.streams.ReadStream;
import io.vertx.core.streams.WriteStream;
/**
* @author <a href="http://tfox.org">Tim Fox</a>
*/
public class PumpFactoryImpl implements PumpFactory {
@Override
public <T> Pump pump(ReadStream<T> rs, WriteStream<T> ws) {
Objects.requireNonNull(rs);
Objects.requireNonNull(ws);
return new PumpImpl<>(rs, ws);
}
@Override
public <T> Pump pump(ReadStream<T> rs, WriteStream<T> ws, int writeQueueMaxSize) {
Objects.requireNonNull(rs);
Objects.requireNonNull(ws);
return new PumpImpl<>(rs, ws, writeQueueMaxSize);
}
}

View File

@@ -16,6 +16,8 @@ import io.vertx.core.streams.Pump;
import io.vertx.core.streams.ReadStream;
import io.vertx.core.streams.WriteStream;
import java.util.Objects;
/**
* Pumps data from a {@link io.vertx.core.streams.ReadStream} to a {@link io.vertx.core.streams.WriteStream} and performs flow control where necessary to
* prevent the write stream buffer from getting overfull.<p>
@@ -46,12 +48,14 @@ public class PumpImpl<T> implements Pump {
* Create a new {@code Pump} with the given {@code ReadStream} and {@code WriteStream}. Set the write queue max size
* of the write stream to {@code maxWriteQueueSize}
*/
PumpImpl(ReadStream<T> rs, WriteStream<T> ws, int maxWriteQueueSize) {
public PumpImpl(ReadStream<T> rs, WriteStream<T> ws, int maxWriteQueueSize) {
this(rs, ws);
this.writeStream.setWriteQueueMaxSize(maxWriteQueueSize);
}
PumpImpl(ReadStream<T> rs, WriteStream<T> ws) {
public PumpImpl(ReadStream<T> rs, WriteStream<T> ws) {
Objects.requireNonNull(rs);
Objects.requireNonNull(ws);
this.readStream = rs;
this.writeStream = ws;
drainHandler = v-> readStream.resume();

View File

@@ -1 +0,0 @@
io.vertx.core.buffer.impl.BufferFactoryImpl

View File

@@ -1 +0,0 @@
io.vertx.core.streams.impl.PumpFactoryImpl

View File

@@ -1 +0,0 @@
io.vertx.core.http.impl.WebSocketFrameFactoryImpl

View File

@@ -14,11 +14,10 @@ package io.vertx.core.buffer;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.IllegalReferenceCountException;
import io.vertx.core.buffer.impl.BufferImpl;
import io.vertx.core.json.DecodeException;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.core.net.impl.PartialPooledByteBufAllocator;
import io.vertx.core.spi.BufferFactory;
import io.vertx.test.core.TestUtils;
import org.junit.Test;
@@ -1103,7 +1102,7 @@ public class BufferTest {
@Test
public void testDirect() {
Buffer buff = Buffer.factory.directBuffer("hello world".getBytes());
Buffer buff = BufferImpl.directBuffer("hello world".getBytes());
assertEquals("hello world", buff.toString());
buff.appendString(" foobar");
assertEquals("hello world foobar", buff.toString());

View File

@@ -14,9 +14,6 @@ package io.vertx.core.streams;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.streams.Pump;
import io.vertx.core.streams.ReadStream;
import io.vertx.core.streams.WriteStream;
import org.junit.Test;
import java.util.ArrayList;