com.mysql.cj.protocol.SocketConnection Java Examples

The following examples show how to use com.mysql.cj.protocol.SocketConnection. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SimplePacketReaderTest.java    From FoxTelem with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void readBasicPayload() throws IOException {
    RuntimeProperty<Integer> maxAllowedPacket = new JdbcPropertySetImpl().getProperty(PropertyKey.maxAllowedPacket);
    SocketConnection connection = new FixedBufferSocketConnection(new byte[] { 3, 2, 1, 6, 5, 4 });
    MessageReader<NativePacketHeader, NativePacketPayload> reader = new SimplePacketReader(connection, maxAllowedPacket);
    NativePacketPayload b = reader.readMessage(Optional.empty(), new NativePacketHeader(new byte[] { 3, 0, 0, 0 }));
    assertEquals(3, b.getByteBuffer()[0]);
    assertEquals(2, b.getByteBuffer()[1]);
    assertEquals(1, b.getByteBuffer()[2]);

    // make sure the first only consumed the requested 3 bytes
    b = reader.readMessage(Optional.empty(), new NativePacketHeader(new byte[] { 3, 0, 0, 0 }));
    assertEquals(6, b.getByteBuffer()[0]);
    assertEquals(5, b.getByteBuffer()[1]);
    assertEquals(4, b.getByteBuffer()[2]);
}
 
Example #2
Source File: XProtocol.java    From FoxTelem with GNU General Public License v3.0 6 votes vote down vote up
public XProtocol(String host, int port, String defaultSchema, PropertySet propertySet) {

        this.defaultSchemaName = defaultSchema;

        // Override common connectTimeout with xdevapi.connect-timeout to provide unified logic in StandardSocketFactory
        RuntimeProperty<Integer> connectTimeout = propertySet.getIntegerProperty(PropertyKey.connectTimeout);
        RuntimeProperty<Integer> xdevapiConnectTimeout = propertySet.getIntegerProperty(PropertyKey.xdevapiConnectTimeout);
        if (xdevapiConnectTimeout.isExplicitlySet() || !connectTimeout.isExplicitlySet()) {
            connectTimeout.setValue(xdevapiConnectTimeout.getValue());
        }

        SocketConnection socketConn = propertySet.getBooleanProperty(PropertyKey.xdevapiUseAsyncProtocol).getValue() ? new XAsyncSocketConnection()
                : new NativeSocketConnection();
        socketConn.connect(host, port, propertySet, null, null, 0);
        init(null, socketConn, propertySet, null);
    }
 
Example #3
Source File: XProtocol.java    From FoxTelem with GNU General Public License v3.0 6 votes vote down vote up
public XProtocol(HostInfo hostInfo, PropertySet propertySet) {
    String host = hostInfo.getHost();
    if (host == null || StringUtils.isEmptyOrWhitespaceOnly(host)) {
        host = "localhost";
    }
    int port = hostInfo.getPort();
    if (port < 0) {
        port = 33060;
    }
    this.defaultSchemaName = hostInfo.getDatabase();

    // Override common connectTimeout with xdevapi.connect-timeout to provide unified logic in StandardSocketFactory
    RuntimeProperty<Integer> connectTimeout = propertySet.getIntegerProperty(PropertyKey.connectTimeout);
    RuntimeProperty<Integer> xdevapiConnectTimeout = propertySet.getIntegerProperty(PropertyKey.xdevapiConnectTimeout);
    if (xdevapiConnectTimeout.isExplicitlySet() || !connectTimeout.isExplicitlySet()) {
        connectTimeout.setValue(xdevapiConnectTimeout.getValue());
    }

    SocketConnection socketConn = propertySet.getBooleanProperty(PropertyKey.xdevapiUseAsyncProtocol).getValue() ? new XAsyncSocketConnection()
            : new NativeSocketConnection();
    socketConn.connect(host, port, propertySet, null, null, 0);
    init(null, socketConn, propertySet, null);
}
 
