use of com.alibaba.cobar.config.model.DataSourceConfig in project cobar by alibaba.
the class XMLSchemaLoader method loadDataSources.
private void loadDataSources(Element root) {
NodeList list = root.getElementsByTagName("dataSource");
for (int i = 0, n = list.getLength(); i < n; ++i) {
Element element = (Element) list.item(i);
ArrayList<DataSourceConfig> dscList = new ArrayList<DataSourceConfig>();
String dsNamePrefix = element.getAttribute("name");
try {
String dsType = element.getAttribute("type");
Element locElement = findPropertyByName(element, "location");
if (locElement == null) {
throw new NullPointerException("dataSource xml Element with name of " + dsNamePrefix + " has no location Element");
}
NodeList locationList = locElement.getElementsByTagName("location");
int dsIndex = 0;
for (int j = 0, m = locationList.getLength(); j < m; ++j) {
String locStr = ((Element) locationList.item(j)).getTextContent();
int colonIndex = locStr.indexOf(':');
int slashIndex = locStr.indexOf('/');
String dsHost = locStr.substring(0, colonIndex).trim();
int dsPort = Integer.parseInt(locStr.substring(colonIndex + 1, slashIndex).trim());
String[] schemas = SplitUtil.split(locStr.substring(slashIndex + 1).trim(), ',', '$', '-');
for (String dsSchema : schemas) {
DataSourceConfig dsConf = new DataSourceConfig();
ParameterMapping.mapping(dsConf, ConfigUtil.loadElements(element));
dscList.add(dsConf);
switch(dsIndex) {
case 0:
dsConf.setName(dsNamePrefix);
break;
case 1:
dscList.get(0).setName(dsNamePrefix + "[0]");
default:
dsConf.setName(dsNamePrefix + "[" + dsIndex + "]");
}
dsConf.setType(dsType);
dsConf.setDatabase(dsSchema);
dsConf.setHost(dsHost);
dsConf.setPort(dsPort);
++dsIndex;
}
}
} catch (Exception e) {
throw new ConfigException("dataSource " + dsNamePrefix + " define error", e);
}
for (DataSourceConfig dsConf : dscList) {
if (dataSources.containsKey(dsConf.getName())) {
throw new ConfigException("dataSource name " + dsConf.getName() + "duplicated!");
}
dataSources.put(dsConf.getName(), dsConf);
}
}
}
use of com.alibaba.cobar.config.model.DataSourceConfig in project cobar by alibaba.
the class ConfigInitializer method getDataNode.
private MySQLDataNode getDataNode(DataNodeConfig dnc, ConfigLoader configLoader) {
String[] dsNames = SplitUtil.split(dnc.getDataSource(), ',');
checkDataSourceExists(dsNames);
MySQLDataNode node = new MySQLDataNode(dnc);
MySQLDataSource[] dsList = new MySQLDataSource[dsNames.length];
int size = dnc.getPoolSize();
for (int i = 0; i < dsList.length; i++) {
DataSourceConfig dsc = dataSources.get(dsNames[i]);
dsList[i] = new MySQLDataSource(node, i, dsc, size);
}
node.setSources(dsList);
return node;
}
use of com.alibaba.cobar.config.model.DataSourceConfig in project cobar by alibaba.
the class ShowDataSource method execute.
public static void execute(ManagerConnection c, String name) {
ByteBuffer buffer = c.allocate();
// write header
buffer = header.write(buffer, c);
// write fields
for (FieldPacket field : fields) {
buffer = field.write(buffer, c);
}
// write eof
buffer = eof.write(buffer, c);
// write rows
byte packetId = eof.packetId;
CobarConfig conf = CobarServer.getInstance().getConfig();
Map<String, DataSourceConfig> dataSources = conf.getDataSources();
List<String> keys = new ArrayList<String>();
if (null != name) {
MySQLDataNode dn = conf.getDataNodes().get(name);
if (dn != null)
for (MySQLDataSource ds : dn.getSources()) {
if (ds != null) {
keys.add(ds.getName());
}
}
} else {
keys.addAll(dataSources.keySet());
}
Collections.sort(keys, new Comparators<String>());
for (String key : keys) {
RowDataPacket row = getRow(dataSources.get(key), c.getCharset());
row.packetId = ++packetId;
buffer = row.write(buffer, c);
}
// write last eof
EOFPacket lastEof = new EOFPacket();
lastEof.packetId = ++packetId;
buffer = lastEof.write(buffer, c);
// post write
c.write(buffer);
}
use of com.alibaba.cobar.config.model.DataSourceConfig in project cobar by alibaba.
the class ShowDataSources method getRow.
private static RowDataPacket getRow(MySQLDataNode node, String charset) {
RowDataPacket row = new RowDataPacket(FIELD_COUNT);
row.add(StringUtil.encode(node.getName(), charset));
MySQLDataSource ds = node.getSource();
if (ds != null) {
DataSourceConfig dsc = ds.getConfig();
row.add(StringUtil.encode(dsc.getType(), charset));
row.add(StringUtil.encode(dsc.getHost(), charset));
row.add(IntegerUtil.toBytes(dsc.getPort()));
row.add(StringUtil.encode(dsc.getDatabase(), charset));
} else {
row.add(null);
row.add(null);
row.add(null);
row.add(null);
}
return row;
}
Aggregations