From bab77b637e7cdd327de5f66989fcbfc0298b9b88 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Tue, 13 Mar 2012 06:35:51 +0100 Subject: Intro.: PropertyAccess ; Added safe PropertyAccess for JNILibLoaderBase, Platform, IOUtil, .. - Intro.: PropertyAccess - Base class of all Debug impl, reduces redundancies. - jnlpAlias'ed trusted property is queried within local AccessControlContext to avoid 'JRE' implementation differences (should not be required). - throw NPE and IllegalArgumentException for invalid property key - Added safe PropertyAccess - JNILibLoaderBase: sun.jnlp.applet.launcher - Platform: jogamp.gluegen.UseTempJarCache - IOUtil: java.io.tmpdir --- .../com/jogamp/common/jvm/JNILibLoaderBase.java | 5 +- src/java/com/jogamp/common/os/Platform.java | 8 +- src/java/com/jogamp/common/util/IOUtil.java | 8 +- src/java/com/jogamp/common/util/locks/Lock.java | 6 +- src/java/com/jogamp/gluegen/Logging.java | 12 +- src/java/jogamp/common/Debug.java | 73 +--------- src/java/jogamp/common/PropertyAccess.java | 147 +++++++++++++++++++++ 7 files changed, 170 insertions(+), 89 deletions(-) create mode 100644 src/java/jogamp/common/PropertyAccess.java (limited to 'src') diff --git a/src/java/com/jogamp/common/jvm/JNILibLoaderBase.java b/src/java/com/jogamp/common/jvm/JNILibLoaderBase.java index fd12b7f..a5c68e7 100644 --- a/src/java/com/jogamp/common/jvm/JNILibLoaderBase.java +++ b/src/java/com/jogamp/common/jvm/JNILibLoaderBase.java @@ -51,6 +51,7 @@ import com.jogamp.common.util.JarUtil; import com.jogamp.common.util.cache.TempJarCache; import jogamp.common.Debug; +import jogamp.common.PropertyAccess; public class JNILibLoaderBase { public static final boolean DEBUG = Debug.debug("JNILibLoader"); @@ -229,8 +230,8 @@ public class JNILibLoaderBase { static { final String sunAppletLauncherProperty = "sun.jnlp.applet.launcher"; final String sunAppletLauncherClassName = "org.jdesktop.applet.util.JNLPAppletLauncher"; - final boolean usingJNLPAppletLauncher = Boolean.valueOf(System.getProperty(sunAppletLauncherProperty)).booleanValue(); - + final boolean usingJNLPAppletLauncher = PropertyAccess.getBooleanProperty(sunAppletLauncherProperty, true, AccessController.getContext()); + Class launcherClass = null; Method loadLibraryMethod = null; diff --git a/src/java/com/jogamp/common/os/Platform.java b/src/java/com/jogamp/common/os/Platform.java index 61b7345..ac1a1d0 100644 --- a/src/java/com/jogamp/common/os/Platform.java +++ b/src/java/com/jogamp/common/os/Platform.java @@ -41,7 +41,7 @@ import com.jogamp.common.util.JarUtil; import com.jogamp.common.util.VersionNumber; import com.jogamp.common.util.cache.TempJarCache; -import jogamp.common.Debug; +import jogamp.common.PropertyAccess; import jogamp.common.jvm.JVMUtil; import jogamp.common.os.MachineDescriptionRuntime; @@ -205,11 +205,7 @@ public class Platform { os_and_arch = getOSAndArch(OS_TYPE, CPU_ARCH); USE_TEMP_JAR_CACHE = (OS_TYPE != OSType.ANDROID) && isRunningFromJarURL() && - AccessController.doPrivileged(new PrivilegedAction() { - public Boolean run() { - return Boolean.valueOf(Debug.getBooleanProperty(true, useTempJarCachePropName, true, AccessController.getContext())); - } - }).booleanValue(); + PropertyAccess.getBooleanProperty(useTempJarCachePropName, true, AccessController.getContext(), true); loadGlueGenRTImpl(); JVMUtil.initSingleton(); // requires gluegen-rt, one-time init. diff --git a/src/java/com/jogamp/common/util/IOUtil.java b/src/java/com/jogamp/common/util/IOUtil.java index 0463c37..cdbeab6 100644 --- a/src/java/com/jogamp/common/util/IOUtil.java +++ b/src/java/com/jogamp/common/util/IOUtil.java @@ -44,6 +44,7 @@ import java.net.URLConnection; import java.nio.ByteBuffer; import jogamp.common.Debug; +import jogamp.common.PropertyAccess; import jogamp.common.os.android.StaticContext; import android.content.Context; @@ -510,11 +511,14 @@ public class IOUtil { * for both platforms w/o the need to handle extra permissions. *