Example #4
Source File: XProtocol.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public static XProtocol getInstance(String host, int port, PropertySet propertySet) {

        SocketConnection socketConnection = propertySet.getBooleanReadableProperty(PropertyDefinitions.PNAME_useAsyncProtocol).getValue()
                ? new XAsyncSocketConnection() :
                // TODO: we should share SocketConnection unless there comes a time where they need to diverge
                new NativeSocketConnection();

        socketConnection.connect(host, port, propertySet, null, null, 0);

        XProtocol protocol = new XProtocol();
        protocol.init(null, socketConnection, propertySet, null);
        return protocol;
    }
 
Example #5
Source File: XProtocol.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void init(Session sess, SocketConnection socketConn, PropertySet propSet, TransactionEventHandler transactionManager) {
    this.socketConnection = socketConn;
    this.propertySet = propSet;
    this.messageBuilder = new XMessageBuilder();

    this.authProvider = new XAuthenticationProvider();
    this.authProvider.init(this, propSet, null);

    this.metadataCharacterSet = "latin1"; // TODO configure from server session
    this.fieldFactory = new FieldFactory(this.metadataCharacterSet);
    this.noticeFactory = new NoticeFactory();
}
 
Example #6
Source File: SimplePacketReaderTest.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void basicHeaderRead() throws IOException {
    RuntimeProperty<Integer> maxAllowedPacket = new JdbcPropertySetImpl().getProperty(PropertyKey.maxAllowedPacket);
    maxAllowedPacket.setValue(100000);
    // mix up the bits so we know they're interpreted correctly
    SocketConnection connection = new FixedBufferSocketConnection(new byte[] { 3, 2, 1, 42 });
    MessageReader<NativePacketHeader, NativePacketPayload> reader = new SimplePacketReader(connection, maxAllowedPacket);
    assertEquals(-1, reader.getMessageSequence());
    NativePacketHeader hdr = reader.readHeader();
    assertEquals(65536 + 512 + 3, hdr.getMessageSize());
    assertEquals(42, hdr.getMessageSequence());
    assertEquals(42, reader.getMessageSequence());
    reader.resetMessageSequence();
    assertEquals(0, reader.getMessageSequence());
}
 
Example #7
Source File: NativeSession.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
public void connect(HostInfo hi, String user, String password, String database, int loginTimeout, TransactionEventHandler transactionManager)
        throws IOException {

    this.hostInfo = hi;

    // reset max-rows to default value
    this.setSessionMaxRows(-1);

    // TODO do we need different types of physical connections?
    SocketConnection socketConnection = new NativeSocketConnection();
    socketConnection.connect(this.hostInfo.getHost(), this.hostInfo.getPort(), this.propertySet, getExceptionInterceptor(), this.log, loginTimeout);

    // we use physical connection to create a -> protocol
    // this configuration places no knowledge of protocol or session on physical connection.
    // physical connection is responsible *only* for I/O streams
    if (this.protocol == null) {
        this.protocol = NativeProtocol.getInstance(this, socketConnection, this.propertySet, this.log, transactionManager);
    } else {
        this.protocol.init(this, socketConnection, this.propertySet, transactionManager);
    }

    // use protocol to create a -> session
    // protocol is responsible for building a session and authenticating (using AuthenticationProvider) internally
    this.protocol.connect(user, password, database);

    // error messages are returned according to character_set_results which, at this point, is set from the response packet
    this.protocol.getServerSession().setErrorMessageEncoding(this.protocol.getAuthenticationProvider().getEncodingForHandshake());

    this.isClosed = false;
}
 
