Java Code Examples for com.mysql.cj.util.StringUtils#isNullOrEmpty()

The following examples show how to use com.mysql.cj.util.StringUtils#isNullOrEmpty() . These examples are extracted from open source projects. 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
/**
 * Injects additional properties into the connection arguments while it's being constructed.
 * 
 * @param props
 *            the properties already containing all known connection arguments
 */
@Override
protected void injectPerTypeProperties(Map<String, String> props) {
    if (props.containsKey(PNAME_loadBalanceAutoCommitStatementThreshold)) {
        try {
            int autoCommitSwapThreshold = Integer.parseInt(props.get(PNAME_loadBalanceAutoCommitStatementThreshold));
            if (autoCommitSwapThreshold > 0) {
                String queryInterceptors = props.get(PNAME_queryInterceptors);
                String lbi = "com.mysql.cj.jdbc.ha.LoadBalancedAutoCommitInterceptor";
                if (StringUtils.isNullOrEmpty(queryInterceptors)) {
                    props.put(PNAME_queryInterceptors, lbi);
                } else {
                    props.put(PNAME_queryInterceptors, queryInterceptors + "," + lbi);
                }
            }
        } catch (Throwable t) {
            // Ignore, this will be handled later.
        }
    }
}
 
Example 2
private static KeyStoreConf getKeyStoreConf(PropertySet propertySet, PropertyKey keyStoreUrlPropertyKey, PropertyKey keyStorePasswordPropertyKey,
        PropertyKey keyStoreTypePropertyKey) {

    String keyStoreUrl = propertySet.getStringProperty(keyStoreUrlPropertyKey).getValue();
    String keyStorePassword = propertySet.getStringProperty(keyStorePasswordPropertyKey).getValue();
    String keyStoreType = propertySet.getStringProperty(keyStoreTypePropertyKey).getValue();

    if (StringUtils.isNullOrEmpty(keyStoreUrl)) {
        keyStoreUrl = System.getProperty("javax.net.ssl.keyStore");
        keyStorePassword = System.getProperty("javax.net.ssl.keyStorePassword");
        keyStoreType = System.getProperty("javax.net.ssl.keyStoreType");
        if (StringUtils.isNullOrEmpty(keyStoreType)) {
            keyStoreType = propertySet.getStringProperty(keyStoreTypePropertyKey).getInitialValue();
        }
        // check URL
        if (!StringUtils.isNullOrEmpty(keyStoreUrl)) {
            try {
                new URL(keyStoreUrl);
            } catch (MalformedURLException e) {
                keyStoreUrl = "file:" + keyStoreUrl;
            }
        }
    }

    return new KeyStoreConf(keyStoreUrl, keyStorePassword, keyStoreType);
}
 
Example 3
/**
 * Injects additional properties into the connection arguments while it's being constructed.
 * 
 * @param props
 *            the properties already containing all known connection arguments
 */
@Override
protected void injectPerTypeProperties(Map<String, String> props) {
    if (props.containsKey(PropertyKey.loadBalanceAutoCommitStatementThreshold.getKeyName())) {
        try {
            int autoCommitSwapThreshold = Integer.parseInt(props.get(PropertyKey.loadBalanceAutoCommitStatementThreshold.getKeyName()));
            if (autoCommitSwapThreshold > 0) {
                String queryInterceptors = props.get(PropertyKey.queryInterceptors.getKeyName());
                String lbi = "com.mysql.cj.jdbc.ha.LoadBalancedAutoCommitInterceptor";
                if (StringUtils.isNullOrEmpty(queryInterceptors)) {
                    props.put(PropertyKey.queryInterceptors.getKeyName(), lbi);
                } else {
                    props.put(PropertyKey.queryInterceptors.getKeyName(), queryInterceptors + "," + lbi);
                }
            }
        } catch (Throwable t) {
            // Ignore, this will be handled later.
        }
    }
}
 
