com.mysql.cj.conf.ConnectionUrlParser Java Examples

The following examples show how to use com.mysql.cj.conf.ConnectionUrlParser. 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: XDevAPIConnectionUrl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected Map<String, String> preprocessPerTypeHostProperties(Map<String, String> hostProps) {
    if (hostProps.containsKey(ADDRESS_PROPERTY_KEY)) {
        String address = hostProps.get(ADDRESS_PROPERTY_KEY);
        Pair<String, Integer> hostPortPair = ConnectionUrlParser.parseHostPortPair(address);
        String host = safeTrim(hostPortPair.left);
        Integer port = hostPortPair.right;
        if (!isNullOrEmpty(host) && !hostProps.containsKey(HOST_PROPERTY_KEY)) {
            hostProps.put(HOST_PROPERTY_KEY, host);
        }
        if (port != -1 && !hostProps.containsKey(PORT_PROPERTY_KEY)) {
            hostProps.put(PORT_PROPERTY_KEY, port.toString());
        }
    }
    return hostProps;
}
 
Example #2
Source File: LoadbalanceConnectionUrl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructs an instance of {@link LoadbalanceConnectionUrl}, performing all the required initializations and validations. A loadbalance connection
 * cannot deal with multiple hosts with same host:port.
 * 
 * @param connStrParser
 *            a {@link ConnectionUrlParser} instance containing the parsed version of the original connection string
 * @param info
 *            the connection arguments map
 */
public LoadbalanceConnectionUrl(ConnectionUrlParser connStrParser, Properties info) {
    super(connStrParser, info);
    this.type = Type.LOADBALANCE_CONNECTION;

    // TODO: Validate the hosts list: there can't be any two hosts with same host:port.
    // Although this should be required, it also is incompatible with our current tests which are creating load-balanced connections
    // using the same host configurations.
    //        Set<String> visitedHosts = new HashSet<>();
    //        for (HostInfo hi : this.hosts) {
    //            if (visitedHosts.contains(hi.getHostPortPair())) {
    //                throw ExceptionFactory.createException(WrongArgumentException.class,
    //                        Messages.getString("ConnectionString.12", new Object[] { hi.getHostPortPair(), Type.LOADBALANCE_CONNECTION.getProtocol() }));
    //            }
    //            visitedHosts.add(hi.getHostPortPair());
    //        }
}
 
Example #3
Source File: LoadbalanceConnectionUrl.java    From FoxTelem with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Constructs an instance of {@link LoadbalanceConnectionUrl}, performing all the required initializations and validations. A loadbalance connection
 * cannot deal with multiple hosts with same host:port.
 * 
 * @param connStrParser
 *            a {@link ConnectionUrlParser} instance containing the parsed version of the original connection string
 * @param info
 *            the connection arguments map
 */
public LoadbalanceConnectionUrl(ConnectionUrlParser connStrParser, Properties info) {
    super(connStrParser, info);
    this.type = Type.LOADBALANCE_CONNECTION;

    // TODO: Validate the hosts list: there can't be any two hosts with same host:port.
    // Although this should be required, it also is incompatible with our current tests which are creating load-balanced connections
    // using the same host configurations.
    //        Set<String> visitedHosts = new HashSet<>();
    //        for (HostInfo hi : this.hosts) {
    //            if (visitedHosts.contains(hi.getHostPortPair())) {
    //                throw ExceptionFactory.createException(WrongArgumentException.class,
    //                        Messages.getString("ConnectionString.12", new Object[] { hi.getHostPortPair(), Type.LOADBALANCE_CONNECTION.getProtocol() }));
    //            }
    //            visitedHosts.add(hi.getHostPortPair());
    //        }
}
 
Example #4
Source File: XDevAPIConnectionUrl.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void preprocessPerTypeHostProperties(Map<String, String> hostProps) {
    if (hostProps.containsKey(PropertyKey.ADDRESS.getKeyName())) {
        String address = hostProps.get(PropertyKey.ADDRESS.getKeyName());
        Pair<String, Integer> hostPortPair = ConnectionUrlParser.parseHostPortPair(address);
        String host = safeTrim(hostPortPair.left);
        Integer port = hostPortPair.right;
        if (!isNullOrEmpty(host) && !hostProps.containsKey(PropertyKey.HOST.getKeyName())) {
            hostProps.put(PropertyKey.HOST.getKeyName(), host);
        }
        if (port != -1 && !hostProps.containsKey(PropertyKey.PORT.getKeyName())) {
            hostProps.put(PropertyKey.PORT.getKeyName(), port.toString());
        }
    }
}
 