Example #8
Source File: NativeSession.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void connect(HostInfo hi, String user, String password, String database, int loginTimeout, TransactionEventHandler transactionManager)
        throws IOException {

    this.hostInfo = hi;

    // reset max-rows to default value
    this.setSessionMaxRows(-1);

    // TODO do we need different types of physical connections?
    SocketConnection socketConnection = new NativeSocketConnection();
    socketConnection.connect(this.hostInfo.getHost(), this.hostInfo.getPort(), this.propertySet, getExceptionInterceptor(), this.log, loginTimeout);

    // we use physical connection to create a -> protocol
    // this configuration places no knowledge of protocol or session on physical connection.
    // physical connection is responsible *only* for I/O streams
    if (this.protocol == null) {
        this.protocol = NativeProtocol.getInstance(this, socketConnection, this.propertySet, this.log, transactionManager);
    } else {
        this.protocol.init(this, socketConnection, this.propertySet, transactionManager);
    }

    // use protocol to create a -> session
    // protocol is responsible for building a session and authenticating (using AuthenticationProvider) internally
    this.protocol.connect(user, password, database);

    // error messages are returned according to character_set_results which, at this point, is set from the response packet
    this.protocol.getServerSession().setErrorMessageEncoding(this.protocol.getAuthenticationProvider().getEncodingForHandshake());

    this.isClosed = false;
}
 
Example #9
Source File: SocketFactory.java    From cloud-sql-jdbc-socket-factory with Apache License 2.0 5 votes vote down vote up
@Override
public <T extends Closeable> T performTlsHandshake(
    SocketConnection socketConnection, ServerSession serverSession) throws IOException {
  @SuppressWarnings("unchecked")
  T socket = (T) socketConnection.getMysqlSocket();
  return socket;
}
 
Example #10
Source File: XProtocol.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
public void init(Session sess, SocketConnection socketConn, PropertySet propSet, TransactionEventHandler transactionManager) {
    this.socketConnection = socketConn;
    this.propertySet = propSet;
    this.messageBuilder = new XMessageBuilder();

    this.authProvider = new XAuthenticationProvider();
    this.authProvider.init(this, propSet, null);

    this.metadataCharacterSet = "latin1"; // TODO configure from server session
    this.fieldFactory = new FieldFactory(this.metadataCharacterSet);
    this.noticeFactory = new NoticeFactory();
}
 
Example #11
Source File: AsyncMessageReader.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
public AsyncMessageReader(PropertySet propertySet, SocketConnection socketConnection) {
    this.propertySet = propertySet;
    this.sc = socketConnection;
    this.asyncTimeout = this.propertySet.getIntegerProperty(PropertyKey.xdevapiAsyncResponseTimeout);

}
 