Example 4
Source Project: lams   File: XProtocol.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void beforeHandshake() {
    this.serverSession = new XServerSession();

    if (this.socketConnection.isSynchronous()) {
        this.reader = new SyncMessageReader(this.socketConnection.getMysqlInput());
        this.writer = new SyncMessageSender(this.socketConnection.getMysqlOutput());
        this.managedResource = this.socketConnection.getMysqlSocket();

        this.serverSession.setCapabilities(getCapabilities());
        return;
    }

    XAsyncSocketConnection sc = (XAsyncSocketConnection) this.socketConnection;
    this.reader = new AsyncMessageReader(this.propertySet, sc);
    ((AsyncMessageReader) this.reader).start();
    this.writer = new AsyncMessageSender(sc.getAsynchronousSocketChannel());

    this.managedResource = sc.getAsynchronousSocketChannel();

    this.serverSession.setCapabilities(getCapabilities());

    SslMode sslMode = this.propertySet.<SslMode> getEnumReadableProperty(PropertyDefinitions.PNAME_sslMode).getValue();
    boolean verifyServerCert = sslMode == SslMode.VERIFY_CA || sslMode == SslMode.VERIFY_IDENTITY;
    String trustStoreUrl = this.propertySet.getStringReadableProperty(PropertyDefinitions.PNAME_sslTrustStoreUrl).getValue();

    if (!verifyServerCert && !StringUtils.isNullOrEmpty(trustStoreUrl)) {
        StringBuilder msg = new StringBuilder("Incompatible security settings. The property '");
        msg.append(PropertyDefinitions.PNAME_sslTrustStoreUrl).append("' requires '");
        msg.append(PropertyDefinitions.PNAME_sslMode).append("' as '");
        msg.append(PropertyDefinitions.SslMode.VERIFY_CA).append("' or '");
        msg.append(PropertyDefinitions.SslMode.VERIFY_IDENTITY).append("'.");
        throw new CJCommunicationsException(msg.toString());
    }

    if (sslMode != SslMode.DISABLED) {
        negotiateSSLConnection(0);
    }
}
 
Example 5
public String getUri() {
    PropertySet pset = this.session.getPropertySet();

    StringBuilder sb = new StringBuilder(ConnectionUrl.Type.XDEVAPI_SESSION.getScheme());
    sb.append("//").append(this.session.getProcessHost()).append(":").append(this.session.getPort()).append("/").append(this.defaultSchemaName).append("?");

    boolean isFirstParam = true;

    for (PropertyKey propKey : PropertyDefinitions.PROPERTY_KEY_TO_PROPERTY_DEFINITION.keySet()) {
        RuntimeProperty<?> propToGet = pset.getProperty(propKey);
        if (propToGet.isExplicitlySet()) {
            String propValue = propToGet.getStringValue();
            Object defaultValue = propToGet.getPropertyDefinition().getDefaultValue();
            if (defaultValue == null && !StringUtils.isNullOrEmpty(propValue) || defaultValue != null && propValue == null
                    || defaultValue != null && propValue != null && !propValue.equals(defaultValue.toString())) {
                if (isFirstParam) {
                    isFirstParam = false;
                } else {
                    sb.append("&");
                }
                sb.append(propKey.getKeyName());
                sb.append("=");
                sb.append(propValue);
            }

            // TODO custom properties?
        }
    }

    // TODO modify for multi-host connections

    return sb.toString();

}
 
Example 6
private static KeyStoreConf getTrustStoreConf(PropertySet propertySet, PropertyKey keyStoreUrlPropertyKey, PropertyKey keyStorePasswordPropertyKey,
        PropertyKey keyStoreTypePropertyKey, boolean required) {

    String trustStoreUrl = propertySet.getStringProperty(keyStoreUrlPropertyKey).getValue();
    String trustStorePassword = propertySet.getStringProperty(keyStorePasswordPropertyKey).getValue();
    String trustStoreType = propertySet.getStringProperty(keyStoreTypePropertyKey).getValue();

    if (StringUtils.isNullOrEmpty(trustStoreUrl)) {
        trustStoreUrl = System.getProperty("javax.net.ssl.trustStore");
        trustStorePassword = System.getProperty("javax.net.ssl.trustStorePassword");
        trustStoreType = System.getProperty("javax.net.ssl.trustStoreType");
        if (StringUtils.isNullOrEmpty(trustStoreType)) {
            trustStoreType = propertySet.getStringProperty(keyStoreTypePropertyKey).getInitialValue();
        }
        // check URL
        if (!StringUtils.isNullOrEmpty(trustStoreUrl)) {
            try {
                new URL(trustStoreUrl);
            } catch (MalformedURLException e) {
                trustStoreUrl = "file:" + trustStoreUrl;
            }
        }
    }

    if (required && StringUtils.isNullOrEmpty(trustStoreUrl)) {
        throw new CJCommunicationsException("No truststore provided to verify the Server certificate.");
    }

    return new KeyStoreConf(trustStoreUrl, trustStorePassword, trustStoreType);
}
 
