Java Code Examples for com.mysql.cj.conf.ConnectionUrl#getType()

The following examples show how to use com.mysql.cj.conf.ConnectionUrl#getType() . 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: MysqlDataSource.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a connection using the specified properties.
 * 
 * @param props
 *            the properties to connect with
 * 
 * @return a connection to the database
 * 
 * @throws SQLException
 *             if an error occurs
 */
protected java.sql.Connection getConnection(Properties props) throws SQLException {
    String jdbcUrlToUse = null;

    if (!this.explicitUrl) {
        jdbcUrlToUse = getUrl();
    } else {
        jdbcUrlToUse = this.url;
    }

    //
    // URL should take precedence over properties
    //
    ConnectionUrl connUrl = ConnectionUrl.getConnectionUrlInstance(jdbcUrlToUse, null);
    if (connUrl.getType() == null) {
        throw SQLError.createSQLException(Messages.getString("MysqlDataSource.BadUrl", new Object[] { jdbcUrlToUse }),
                MysqlErrorNumbers.SQL_STATE_CONNECTION_FAILURE, null);
    }
    Properties urlProps = connUrl.getConnectionArgumentsAsProperties();
    urlProps.remove(PropertyDefinitions.DBNAME_PROPERTY_KEY);
    urlProps.remove(PropertyDefinitions.HOST_PROPERTY_KEY);
    urlProps.remove(PropertyDefinitions.PORT_PROPERTY_KEY);
    urlProps.stringPropertyNames().stream().forEach(k -> props.setProperty(k, urlProps.getProperty(k)));

    return mysqlDriver.connect(jdbcUrlToUse, props);
}
 
Example 2
Source File: NonRegisteringDriver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Try to make a database connection to the given URL. The driver should return "null" if it realizes it is the wrong kind of driver to connect to the given
 * URL. This will be common, as when the JDBC driverManager is asked to connect to a given URL, it passes the URL to each loaded driver in turn.
 * 
 * <p>
 * The driver should raise an SQLException if the URL is null or if it is the right driver to connect to the given URL, but has trouble connecting to the
 * database.
 * </p>
 * 
 * <p>
 * The java.util.Properties argument can be used to pass arbitrary string tag/value pairs as connection arguments. These properties take precedence over any
 * properties sent in the URL.
 * </p>
 * 
 * <p>
 * MySQL protocol takes the form:
 * 
 * <PRE>
 * jdbc:mysql://host:port/database
 * </PRE>
 * 
 * </p>
 * 
 * @param url
 *            the URL of the database to connect to
 * @param info
 *            a list of arbitrary tag/value pairs as connection arguments
 * 
 * @return a connection to the URL or null if it isn't us
 * 
 * @exception SQLException
 *                if a database access error occurs or the url is {@code null}
 * 
 * @see java.sql.Driver#connect
 */
public java.sql.Connection connect(String url, Properties info) throws SQLException {

    try {
        ConnectionUrl conStr = ConnectionUrl.getConnectionUrlInstance(url, info);
        if (conStr.getType() == null) {
            /*
             * According to JDBC spec:
             * The driver should return "null" if it realizes it is the wrong kind of driver to connect to the given URL. This will be common, as when the
             * JDBC driver manager is asked to connect to a given URL it passes the URL to each loaded driver in turn.
             */
            return null;
        }

        switch (conStr.getType()) {
            case LOADBALANCE_CONNECTION:
                return LoadBalancedConnectionProxy.createProxyInstance((LoadbalanceConnectionUrl) conStr);

            case FAILOVER_CONNECTION:
                return FailoverConnectionProxy.createProxyInstance(conStr);

            case REPLICATION_CONNECTION:
                return ReplicationConnectionProxy.createProxyInstance((ReplicationConnectionUrl) conStr);

            case XDEVAPI_SESSION:
                // TODO test it
                //return new XJdbcConnection(conStr.getProperties());

            default:
                return com.mysql.cj.jdbc.ConnectionImpl.getInstance(conStr.getMainHost());

        }

    } catch (CJException ex) {
        throw ExceptionFactory.createException(UnableToConnectException.class,
                Messages.getString("NonRegisteringDriver.17", new Object[] { ex.toString() }), ex);
    }
}
 
Example 3
Source File: SessionFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Parses the connection string URL.
 * 
 * @param url
 *            the connection string URL.
 * @return a {@link ConnectionUrl} instance containing the URL components.
 */