Example #12
Source File: NativeProtocol.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void init(Session sess, SocketConnection phConnection, PropertySet propSet, TransactionEventHandler trManager) {

    this.session = sess;
    this.propertySet = propSet;

    this.socketConnection = phConnection;
    this.exceptionInterceptor = this.socketConnection.getExceptionInterceptor();

    this.transactionManager = trManager;

    this.maintainTimeStats = this.propertySet.getBooleanProperty(PropertyKey.maintainTimeStats);
    this.maxQuerySizeToLog = this.propertySet.getIntegerProperty(PropertyKey.maxQuerySizeToLog);
    this.useAutoSlowLog = this.propertySet.getBooleanProperty(PropertyKey.autoSlowLog).getValue();
    this.logSlowQueries = this.propertySet.getBooleanProperty(PropertyKey.logSlowQueries).getValue();
    this.maxAllowedPacket = this.propertySet.getIntegerProperty(PropertyKey.maxAllowedPacket);
    this.profileSQL = this.propertySet.getBooleanProperty(PropertyKey.profileSQL).getValue();
    this.autoGenerateTestcaseScript = this.propertySet.getBooleanProperty(PropertyKey.autoGenerateTestcaseScript).getValue();
    this.useServerPrepStmts = this.propertySet.getBooleanProperty(PropertyKey.useServerPrepStmts);

    this.reusablePacket = new NativePacketPayload(INITIAL_PACKET_SIZE);
    //this.sendPacket = new Buffer(INITIAL_PACKET_SIZE);

    this.packetSender = new SimplePacketSender(this.socketConnection.getMysqlOutput());
    this.packetReader = new SimplePacketReader(this.socketConnection, this.maxAllowedPacket);

    //this.needToGrabQueryFromPacket = (this.profileSQL || this.logSlowQueries || this.autoGenerateTestcaseScript);

    if (this.propertySet.getBooleanProperty(PropertyKey.useNanosForElapsedTime).getValue() && TimeUtil.nanoTimeAvailable()) {
        this.useNanosForElapsedTime = true;

        this.queryTimingUnits = Messages.getString("Nanoseconds");
    } else {
        this.queryTimingUnits = Messages.getString("Milliseconds");
    }

    if (this.propertySet.getBooleanProperty(PropertyKey.logSlowQueries).getValue()) {
        calculateSlowQueryThreshold();
    }

    this.authProvider = new NativeAuthenticationProvider();
    this.authProvider.init(this, this.getPropertySet(), this.socketConnection.getExceptionInterceptor());

    Map<Class<? extends ProtocolEntity>, ProtocolEntityReader<? extends ProtocolEntity, NativePacketPayload>> protocolEntityClassToTextReader = new HashMap<>();
    protocolEntityClassToTextReader.put(ColumnDefinition.class, new ColumnDefinitionReader(this));
    protocolEntityClassToTextReader.put(ResultsetRow.class, new ResultsetRowReader(this));
    protocolEntityClassToTextReader.put(Resultset.class, new TextResultsetReader(this));
    this.PROTOCOL_ENTITY_CLASS_TO_TEXT_READER = Collections.unmodifiableMap(protocolEntityClassToTextReader);

    Map<Class<? extends ProtocolEntity>, ProtocolEntityReader<? extends ProtocolEntity, NativePacketPayload>> protocolEntityClassToBinaryReader = new HashMap<>();
    protocolEntityClassToBinaryReader.put(ColumnDefinition.class, new ColumnDefinitionReader(this));
    protocolEntityClassToBinaryReader.put(Resultset.class, new BinaryResultsetReader(this));
    this.PROTOCOL_ENTITY_CLASS_TO_BINARY_READER = Collections.unmodifiableMap(protocolEntityClassToBinaryReader);

}
 
Example #13
Source File: NativeProtocol.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
public static NativeProtocol getInstance(Session session, SocketConnection socketConnection, PropertySet propertySet, Log log,
        TransactionEventHandler transactionManager) {
    NativeProtocol protocol = new NativeProtocol(log);
    protocol.init(session, socketConnection, propertySet, transactionManager);
    return protocol;
}
 
Example #14
Source File: SimplePacketReader.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
public SimplePacketReader(SocketConnection socketConnection, RuntimeProperty<Integer> maxAllowedPacket) {
    this.socketConnection = socketConnection;
    this.maxAllowedPacket = maxAllowedPacket;
}
 
Example #15
Source File: SocketFactoryWrapper.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T extends Closeable> T performTlsHandshake(SocketConnection socketConnection, ServerSession serverSession) throws IOException {
    return (T) super.performTlsHandshake(socketConnection, serverSession);
}
 
Example #16
Source File: SocketFactoryWrapper.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T extends Closeable> T performTlsHandshake(SocketConnection socketConnection, ServerSession serverSession) throws IOException {
    return (T) super.performTlsHandshake(socketConnection, serverSession);
}
 