Example 7
Source Project: lams   File: StatementImpl.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * Cancels this Statement object if both the DBMS and driver support
 * aborting an SQL statement. This method can be used by one thread to
 * cancel a statement that is being executed by another thread.
 */
public void cancel() throws SQLException {
    if (!this.query.getStatementExecuting().get()) {
        return;
    }

    if (!this.isClosed && this.connection != null) {
        JdbcConnection cancelConn = null;
        java.sql.Statement cancelStmt = null;

        try {
            HostInfo hostInfo = this.session.getHostInfo();
            String database = hostInfo.getDatabase();
            String user = StringUtils.isNullOrEmpty(hostInfo.getUser()) ? "" : hostInfo.getUser();
            String password = StringUtils.isNullOrEmpty(hostInfo.getPassword()) ? "" : hostInfo.getPassword();
            NativeSession newSession = new NativeSession(this.session.getHostInfo(), this.session.getPropertySet());
            newSession.connect(hostInfo, user, password, database, 30000, new TransactionEventHandler() {
                @Override
                public void transactionCompleted() {
                }

                public void transactionBegun() {
                }
            });
            newSession.sendCommand(new NativeMessageBuilder().buildComQuery(newSession.getSharedSendPacket(), "KILL QUERY " + this.session.getThreadId()),
                    false, 0);
            setCancelStatus(CancelStatus.CANCELED_BY_USER);
        } catch (IOException e) {
            throw SQLExceptionsMapping.translateException(e, this.exceptionInterceptor);
        } finally {
            if (cancelStmt != null) {
                cancelStmt.close();
            }

            if (cancelConn != null) {
                cancelConn.close();
            }
        }

    }
}
 
Example 8
public ServerAffinityStrategy(String affinityOrdervers) {
    super();
    if (!StringUtils.isNullOrEmpty(affinityOrdervers)) {
        this.affinityOrderedServers = affinityOrdervers.split(",");
    }
}
 
Example 9
@Override
public void run() {

    Thread cancelThread = new Thread() {

        @Override
        public void run() {
            Query localQueryToCancel = CancelQueryTaskImpl.this.queryToCancel;
            if (localQueryToCancel == null) {
                return;
            }
            NativeSession session = (NativeSession) localQueryToCancel.getSession();
            if (session == null) {
                return;
            }

            try {
                if (CancelQueryTaskImpl.this.queryTimeoutKillsConnection) {
                    localQueryToCancel.setCancelStatus(CancelStatus.CANCELED_BY_TIMEOUT);
                    session.invokeCleanupListeners(new OperationCancelledException(Messages.getString("Statement.ConnectionKilledDueToTimeout")));
                } else {
                    synchronized (localQueryToCancel.getCancelTimeoutMutex()) {
                        long origConnId = session.getThreadId();
                        HostInfo hostInfo = session.getHostInfo();
                        String database = hostInfo.getDatabase();
                        String user = StringUtils.isNullOrEmpty(hostInfo.getUser()) ? "" : hostInfo.getUser();
                        String password = StringUtils.isNullOrEmpty(hostInfo.getPassword()) ? "" : hostInfo.getPassword();

                        NativeSession newSession = new NativeSession(hostInfo, session.getPropertySet());
                        newSession.connect(hostInfo, user, password, database, 30000, new TransactionEventHandler() {
                            @Override
                            public void transactionCompleted() {
                            }

                            public void transactionBegun() {
                            }
                        });
                        newSession.sendCommand(new NativeMessageBuilder().buildComQuery(newSession.getSharedSendPacket(), "KILL QUERY " + origConnId), false, 0);

                        localQueryToCancel.setCancelStatus(CancelStatus.CANCELED_BY_TIMEOUT);
                    }
                }
                // } catch (NullPointerException npe) {
                // Case when connection closed while starting to cancel.
                // We can't easily synchronise this, because then one thread can't cancel() a running query.
                // Ignore, we shouldn't re-throw this, because the connection's already closed, so the statement has been timed out.
            } catch (Throwable t) {
                CancelQueryTaskImpl.this.caughtWhileCancelling = t;
            } finally {
                setQueryToCancel(null);
            }
        }
    };

    cancelThread.start();
}
 
