newConnectionInfo.is_superuser = true;
newConnectionInfo.connectString = connectString;
// if there are existing connections and DatabaseConfigInfo exists,
// check the password. otherwise, allow anything.
ConnectionConfig config = getConnectionConfig();
if (config.getConnectionInfoNames().size() > 0 && config.getDatabaseConfigInfo() != null)
{
authenticate(currentUser, currentPass);
// non-superusers can't save connection info
if (!config.getConnectionInfo(currentUser).is_superuser)
throw new RemoteException(String.format(
"User \"%s\" does not have permission to modify connections.", currentUser));
// is_superuser for the new connection will only be false if there
// is an existing superuser connection and grantSuperuser is false.
newConnectionInfo.is_superuser = grantSuperuser;
}
// test connection only - to validate parameters
Connection conn = null;
try
{
conn = newConnectionInfo.getConnection();
SQLUtils.testConnection(conn);
}
catch (Exception e)
{
throw new RemoteException(
String.format("The connection named \"%s\" was not created because the server could not"
+ " connect to the specified database with the given parameters.", newConnectionInfo.name),
e);
}
finally
{
// close the connection, as we will not use it later
SQLUtils.cleanup(conn);
}
// if the connection already exists AND overwrite == false throw error
if (!configOverwrite && config.getConnectionInfoNames().contains(newConnectionInfo.name))
{
throw new RemoteException(String.format(
"The connection named \"%s\" already exists. Action cancelled.", newConnectionInfo.name));
}
// generate config connection entry
try
{
// do not delete if this is the last user (which must be a
// superuser)
Collection<String> connectionNames = config.getConnectionInfoNames();
// check for number of superusers
int numSuperUsers = 0;
for (String name : connectionNames)
{
if (config.getConnectionInfo(name).is_superuser)
++numSuperUsers;
if (numSuperUsers >= 2)
break;
}
// sanity check
if (Strings.equal(currentUser, newUser) && numSuperUsers == 1 && !newConnectionInfo.is_superuser)
throw new RemoteException("Cannot remove superuser privileges from last remaining superuser.");
config.saveConnectionInfo(newConnectionInfo);
}
catch (Exception e)
{
e.printStackTrace();
throw new RemoteException(String.format(