From 66b6029e4058e1d29f35e4170423d127c2ebb2c4 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Fri, 23 Jan 2009 18:02:32 -0700 Subject: Ported xmlclient to subclass from Connectible --- ipalib/backend.py | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) (limited to 'ipalib/backend.py') diff --git a/ipalib/backend.py b/ipalib/backend.py index e286c5079..bb8d31012 100644 --- a/ipalib/backend.py +++ b/ipalib/backend.py @@ -36,26 +36,36 @@ class Backend(plugable.Plugin): class Connectible(Backend): - # Override in subclass: - connection_klass = None - def connect(self, *args, **kw): """ Create thread-local connection. """ if hasattr(context, self.name): raise StandardError( - "connection 'context.%s' already exists in thread %r" % ( + "connect: 'context.%s' already exists in thread %r" % ( self.name, threading.currentThread().getName() ) ) - if not issubclass(self.connection_klass, Connection): - raise ValueError( - '%s.connection_klass must be a request.Connection subclass' % self.name + conn = self.create_connection(*args, **kw) + setattr(context, self.name, Connection(conn, self.disconnect)) + assert self.conn is conn + self.info('Created connection context.%s' % self.name) + + def create_connection(self, *args, **kw): + raise NotImplementedError('%s.create_connection()' % self.name) + + def disconnect(self): + if not hasattr(context, self.name): + raise StandardError( + "disconnect: 'context.%s' does not exist in thread %r" % ( + self.name, threading.currentThread().getName() + ) ) - conn = self.connection_klass(*args, **kw) - setattr(context, self.name, conn) - assert self.conn is conn.conn + self.destroy_connection() + self.info('Destroyed connection context.%s' % self.name) + + def destroy_connection(self): + raise NotImplementedError('%s.destroy_connection()' % self.name) def isconnected(self): """ -- cgit