24package org.cipherpack;
26import java.io.IOException;
27import java.io.InputStream;
29import java.security.PrivilegedAction;
30import java.util.Enumeration;
31import java.util.Iterator;
32import java.util.Properties;
34import java.util.concurrent.atomic.AtomicBoolean;
35import java.util.jar.Attributes;
36import java.util.jar.Manifest;
38import org.jau.util.VersionUtil;
53 private static String APIVersion;
59 private static String ImplVersion;
75 public static final boolean DEBUG;
94 @SuppressWarnings({
"deprecation",
"removal" })
96 return java.security.AccessController.doPrivileged( o );
99 private static final String implementationNativeLibraryBasename =
"cipherpack";
100 private static final String javaNativeLibraryBasename =
"javacipherpack";
102 private static AtomicBoolean initializedID =
new AtomicBoolean(
false);
106 final String v = System.getProperty(
"org.cipherpack.debug",
"false");
107 DEBUG = Boolean.valueOf(v);
112 final String v = System.getProperty(
"org.cipherpack.verbose",
"false");
116 boolean isJaulibAvail =
false;
118 isJaulibAvail =
null != Class.forName(
"org.jau.sys.RuntimeProps",
true , CPFactory.class.getClassLoader());
119 }
catch(
final Throwable t ) {
121 System.err.println(
"CPFactory Caught: "+t.getMessage());
127 if( isJaulibAvail ) {
136 final ClassLoader cl = CPFactory.class.getClassLoader();
137 boolean libsLoaded =
false;
141 org.jau.pkg.JNIJarLibrary.addNativeJarLibs(
new Class<?>[] { CPFactory.class },
null);
142 }
catch (
final Exception e0) {
143 System.err.println(
"CPFactory Caught "+e0.getClass().getSimpleName()+
": "+e0.getMessage()+
", while JNILibLoaderBase.addNativeJarLibs(..)");
145 e0.printStackTrace();
150 if(
null != org.jau.sys.dl.NativeLibrary.open(implementationNativeLibraryBasename,
151 true ,
false , cl,
true ) )
153 org.jau.sys.JNILibrary.loadLibrary(javaNativeLibraryBasename,
false, cl);
156 }
catch (
final Throwable t) {
157 System.err.println(
"Caught "+t.getClass().getSimpleName()+
": "+t.getMessage()+
", while loading libs..");
163 System.err.println(
"Jaulib: Native libs loaded: "+ libsLoaded);
168 final Throwable[] t = {
null };
169 if( !PlatformToolkit.loadLibrary(implementationNativeLibraryBasename, cl, t) ) {
170 throw new RuntimeException(
"Couldn't load native tool library with basename <"+implementationNativeLibraryBasename+
">", t[0]);
172 if( !PlatformToolkit.loadLibrary(javaNativeLibraryBasename, cl, t) ) {
173 throw new RuntimeException(
"Couldn't load native java library with basename <"+javaNativeLibraryBasename+
">", t[0]);
175 }
catch (
final Throwable e) {
176 System.err.println(
"Caught "+e.getClass().getSimpleName()+
": "+e.getMessage()+
", while loading libs (2) ..");
187 System.err.println(
"CPFactory: Mapping '[org.|jau.]cipherpack.*' properties to native environment");
189 final Properties props =
doPrivileged(
new PrivilegedAction<Properties>() {
191 public Properties run() {
192 return System.getProperties();
195 final Enumeration<?> enums = props.propertyNames();
196 while (enums.hasMoreElements()) {
197 final String key = (String) enums.nextElement();
198 if( key.startsWith(
"org.cipherpack.") || key.startsWith(
"jau.cipherpack.") ||
199 key.startsWith(
"cipherpack.") )
201 final String value = props.getProperty(key);
203 System.err.println(
" <"+key+
"> := <"+value+
">");
205 setenv(key, value,
true );
208 }
catch (
final Throwable e) {
209 System.err.println(
"Caught exception while forwarding system properties: "+e.getMessage());
214 final Manifest manifest =
getManifest(cl,
new String[] {
"org.cipherpack" } );
215 final Attributes mfAttributes =
null != manifest ? manifest.getMainAttributes() :
null;
219 final String JAPIVersion =
null != mfAttributes ? mfAttributes.getValue(Attributes.Name.SPECIFICATION_VERSION) :
null;
220 if (
null != JAPIVersion && JAPIVersion.equals(NAPIVersion) ==
false) {
221 final String[] NAPIVersionCode = NAPIVersion.split(
"\\D");
222 final String[] JAPIVersionCode = JAPIVersion.split(
"\\D");
223 if (JAPIVersionCode[0].equals(NAPIVersionCode[0]) ==
false) {
224 if (Integer.valueOf(JAPIVersionCode[0]) < Integer.valueOf(NAPIVersionCode[0])) {
225 throw new RuntimeException(
"Java library "+JAPIVersion+
" < native library "+NAPIVersion+
". Please update the Java library.");
227 throw new RuntimeException(
"Native library "+NAPIVersion+
" < java library "+JAPIVersion+
". Please update the native library.");
229 }
else if (JAPIVersionCode[1].equals(NAPIVersionCode[1]) ==
false) {
230 if (Integer.valueOf(JAPIVersionCode[1]) < Integer.valueOf(NAPIVersionCode[1])) {
231 throw new RuntimeException(
"Java library "+JAPIVersion+
" < native library "+NAPIVersion+
". Please update the Java library.");
233 throw new RuntimeException(
"Native library "+NAPIVersion+
" < java library "+JAPIVersion+
". Please update the native library.");
237 initializedID.set(
true );
239 APIVersion = JAPIVersion;
240 ImplVersion =
null != mfAttributes ? mfAttributes.getValue(Attributes.Name.IMPLEMENTATION_VERSION) :
null;
242 System.err.println(
"Cipherpack loaded");
243 System.err.println(
"Cipherpack java api version "+JAPIVersion);
244 System.err.println(
"Cipherpack native api version "+NAPIVersion);
245 if(
null != mfAttributes ) {
246 final Attributes.Name[] versionAttributeNames =
new Attributes.Name[] {
247 Attributes.Name.SPECIFICATION_TITLE,
248 Attributes.Name.SPECIFICATION_VENDOR,
249 Attributes.Name.SPECIFICATION_VERSION,
250 Attributes.Name.IMPLEMENTATION_TITLE,
251 Attributes.Name.IMPLEMENTATION_VENDOR,
252 Attributes.Name.IMPLEMENTATION_VERSION,
253 new Attributes.Name(
"Implementation-Commit") };
254 for(
final Attributes.Name an : versionAttributeNames ) {
255 System.err.println(
" "+an+
": "+mfAttributes.getValue(an));
258 System.err.println(
" No Manifest available;");
261 }
catch (
final Throwable e) {
262 System.err.println(
"Error querying manifest information.");
270 if(
false == initializedID.get() ) {
271 throw new IllegalStateException(
"Cipherpack not initialized.");
275 return false != initializedID.get();
285 public static final Manifest
getManifest(
final ClassLoader cl,
final String[] extensions) {
286 final Manifest[] extManifests =
new Manifest[extensions.length];
288 final Enumeration<URL> resources = cl.getResources(
"META-INF/MANIFEST.MF");
289 while (resources.hasMoreElements()) {
290 final URL resURL = resources.nextElement();
292 System.err.println(
"resource: "+resURL);
294 final InputStream is = resURL.openStream();
295 final Manifest manifest;
297 manifest =
new Manifest(is);
301 }
catch (
final IOException e) {}
303 final Attributes attributes = manifest.getMainAttributes();
304 if(attributes !=
null) {
305 final String attributesExtName = attributes.getValue( Attributes.Name.EXTENSION_NAME );
306 for(
int i=0; i < extensions.length &&
null == extManifests[i]; i++) {
307 final String extension = extensions[i];
308 if( extension.equals( attributesExtName ) ) {
312 extManifests[i] = manifest;
317 }
catch (
final IOException ex) {
318 throw new RuntimeException(
"Unable to read manifest.", ex);
320 for(
int i=1; i<extManifests.length; i++) {
321 if(
null != extManifests[i] ) {
322 return extManifests[i];
328 public static void main(
final String args[]) {
331 System.err.println(VersionUtil.getPlatformInfo());
332 System.err.println(
"Version Info:");
334 System.err.println(v);
335 System.err.println(
"");
336 System.err.println(
"Full Manifest:");
337 System.err.println(v.getFullManifestInfo(
null));
339 System.err.println(
"Full Manifest:");
340 final Manifest manifest =
getManifest(
CPFactory.class.getClassLoader(),
new String[] {
"org.cipherpack" } );
341 final Attributes attr = manifest.getMainAttributes();
342 final Set<Object> keys = attr.keySet();
343 final StringBuilder sb =
new StringBuilder();
344 for(
final Iterator<Object> iter=keys.iterator(); iter.hasNext(); ) {
345 final Attributes.Name key = (Attributes.Name) iter.next();
346 final String val = attr.getValue(key);
351 sb.append(System.lineSeparator());
353 System.err.println(sb);
357 }
catch (
final Throwable t ) {
364 private native
static void setenv(String name, String value,
boolean overwrite);
Cipherpack Factory, called by automatically to load the native library.
static final Manifest getManifest(final ClassLoader cl, final String[] extensions)
Helper function to retrieve a Manifest instance.
static native String getNativeVersion()
static final boolean JAULIB_JARCACHE_USED
True if jaulib JAULIB_AVAILABLE and its org.jau.sys.PlatformProps#USE_TEMP_JAR_CACHE is true,...
static final boolean JAULIB_AVAILABLE
True if jaulib org.jau.sys.PlatformProps has been detected.
static native String getNativeAPIVersion()
static void checkInitialized()
static final String getImplVersion()
Manifest's Attributes.Name#IMPLEMENTATION_VERSION or null if not available.
static boolean isInitialized()
static final String getAPIVersion()
Manifest's Attributes.Name#SPECIFICATION_VERSION or null if not available.
static synchronized void initLibrary()
static final boolean VERBOSE
Verbose logging enabled or disabled.
static< T > T doPrivileged(final PrivilegedAction< T > o)
Deprecated call to java.security.AccessController#doPrivileged(PrivilegedAction) w/o warnings.
static void main(final String args[])
static final boolean DEBUG
Debug logging enabled or disabled.
This jaulib derived version info class is only usable when having jaulib available,...
static CPVersion getInstance()
Returns a transient new instance.