Example #5
Source File: BaseTestCase.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
protected String getPortFreeHostname(Properties props) throws SQLException {
    String host;
    if (props == null || (host = props.getProperty(PropertyKey.HOST.getKeyName())) == null) {
        return mainConnectionUrl.getMainHost().getHost();
    }
    return ConnectionUrlParser.parseHostPortPair(host).left;
}
 
Example #6
Source File: ConnectionImplCreateInterceptorTest.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Test
public void testResultIsEnhanceInstance() throws Throwable {
    final ConnectionUrlParser connectionUrlParser = ConnectionUrlParser.parseConnectionString("jdbc:mysql:replication://localhost:3360,localhost:3360,localhost:3360/test?useUnicode=true&characterEncoding=utf8&useSSL=false&roundRobinLoadBalance=true");

    interceptor.afterMethod(null, null, connectionUrlParser.getHosts().toArray(), null, objectInstance);
    verify(objectInstance).setSkyWalkingDynamicField(Matchers.any());
}
 
Example #7
Source File: ReplicationConnectionUrl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Constructs an instance of {@link ReplicationConnectionUrl}, performing all the required initializations.
 * 
 * @param connStrParser
 *            a {@link ConnectionUrlParser} instance containing the parsed version of the original connection string
 * @param info
 *            the connection arguments map
 */
public ReplicationConnectionUrl(ConnectionUrlParser connStrParser, Properties info) {
    super(connStrParser, info);
    this.type = Type.REPLICATION_CONNECTION;

    // Split masters and slaves:
    LinkedList<HostInfo> undefinedHosts = new LinkedList<>();
    for (HostInfo hi : this.hosts) {
        Map<String, String> hostProperties = hi.getHostProperties();
        if (hostProperties.containsKey(TYPE_PROPERTY_KEY)) {
            if (TYPE_MASTER.equalsIgnoreCase(hostProperties.get(TYPE_PROPERTY_KEY))) {
                this.masterHosts.add(hi);
            } else if (TYPE_SLAVE.equalsIgnoreCase(hostProperties.get(TYPE_PROPERTY_KEY))) {
                this.slaveHosts.add(hi);
            } else {
                undefinedHosts.add(hi);
            }
        } else {
            undefinedHosts.add(hi);
        }
    }
    if (!undefinedHosts.isEmpty()) {
        if (this.masterHosts.isEmpty()) {
            this.masterHosts.add(undefinedHosts.removeFirst());
        }
        this.slaveHosts.addAll(undefinedHosts);
    }

    // TODO: Validate the hosts list: there can't be any two hosts with same host:port.
    // Although this should be required, it also is incompatible with our current tests which are creating replication connections
    // using the same host configurations.
    //        Set<String> visitedHosts = new HashSet<>();
    //        for (List<HostInfo> hostsLists : Arrays.asList(this.masterHosts, this.slaveHosts)) {
    //            for (HostInfo hi : hostsLists) {
    //                if (visitedHosts.contains(hi.getHostPortPair())) {
    //                    throw ExceptionFactory.createException(WrongArgumentException.class,
    //                            Messages.getString("ConnectionString.13", new Object[] { hi.getHostPortPair(), Type.REPLICATION_CONNECTION.getProtocol() }));
    //                }
    //                visitedHosts.add(hi.getHostPortPair());
    //            }
    //        }
}
 
Example #8
Source File: XDevAPIConnectionUrl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Constructs an instance of {@link XDevAPIConnectionUrl}, performing all the required initializations.
 * 
 * @param connStrParser
 *            a {@link ConnectionUrlParser} instance containing the parsed version of the original connection string
 * @param info
 *            the connection arguments map
 */