Example #17
Source File: NativeProtocol.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void init(Session sess, SocketConnection phConnection, PropertySet propSet, TransactionEventHandler trManager) {

    this.session = sess;
    this.propertySet = propSet;

    this.socketConnection = phConnection;
    this.exceptionInterceptor = this.socketConnection.getExceptionInterceptor();

    this.transactionManager = trManager;

    this.maintainTimeStats = this.propertySet.getBooleanReadableProperty(PropertyDefinitions.PNAME_maintainTimeStats);
    this.maxQuerySizeToLog = this.propertySet.getIntegerReadableProperty(PropertyDefinitions.PNAME_maxQuerySizeToLog);
    this.useAutoSlowLog = this.propertySet.getBooleanReadableProperty(PropertyDefinitions.PNAME_autoSlowLog).getValue();
    this.logSlowQueries = this.propertySet.getBooleanReadableProperty(PropertyDefinitions.PNAME_logSlowQueries).getValue();
    this.maxAllowedPacket = this.propertySet.getModifiableProperty(PropertyDefinitions.PNAME_maxAllowedPacket);
    this.profileSQL = this.propertySet.getBooleanReadableProperty(PropertyDefinitions.PNAME_profileSQL).getValue();
    this.autoGenerateTestcaseScript = this.propertySet.getBooleanReadableProperty(PropertyDefinitions.PNAME_autoGenerateTestcaseScript).getValue();
    this.useServerPrepStmts = this.propertySet.getBooleanReadableProperty(PropertyDefinitions.PNAME_useServerPrepStmts);

    this.reusablePacket = new NativePacketPayload(INITIAL_PACKET_SIZE);
    //this.sendPacket = new Buffer(INITIAL_PACKET_SIZE);

    this.packetSender = new SimplePacketSender(this.socketConnection.getMysqlOutput());
    this.packetReader = new SimplePacketReader(this.socketConnection, this.maxAllowedPacket);

    //this.needToGrabQueryFromPacket = (this.profileSQL || this.logSlowQueries || this.autoGenerateTestcaseScript);

    if (this.propertySet.getBooleanReadableProperty(PropertyDefinitions.PNAME_useNanosForElapsedTime).getValue() && TimeUtil.nanoTimeAvailable()) {
        this.useNanosForElapsedTime = true;

        this.queryTimingUnits = Messages.getString("Nanoseconds");
    } else {
        this.queryTimingUnits = Messages.getString("Milliseconds");
    }

    if (this.propertySet.getBooleanReadableProperty(PropertyDefinitions.PNAME_logSlowQueries).getValue()) {
        calculateSlowQueryThreshold();
    }

    this.authProvider = new NativeAuthenticationProvider(this.log);
    this.authProvider.init(this, this.getPropertySet(), this.socketConnection.getExceptionInterceptor());

    Map<Class<? extends ProtocolEntity>, ProtocolEntityReader<? extends ProtocolEntity, NativePacketPayload>> protocolEntityClassToTextReader = new HashMap<>();
    protocolEntityClassToTextReader.put(ColumnDefinition.class, new ColumnDefinitionReader(this));
    protocolEntityClassToTextReader.put(ResultsetRow.class, new ResultsetRowReader(this));
    protocolEntityClassToTextReader.put(Resultset.class, new TextResultsetReader(this));
    this.PROTOCOL_ENTITY_CLASS_TO_TEXT_READER = Collections.unmodifiableMap(protocolEntityClassToTextReader);

    Map<Class<? extends ProtocolEntity>, ProtocolEntityReader<? extends ProtocolEntity, NativePacketPayload>> protocolEntityClassToBinaryReader = new HashMap<>();
    protocolEntityClassToBinaryReader.put(ColumnDefinition.class, new ColumnDefinitionReader(this));
    protocolEntityClassToBinaryReader.put(Resultset.class, new BinaryResultsetReader(this));
    this.PROTOCOL_ENTITY_CLASS_TO_BINARY_READER = Collections.unmodifiableMap(protocolEntityClassToBinaryReader);

}
 
Example #18
Source File: NativeProtocol.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public static NativeProtocol getInstance(Session session, SocketConnection socketConnection, PropertySet propertySet, Log log,
        TransactionEventHandler transactionManager) {
    NativeProtocol protocol = new NativeProtocol(log);
    protocol.init(session, socketConnection, propertySet, transactionManager);
    return protocol;
}
 
Example #19
Source File: SimplePacketReader.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public SimplePacketReader(SocketConnection socketConnection, ReadableProperty<Integer> maxAllowedPacket) {
    this.socketConnection = socketConnection;
    this.maxAllowedPacket = maxAllowedPacket;
}
 
Example #20
Source File: AsyncMessageReader.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public AsyncMessageReader(PropertySet propertySet, SocketConnection socketConnection) {
    this.propertySet = propertySet;
    this.sc = socketConnection;
}