Java源码示例:com.mysql.cj.conf.ConnectionUrlParser
示例1
@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;
}
示例2
/**
* 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());
// }
}
示例3
/**
* 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());
// }
}
示例4
@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());
}
}
}
示例5
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;
}
示例6
@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());
}
示例7
/**
* 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());
// }
// }
}
示例8
/**
* 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());
}
}
示例9
/**
* 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());
// }
// }
}
示例10
/**
* 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());
}
}
示例11
/**
* 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;
}
示例12
/**
* 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;
}
示例13
/**
* 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;
}
示例14
/**
* 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;
}