public XDevAPIConnectionUrl(ConnectionUrlParser connStrParser, Properties info) {
    super(connStrParser, info);
    this.type = Type.XDEVAPI_SESSION;

    /*
     * Validate the hosts list:
     * 1. Same user and password are required in all hosts.
     * 2. If the host property 'priority' is set for one host, then in needs to be set on all others too.
     * 3. 'Priority' value must be between 0 and 100.
     */
    boolean first = true;
    String user = null;
    String password = null;
    boolean hasPriority = false;
    for (HostInfo hi : this.hosts) {
        if (first) {
            first = false;
            user = hi.getUser();
            password = hi.getPassword();
            hasPriority = hi.getHostProperties().containsKey(PRIORITY_PROPERTY_KEY);
        } else {
            if (!user.equals(hi.getUser()) || !password.equals(hi.getPassword())) {
                throw ExceptionFactory.createException(WrongArgumentException.class,
                        Messages.getString("ConnectionString.14", new Object[] { Type.XDEVAPI_SESSION.getProtocol() }));
            }
            if (hasPriority ^ hi.getHostProperties().containsKey(PRIORITY_PROPERTY_KEY)) {
                throw ExceptionFactory.createException(WrongArgumentException.class,
                        Messages.getString("ConnectionString.15", new Object[] { Type.XDEVAPI_SESSION.getProtocol() }));
            }
        }
        if (hasPriority) {
            try {
                int priority = Integer.parseInt(hi.getProperty(PRIORITY_PROPERTY_KEY));
                if (priority < 0 || priority > 100) {
                    throw ExceptionFactory.createException(WrongArgumentException.class,
                            Messages.getString("ConnectionString.16", new Object[] { Type.XDEVAPI_SESSION.getProtocol() }));
                }
            } catch (NumberFormatException e) {
                throw ExceptionFactory.createException(WrongArgumentException.class,
                        Messages.getString("ConnectionString.16", new Object[] { Type.XDEVAPI_SESSION.getProtocol() }));
            }
        }
    }

    // Sort the hosts list according to their priority settings.
    if (hasPriority) {
        this.hosts.sort(Comparator.<HostInfo, Integer> comparing(hi -> Integer.parseInt(hi.getHostProperties().get(PRIORITY_PROPERTY_KEY))).reversed());
    }
}
 
Example #9
Source File: ReplicationConnectionUrl.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Constructs an instance of {@link ReplicationConnectionUrl}, performing all the required initializations.
 * 
 * @param connStrParser
 *            a {@link ConnectionUrlParser} instance containing the parsed version of the original connection string
 * @param info
 *            the connection arguments map
 */
public ReplicationConnectionUrl(ConnectionUrlParser connStrParser, Properties info) {
    super(connStrParser, info);
    this.type = Type.REPLICATION_CONNECTION;

    // Split masters and slaves:
    LinkedList<HostInfo> undefinedHosts = new LinkedList<>();
    for (HostInfo hi : this.hosts) {
        Map<String, String> hostProperties = hi.getHostProperties();
        if (hostProperties.containsKey(PropertyKey.TYPE.getKeyName())) {
            if (TYPE_MASTER.equalsIgnoreCase(hostProperties.get(PropertyKey.TYPE.getKeyName()))) {
                this.masterHosts.add(hi);
            } else if (TYPE_SLAVE.equalsIgnoreCase(hostProperties.get(PropertyKey.TYPE.getKeyName()))) {
                this.slaveHosts.add(hi);
            } else {
                undefinedHosts.add(hi);
            }
        } else {
            undefinedHosts.add(hi);
        }
    }
    if (!undefinedHosts.isEmpty()) {
        if (this.masterHosts.isEmpty()) {
            this.masterHosts.add(undefinedHosts.removeFirst());
        }
        this.slaveHosts.addAll(undefinedHosts);
    }

    // TODO: Validate the hosts list: there can't be any two hosts with same host:port.
    // Although this should be required, it also is incompatible with our current tests which are creating replication connections
    // using the same host configurations.
    //        Set<String> visitedHosts = new HashSet<>();
    //        for (List<HostInfo> hostsLists : Arrays.asList(this.masterHosts, this.slaveHosts)) {
    //            for (HostInfo hi : hostsLists) {
    //                if (visitedHosts.contains(hi.getHostPortPair())) {
    //                    throw ExceptionFactory.createException(WrongArgumentException.class,
    //                            Messages.getString("ConnectionString.13", new Object[] { hi.getHostPortPair(), Type.REPLICATION_CONNECTION.getProtocol() }));
    //                }
    //                visitedHosts.add(hi.getHostPortPair());
    //            }
    //        }
}
 
Example #10
Source File: XDevAPIConnectionUrl.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Constructs an instance of {@link XDevAPIConnectionUrl}, performing all the required initializations.
 * 
 * @param connStrParser
 *            a {@link ConnectionUrlParser} instance containing the parsed version of the original connection string
 * @param info
 *            the connection arguments map
 */
