From f3b3cc5a2f8cea7c2148b6ba82b49f7538d7d729 Mon Sep 17 00:00:00 2001 From: Matthew Johnson Date: Tue, 5 Feb 2008 11:37:49 +0000 Subject: make weakreferences optional --- org/freedesktop/dbus/AbstractConnection.java | 18 +++++++++++++++--- org/freedesktop/dbus/ExportedObject.java | 10 +++++++--- 2 files changed, 22 insertions(+), 6 deletions(-) (limited to 'org') 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(); _globalhandlerreference = new _globalhandler(); synchronized (exportedObjects) { - exportedObjects.put(null, new ExportedObject(_globalhandlerreference)); + exportedObjects.put(null, new ExportedObject(_globalhandlerreference, weakreferences)); } handledSignals = new HashMap>>(); 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 methods; - WeakReference object; + Reference object; String introspectiondata; - public ExportedObject(DBusInterface object) throws DBusException + public ExportedObject(DBusInterface object, boolean weakreferences) throws DBusException { - this.object = new WeakReference(object); + if (weakreferences) + this.object = new WeakReference(object); + else + this.object = new StrongReference(object); introspectiondata = ""; methods = getExportedMethods(object.getClass()); introspectiondata += -- cgit v1.2.3