Search in sources :

Example 1 with SortedProperties

use of org.h2.util.SortedProperties in project h2database by h2database.

the class ConnectionInfo method remapURL.

private static String remapURL(String url) {
    String urlMap = SysProperties.URL_MAP;
    if (urlMap != null && urlMap.length() > 0) {
        try {
            SortedProperties prop;
            prop = SortedProperties.loadProperties(urlMap);
            String url2 = prop.getProperty(url);
            if (url2 == null) {
                prop.put(url, "");
                prop.store(urlMap);
            } else {
                url2 = url2.trim();
                if (url2.length() > 0) {
                    return url2;
                }
            }
        } catch (IOException e) {
            throw DbException.convert(e);
        }
    }
    return url;
}
Also used : IOException(java.io.IOException) SortedProperties(org.h2.util.SortedProperties)

Example 2 with SortedProperties

use of org.h2.util.SortedProperties in project h2database by h2database.

the class WebApp method adminSave.

private String adminSave() {
    try {
        Properties prop = new SortedProperties();
        int port = Integer.decode((String) attributes.get("port"));
        prop.setProperty("webPort", String.valueOf(port));
        server.setPort(port);
        boolean allowOthers = Utils.parseBoolean((String) attributes.get("allowOthers"), false, false);
        prop.setProperty("webAllowOthers", String.valueOf(allowOthers));
        server.setAllowOthers(allowOthers);
        boolean ssl = Utils.parseBoolean((String) attributes.get("ssl"), false, false);
        prop.setProperty("webSSL", String.valueOf(ssl));
        server.setSSL(ssl);
        server.saveProperties(prop);
    } catch (Exception e) {
        trace(e.toString());
    }
    return admin();
}
Also used : SortedProperties(org.h2.util.SortedProperties) SysProperties(org.h2.engine.SysProperties) Properties(java.util.Properties) SortedProperties(org.h2.util.SortedProperties) DbException(org.h2.message.DbException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SQLException(java.sql.SQLException) JdbcSQLException(org.h2.jdbc.JdbcSQLException)

Example 3 with SortedProperties

use of org.h2.util.SortedProperties in project h2database by h2database.

the class FileLock method lockFile.

private void lockFile() {
    method = FILE;
    properties = new SortedProperties();
    properties.setProperty("method", String.valueOf(method));
    setUniqueId();
    FileUtils.createDirectories(FileUtils.getParent(fileName));
    if (!FileUtils.createFile(fileName)) {
        waitUntilOld();
        String m2 = load().getProperty("method", FILE);
        if (!m2.equals(FILE)) {
            throw getExceptionFatal("Unsupported lock method " + m2, null);
        }
        save();
        sleep(2 * sleep);
        if (!load().equals(properties)) {
            throw getExceptionAlreadyInUse("Locked by another process: " + fileName);
        }
        FileUtils.delete(fileName);
        if (!FileUtils.createFile(fileName)) {
            throw getExceptionFatal("Another process was faster", null);
        }
    }
    save();
    sleep(SLEEP_GAP);
    if (!load().equals(properties)) {
        fileName = null;
        throw getExceptionFatal("Concurrent update", null);
    }
    locked = true;
    watchdog = new Thread(this, "H2 File Lock Watchdog " + fileName);
    Driver.setThreadContextClassLoader(watchdog);
    watchdog.setDaemon(true);
    watchdog.setPriority(Thread.MAX_PRIORITY - 1);
    watchdog.start();
}
Also used : SortedProperties(org.h2.util.SortedProperties)

Example 4 with SortedProperties

use of org.h2.util.SortedProperties in project h2database by h2database.

the class WebServer method saveProperties.

/**
 * Save the settings to the properties file.
 *
 * @param prop null or the properties webPort, webAllowOthers, and webSSL
 */
synchronized void saveProperties(Properties prop) {
    try {
        if (prop == null) {
            Properties old = loadProperties();
            prop = new SortedProperties();
            prop.setProperty("webPort", "" + SortedProperties.getIntProperty(old, "webPort", port));
            prop.setProperty("webAllowOthers", "" + SortedProperties.getBooleanProperty(old, "webAllowOthers", allowOthers));
            prop.setProperty("webSSL", "" + SortedProperties.getBooleanProperty(old, "webSSL", ssl));
            if (commandHistoryString != null) {
                prop.setProperty(COMMAND_HISTORY, commandHistoryString);
            }
        }
        ArrayList<ConnectionInfo> settings = getSettings();
        int len = settings.size();
        for (int i = 0; i < len; i++) {
            ConnectionInfo info = settings.get(i);
            if (info != null) {
                prop.setProperty(String.valueOf(len - i - 1), info.getString());
            }
        }
        if (!"null".equals(serverPropertiesDir)) {
            OutputStream out = FileUtils.newOutputStream(serverPropertiesDir + "/" + Constants.SERVER_PROPERTIES_NAME, false);
            prop.store(out, "H2 Server Properties");
            out.close();
        }
    } catch (Exception e) {
        DbException.traceThrowable(e);
    }
}
Also used : OutputStream(java.io.OutputStream) SortedProperties(org.h2.util.SortedProperties) SysProperties(org.h2.engine.SysProperties) Properties(java.util.Properties) SortedProperties(org.h2.util.SortedProperties) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 5 with SortedProperties

use of org.h2.util.SortedProperties in project h2database by h2database.

the class TestFileLockSerialized method testPendingWrite.

private void testPendingWrite() throws Exception {
    deleteDb("fileLockSerialized");
    String url = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized";
    String writeUrl = url + ";FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE;WRITE_DELAY=0";
    Connection conn = getConnection(writeUrl, "sa", "sa");
    Statement stat = conn.createStatement();
    stat.execute("create table test(id int primary key)");
    Thread.sleep(100);
    String propFile = getBaseDir() + "/fileLockSerialized.lock.db";
    SortedProperties p = SortedProperties.loadProperties(propFile);
    p.setProperty("changePending", "true");
    p.setProperty("modificationDataId", "1000");
    try (OutputStream out = FileUtils.newOutputStream(propFile, false)) {
        p.store(out, "test");
    }
    Thread.sleep(100);
    stat.execute("select * from test");
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) OutputStream(java.io.OutputStream) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) SortedProperties(org.h2.util.SortedProperties)

Aggregations

SortedProperties (org.h2.util.SortedProperties)13 Properties (java.util.Properties)6 IOException (java.io.IOException)4 DbException (org.h2.message.DbException)4 File (java.io.File)3 SQLException (java.sql.SQLException)3 FileInputStream (java.io.FileInputStream)2 InputStreamReader (java.io.InputStreamReader)2 OutputStream (java.io.OutputStream)2 Connection (java.sql.Connection)2 Statement (java.sql.Statement)2 ArrayList (java.util.ArrayList)2 SysProperties (org.h2.engine.SysProperties)2 InputStream (java.io.InputStream)1 LineNumberReader (java.io.LineNumberReader)1 RandomAccessFile (java.io.RandomAccessFile)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 BindException (java.net.BindException)1 ConnectException (java.net.ConnectException)1 InetAddress (java.net.InetAddress)1