@Override public ConnectionInfo clone() throws CloneNotSupportedException { ConnectionInfo clone = (ConnectionInfo) super.clone(); clone.prop = (Properties) prop.clone(); clone.filePasswordHash = Utils.cloneByteArray(filePasswordHash); clone.fileEncryptionKey = Utils.cloneByteArray(fileEncryptionKey); clone.userPasswordHash = Utils.cloneByteArray(userPasswordHash); return clone; }
private static Image loadImage(String name) { try { byte[] imageData = Utils.getResource(name); if (imageData == null) { return null; } return Toolkit.getDefaultToolkit().createImage(imageData); } catch (IOException e) { e.printStackTrace(); return null; } }
@Override protected int compareSecure(Value v, CompareMode mode) { Object o1 = getObject(); Object o2 = v.getObject(); boolean o1Comparable = o1 instanceof Comparable; boolean o2Comparable = o2 instanceof Comparable; if (o1Comparable && o2Comparable && Utils.haveCommonComparableSuperclass(o1.getClass(), o2.getClass())) { @SuppressWarnings("unchecked") Comparable<Object> c1 = (Comparable<Object>) o1; return c1.compareTo(o2); } // group by types if (o1.getClass() != o2.getClass()) { if (o1Comparable != o2Comparable) { return o1Comparable ? -1 : 1; } return o1.getClass().getName().compareTo(o2.getClass().getName()); } // compare hash codes int h1 = hashCode(); int h2 = v.hashCode(); if (h1 == h2) { if (o1.equals(o2)) { return 0; } return Bits.compareNotNullSigned(getBytesNoCopy(), v.getBytesNoCopy()); } return h1 > h2 ? 1 : -1; }
public static Class<?> parseClass(String source, String packageAndClassName) { if (LOADER == null) { throw new RuntimeException( "Compile fail: no Groovy jar in the classpath", INIT_FAIL_EXCEPTION); } try { Object codeSource = Utils.newInstance("groovy.lang.GroovyCodeSource", source, packageAndClassName + ".groovy", "UTF-8"); Utils.callMethod(codeSource, "setCachable", false); return (Class<?>) Utils.callMethod( LOADER, "parseClass", codeSource); } catch (Exception e) { throw new RuntimeException(e); } } }
private static Object callMethod( Object instance, Class<?> clazz, String methodName, Object... params) throws Exception { Method best = null; int bestMatch = 0; boolean isStatic = instance == null; for (Method m : clazz.getMethods()) { if (Modifier.isStatic(m.getModifiers()) == isStatic && m.getName().equals(methodName)) { int p = match(m.getParameterTypes(), params); if (p > bestMatch) { bestMatch = p; best = m; } } } if (best == null) { throw new NoSuchMethodException(methodName); } return best.invoke(instance, params); }
private String readPassword() throws IOException { try { Object console = Utils.callStaticMethod("java.lang.System.console"); print("Password "); char[] password = (char[]) Utils.callMethod(console, "readPassword"); return password == null ? null : new String(password); } catch (Exception e) { // ignore, use the default solution } Thread passwordHider = new Thread(this, "Password hider"); stopHide = false; passwordHider.start(); print("Password > "); String p = readLine(); stopHide = true; try { passwordHider.join(); } catch (InterruptedException e) { // ignore } print("\b\b"); return p; }
private boolean createTrayIcon() { try { boolean supported = (Boolean) Utils.callStaticMethod( "java.awt.SystemTray.isSupported"); if (!supported) { tray = Utils.callStaticMethod("java.awt.SystemTray.getSystemTray"); Dimension d = (Dimension) Utils.callMethod(tray, "getTrayIconSize"); String iconFile; if (d.width >= 24 && d.height >= 24) { trayIcon = Utils.newInstance("java.awt.TrayIcon", icon, "H2 Database Engine", menuConsole); Utils.callMethod(trayIcon, "addMouseListener", this); Utils.callMethod(tray, "add", trayIcon);
rs.addColumn("Type", Types.VARCHAR, 0, 0); rs.addColumn("KB", Types.VARCHAR, 0, 0); rs.addRow("Used Memory", "" + Utils.getMemoryUsed()); rs.addRow("Free Memory", "" + Utils.getMemoryFree()); return rs; } else if (isBuiltIn(sql, "@info")) {
@Override public int hashCode() { if (hash == 0) { hash = Utils.getByteArrayHash(value); } return hash; }
/** * Check if the file password hash is correct. * * @param testCipher the cipher algorithm * @param testHash the hash code * @return true if the cipher algorithm and the password match */ boolean validateFilePasswordHash(String testCipher, byte[] testHash) { if (!Objects.equals(testCipher, this.cipher)) { return false; } return Utils.compareSecure(testHash, filePasswordHash); }
/** * Called after a database has been closed, to remove the object from the * list of open databases. * * @param name the database name */ void close(String name) { if (jmx) { try { Utils.callStaticMethod("org.h2.jmx.DatabaseInfo.unregisterMBean", name); } catch (Exception e) { throw DbException.get(ErrorCode.FEATURE_NOT_SUPPORTED_1, e, "JMX"); } } synchronized (DATABASES) { DATABASES.remove(name); } }
@Override protected int compareSecure(Value v, CompareMode mode) { Object o1 = getObject(); Object o2 = v.getObject(); boolean o1Comparable = o1 instanceof Comparable; boolean o2Comparable = o2 instanceof Comparable; if (o1Comparable && o2Comparable && Utils.haveCommonComparableSuperclass(o1.getClass(), o2.getClass())) { @SuppressWarnings("unchecked") Comparable<Object> c1 = (Comparable<Object>) o1; return c1.compareTo(o2); } // group by types if (o1.getClass() != o2.getClass()) { if (o1Comparable != o2Comparable) { return o1Comparable ? -1 : 1; } return o1.getClass().getName().compareTo(o2.getClass().getName()); } // compare hash codes int h1 = hashCode(); int h2 = v.hashCode(); if (h1 == h2) { if (o1.equals(o2)) { return 0; } return Utils.compareNotNullSigned(getBytesNoCopy(), v.getBytesNoCopy()); } return h1 > h2 ? 1 : -1; }
try { String osName = StringUtils.toLowerEnglish( Utils.getProperty("os.name", "linux")); Runtime rt = Runtime.getRuntime(); String browser = Utils.getProperty(SysProperties.H2_BROWSER, null); if (browser == null) { if (browser.startsWith("call:")) { browser = browser.substring("call:".length()); Utils.callStaticMethod(browser, url); } else if (browser.contains("%url")) { String[] args = StringUtils.arraySplit(browser, ',', false);
/** * Get the free memory in KB. * This method possibly calls System.gc(). * * @return the free memory */ public static int getMemoryFree() { collectGarbage(); Runtime rt = Runtime.getRuntime(); long mem = rt.freeMemory(); return (int) (mem >> 10); }
/** * Calls a static method via reflection. This will try to use the method * where the most parameter classes match exactly (this algorithm is simpler * than the one in the Java specification, but works well for most cases). * * @param classAndMethod a string with the entire class and method name, eg. * "java.lang.System.gc" * @param params the method parameters * @return the return value from this call */ public static Object callStaticMethod(String classAndMethod, Object... params) throws Exception { int lastDot = classAndMethod.lastIndexOf('.'); String className = classAndMethod.substring(0, lastDot); String methodName = classAndMethod.substring(lastDot + 1); return callMethod(null, Class.forName(className), methodName, params); }
"user.language", "user.variant", "file.encoding" }; for (String s : settings) { add(rows, "property." + s, Utils.getProperty(s, "")); String resource = "/org/h2/res/help.csv"; try { byte[] data = Utils.getResource(resource); Reader reader = new InputStreamReader( new ByteArrayInputStream(data));
int len = MathUtils.roundUpInt(buff.length + lineSeparator.length, Constants.FILE_BLOCK_SIZE); buffer = Utils.copy(buff, buffer);
private boolean createTrayIcon() { try { boolean supported = (Boolean) Utils.callStaticMethod( "java.awt.SystemTray.isSupported"); if (!supported) { tray = Utils.callStaticMethod("java.awt.SystemTray.getSystemTray"); Dimension d = (Dimension) Utils.callMethod(tray, "getTrayIconSize"); String iconFile; if (d.width >= 24 && d.height >= 24) { trayIcon = Utils.newInstance("java.awt.TrayIcon", icon, "H2 Database Engine", menuConsole); Utils.callMethod(trayIcon, "addMouseListener", this); Utils.callMethod(tray, "add", trayIcon);
result = ValueInt.get(Utils.getMemoryFree()); break; case MEMORY_USED: session.getUser().checkAdmin(); result = ValueInt.get(Utils.getMemoryUsed()); break; case LOCK_MODE:
public static Class<?> parseClass(String source, String packageAndClassName) { if (LOADER == null) { throw new RuntimeException( "Compile fail: no Groovy jar in the classpath", INIT_FAIL_EXCEPTION); } try { Object codeSource = Utils.newInstance("groovy.lang.GroovyCodeSource", source, packageAndClassName + ".groovy", "UTF-8"); Utils.callMethod(codeSource, "setCachable", false); Class<?> clazz = (Class<?>) Utils.callMethod( LOADER, "parseClass", codeSource); return clazz; } catch (Exception e) { throw new RuntimeException(e); } } }