25package org.cipherpack;
27import java.util.concurrent.atomic.AtomicBoolean;
42 abstract class CPNativeDownlink
45 private long nativeInstance;
46 private final AtomicBoolean isNativeValid =
new AtomicBoolean(
false);
47 private final Object nativeLock =
new Object();
53 protected CPNativeDownlink(
final long nativeInstance)
55 this.nativeInstance = nativeInstance;
56 isNativeValid.set(
true);
57 initNativeJavaObject(nativeInstance);
59 protected CPNativeDownlink()
61 this.nativeInstance = 0;
62 isNativeValid.set(
false);
64 protected final void initDownlink(
final long nativeInstance) {
65 synchronized (nativeLock) {
66 if( isNativeValid.compareAndSet(
false,
true) ) {
67 this.nativeInstance = nativeInstance;
68 initNativeJavaObject(nativeInstance);
70 System.err.println(
"DBTNativeDownlink.init2: Already valid, dropping nativeInstance 0x"+Long.toHexString(nativeInstance));
78 protected final boolean isNativeValid() {
return isNativeValid.get(); }
81 protected void finalize()
94 public final void delete() {
95 synchronized (nativeLock) {
96 if( !isNativeValid.compareAndSet(
true,
false) ) {
97 if( CPFactory.DEBUG ) {
98 System.err.println(
"JAVA: delete: !valid -> bail: "+getClass().getSimpleName());
102 if( CPFactory.DEBUG ) {
103 System.err.println(
"JAVA: delete.0: "+getClass().getSimpleName()+
": valid, handle 0x"+Long.toHexString(nativeInstance));
105 final long _nativeInstance = nativeInstance;
107 deleteNativeJavaObject(_nativeInstance);
108 deleteImpl(_nativeInstance);
109 if( CPFactory.DEBUG ) {
110 System.err.println(
"JAVA: delete.X: "+getClass().getSimpleName()+
": handle 0x"+Long.toHexString(nativeInstance));
119 private final void notifyDeleted() {
120 synchronized (nativeLock) {
121 final boolean _isValid = isNativeValid.get();
122 final long _nativeInstance = nativeInstance;
123 isNativeValid.set(
false);
125 if( CPFactory.DEBUG ) {
126 System.err.println(
"JAVA: delete.notifyDeleted: "+getClass().getSimpleName()+
", was: valid "+_isValid+
", handle 0x"+Long.toHexString(_nativeInstance)+
": "+toString());
142 protected abstract void deleteImpl(
long nativeInstance);
144 private native
void initNativeJavaObject(
final long nativeInstance);
145 private native
void deleteNativeJavaObject(
final long nativeInstance);