* + * @throws SecurityException + * @throws RuntimeException + * * @see StaticContext#setContext(Context) * @see Context#getDir(String, int) */ public static File getTempRoot() - throws SecurityException + throws SecurityException, RuntimeException { if(AndroidVersion.isAvailable) { final Context ctx = StaticContext.getContext(); @@ -526,7 +530,7 @@ public class IOUtil { return tmpRoot; } } - final String tmpRootName = System.getProperty("java.io.tmpdir"); + final String tmpRootName = PropertyAccess.getProperty("java.io.tmpdir", false, AccessController.getContext()); final File tmpRoot = new File(tmpRootName); if(DEBUG) { System.err.println("IOUtil.getTempRoot(isAndroid: "+AndroidVersion.isAvailable+"): temp dir: "+tmpRoot.getAbsolutePath()); diff --git a/src/java/com/jogamp/common/util/locks/Lock.java b/src/java/com/jogamp/common/util/locks/Lock.java index 15d01ec..b2d6acc 100644 --- a/src/java/com/jogamp/common/util/locks/Lock.java +++ b/src/java/com/jogamp/common/util/locks/Lock.java @@ -29,6 +29,8 @@ package com.jogamp.common.util.locks; import jogamp.common.Debug; +import jogamp.common.PropertyAccess; + import java.security.AccessController; /** @@ -37,7 +39,7 @@ import java.security.AccessController; public interface Lock { /** Enable via the property jogamp.debug.Lock.TraceLock */ - public static final boolean TRACE_LOCK = Debug.isPropertyDefined("jogamp.debug.Lock.TraceLock", true, AccessController.getContext()); + public static final boolean TRACE_LOCK = PropertyAccess.isPropertyDefined("jogamp.debug.Lock.TraceLock", true, AccessController.getContext()); /** Enable via the property jogamp.debug.Lock */ public static final boolean DEBUG = Debug.debug("Lock"); @@ -50,7 +52,7 @@ public interface Lock { * and defaults to {@link #DEFAULT_TIMEOUT}.
* It can be overridden via the system property jogamp.common.utils.locks.Lock.timeout. */ - public static final long TIMEOUT = Debug.getLongProperty("jogamp.common.utils.locks.Lock.timeout", true, AccessController.getContext(), DEFAULT_TIMEOUT); + public static final long TIMEOUT = PropertyAccess.getLongProperty("jogamp.common.utils.locks.Lock.timeout", true, AccessController.getContext(), DEFAULT_TIMEOUT); /** * Blocking until the lock is acquired by this Thread or {@link #TIMEOUT} is reached. diff --git a/src/java/com/jogamp/gluegen/Logging.java b/src/java/com/jogamp/gluegen/Logging.java index 9ad3bf7..556819e 100644 --- a/src/java/com/jogamp/gluegen/Logging.java +++ b/src/java/com/jogamp/gluegen/Logging.java @@ -31,12 +31,15 @@ */ package com.jogamp.gluegen; +import java.security.AccessController; import java.util.logging.ConsoleHandler; import java.util.logging.Formatter; import java.util.logging.Level; import java.util.logging.LogRecord; import java.util.logging.Logger; +import jogamp.common.PropertyAccess; + /** * * @author Michael Bien @@ -44,9 +47,8 @@ import java.util.logging.Logger; public class Logging { static void init() { - - String pakage = Logging.class.getPackage().getName(); - String property = System.getProperty(pakage+".level"); + final String packageName = Logging.class.getPackage().getName(); + final String property = PropertyAccess.getProperty(packageName+".level", true, AccessController.getContext()); Level level; if(property != null) { level = Level.parse(property); @@ -63,7 +65,7 @@ public class Logging { handler.setFormatter(new PlainLogFormatter()); handler.setLevel(level); - Logger rootPackageLogger = Logger.getLogger(pakage); + Logger rootPackageLogger = Logger.getLogger(packageName); rootPackageLogger.setUseParentHandlers(false); rootPackageLogger.setLevel(level); rootPackageLogger.addHandler(handler); @@ -75,7 +77,7 @@ public class Logging { */ private static class PlainLogFormatter extends Formatter { - //@Override + @Override public String format(LogRecord record) { StringBuilder sb = new StringBuilder(128); sb.append("[").append(record.getLevel()).append(' ').append(record.getSourceClassName()).append("]: "); diff --git a/src/java/jogamp/common/Debug.java b/src/java/jogamp/common/Debug.java index cf07255..b8ed098 100644 --- a/src/java/jogamp/common/Debug.java +++ b/src/java/jogamp/common/Debug.java @@ -43,7 +43,7 @@ import java.security.*; /** Helper routines for logging and debugging. */ -public class Debug { +public class Debug extends PropertyAccess { // Some common properties private static boolean verbose; private static boolean debugAll; @@ -59,89 +59,18 @@ public class Debug { return getIntProperty(property, jnlpAlias, localACC, 0); } - public static int getIntProperty(final String property, final boolean jnlpAlias, final AccessControlContext acc, int defaultValue) { - int i=defaultValue; - try { - String sv = Debug.getProperty(property, jnlpAlias, acc); - if(null!=sv) { - Integer iv = Integer.valueOf(sv); - i = iv.intValue(); - } - } catch (NumberFormatException nfe) {} - return i; - } - - public static long getLongProperty(final String property, final boolean jnlpAlias, final AccessControlContext acc, long defaultValue) { - long l=defaultValue; - try { - String sv = Debug.getProperty(property, jnlpAlias, acc); - if(null!=sv) { - Long lv = Long.valueOf(sv); - l = lv.longValue(); - } - } catch (NumberFormatException nfe) {} - return l; - } - static boolean getBooleanProperty(final String property, final boolean jnlpAlias) { return getBooleanProperty(property, jnlpAlias, localACC); } - public static boolean getBooleanProperty(final String property, final boolean jnlpAlias, final AccessControlContext acc) { - return Boolean.valueOf(Debug.getProperty(property, jnlpAlias, acc)).booleanValue(); - } - - public static boolean getBooleanProperty(boolean defaultValue, final String property, final boolean jnlpAlias, final AccessControlContext acc) { - final String valueS = Debug.getProperty(property, jnlpAlias, acc); - if(null != valueS) { - return Boolean.valueOf(Debug.getProperty(property, jnlpAlias, acc)).booleanValue(); - } - return defaultValue; - } - static boolean isPropertyDefined(final String property, final boolean jnlpAlias) { return isPropertyDefined(property, jnlpAlias, localACC); } - public static boolean isPropertyDefined(final String property, final boolean jnlpAlias, final AccessControlContext acc) { - return (Debug.getProperty(property, jnlpAlias, acc) != null) ? true : false; - } - static String getProperty(final String property, final boolean jnlpAlias) { return getProperty(property, jnlpAlias, localACC); } - public static String getProperty(final String property, final boolean jnlpAlias, final AccessControlContext acc) { - String s=null; - if(null!=acc && acc.equals(localACC)) { - s = AccessController.doPrivileged(new PrivilegedAction() { - public String run() { - String val=null; - try { - val = System.getProperty(property); - } catch (Exception e) {} - if(null==val && jnlpAlias && !property.startsWith(jnlp_prefix)) { - try { - val = System.getProperty(jnlp_prefix + property); - } catch (Exception e) {} - } - return val; - } - }); - } else { - try { - s = System.getProperty(property); - } catch (Exception e) {} - if(null==s && jnlpAlias && !property.startsWith(jnlp_prefix)) { - try { - s = System.getProperty(jnlp_prefix + property); - } catch (Exception e) {} - } - } - return s; - } - public static final String jnlp_prefix = "jnlp." ; - public static boolean verbose() { return verbose; } diff --git a/src/java/jogamp/common/PropertyAccess.java b/src/java/jogamp/common/PropertyAccess.java new file mode 100644 index 0000000..88d6dec --- /dev/null +++ b/src/java/jogamp/common/PropertyAccess.java @@ -0,0 +1,147 @@ +/** + * Copyright 2012 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. 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. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``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 JogAmp Community 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. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ + +package jogamp.common; + +import java.security.*; + +/** Helper routines for accessing properties. */ +public class PropertyAccess { + public static final String jnlp_prefix = "jnlp." ; + + /** @see #getProperty(String, boolean, AccessControlContext) */ + public static final int getIntProperty(final String property, final boolean jnlpAlias, final AccessControlContext acc, int defaultValue) { + int i=defaultValue; + try { + final String sv = PropertyAccess.getProperty(property, jnlpAlias, acc); + if(null!=sv) { + i = Integer.valueOf(sv).intValue(); + } + } catch (NumberFormatException nfe) {} + return i; + } + + /** @see #getProperty(String, boolean, AccessControlContext) */ + public static final long getLongProperty(final String property, final boolean jnlpAlias, final AccessControlContext acc, long defaultValue) { + long l=defaultValue; + try { + final String sv = PropertyAccess.getProperty(property, jnlpAlias, acc); + if(null!=sv) { + l = Long.valueOf(sv).longValue(); + } + } catch (NumberFormatException nfe) {} + return l; + } + + /** @see #getProperty(String, boolean, AccessControlContext) */ + public static final boolean getBooleanProperty(final String property, final boolean jnlpAlias, final AccessControlContext acc) { + return Boolean.valueOf(PropertyAccess.getProperty(property, jnlpAlias, acc)).booleanValue(); + } + + /** @see #getProperty(String, boolean, AccessControlContext) */ + public static final boolean getBooleanProperty(final String property, final boolean jnlpAlias, final AccessControlContext acc, boolean defaultValue) { + final String valueS = PropertyAccess.getProperty(property, jnlpAlias, acc); + if(null != valueS) { + return Boolean.valueOf(valueS).booleanValue(); + } + return defaultValue; + } + + /** @see #getProperty(String, boolean, AccessControlContext) */ + public static final boolean isPropertyDefined(final String property, final boolean jnlpAlias, final AccessControlContext acc) { + return (PropertyAccess.getProperty(property, jnlpAlias, acc) != null) ? true : false; + } + + /** + * Query the property with the name propertyKey. + *

+ * If jnlpAlias is true and the plain propertyKey + * could not be resolved, an attempt to resolve the JNLP aliased trusted property is made.
+ * Example: For the propertyName OneTwo, the jnlp alias name is jnlp.OneTwo, which is considered trusted.
+ *

+ * + * @param propertyKey the property name to query. + * @param jnlpAlias true if a fallback attempt to query the JNLP aliased trusted property shall be made, + * otherwise false. + * @param acc the AccessControlerContext to be used for privileged access to the system property, or null. + * + * @return the property value if exists, or null + * + * @throws NullPointerException if the property name is null + * @throws IllegalArgumentException if the property name is of length 0 + * @throws SecurityException if no access to the JNLP aliased trusted property is allowed. + * This is actually a bug in the JRE implementation, since the JNLP aliased trusted properties + * shall be allowed without extended priviledges. + * + * @see System#getProperty(String) + */ + public static final String getProperty(final String propertyKey, final boolean jnlpAlias, final AccessControlContext acc) + throws SecurityException, NullPointerException, IllegalArgumentException { + if(null == propertyKey) { + throw new NullPointerException("propertyKey is NULL"); + } + if(0 == propertyKey.length()) { + throw new IllegalArgumentException("propertyKey is empty"); + } + String s=null; + if( null!=acc ) { + s = AccessController.doPrivileged(new PrivilegedAction() { + public String run() { + String val=null; + try { + val = System.getProperty(propertyKey); + } catch (SecurityException se) {} + return val; + } + }, acc); + } else { + try { + s = System.getProperty(propertyKey); + } catch (SecurityException se) {} + } + if(null==s && jnlpAlias && !propertyKey.startsWith(jnlp_prefix)) { + // Properties within the namespace "jnlp." or "javaws." should be considered trusted, + // i.e. always granted w/o special priviledges. + // FIXME: Nevertheless we use this class AccessControlContext to ensure access + // on all supported implementations. + s = AccessController.doPrivileged(new PrivilegedAction() { + public String run() { + final String propertyKeyAliased = jnlp_prefix + propertyKey; + String val = null; + try { + val = System.getProperty(propertyKeyAliased); + } catch (SecurityException se) { + throw new SecurityException("Could not access trusted property 'propertyKeyAliased'", se); + } + return val; + } + }); + } + return s; + } +} -- cgit v1.2.3