web/lib/Zend/Service/WindowsAzure/Diagnostics/Manager.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Service_WindowsAzure
       
    17  * @subpackage Diagnostics
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id$
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_Service_WindowsAzure_Storage_Blob
       
    25  */
       
    26 require_once 'Zend/Service/WindowsAzure/Storage/Blob.php';
       
    27 
       
    28 /**
       
    29  * @see Zend_Service_WindowsAzure_Diagnostics_Exception
       
    30  */
       
    31 require_once 'Zend/Service/WindowsAzure/Diagnostics/Exception.php';
       
    32 
       
    33 /**
       
    34  * @see Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance
       
    35  */
       
    36 require_once 'Zend/Service/WindowsAzure/Diagnostics/ConfigurationInstance.php';
       
    37 
       
    38 /**
       
    39  * @category   Zend
       
    40  * @package    Zend_Service_WindowsAzure
       
    41  * @subpackage Diagnostics
       
    42  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    43  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    44  */
       
    45 class Zend_Service_WindowsAzure_Diagnostics_Manager
       
    46 {
       
    47 	/**
       
    48 	 * Blob storage client
       
    49 	 * 
       
    50 	 * @var Zend_Service_WindowsAzure_Storage_Blob
       
    51 	 */
       
    52 	protected $_blobStorageClient = null;
       
    53 	
       
    54 	/**
       
    55 	 * Control container name
       
    56 	 * 
       
    57 	 * @var string
       
    58 	 */
       
    59 	protected $_controlContainer = '';
       
    60 
       
    61 	/**
       
    62 	 * Create a new instance of Zend_Service_WindowsAzure_Diagnostics_Manager
       
    63 	 * 
       
    64 	 * @param Zend_Service_WindowsAzure_Storage_Blob $blobStorageClient Blob storage client
       
    65 	 * @param string $controlContainer Control container name
       
    66 	 */
       
    67 	public function __construct(Zend_Service_WindowsAzure_Storage_Blob $blobStorageClient = null, $controlContainer = 'wad-control-container')
       
    68 	{
       
    69 		$this->_blobStorageClient = $blobStorageClient;
       
    70 		$this->_controlContainer = $controlContainer;
       
    71 
       
    72 		$this->_ensureStorageInitialized();
       
    73 	}
       
    74 
       
    75 	/**
       
    76 	 * Ensure storage has been initialized
       
    77 	 */
       
    78 	protected function _ensureStorageInitialized()
       
    79 	{
       
    80 		if (!$this->_blobStorageClient->containerExists($this->_controlContainer)) {
       
    81 			$this->_blobStorageClient->createContainer($this->_controlContainer);
       
    82 		}
       
    83 	}
       
    84 	
       
    85 	/**
       
    86 	 * Get default configuration values
       
    87 	 * 
       
    88 	 * @return Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance
       
    89 	 */
       
    90 	public function getDefaultConfiguration()
       
    91 	{
       
    92 		return new Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance();
       
    93 	}
       
    94 	
       
    95 	/**
       
    96 	 * Checks if a configuration for a specific role instance exists.
       
    97 	 * 
       
    98 	 * @param string $roleInstance Role instance name, can be found in $_SERVER['RdRoleId'] when hosted on Windows Azure.
       
    99 	 * @return boolean
       
   100 	 * @throws Zend_Service_WindowsAzure_Diagnostics_Exception
       
   101 	 */
       
   102 	public function configurationForRoleInstanceExists($roleInstance = null)
       
   103 	{
       
   104 		if ($roleInstance === null) {
       
   105 			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.');
       
   106 		}
       
   107 
       
   108 		return $this->_blobStorageClient->blobExists($this->_controlContainer, $roleInstance);
       
   109 	}
       
   110 	
       
   111 	/**
       
   112 	 * Checks if a configuration for current role instance exists. Only works on Development Fabric or Windows Azure Fabric.
       
   113 	 * 
       
   114 	 * @return boolean
       
   115 	 * @throws Zend_Service_WindowsAzure_Diagnostics_Exception
       
   116 	 */
       
   117 	public function configurationForCurrentRoleInstanceExists()
       
   118 	{
       
   119 		if (!isset($_SERVER['RdRoleId'])) {
       
   120 			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.');
       
   121 		}
       
   122 
       
   123 		return $this->_blobStorageClient->blobExists($this->_controlContainer, $_SERVER['RdRoleId']);
       
   124 	}
       
   125 	
       
   126 	/**
       
   127 	 * Get configuration for current role instance. Only works on Development Fabric or Windows Azure Fabric.
       
   128 	 * 
       
   129 	 * @return Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance
       
   130 	 * @throws Zend_Service_WindowsAzure_Diagnostics_Exception
       
   131 	 */
       
   132 	public function getConfigurationForCurrentRoleInstance()
       
   133 	{
       
   134 		if (!isset($_SERVER['RdRoleId'])) {
       
   135 			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.');
       
   136 		}
       
   137 		return $this->getConfigurationForRoleInstance($_SERVER['RdRoleId']);
       
   138 	}
       
   139 	
       
   140 	/**
       
   141 	 * Set configuration for current role instance. Only works on Development Fabric or Windows Azure Fabric.
       
   142 	 * 
       
   143 	 * @param Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance $configuration Configuration to apply
       
   144 	 * @throws Zend_Service_WindowsAzure_Diagnostics_Exception
       
   145 	 */
       
   146 	public function setConfigurationForCurrentRoleInstance(Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance $configuration)
       
   147 	{
       
   148 		if (!isset($_SERVER['RdRoleId'])) {
       
   149 			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.');
       
   150 		}
       
   151 		$this->setConfigurationForRoleInstance($_SERVER['RdRoleId'], $configuration);
       
   152 	}
       
   153 	
       
   154 	/**
       
   155 	 * Get configuration for a specific role instance
       
   156 	 * 
       
   157 	 * @param string $roleInstance Role instance name, can be found in $_SERVER['RdRoleId'] when hosted on Windows Azure.
       
   158 	 * @return Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance
       
   159 	 * @throws Zend_Service_WindowsAzure_Diagnostics_Exception
       
   160 	 */
       
   161 	public function getConfigurationForRoleInstance($roleInstance = null)
       
   162 	{
       
   163 		if ($roleInstance === null) {
       
   164 			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.');
       
   165 		}
       
   166 
       
   167 		if ($this->_blobStorageClient->blobExists($this->_controlContainer, $roleInstance)) {
       
   168 			$configurationInstance = new Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance();
       
   169 			$configurationInstance->loadXml( $this->_blobStorageClient->getBlobData($this->_controlContainer, $roleInstance) );
       
   170 			return $configurationInstance;
       
   171 		}
       
   172 
       
   173 		return new Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance();
       
   174 	}
       
   175 	
       
   176 	/**
       
   177 	 * Set configuration for a specific role instance
       
   178 	 * 
       
   179 	 * @param string $roleInstance Role instance name, can be found in $_SERVER['RdRoleId'] when hosted on Windows Azure.
       
   180 	 * @param Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance $configuration Configuration to apply
       
   181 	 * @throws Zend_Service_WindowsAzure_Diagnostics_Exception
       
   182 	 */
       
   183 	public function setConfigurationForRoleInstance($roleInstance = null, Zend_Service_WindowsAzure_Diagnostics_ConfigurationInstance $configuration)
       
   184 	{
       
   185 		if ($roleInstance === null) {
       
   186 			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.');
       
   187 		}
       
   188 
       
   189 		$this->_blobStorageClient->putBlobData($this->_controlContainer, $roleInstance, $configuration->toXml(), array(), null, array('Content-Type' => 'text/xml'));
       
   190 	}
       
   191 }