private ConnectionUrl parseUrl(String url) {
    ConnectionUrl connUrl = ConnectionUrl.getConnectionUrlInstance(url, null);
    if (connUrl.getType() != ConnectionUrl.Type.XDEVAPI_SESSION) {
        throw ExceptionFactory.createException(InvalidConnectionAttributeException.class, "Initialization via URL failed for \"" + url + "\"");
    }
    return connUrl;
}
 
Example 4
Source File: SessionFactory.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Parses the connection string URL.
 * 
 * @param url
 *            the connection string URL.
 * @return a {@link ConnectionUrl} instance containing the URL components.
 */
protected ConnectionUrl parseUrl(String url) {
    ConnectionUrl connUrl = ConnectionUrl.getConnectionUrlInstance(url, null);
    if (connUrl == null || connUrl.getType() != ConnectionUrl.Type.XDEVAPI_SESSION) {
        throw ExceptionFactory.createException(InvalidConnectionAttributeException.class, "Initialization via URL failed for \"" + url + "\"");
    }
    return connUrl;
}
 
Example 5
Source File: NonRegisteringDriver.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * The getPropertyInfo method is intended to allow a generic GUI tool to
 * discover what properties it should prompt a human for in order to get
 * enough information to connect to a database.
 * 
 * <p>
 * Note that depending on the values the human has supplied so far, additional values may become necessary, so it may be necessary to iterate through
 * several calls to getPropertyInfo
 * </p>
 * 
 * @param url
 *            the Url of the database to connect to
 * @param info
 *            a proposed list of tag/value pairs that will be sent on
 *            connect open.
 * 
 * @return An array of DriverPropertyInfo objects describing possible
 *         properties. This array may be an empty array if no properties are
 *         required
 * 
 * @exception SQLException
 *                if a database-access error occurs
 * 
 * @see java.sql.Driver#getPropertyInfo
 */
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
    String host = "";
    String port = "";
    String database = "";
    String user = "";
    String password = "";

    if (!isNullOrEmpty(url)) {
        ConnectionUrl connStr = ConnectionUrl.getConnectionUrlInstance(url, info);
        if (connStr.getType() == Type.SINGLE_CONNECTION) {
            HostInfo hostInfo = connStr.getMainHost();
            info = hostInfo.exposeAsProperties();
        }
    }

    if (info != null) {
        host = info.getProperty(PropertyDefinitions.HOST_PROPERTY_KEY);
        port = info.getProperty(PropertyDefinitions.PORT_PROPERTY_KEY);
        database = info.getProperty(PropertyDefinitions.DBNAME_PROPERTY_KEY);
        user = info.getProperty(PropertyDefinitions.PNAME_user);
        password = info.getProperty(PropertyDefinitions.PNAME_password);
    }

    DriverPropertyInfo hostProp = new DriverPropertyInfo(PropertyDefinitions.HOST_PROPERTY_KEY, host);
    hostProp.required = true;
    hostProp.description = Messages.getString("NonRegisteringDriver.3");

    DriverPropertyInfo portProp = new DriverPropertyInfo(PropertyDefinitions.PORT_PROPERTY_KEY, port);
    portProp.required = false;
    portProp.description = Messages.getString("NonRegisteringDriver.7");

    DriverPropertyInfo dbProp = new DriverPropertyInfo(PropertyDefinitions.DBNAME_PROPERTY_KEY, database);
    dbProp.required = false;
    dbProp.description = Messages.getString("NonRegisteringDriver.10");

    DriverPropertyInfo userProp = new DriverPropertyInfo(PropertyDefinitions.PNAME_user, user);
    userProp.required = true;
    userProp.description = Messages.getString("NonRegisteringDriver.13");

    DriverPropertyInfo passwordProp = new DriverPropertyInfo(PropertyDefinitions.PNAME_password, password);
    passwordProp.required = true;
    passwordProp.description = Messages.getString("NonRegisteringDriver.16");

    DriverPropertyInfo[] dpi;
    dpi = new JdbcPropertySetImpl().exposeAsDriverPropertyInfo(info, 5);

    dpi[0] = hostProp;
    dpi[1] = portProp;
    dpi[2] = dbProp;
    dpi[3] = userProp;
    dpi[4] = passwordProp;

    return dpi;
}
 
