Add Null Horde_Lock driver
authorMichael M Slusarz <slusarz@curecanti.org>
Tue, 11 May 2010 19:42:13 +0000 (13:42 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Tue, 11 May 2010 19:50:47 +0000 (13:50 -0600)
babel/view.php
framework/Core/lib/Horde/Core/Binder/Lock.php
framework/Lock/lib/Horde/Lock.php
framework/Lock/lib/Horde/Lock/Driver.php
framework/Lock/lib/Horde/Lock/Exception.php
framework/Lock/lib/Horde/Lock/Null.php [new file with mode: 0644]
framework/Lock/lib/Horde/Lock/Sql.php
framework/Lock/package.xml
horde/config/conf.xml

index f732316..3eb596c 100644 (file)
@@ -52,10 +52,9 @@ echo $template->fetch(BABEL_TEMPLATES . '/layout.html');
 
 $app = Horde_Util::getFormData('module');
 $show = 'edit';
-$vars = &Horde_Variables::getDefaultVariables();
+$vars = Horde_Variables::getDefaultVariables();
 
 if ($app) {
-
     $napp = ($app == 'horde') ? '' : $app;
     $pofile = HORDE_BASE . '/' . $napp . '/po/' . $lang . '.po';
     $po = new File_Gettext_PO();
@@ -65,7 +64,7 @@ if ($app) {
     $lockscope = sprintf("babel-%s-%s", $app, $lang);
 
     // Initialize Horde_Lock class
-    $locks = Horde_Lock::singleton('Sql', Horde::getDriverConfig('lock', 'Sql'));
+    $locks = $injector->getInstance('Horde_Lock');
 
 //    $curlocks = $locks->getLocks($lockscope);
 //    var_dump($curlocks);
index 7c605f6..bb1e3b9 100644 (file)
@@ -8,15 +8,22 @@ class Horde_Core_Binder_Lock implements Horde_Injector_Binder
     public function create(Horde_Injector $injector)
     {
         if (empty($GLOBALS['conf']['lock']['driver'])) {
-            $driver = null;
+            $driver = 'Null';
         } else {
             $driver = $GLOBALS['conf']['lock']['driver'];
-            $params = Horde::getDriverConfig('lock', $driver);
+            if (Horde_String::lower($driver) == 'none') {
+                $driver = 'Null';
+            }
         }
 
+        $params = Horde::getDriverConfig('lock', $driver);
         $params['logger'] = $injector->getInstance('Horde_Log_Logger');
 
-        return Horde_Lock::singleton($driver, $params);
+        if (Horde_String::lower($driver) == 'sql') {
+            Horde_Util::assertDriverConfig($params, array('phptype'), 'Lock SQL');
+        }
+
+        return Horde_Lock::factory($driver, $params);
     }
 
     public function equals(Horde_Injector_Binder $binder)
index af68992..fd3588b 100644 (file)
@@ -8,8 +8,9 @@
  * See the enclosed file COPYING for license information (LGPL). If you did
  * not receive this file, see http://opensource.org/licenses/lgpl-license.php.
  *
- * @author  Ben Klang <ben@alkaloid.net>
- * @package Horde_Lock
+ * @author   Ben Klang <ben@alkaloid.net>
+ * @category Horde
+ * @package  Lock
  */
 class Horde_Lock
 {
@@ -18,13 +19,6 @@ class Horde_Lock
     const TYPE_SHARED = 2;
 
     /**
-     * Singleton instances.
-     *
-     * @var array
-     */
-    static protected $_instances = array();
-
-    /**
      * Attempts to return a concrete instance based on $driver.
      *
      * @param mixed $driver  The type of concrete subclass to return.
@@ -48,33 +42,4 @@ class Horde_Lock
         throw new Horde_Lock_Exception('Horde_Lock driver (' . $class . ') not found');
     }
 
-    /**
-     * Attempts to return a reference to a concrete instance based on
-     * $driver. It will only create a new instance if no instance
-     * with the same parameters currently exists.
-     *
-     * This should be used if multiple authentication sources (and, thus,
-     * multiple Horde_Lock instances) are required.
-     *
-     * @param string $driver  The type of concrete Horde_Lock subclass to
-     *                        return.
-     *                        This is based on the storage driver ($driver).
-     *                        The code is dynamically included.
-     * @param array $params   A hash containing any additional configuration or
-     *                        connection parameters a subclass might need.
-     *
-     * @return Horde_Lock_Driver  The concrete reference.
-     * @throws Horde_Lock_Exception
-     */
-    static public function singleton($driver, $params = array())
-    {
-        ksort($params);
-        $signature = hash('md5', serialize(array($driver, $params)));
-        if (empty(self::$_instances[$signature])) {
-            self::$_instances[$signature] = self::factory($driver, $params);
-        }
-
-        return self::$_instances[$signature];
-    }
-
 }
index 0628092..86f2197 100644 (file)
@@ -7,8 +7,9 @@
  * See the enclosed file COPYING for license information (LGPL). If you did
  * not receive this file, see http://opensource.org/licenses/lgpl-license.php.
  *
- * @author  Ben Klang <ben@alkaloid.net>
- * @package Horde_Lock
+ * @author   Ben Klang <ben@alkaloid.net>
+ * @category Horde
+ * @package  Lock
  */
 abstract class Horde_Lock_Driver
 {
@@ -80,7 +81,7 @@ abstract class Horde_Lock_Driver
      *                         lock.
      * @param integer $extend  Extend lock this many seconds from now.
      *
-     * @return boolean  TODO
+     * @return boolean  Returns true on success.
      * @throws Horde_Lock_Exception
      */
     abstract public function resetLock($lockid, $extend);
@@ -133,7 +134,7 @@ abstract class Horde_Lock_Driver
      * @param string $lockid  The lock ID as generated by a previous call
      *                        to setLock()
      *
-     * @return boolean  TODO
+     * @return boolean  Returns true on success.
      * @throws Horde_Lock_Exception
      */
     abstract public function clearLock($lockid);
index 6f7a20d..249c36e 100644 (file)
@@ -9,6 +9,6 @@
  *
  * @author   Michael Slusarz <slusarz@horde.org>
  * @category Horde
- * @package  Horde_Lock
+ * @package  Lock
  */
 class Horde_Lock_Exception extends Horde_Exception_Prior {}
diff --git a/framework/Lock/lib/Horde/Lock/Null.php b/framework/Lock/lib/Horde/Lock/Null.php
new file mode 100644 (file)
index 0000000..d8784d2
--- /dev/null
@@ -0,0 +1,123 @@
+<?php
+/**
+ * A null driver for Horde_Lock.
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you did
+ * not receive this file, see http://opensource.org/licenses/lgpl-license.php.
+ *
+ * @author   Michael Slusarz <slusarz@curecanti.org>
+ * @category Horde
+ * @package  Lock
+ */
+class Horde_Lock_Null extends Horde_Lock_Driver
+{
+    /**
+     * Return an array of information about the requested lock.
+     *
+     * @param string $lockid  Lock ID to look up.
+     *
+     * @return array  Lock information.
+     * @throws Horde_Lock_Exception
+     */
+    public function getLockInfo($lockid)
+    {
+        return array();
+    }
+
+    /**
+     * Return a list of valid locks with the option to limit the results
+     * by principal, scope and/or type.
+     *
+     * @param string $scope      The scope of the lock.  Typically the name of
+     *                           the application requesting the lock or some
+     *                           other identifier used to group locks together.
+     * @param string $principal  Principal for which to check for locks
+     * @param integer $type      Only return locks of the given type.
+     *                           Defaults to null, or all locks
+     *
+     * @return array  Array of locks with the ID as the key and the lock details
+     *                as the value. If there are no current locks this will
+     *                return an empty array.
+     * @throws Horde_Lock_Exception
+     */
+    public function getLocks($scope = null, $principal = null, $type = null)
+    {
+        return array();
+    }
+
+    /**
+     * Extend the valid lifetime of a valid lock to now + $extend.
+     *
+     * @param string $lockid   Lock ID to reset. Must be a valid, non-expired
+     *                         lock.
+     * @param integer $extend  Extend lock this many seconds from now.
+     *
+     * @return boolean  Returns true on success.
+     * @throws Horde_Lock_Exception
+     */
+    public function resetLock($lockid, $extend)
+    {
+        return true;
+    }
+
+    /**
+     * Sets a lock on the requested principal and returns the generated lock
+     * ID. NOTE: No security checks are done in the Horde_Lock API. It is
+     * expected that the calling application has done all necessary security
+     * checks before requesting a lock be granted.
+     *
+     * @param string $requestor  User ID of the lock requestor.
+     * @param string $scope      The scope of the lock.  Typically the name of
+     *                           the application requesting the lock or some
+     *                           other identifier used to group locks
+     *                           together.
+     * @param string $principal  A principal on which a lock should be
+     *                           granted. The format can be any string but is
+     *                           suggested to be in URI form.
+     * @param integer $lifetime  Time (in seconds) for which the lock will be
+     *                           considered valid.
+     * @param string exclusive   One of Horde_Lock::TYPE_SHARED or
+     *                           Horde_Lock::TYPE_EXCLUSIVE.
+     *                           - An exclusive lock will be enforced strictly
+     *                             and must be interpreted to mean that the
+     *                             resource can not be modified. Only one
+     *                             exclusive lock per principal is allowed.
+     *                           - A shared lock is one that notifies other
+     *                             potential lock requestors that the resource
+     *                             is in use. This lock can be overridden
+     *                             (cleared or replaced with a subsequent
+     *                             call to setLock()) by other users. Multiple
+     *                             users may request (and will be granted) a
+     *                             shared lock on a given principal. All locks
+     *                             will be considered valid until they are
+     *                             cleared or expire.
+     *
+     * @return mixed   A string lock ID.
+     * @throws Horde_Lock_Exception
+     */
+    public function setLock($requestor, $scope, $principal, $lifetime = 1,
+                            $exclusive = Horde_Lock::TYPE_SHARED)
+    {
+        return strval(new Horde_Support_Uuid());
+    }
+
+    /**
+     * Removes a lock given the lock ID.
+     * NOTE: No security checks are done in the Horde_Lock API.  It is
+     * expected that the calling application has done all necessary security
+     * checks before requesting a lock be cleared.
+     *
+     * @param string $lockid  The lock ID as generated by a previous call
+     *                        to setLock()
+     *
+     * @return boolean  Returns true on success.
+     * @throws Horde_Lock_Exception
+     */
+    public function clearLock($lockid)
+    {
+        return true;
+    }
+
+}
index b3f669f..51c6264 100644 (file)
@@ -48,8 +48,9 @@
  * See the enclosed file COPYING for license information (LGPL). If you did
  * not receive this file, see http://opensource.org/licenses/lgpl-license.php.
  *
- * @author  Ben Klang <ben@alkaloid.net>
- * @package Horde_Lock
+ * @author   Ben Klang <ben@alkaloid.net>
+ * @category Horde
+ * @package  Lock
  */
 class Horde_Lock_Sql extends Horde_Lock_Driver
 {
@@ -219,6 +220,7 @@ class Horde_Lock_Sql extends Horde_Lock_Driver
         if ($this->_logger) {
             $this->_logger->log(sprintf('Lock %s reset successfully.', $lockid), 'DEBUG');
         }
+
         return true;
     }
 
@@ -311,15 +313,6 @@ class Horde_Lock_Sql extends Horde_Lock_Driver
             return;
         }
 
-        try {
-            Horde_Util::assertDriverConfig($this->_params, array('phptype'), 'Lock SQL');
-        } catch (Horde_Exception $e) {
-            if ($this->_logger) {
-                $this->_logger->log($e, 'ERR');
-            }
-            throw new Horde_Lock_Exception($e);
-        }
-
         $this->_write_db = DB::connect(
             $this->_params,
             array('persistent' => !empty($this->_params['persistent']),
index 43bc797..31b14bd 100644 (file)
@@ -25,7 +25,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
   <api>alpha</api>
  </stability>
  <license uri="http://www.gnu.org/copyleft/lesser.html">LGPL</license>
- <notes>* Initial release
+ <notes>* Add Null driver.
+* Initial release
  </notes>
  <contents>
  <dir name="/">
@@ -34,6 +35,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
      <dir name="Lock">
       <file name="Driver.php" role="php" />
       <file name="Exception.php" role="php" />
+      <file name="Null.php" role="php" />
       <file name="Sql.php" role="php" />
      </dir> <!-- /lib/Horde/Lock -->
      <file name="Lock.php" role="php" />
@@ -50,10 +52,6 @@ http://pear.php.net/dtd/package-2.0.xsd">
     <min>1.7.0</min>
    </pearinstaller>
    <package>
-    <name>Core</name>
-    <channel>pear.horde.org</channel>
-   </package>
-   <package>
     <name>Support</name>
     <channel>pear.horde.org</channel>
    </package>
@@ -76,6 +74,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
   <filelist>
    <install name="lib/Horde/Lock/Driver.php" as="Horde/Lock/Driver.php" />
    <install name="lib/Horde/Lock/Exception.php" as="Horde/Lock/Exception.php" />
+   <install name="lib/Horde/Lock/Null.php" as="Horde/Lock/Null.php" />
    <install name="lib/Horde/Lock/Sql.php" as="Horde/Lock/Sql.php" />
    <install name="lib/Horde/Lock.php" as="Horde/Lock.php" />
   </filelist>
index f0fd854..a17c838 100644 (file)
   <configsection name="lock">
    <configheader>Lock System Settings</configheader>
    <configswitch name="driver" desc="If you want to enable resource locking
-   you must choose a driver here.">Sql
-    <case name="none" desc="Disable Horde Locks"/>
+   you must choose a driver here.">Null
+    <case name="Null" desc="Disable Horde Locks"/>
     <case name="Sql" desc="SQL-based locking">
      <configsection name="params">
       <configsql switchname="driverconfig">