summaryrefslogtreecommitdiff
path: root/org
diff options
context:
space:
mode:
authorMatthew Johnson <mjj29@hecate.matthew.ath.cx>2008-02-05 11:37:49 +0000
committerMatthew Johnson <mjj29@hecate.matthew.ath.cx>2008-02-05 11:37:49 +0000
commitf3b3cc5a2f8cea7c2148b6ba82b49f7538d7d729 (patch)
tree1dd76378561aa2b256e4e4e8ce2172b7ad93b039 /org
parentd5a84e681639aac6cca9b3fe50532729d2b60025 (diff)
make weakreferences optional
Diffstat (limited to 'org')
-rw-r--r--org/freedesktop/dbus/AbstractConnection.java18
-rw-r--r--org/freedesktop/dbus/ExportedObject.java10
2 files changed, 22 insertions, 6 deletions
diff --git a/org/freedesktop/dbus/AbstractConnection.java b/org/freedesktop/dbus/AbstractConnection.java
index 1916ba6..701a131 100644
--- a/org/freedesktop/dbus/AbstractConnection.java
+++ b/org/freedesktop/dbus/AbstractConnection.java
@@ -247,6 +247,7 @@ public abstract class AbstractConnection
protected _sender sender;
protected Transport transport;
protected String addr;
+ protected boolean weakreferences = false;
static final Pattern dollar_pattern = Pattern.compile("[$]");
public static final boolean EXCEPTION_DEBUG;
static final boolean FLOAT_SUPPORT;
@@ -279,7 +280,7 @@ public abstract class AbstractConnection
importedObjects = new HashMap<DBusInterface,RemoteObject>();
_globalhandlerreference = new _globalhandler();
synchronized (exportedObjects) {
- exportedObjects.put(null, new ExportedObject(_globalhandlerreference));
+ exportedObjects.put(null, new ExportedObject(_globalhandlerreference, weakreferences));
}
handledSignals = new HashMap<SignalTuple,Vector<DBusSigHandler<? extends DBusSignal>>>();
pendingCalls = new EfficientMap(PENDING_MAP_INITIAL_SIZE);
@@ -372,6 +373,17 @@ public abstract class AbstractConnection
return info;
}
+ /**
+ * If set to true the bus will not hold a strong reference to exported objects.
+ * If they go out of scope they will automatically be unexported from the bus.
+ * The default is to hold a strong reference, which means objects must be
+ * explicitly unexported before they will be garbage collected.
+ */
+ public void setWeakReferences(boolean weakreferences)
+ {
+ this.weakreferences = weakreferences;
+ }
+
/**
* Export an object so that its methods can be called on DBus.
* @param objectpath The path to the object we are exposing. MUST be in slash-notation, like "/org/freedesktop/Local",
@@ -390,7 +402,7 @@ public abstract class AbstractConnection
synchronized (exportedObjects) {
if (null != exportedObjects.get(objectpath))
throw new DBusException(_("Object already exported"));
- ExportedObject eo = new ExportedObject(object);
+ ExportedObject eo = new ExportedObject(object, weakreferences);
exportedObjects.put(objectpath, eo);
objectTree.add(objectpath, eo, eo.introspectiondata);
}
@@ -410,7 +422,7 @@ public abstract class AbstractConnection
throw new DBusException(_("Must Specify an Object Path"));
if (!objectprefix.matches(OBJECT_REGEX)||objectprefix.length() > MAX_NAME_LENGTH)
throw new DBusException(_("Invalid object path: ")+objectprefix);
- ExportedObject eo = new ExportedObject(object);
+ ExportedObject eo = new ExportedObject(object, weakreferences);
fallbackcontainer.add(objectprefix, eo);
}
/**
diff --git a/org/freedesktop/dbus/ExportedObject.java b/org/freedesktop/dbus/ExportedObject.java
index 3677aa9..c7bb905 100644
--- a/org/freedesktop/dbus/ExportedObject.java
+++ b/org/freedesktop/dbus/ExportedObject.java
@@ -12,6 +12,7 @@ package org.freedesktop.dbus;
import static org.freedesktop.dbus.Gettext._;
+import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.lang.annotation.Annotation;
@@ -138,11 +139,14 @@ class ExportedObject
return m;
}
Map<MethodTuple,Method> methods;
- WeakReference<DBusInterface> object;
+ Reference<DBusInterface> object;
String introspectiondata;
- public ExportedObject(DBusInterface object) throws DBusException
+ public ExportedObject(DBusInterface object, boolean weakreferences) throws DBusException
{
- this.object = new WeakReference<DBusInterface>(object);
+ if (weakreferences)
+ this.object = new WeakReference<DBusInterface>(object);
+ else
+ this.object = new StrongReference<DBusInterface>(object);
introspectiondata = "";
methods = getExportedMethods(object.getClass());
introspectiondata +=