Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright (c) 2013, impossibl.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of impossibl.com nor the names of its contributors may
* be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.impossibl.postgres.jdbc;
import com.impossibl.postgres.system.Settings;
import com.impossibl.postgres.system.SystemSettings;
import com.impossibl.postgres.types.SharedRegistry;
import static com.impossibl.postgres.jdbc.ErrorUtils.makeSQLException;
import static com.impossibl.postgres.jdbc.JDBCSettings.HOUSEKEEPER;
import static com.impossibl.postgres.system.SystemSettings.DATABASE_NAME;
import static com.impossibl.postgres.system.SystemSettings.DATABASE_URL;
import static com.impossibl.postgres.utils.guava.Strings.emptyToNull;
import static com.impossibl.postgres.utils.guava.Strings.nullToEmpty;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.Inet6Address;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.util.Comparator.comparing;
import io.netty.channel.unix.DomainSocketAddress;
/**
* Utility class for connection
* @author Kevin Wooten
* @author Jesper Pedersen
*/
class ConnectionUtil {
private static final String POSTGRES_UNIX_SOCKET_BASE_NAME = ".s.PGSQL";
private static final String POSTGRES_UNIX_SOCKET_INVALID_EXT = ".lock";
private static Logger logger = Logger.getLogger(ConnectionUtil.class.getName());
static class ConnectionSpecifier {
private List addresses;
private String database;
private Properties parameters;
ConnectionSpecifier() {
addresses = new ArrayList<>();
database = null;
parameters = new Properties();
}
String getDatabase() {
return database;
}
void setDatabase(String v) {
database = v;
}
List getAddresses() {
return addresses;
}
void prependAddress(SocketAddress v) {
addresses.add(0, v);
}
void appendAddress(SocketAddress v) {
addresses.add(v);
}
Properties getParameters() {
return parameters;
}
void addParameter(String key, String value) {
parameters.setProperty(key, value);
}
private List getAddresses(Class type) {
List found = new ArrayList<>();
for (SocketAddress address : addresses) {
if (type.isInstance(address)) {
found.add(type.cast(address));
}
}
return found;
}
private String getInetHosts() {
List inetAddresses = getAddresses(InetSocketAddress.class);
if (inetAddresses.isEmpty()) {
return null;
}
StringBuilder hosts = new StringBuilder();
Iterator addrIter = inetAddresses.iterator();
while (addrIter.hasNext()) {
InetSocketAddress addr = addrIter.next();
if (addr.getAddress() instanceof Inet6Address) {
hosts.append("[").append(addr.getHostString()).append("]");
}
else {
hosts.append(addr.getHostString());
}
if (addr.getPort() != 5432) {
hosts.append(':');
hosts.append(addr.getPort());
}
if (addrIter.hasNext()) {
hosts.append(",");
}
}
return hosts.toString();
}
private String getUnixPath() {
List unixSockets = getAddresses(DomainSocketAddress.class);
if (unixSockets.isEmpty()) {
return null;
}
return unixSockets.get(0).path();
}
private SortedSet> getSortedParameters() {
SortedSet> sortedParams =
new TreeSet<>(comparing((Map.Entry