jaulib v1.3.0
Jau Support Library (C++, Java, ..)
PlatformRuntime.java
Go to the documentation of this file.
1package jau.pkg;
2
3import java.security.PrivilegedAction;
4import java.util.concurrent.atomic.AtomicBoolean;
5
6import org.jau.net.Uri;
7import org.jau.pkg.JNIJarLibrary;
8import org.jau.pkg.JarUtil;
9import org.jau.pkg.cache.TempJarCache;
10import org.jau.sec.SecurityUtil;
11import org.jau.sys.JNILibrary;
12import org.jau.sys.PlatformProps;
13import org.jau.sys.MachineDataInfo;
14import org.jau.sys.PlatformTypes.OSType;
15
16import org.jau.sys.PropertyAccess;
17
18import jau.sys.MachineDataInfoRuntime;
19
20/**
21 * Initialized by {@link org.jau.sys.RuntimeProps}
22 */
23public class PlatformRuntime {
24 private static final String useTempJarCachePropName = "jau.pkg.UseTempJarCache";
25
26 //
27 // static initialization order:
28 //
29
30 /**
31 * true if enabled and in use.
32 * <p>
33 * System property: 'jau.pkg.UseTempJarCache',
34 * defaults to true if {@link #OS_TYPE} is not {@link OSType#ANDROID}.
35 * </p>
36 */
37 public static final boolean USE_TEMP_JAR_CACHE;
38
39 //
40 // post loading native lib:
41 //
42
43 public static final boolean isRunningFromJarURL;
44
45 /** Runtime determined {@link MachineDataInfo}. */
46 public static final MachineDataInfo MACH_DESC_RT;
47
48 private static AtomicBoolean initializedID = new AtomicBoolean(false);
49
50 static {
51 final boolean[] _isRunningFromJarURL = new boolean[] { false };
52 final boolean[] _USE_TEMP_JAR_CACHE = new boolean[] { false };
53 final boolean[] _libsLoaded = new boolean[] { false };
54
55 SecurityUtil.doPrivileged(new PrivilegedAction<Object>() {
56 @Override
57 public Object run() {
58
59 final ClassLoader cl = PlatformRuntime.class.getClassLoader();
60
61 final Uri platformClassJarURI;
62 {
63 Uri _platformClassJarURI = null;
64 try {
65 _platformClassJarURI = JarUtil.getJarUri(PlatformRuntime.class.getName(), cl);
66 } catch (final Exception e) { }
67 platformClassJarURI = _platformClassJarURI;
68 }
69 _isRunningFromJarURL[0] = null != platformClassJarURI;
70
71 _USE_TEMP_JAR_CACHE[0] = ( PlatformProps.OS != OSType.ANDROID ) && ( PlatformProps.OS != OSType.IOS ) &&
72 ( null != platformClassJarURI ) &&
73 PropertyAccess.getBooleanProperty(useTempJarCachePropName, true, true);
74
75 // load jaulib native library
76 if(_USE_TEMP_JAR_CACHE[0] && TempJarCache.initSingleton() && TempJarCache.isInitialized(true) ) {
77 try {
78 JNIJarLibrary.addNativeJarLibs(new Class<?>[] { org.jau.sys.Debug.class }, null);
79 } catch (final Exception e0) {
80 // IllegalArgumentException, IOException
81 _USE_TEMP_JAR_CACHE[0] = false;
82 System.err.println("Caught "+e0.getClass().getSimpleName()+": "+e0.getMessage()+", while JNILibLoaderBase.addNativeJarLibs(..)");
83 e0.printStackTrace();
84 }
85 } else {
86 _USE_TEMP_JAR_CACHE[0] = false;
87 }
88 try {
89 if( !JNILibrary.loadLibrary("jaulib", false, cl) ) {
90 throw new RuntimeException("Couldn't load native tool library with basename <jaulib>");
91 }
92 if( !JNILibrary.loadLibrary("jaulib_jni_jni", false, cl) ) {
93 throw new RuntimeException("Couldn't load native java library with basename <jaulib_jni_jni>");
94 }
95 if( OSType.MACOS == PlatformProps.OS || OSType.IOS == PlatformProps.OS ) {
96 if( !JNILibrary.loadLibrary("jaulib_pkg_jni", false, cl) ) {
97 throw new RuntimeException("Couldn't load native MACOS/OS java library with basename <jaulib_pkg_jni>");
98 }
99 }
100 _libsLoaded[0] = true;
101 } catch (final Throwable t) {
102 System.err.println("Caught "+t.getClass().getSimpleName()+": "+t.getMessage()+", while loading libs..");
103 t.printStackTrace();
104 }
105 return null;
106 } } );
107 isRunningFromJarURL = _isRunningFromJarURL[0];
108 USE_TEMP_JAR_CACHE = _USE_TEMP_JAR_CACHE[0];
109
110 initializedID.set(_libsLoaded[0]);
111
112 //
113 // Validate and setup MachineDataInfo.StaticConfig
114 //
115 MachineDataInfoRuntime.initialize();
116 MACH_DESC_RT = MachineDataInfoRuntime.getRuntime();
117 }
118
119 public static void checkInitialized() {
120 if( false == initializedID.get() ) {
121 throw new IllegalStateException("Jaulib not initialized.");
122 }
123 }
124 public static boolean isInitialized() {
125 return false != initializedID.get();
126 }
127 /**
128 * kick off static initialization of <i>platform property information</i> and <i>native gluegen_rt lib loading</i>
129 */
130 public static void initSingleton() { }
131
132
133 /**
134 * Returns the MachineDataInfo of the running machine.
135 */
137 return MACH_DESC_RT;
138 }
139
140 /**
141 * Returns true if enabled and in use.
142 * <p>
143 * System property: 'jau.pkg.UseTempJarCache',
144 * defaults to true if {@link #OS_TYPE} is not {@link OSType#ANDROID}.
145 * </p>
146 */
147 public static final boolean getUseTempJarCache() { return USE_TEMP_JAR_CACHE; }
148}
Initialized by org.jau.sys.RuntimeProps.
static final boolean isRunningFromJarURL
static final boolean getUseTempJarCache()
Returns true if enabled and in use.
static final boolean USE_TEMP_JAR_CACHE
true if enabled and in use.
static final MachineDataInfo MACH_DESC_RT
Runtime determined MachineDataInfo.
static void initSingleton()
kick off static initialization of platform property information and native gluegen_rt lib loading
static boolean isInitialized()
static MachineDataInfo getMachineDataInfo()
Returns the MachineDataInfo of the running machine.
Machine data description for alignment and size onle, see com.jogamp.gluegen.