public XDevAPIConnectionUrl(ConnectionUrlParser connStrParser, Properties info) {
    super(connStrParser, info);
    this.type = Type.XDEVAPI_SESSION;

    /*
     * Validate the hosts list:
     * 1. Same user and password are required in all hosts.
     * 2. If the host property 'priority' is set for one host, then in needs to be set on all others too.
     * 3. 'Priority' value must be between 0 and 100.
     */
    boolean first = true;
    String user = null;
    String password = null;
    boolean hasPriority = false;
    for (HostInfo hi : this.hosts) {
        if (first) {
            first = false;
            user = hi.getUser();
            password = hi.getPassword();
            hasPriority = hi.getHostProperties().containsKey(PropertyKey.PRIORITY.getKeyName());
        } else {
            if (!user.equals(hi.getUser()) || !password.equals(hi.getPassword())) {
                throw ExceptionFactory.createException(WrongArgumentException.class,
                        Messages.getString("ConnectionString.14", new Object[] { Type.XDEVAPI_SESSION.getScheme() }));
            }
            if (hasPriority ^ hi.getHostProperties().containsKey(PropertyKey.PRIORITY.getKeyName())) {
                throw ExceptionFactory.createException(WrongArgumentException.class,
                        Messages.getString("ConnectionString.15", new Object[] { Type.XDEVAPI_SESSION.getScheme() }));
            }
        }
        if (hasPriority) {
            try {
                int priority = Integer.parseInt(hi.getProperty(PropertyKey.PRIORITY.getKeyName()));
                if (priority < 0 || priority > 100) {
                    throw ExceptionFactory.createException(WrongArgumentException.class,
                            Messages.getString("ConnectionString.16", new Object[] { Type.XDEVAPI_SESSION.getScheme() }));
                }
            } catch (NumberFormatException e) {
                throw ExceptionFactory.createException(WrongArgumentException.class,
                        Messages.getString("ConnectionString.16", new Object[] { Type.XDEVAPI_SESSION.getScheme() }));
            }
        }
    }

    // Sort the hosts list according to their priority settings.
    if (hasPriority) {
        this.hosts.sort(
                Comparator.<HostInfo, Integer> comparing(hi -> Integer.parseInt(hi.getHostProperties().get(PropertyKey.PRIORITY.getKeyName()))).reversed());
    }
}
 
Example #11
Source File: FailoverConnectionUrl.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Constructs an instance of {@link FailoverConnectionUrl}, performing all the required initializations.
 * 
 * @param connStrParser
 *            a {@link ConnectionUrlParser} instance containing the parsed version of the original connection string
 * @param info
 *            the connection arguments map
 */
public FailoverConnectionUrl(ConnectionUrlParser connStrParser, Properties info) {
    super(connStrParser, info);
    this.type = Type.FAILOVER_CONNECTION;
}
 
Example #12
Source File: SingleConnectionUrl.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Constructs an instance of {@link SingleConnectionUrl}, performing all the required initializations.
 * 
 * @param connStrParser
 *            a {@link ConnectionUrlParser} instance containing the parsed version of the original connection string
 * @param info
 *            the connection arguments map
 */
public SingleConnectionUrl(ConnectionUrlParser connStrParser, Properties info) {
    super(connStrParser, info);
    this.type = Type.SINGLE_CONNECTION;
}
 
Example #13
Source File: FailoverConnectionUrl.java    From FoxTelem with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Constructs an instance of {@link FailoverConnectionUrl}, performing all the required initializations.
 * 
 * @param connStrParser
 *            a {@link ConnectionUrlParser} instance containing the parsed version of the original connection string
 * @param info
 *            the connection arguments map
 */
public FailoverConnectionUrl(ConnectionUrlParser connStrParser, Properties info) {
    super(connStrParser, info);
    this.type = Type.FAILOVER_CONNECTION;
}
 
Example #14
Source File: SingleConnectionUrl.java    From FoxTelem with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Constructs an instance of {@link SingleConnectionUrl}, performing all the required initializations.
 * 
 * @param connStrParser
 *            a {@link ConnectionUrlParser} instance containing the parsed version of the original connection string
 * @param info
 *            the connection arguments map
 */
public SingleConnectionUrl(ConnectionUrlParser connStrParser, Properties info) {
    super(connStrParser, info);
    this.type = Type.SINGLE_CONNECTION;
}