File indexing completed on 2024-06-23 05:55:44

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Service_WindowsAzure
0017  * @subpackage Diagnostics
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * @see Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance
0025  */
0026 // require_once 'Zend/Service/WindowsAzure/Diagnostics/ConfigurationInstance.php';
0027 
0028 /**
0029  * @category   Zend
0030  * @package    Zend_Service_WindowsAzure
0031  * @subpackage Diagnostics
0032  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0033  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0034  */
0035 class Zend_Service_WindowsAzure_Diagnostics_Manager
0036 {
0037   /**
0038    * Blob storage client
0039    * 
0040    * @var Zend_Service_WindowsAzure_Storage_Blob
0041    */
0042   protected $_blobStorageClient = null;
0043   
0044   /**
0045    * Control container name
0046    * 
0047    * @var string
0048    */
0049   protected $_controlContainer = '';
0050 
0051   /**
0052    * Create a new instance of Zend_Service_WindowsAzure_Diagnostics_Manager
0053    * 
0054    * @param Zend_Service_WindowsAzure_Storage_Blob $blobStorageClient Blob storage client
0055    * @param string $controlContainer Control container name
0056    */
0057   public function __construct(Zend_Service_WindowsAzure_Storage_Blob $blobStorageClient = null, $controlContainer = 'wad-control-container')
0058   {
0059     $this->_blobStorageClient = $blobStorageClient;
0060     $this->_controlContainer = $controlContainer;
0061 
0062     $this->_ensureStorageInitialized();
0063   }
0064 
0065   /**
0066    * Ensure storage has been initialized
0067    */
0068   protected function _ensureStorageInitialized()
0069   {
0070     if (!$this->_blobStorageClient->containerExists($this->_controlContainer)) {
0071       $this->_blobStorageClient->createContainer($this->_controlContainer);
0072     }
0073   }
0074   
0075   /**
0076    * Get default configuration values
0077    * 
0078    * @return Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance
0079    */
0080   public function getDefaultConfiguration()
0081   {
0082     return new Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance();
0083   }
0084   
0085   /**
0086    * Checks if a configuration for a specific role instance exists.
0087    * 
0088    * @param string $roleInstance Role instance name, can be found in $_SERVER['RdRoleId'] when hosted on Windows Azure.
0089    * @return boolean
0090    * @throws Zend_Service_WindowsAzure_Diagnostics_Exception
0091    */
0092   public function configurationForRoleInstanceExists($roleInstance = null)
0093   {
0094     if (is_null($roleInstance)) {
0095       // require_once 'Zend/Service/WindowsAzure/Diagnostics/Exception.php';
0096       throw new Zend_Service_WindowsAzure_Diagnostics_Exception('Role instance should be specified. Try reading $_SERVER[\'RdRoleId\'] for this information if the application is hosted on Windows Azure Fabric or Development Fabric.');
0097     }
0098 
0099     return $this->_blobStorageClient->blobExists($this->_controlContainer, $roleInstance);
0100   }
0101   
0102   /**
0103    * Checks if a configuration for current role instance exists. Only works on Development Fabric or Windows Azure Fabric.
0104    * 
0105    * @return boolean
0106    * @throws Zend_Service_WindowsAzure_Diagnostics_Exception
0107    */
0108   public function configurationForCurrentRoleInstanceExists()
0109   {
0110     if (!isset($_SERVER['RdRoleId'])) {
0111       // require_once 'Zend/Service/WindowsAzure/Diagnostics/Exception.php';
0112       throw new Zend_Service_WindowsAzure_Diagnostics_Exception('Server variable \'RdRoleId\' is unknown. Please verify the application is running in Development Fabric or Windows Azure Fabric.');
0113     }
0114 
0115     return $this->_blobStorageClient->blobExists($this->_controlContainer, $this->_getCurrentRoleInstanceId());
0116   }
0117   
0118   /**
0119    * Get configuration for current role instance. Only works on Development Fabric or Windows Azure Fabric.
0120    * 
0121    * @return Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance
0122    * @throws Zend_Service_WindowsAzure_Diagnostics_Exception
0123    */
0124   public function getConfigurationForCurrentRoleInstance()
0125   {
0126     if (!isset($_SERVER['RdRoleId'])) {
0127       // require_once 'Zend/Service/WindowsAzure/Diagnostics/Exception.php';
0128       throw new Zend_Service_WindowsAzure_Diagnostics_Exception('Server variable \'RdRoleId\' is unknown. Please verify the application is running in Development Fabric or Windows Azure Fabric.');
0129     }
0130     return $this->getConfigurationForRoleInstance($this->_getCurrentRoleInstanceId());
0131   }
0132   
0133   /**
0134    * Get the current role instance ID. Only works on Development Fabric or Windows Azure Fabric.
0135    * 
0136    * @return string
0137    * @throws Zend_Service_WindowsAzure_Diagnostics_Exception
0138    */
0139   protected function _getCurrentRoleInstanceId()
0140   {
0141     if (!isset($_SERVER['RdRoleId'])) {
0142       // require_once 'Zend/Service/WindowsAzure/Diagnostics/Exception.php';
0143       throw new Zend_Service_WindowsAzure_Diagnostics_Exception('Server variable \'RdRoleId\' is unknown. Please verify the application is running in Development Fabric or Windows Azure Fabric.');
0144     }
0145     
0146     if (strpos($_SERVER['RdRoleId'], 'deployment(') === false) {
0147       return $_SERVER['RdRoleId'];
0148     } else {
0149       $roleIdParts = explode('.', $_SERVER['RdRoleId']);
0150       return $roleIdParts[0] . '/' . $roleIdParts[2] . '/' . $_SERVER['RdRoleId'];
0151     }
0152 
0153     if (!isset($_SERVER['RoleDeploymentID']) && !isset($_SERVER['RoleInstanceID']) && !isset($_SERVER['RoleName'])) {
0154       throw new Exception('Server variables \'RoleDeploymentID\', \'RoleInstanceID\' and \'RoleName\' are unknown. Please verify the application is running in Development Fabric or Windows Azure Fabric.');
0155     }
0156     
0157     if (strpos($_SERVER['RdRoleId'], 'deployment(') === false) {
0158       return $_SERVER['RdRoleId'];
0159     } else {
0160       return $_SERVER['RoleDeploymentID'] . '/' . $_SERVER['RoleInstanceID'] . '/' . $_SERVER['RoleName'];
0161     }
0162   }
0163   
0164   /**
0165    * Set configuration for current role instance. Only works on Development Fabric or Windows Azure Fabric.
0166    * 
0167    * @param Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance $configuration Configuration to apply
0168    * @throws Zend_Service_WindowsAzure_Diagnostics_Exception
0169    */
0170   public function setConfigurationForCurrentRoleInstance(Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance $configuration)
0171   {
0172     if (!isset($_SERVER['RdRoleId'])) {
0173       // require_once 'Zend/Service/WindowsAzure/Diagnostics/Exception.php';
0174       throw new Zend_Service_WindowsAzure_Diagnostics_Exception('Server variable \'RdRoleId\' is unknown. Please verify the application is running in Development Fabric or Windows Azure Fabric.');
0175     }
0176     
0177     $this->setConfigurationForRoleInstance($this->_getCurrentRoleInstanceId(), $configuration);
0178   }
0179   
0180   /**
0181    * Get configuration for a specific role instance
0182    * 
0183    * @param string $roleInstance Role instance name, can be found in $_SERVER['RdRoleId'] when hosted on Windows Azure.
0184    * @return Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance
0185    * @throws Zend_Service_WindowsAzure_Diagnostics_Exception
0186    */
0187   public function getConfigurationForRoleInstance($roleInstance = null)
0188   {
0189     if (is_null($roleInstance)) {
0190       // require_once 'Zend/Service/WindowsAzure/Diagnostics/Exception.php';
0191       throw new Zend_Service_WindowsAzure_Diagnostics_Exception('Role instance should be specified. Try reading $_SERVER[\'RdRoleId\'] for this information if the application is hosted on Windows Azure Fabric or Development Fabric.');
0192     }
0193 
0194     
0195     
0196     if ($this->_blobStorageClient->blobExists($this->_controlContainer, $roleInstance)) {
0197       $configurationInstance = new Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance();
0198       $configurationInstance->loadXml( $this->_blobStorageClient->getBlobData($this->_controlContainer, $roleInstance) );
0199       return $configurationInstance;
0200     }
0201 
0202     return new Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance();
0203   }
0204   
0205   /**
0206    * Set configuration for a specific role instance
0207    * 
0208    * @param string $roleInstance Role instance name, can be found in $_SERVER['RdRoleId'] when hosted on Windows Azure.
0209    * @param Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance $configuration Configuration to apply
0210    * @throws Zend_Service_WindowsAzure_Diagnostics_Exception
0211    */
0212   public function setConfigurationForRoleInstance($roleInstance = null, Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance $configuration)
0213   {
0214     if (is_null($roleInstance)) {
0215       // require_once 'Zend/Service/WindowsAzure/Diagnostics/Exception.php';
0216       throw new Zend_Service_WindowsAzure_Diagnostics_Exception('Role instance should be specified. Try reading $_SERVER[\'RdRoleId\'] for this information if the application is hosted on Windows Azure Fabric or Development Fabric.');
0217     }
0218 
0219     $this->_blobStorageClient->putBlobData($this->_controlContainer, $roleInstance, $configuration->toXml(), array(), null, array('Content-Type' => 'text/xml'));
0220   }
0221 }