Example 6
Source File: NonRegisteringDriver.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Try to make a database connection to the given URL. The driver should return "null" if it realizes it is the wrong kind of driver to connect to the given
 * URL. This will be common, as when the JDBC driverManager is asked to connect to a given URL, it passes the URL to each loaded driver in turn.
 * 
 * <p>
 * The driver should raise an SQLException if the URL is null or if it is the right driver to connect to the given URL, but has trouble connecting to the
 * database.
 * </p>
 * 
 * <p>
 * The java.util.Properties argument can be used to pass arbitrary string tag/value pairs as connection arguments. These properties take precedence over any
 * properties sent in the URL.
 * </p>
 * 
 * <p>
 * MySQL protocol takes the form: jdbc:mysql://host:port/database
 * </p>
 * 
 * @param url
 *            the URL of the database to connect to
 * @param info
 *            a list of arbitrary tag/value pairs as connection arguments
 * 
 * @return a connection to the URL or null if it isn't us
 * 
 * @exception SQLException
 *                if a database access error occurs or the url is {@code null}
 */
@Override
public java.sql.Connection connect(String url, Properties info) throws SQLException {

    try {
        if (!ConnectionUrl.acceptsUrl(url)) {
            /*
             * According to JDBC spec:
             * The driver should return "null" if it realizes it is the wrong kind of driver to connect to the given URL. This will be common, as when the
             * JDBC driver manager is asked to connect to a given URL it passes the URL to each loaded driver in turn.
             */
            return null;
        }

        ConnectionUrl conStr = ConnectionUrl.getConnectionUrlInstance(url, info);
        switch (conStr.getType()) {
            case SINGLE_CONNECTION:
                return com.mysql.cj.jdbc.ConnectionImpl.getInstance(conStr.getMainHost());

            case LOADBALANCE_CONNECTION:
                return LoadBalancedConnectionProxy.createProxyInstance((LoadbalanceConnectionUrl) conStr);

            case FAILOVER_CONNECTION:
                return FailoverConnectionProxy.createProxyInstance(conStr);

            case REPLICATION_CONNECTION:
                return ReplicationConnectionProxy.createProxyInstance((ReplicationConnectionUrl) conStr);

            default:
                return null;
        }

    } catch (UnsupportedConnectionStringException e) {
        // when Connector/J can't handle this connection string the Driver must return null
        return null;

    } catch (CJException ex) {
        throw ExceptionFactory.createException(UnableToConnectException.class,
                Messages.getString("NonRegisteringDriver.17", new Object[] { ex.toString() }), ex);
    }
}
 
Example 7
Source File: NonRegisteringDriver.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
@Override
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
    String host = "";
    String port = "";
    String database = "";
    String user = "";
    String password = "";

    if (!isNullOrEmpty(url)) {
        ConnectionUrl connStr = ConnectionUrl.getConnectionUrlInstance(url, info);
        if (connStr.getType() == Type.SINGLE_CONNECTION) {
            HostInfo hostInfo = connStr.getMainHost();
            info = hostInfo.exposeAsProperties();
        }
    }

    if (info != null) {
        host = info.getProperty(PropertyKey.HOST.getKeyName());
        port = info.getProperty(PropertyKey.PORT.getKeyName());
        database = info.getProperty(PropertyKey.DBNAME.getKeyName());
        user = info.getProperty(PropertyKey.USER.getKeyName());
        password = info.getProperty(PropertyKey.PASSWORD.getKeyName());
    }

    DriverPropertyInfo hostProp = new DriverPropertyInfo(PropertyKey.HOST.getKeyName(), host);
    hostProp.required = true;
    hostProp.description = Messages.getString("NonRegisteringDriver.3");

    DriverPropertyInfo portProp = new DriverPropertyInfo(PropertyKey.PORT.getKeyName(), port);
    portProp.required = false;
    portProp.description = Messages.getString("NonRegisteringDriver.7");

    DriverPropertyInfo dbProp = new DriverPropertyInfo(PropertyKey.DBNAME.getKeyName(), database);
    dbProp.required = false;
    dbProp.description = Messages.getString("NonRegisteringDriver.10");

    DriverPropertyInfo userProp = new DriverPropertyInfo(PropertyKey.USER.getKeyName(), user);
    userProp.required = true;
    userProp.description = Messages.getString("NonRegisteringDriver.13");

    DriverPropertyInfo passwordProp = new DriverPropertyInfo(PropertyKey.PASSWORD.getKeyName(), password);
    passwordProp.required = true;
    passwordProp.description = Messages.getString("NonRegisteringDriver.16");

    DriverPropertyInfo[] dpi;
    dpi = new JdbcPropertySetImpl().exposeAsDriverPropertyInfo(info, 5);

    dpi[0] = hostProp;
    dpi[1] = portProp;
    dpi[2] = dbProp;
    dpi[3] = userProp;
    dpi[4] = passwordProp;

    return dpi;
}