Example 10
@Override
public void cancel() throws SQLException {
    if (!this.query.getStatementExecuting().get()) {
        return;
    }

    if (!this.isClosed && this.connection != null) {
        JdbcConnection cancelConn = null;
        java.sql.Statement cancelStmt = null;

        try {
            HostInfo hostInfo = this.session.getHostInfo();
            String database = hostInfo.getDatabase();
            String user = StringUtils.isNullOrEmpty(hostInfo.getUser()) ? "" : hostInfo.getUser();
            String password = StringUtils.isNullOrEmpty(hostInfo.getPassword()) ? "" : hostInfo.getPassword();
            NativeSession newSession = new NativeSession(this.session.getHostInfo(), this.session.getPropertySet());
            newSession.connect(hostInfo, user, password, database, 30000, new TransactionEventHandler() {
                @Override
                public void transactionCompleted() {
                }

                @Override
                public void transactionBegun() {
                }
            });
            newSession.sendCommand(new NativeMessageBuilder().buildComQuery(newSession.getSharedSendPacket(), "KILL QUERY " + this.session.getThreadId()),
                    false, 0);
            setCancelStatus(CancelStatus.CANCELED_BY_USER);
        } catch (IOException e) {
            throw SQLExceptionsMapping.translateException(e, this.exceptionInterceptor);
        } finally {
            if (cancelStmt != null) {
                cancelStmt.close();
            }

            if (cancelConn != null) {
                cancelConn.close();
            }
        }

    }
}
 
Example 11
public ServerAffinityStrategy(String affinityOrdervers) {
    super();
    if (!StringUtils.isNullOrEmpty(affinityOrdervers)) {
        this.affinityOrderedServers = affinityOrdervers.split(",");
    }
}
 
Example 12
public ClientImpl(String url, String clientPropsJson) {
    Properties clientProps = StringUtils.isNullOrEmpty(clientPropsJson) ? new Properties() : clientPropsFromJson(clientPropsJson);
    init(url, clientProps);
}
 
Example 13
@Override
public void run() {

    Thread cancelThread = new Thread() {

        @Override
        public void run() {
            Query localQueryToCancel = CancelQueryTaskImpl.this.queryToCancel;
            if (localQueryToCancel == null) {
                return;
            }
            NativeSession session = (NativeSession) localQueryToCancel.getSession();
            if (session == null) {
                return;
            }

            try {
                if (CancelQueryTaskImpl.this.queryTimeoutKillsConnection) {
                    localQueryToCancel.setCancelStatus(CancelStatus.CANCELED_BY_TIMEOUT);
                    session.invokeCleanupListeners(new OperationCancelledException(Messages.getString("Statement.ConnectionKilledDueToTimeout")));
                } else {
                    synchronized (localQueryToCancel.getCancelTimeoutMutex()) {
                        long origConnId = session.getThreadId();
                        HostInfo hostInfo = session.getHostInfo();
                        String database = hostInfo.getDatabase();
                        String user = StringUtils.isNullOrEmpty(hostInfo.getUser()) ? "" : hostInfo.getUser();
                        String password = StringUtils.isNullOrEmpty(hostInfo.getPassword()) ? "" : hostInfo.getPassword();

                        NativeSession newSession = new NativeSession(hostInfo, session.getPropertySet());
                        newSession.connect(hostInfo, user, password, database, 30000, new TransactionEventHandler() {
                            @Override
                            public void transactionCompleted() {
                            }

                            public void transactionBegun() {
                            }
                        });
                        newSession.sendCommand(new NativeMessageBuilder().buildComQuery(newSession.getSharedSendPacket(), "KILL QUERY " + origConnId),
                                false, 0);

                        localQueryToCancel.setCancelStatus(CancelStatus.CANCELED_BY_TIMEOUT);
                    }
                }
                // } catch (NullPointerException npe) {
                // Case when connection closed while starting to cancel.
                // We can't easily synchronise this, because then one thread can't cancel() a running query.
                // Ignore, we shouldn't re-throw this, because the connection's already closed, so the statement has been timed out.
            } catch (Throwable t) {
                CancelQueryTaskImpl.this.caughtWhileCancelling = t;
            } finally {
                setQueryToCancel(null);
            }
        }
    };

    cancelThread.start();
}