You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2011/09/16 18:57:40 UTC
svn commit: r1171664 - in /commons/sandbox/runtime/trunk/src/main:
java/org/apache/commons/runtime/ssl/ native/modules/openssl/
test/org/apache/commons/runtime/
Author: mturk
Date: Fri Sep 16 16:57:38 2011
New Revision: 1171664
URL: http://svn.apache.org/viewvc?rev=1171664&view=rev
Log:
Add finalize to key and cert so they get chance to be destroyed if not used
Modified:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/SSLCertificate.java
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/SSLKey.java
commons/sandbox/runtime/trunk/src/main/native/modules/openssl/cert.c
commons/sandbox/runtime/trunk/src/main/native/modules/openssl/key.c
commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestOpenSSL.java
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/SSLCertificate.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/SSLCertificate.java?rev=1171664&r1=1171663&r2=1171664&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/SSLCertificate.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/SSLCertificate.java Fri Sep 16 16:57:38 2011
@@ -32,8 +32,9 @@ public final class SSLCertificate extend
private static native long load0(String file, String desc, int format, long pcb)
throws InvalidDataException;
- private static native long load1(String file, String desc, int format, String password)
+ private static native long load1(String file, int format, String password)
throws InvalidDataException;
+ private static native void free0(long key);
private SSLCertificate()
{
@@ -76,7 +77,24 @@ public final class SSLCertificate extend
// Already loaded
// TODO: Throw exception
}
- super.pointer = load1(file, desc, format.valueOf(), password);
+ super.pointer = load1(file, format.valueOf(), password);
+ }
+
+ /**
+ * Called by the garbage collector when the object is destroyed.
+ * The class will free internal resources allocated by the
+ * Operating system only if there are no additional references
+ * to this object.
+ *
+ * @see Object#finalize()
+ * @throws Throwable the {@code Exception} raised by this method.
+ */
+ @Override
+ protected final void finalize()
+ throws Throwable
+ {
+ if (super.pointer != 0L)
+ free0(super.pointer);
}
}
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/SSLKey.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/SSLKey.java?rev=1171664&r1=1171663&r2=1171664&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/SSLKey.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/SSLKey.java Fri Sep 16 16:57:38 2011
@@ -32,8 +32,9 @@ public final class SSLKey extends Native
private static native long load0(String file, String desc, int format, long pcb)
throws InvalidDataException;
- private static native long load1(String file, String desc, int format, String password)
+ private static native long load1(String file, int format, String password)
throws InvalidDataException;
+ private static native void free0(long key);
private SSLKey()
{
@@ -66,7 +67,7 @@ public final class SSLKey extends Native
// Already loaded
// TODO: Throw exception
}
- super.pointer = load1(file, desc, format.valueOf(), password);
+ super.pointer = load1(file, format.valueOf(), password);
}
public void load(String file, SSLKeyFormat format)
@@ -79,4 +80,21 @@ public final class SSLKey extends Native
super.pointer = load0(file, desc, format.valueOf(), 0L);
}
+ /**
+ * Called by the garbage collector when the object is destroyed.
+ * The class will free internal resources allocated by the
+ * Operating system only if there are no additional references
+ * to this object.
+ *
+ * @see Object#finalize()
+ * @throws Throwable the {@code Exception} raised by this method.
+ */
+ @Override
+ protected final void finalize()
+ throws Throwable
+ {
+ if (super.pointer != 0L)
+ free0(super.pointer);
+ }
+
}
Modified: commons/sandbox/runtime/trunk/src/main/native/modules/openssl/cert.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/modules/openssl/cert.c?rev=1171664&r1=1171663&r2=1171664&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/modules/openssl/cert.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/modules/openssl/cert.c Fri Sep 16 16:57:38 2011
@@ -81,7 +81,6 @@ ACR_SSL_EXPORT(jlong, SSLCertificate, lo
}
ACR_SSL_EXPORT(jlong, SSLCertificate, load1)(JNI_STDARGS, jstring file,
- jstring desc,
jint format,
jstring password)
{
@@ -89,19 +88,22 @@ ACR_SSL_EXPORT(jlong, SSLCertificate, lo
X509 *cert = 0;
WITH_CSTR(file) {
- WITH_CSTR(desc) {
WITH_CSTR(password) {
if (J2S(password) != 0) {
cb.password = J2S(password);
cb.password_len = strlen(J2S(password));
}
/* Load key */
- cert = load_cert(&cb, format, J2S(file), J2S(desc));
+ cert = load_cert(&cb, format, J2S(file), 0);
if (cert == 0)
ssl_throw_errno(env, ACR_EX_EILSEQ);
} DONE_WITH_STR(password);
- } DONE_WITH_STR(desc);
} DONE_WITH_STR(file);
return P2J(cert);
}
+
+ACR_SSL_EXPORT(void, SSLCertificate, free0)(JNI_STDARGS, jlong key)
+{
+ X509_free(J2P(key, X509 *));
+}
Modified: commons/sandbox/runtime/trunk/src/main/native/modules/openssl/key.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/modules/openssl/key.c?rev=1171664&r1=1171663&r2=1171664&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/modules/openssl/key.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/modules/openssl/key.c Fri Sep 16 16:57:38 2011
@@ -82,7 +82,6 @@ ACR_SSL_EXPORT(jlong, SSLKey, load0)(JNI
}
ACR_SSL_EXPORT(jlong, SSLKey, load1)(JNI_STDARGS, jstring file,
- jstring desc,
jint format,
jstring password)
{
@@ -90,19 +89,22 @@ ACR_SSL_EXPORT(jlong, SSLKey, load1)(JNI
EVP_PKEY *key = 0;
WITH_CSTR(file) {
- WITH_CSTR(desc) {
WITH_CSTR(password) {
if (J2S(password) != 0) {
cb.password = J2S(password);
cb.password_len = strlen(J2S(password));
}
/* Load key */
- key = load_key(&cb, format, J2S(file), J2S(desc));
+ key = load_key(&cb, format, J2S(file), 0);
if (key == 0)
ssl_throw_errno(env, ACR_EX_EILSEQ);
} DONE_WITH_STR(password);
- } DONE_WITH_STR(desc);
} DONE_WITH_STR(file);
return P2J(key);
}
+
+ACR_SSL_EXPORT(void, SSLKey, free0)(JNI_STDARGS, jlong key)
+{
+ EVP_PKEY_free(J2P(key, EVP_PKEY *));
+}
Modified: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestOpenSSL.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestOpenSSL.java?rev=1171664&r1=1171663&r2=1171664&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestOpenSSL.java (original)
+++ commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestOpenSSL.java Fri Sep 16 16:57:38 2011
@@ -48,7 +48,18 @@ public class TestOpenSSL extends Assert
return "secret";
}
}
-
+
+ public class TestPointer extends NativePointer
+ {
+ // Hide NativePointer
+ private final long pointer = 0L;
+ public TestPointer()
+ {
+ super.pointer = 1234L;
+ }
+
+ }
+
@BeforeSuite(groups = { "openssl" })
public void setUp()
{
@@ -67,6 +78,15 @@ public class TestOpenSSL extends Assert
}
@Test(groups = { "openssl" })
+ public void nativePointer()
+ {
+ TestPointer p = new TestPointer();
+ assertEquals(((NativePointer)p).pointer, 1234L);
+ ((NativePointer)p).pointer = 0L;
+ assertEquals(((NativePointer)p).pointer, 0L);
+ }
+
+ @Test(groups = { "openssl" })
public void setupPasswordCallback()
{
PasswordHandler h = new PasswordHandler();