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); } } }
@Override public byte[] getBytes() { return Utils.cloneByteArray(getBytesNoCopy()); }
/** * 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); }
@Override protected int compareSecure(Value v, CompareMode mode) { byte[] v2 = ((ValueBytes) v).value; if (mode.isBinaryUnsigned()) { return Utils.compareNotNullUnsigned(value, v2); } return Utils.compareNotNullSigned(value, v2); }
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); }
public static Privilege getPrivilege() { String path = SysProperties.SERVERUSER_CONFIG_LOCATION; InputStream source = Utils.getResourceAsStream(path); if(source == null) { logger.debug("Can't load privilege config from {}, using ", path); return TRUE_PRIVILEGE; } try { logger.debug("Using privilege config from {}" , path); Properties prop = new Properties(); prop.load(source); return new PrivilegeDefault(prop); } catch (Exception e) { logger.info("error load privilege config from " + path, e); return TRUE_PRIVILEGE; } } }
@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; }
result = ValueInt.get(Utils.getMemoryFree()); break; case MEMORY_USED: session.getUser().checkAdmin(); result = ValueInt.get(Utils.getMemoryUsed()); break; case SCHEMA:
@Override public int hashCode() { if (hash == 0) { hash = Utils.getByteArrayHash(value); } return hash; }
@Override protected int compareSecure(Value v, CompareMode mode) { if (type == Value.CLOB) { return Integer.signum(getString().compareTo(v.getString())); } byte[] v2 = v.getBytesNoCopy(); return Utils.compareNotNullSigned(getBytes(), v2); }
private static int match(Class<?>[] params, Object[] values) { int len = params.length; if (len == values.length) { int points = 1; for (int i = 0; i < len; i++) { Class<?> pc = getNonPrimitiveClass(params[i]); Object v = values[i]; Class<?> vc = v == null ? null : v.getClass(); if (pc == vc) { points++; } else if (vc == null) { // can't verify } else if (!pc.isAssignableFrom(vc)) { return 0; } } return points; } return 0; }
/** * 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); }
public SessionFactoryBuilder fromXml(String fromXml) { if (StringUtils.isNullOrEmpty(fromXml)) { throw new IllegalArgumentException("xml config file must not be null"); } InputStream source = null; try { source = Utils.getResourceAsStream(fromXml); if (source == null) { throw new IllegalArgumentException("Can't load config file " + fromXml + " from classpath."); } XmlConfigParser parser = new XmlConfigParser(source); parser.parse(configuration); } finally { try { source.close(); } catch (Exception e) { // ignored } } return this; }
@Override public int hashCode() { if (hash == 0) { if (precision > 4096) { // TODO: should calculate the hash code when saving, and store // it in the database file return (int) (precision ^ (precision >>> 32)); } if (type == CLOB) { hash = getString().hashCode(); } else { hash = Utils.getByteArrayHash(getBytes()); } } return hash; }
@Override protected int compareSecure(Value v, CompareMode mode) { if (v instanceof ValueLobDb) { ValueLobDb v2 = (ValueLobDb) v; if (v == this) { return 0; } if (lobId == v2.lobId && small == null && v2.small == null) { return 0; } } if (type == Value.CLOB) { return Integer.signum(getString().compareTo(v.getString())); } byte[] v2 = v.getBytesNoCopy(); return Utils.compareNotNullSigned(getBytes(), v2); }
x = Utils.getNonPrimitiveClass(x);
/** * Get or create a bytes value for the given byte array. * Clone the data. * * @param b the byte array * @return the value */ public static ValueBytes get(byte[] b) { if (b.length == 0) { return EMPTY; } b = Utils.cloneByteArray(b); return getNoCopy(b); }
@Override public int hashCode() { if (hash == 0) { if (precision > 4096) { // TODO: should calculate the hash code when saving, and store // it in the database file return (int) (precision ^ (precision >>> 32)); } if (type == CLOB) { hash = getString().hashCode(); } else { hash = Utils.getByteArrayHash(getBytes()); } } return hash; }
/** * Get the used memory in KB. * This method possibly calls System.gc(). * * @return the used memory */ public static int getMemoryUsed() { collectGarbage(); Runtime rt = Runtime.getRuntime(); long mem = rt.totalMemory() - rt.freeMemory(); return (int) (mem >> 10); }
@Override public byte[] getBytes() { if (type == CLOB) { // convert hex to string return super.getBytes(); } byte[] data = getBytesNoCopy(); return Utils.cloneByteArray(data); }