PageRenderTime 164ms CodeModel.GetById 91ms app.highlight 6ms RepoModel.GetById 64ms app.codeStats 0ms

/vendor/Microsoft/WindowsAzure/Diagnostics/Manager.php

https://bitbucket.org/ktos/tinyshare
PHP | 225 lines | 90 code | 23 blank | 112 comment | 16 complexity | 0bbbb3a516a4bb4b72fac43107eea1e3 MD5 | raw file
  1<?php
  2/**
  3 * Copyright (c) 2009 - 2011, RealDolmen
  4 * All rights reserved.
  5 *
  6 * Redistribution and use in source and binary forms, with or without
  7 * modification, are permitted provided that the following conditions are met:
  8 *     * Redistributions of source code must retain the above copyright
  9 *       notice, this list of conditions and the following disclaimer.
 10 *     * Redistributions in binary form must reproduce the above copyright
 11 *       notice, this list of conditions and the following disclaimer in the
 12 *       documentation and/or other materials provided with the distribution.
 13 *     * Neither the name of RealDolmen nor the
 14 *       names of its contributors may be used to endorse or promote products
 15 *       derived from this software without specific prior written permission.
 16 *
 17 * THIS SOFTWARE IS PROVIDED BY RealDolmen ''AS IS'' AND ANY
 18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 20 * DISCLAIMED. IN NO EVENT SHALL RealDolmen BE LIABLE FOR ANY
 21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 26 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 27 *
 28 * @category   Microsoft
 29 * @package    Microsoft_WindowsAzure
 30 * @subpackage Diagnostics
 31 * @copyright  Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
 32 * @license    http://phpazure.codeplex.com/license
 33 * @version    $Id: Storage.php 45989 2010-05-03 12:19:10Z unknown $
 34 */
 35
 36/**
 37 * @see Microsoft_AutoLoader
 38 */
 39require_once dirname(__FILE__) . '/../../AutoLoader.php';
 40
 41/**
 42 * @category   Microsoft
 43 * @package    Microsoft_WindowsAzure
 44 * @subpackage Diagnostics
 45 * @copyright  Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
 46 * @license    http://phpazure.codeplex.com/license
 47 */
 48class Microsoft_WindowsAzure_Diagnostics_Manager
 49{
 50	/**
 51	 * Blob storage client
 52	 * 
 53	 * @var Microsoft_WindowsAzure_Storage_Blob
 54	 */
 55	protected $_blobStorageClient = null;
 56	
 57	/**
 58	 * Control container name
 59	 * 
 60	 * @var string
 61	 */
 62	protected $_controlContainer = '';
 63
 64	/**
 65	 * Create a new instance of Microsoft_WindowsAzure_Diagnostics_Manager
 66	 * 
 67	 * @param Microsoft_WindowsAzure_Storage_Blob $blobStorageClient Blob storage client
 68	 * @param string $controlContainer Control container name
 69	 */
 70	public function __construct(Microsoft_WindowsAzure_Storage_Blob $blobStorageClient = null, $controlContainer = 'wad-control-container')
 71	{
 72		$this->_blobStorageClient = $blobStorageClient;
 73		$this->_controlContainer = $controlContainer;
 74
 75		$this->_ensureStorageInitialized();
 76	}
 77
 78	/**
 79	 * Ensure storage has been initialized
 80	 */
 81	protected function _ensureStorageInitialized()
 82	{
 83		if (!$this->_blobStorageClient->containerExists($this->_controlContainer)) {
 84			$this->_blobStorageClient->createContainer($this->_controlContainer);
 85		}
 86	}
 87	
 88	/**
 89	 * Get default configuration values
 90	 * 
 91	 * @return Microsoft_WindowsAzure_Diagnostics_ConfigurationInstance
 92	 */
 93	public function getDefaultConfiguration()
 94	{
 95		return new Microsoft_WindowsAzure_Diagnostics_ConfigurationInstance();
 96	}
 97	
 98	/**
 99	 * Checks if a configuration for a specific role instance exists.
100	 * 
101	 * @param string $roleInstance Role instance name, can be found in $_SERVER['RdRoleId'] when hosted on Windows Azure.
102	 * @return boolean
103	 * @throws Microsoft_WindowsAzure_Diagnostics_Exception
104	 */
105	public function configurationForRoleInstanceExists($roleInstance = null)
106	{
107		if (is_null($roleInstance)) {
108			throw new Microsoft_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.');
109		}
110
111		return $this->_blobStorageClient->blobExists($this->_controlContainer, $roleInstance);
112	}
113	
114	/**
115	 * Checks if a configuration for current role instance exists. Only works on Development Fabric or Windows Azure Fabric.
116	 * 
117	 * @return boolean
118	 * @throws Microsoft_WindowsAzure_Diagnostics_Exception
119	 */
120	public function configurationForCurrentRoleInstanceExists()
121	{
122		if (!isset($_SERVER['RdRoleId'])) {
123			throw new Microsoft_WindowsAzure_Diagnostics_Exception('Server variable \'RdRoleId\' is unknown. Please verify the application is running in Development Fabric or Windows Azure Fabric.');
124		}
125
126		return $this->_blobStorageClient->blobExists($this->_controlContainer, $this->_getCurrentRoleInstanceId());
127	}
128	
129	/**
130	 * Get configuration for current role instance. Only works on Development Fabric or Windows Azure Fabric.
131	 * 
132	 * @return Microsoft_WindowsAzure_Diagnostics_ConfigurationInstance
133	 * @throws Microsoft_WindowsAzure_Diagnostics_Exception
134	 */
135	public function getConfigurationForCurrentRoleInstance()
136	{
137		if (!isset($_SERVER['RdRoleId'])) {
138			throw new Microsoft_WindowsAzure_Diagnostics_Exception('Server variable \'RdRoleId\' is unknown. Please verify the application is running in Development Fabric or Windows Azure Fabric.');
139		}
140		return $this->getConfigurationForRoleInstance($this->_getCurrentRoleInstanceId());
141	}
142	
143	/**
144	 * Get the current role instance ID. Only works on Development Fabric or Windows Azure Fabric.
145	 * 
146	 * @return string
147	 * @throws Microsoft_WindowsAzure_Diagnostics_Exception
148	 */
149	protected function _getCurrentRoleInstanceId()
150	{
151		if (!isset($_SERVER['RdRoleId'])) {
152			throw new Microsoft_WindowsAzure_Diagnostics_Exception('Server variable \'RdRoleId\' is unknown. Please verify the application is running in Development Fabric or Windows Azure Fabric.');
153		}
154		
155		if (strpos($_SERVER['RdRoleId'], 'deployment(') === false) {
156			return $_SERVER['RdRoleId'];
157		} else {
158			$roleIdParts = explode('.', $_SERVER['RdRoleId']);
159			return $roleIdParts[0] . '/' . $roleIdParts[2] . '/' . $_SERVER['RdRoleId'];
160		}
161
162		if (!isset($_SERVER['RoleDeploymentID']) && !isset($_SERVER['RoleInstanceID']) && !isset($_SERVER['RoleName'])) {
163			throw new Exception('Server variables \'RoleDeploymentID\', \'RoleInstanceID\' and \'RoleName\' are unknown. Please verify the application is running in Development Fabric or Windows Azure Fabric.');
164		}
165		
166		if (strpos($_SERVER['RdRoleId'], 'deployment(') === false) {
167			return $_SERVER['RdRoleId'];
168		} else {
169			return $_SERVER['RoleDeploymentID'] . '/' . $_SERVER['RoleInstanceID'] . '/' . $_SERVER['RoleName'];
170		}
171	}
172	
173	/**
174	 * Set configuration for current role instance. Only works on Development Fabric or Windows Azure Fabric.
175	 * 
176	 * @param Microsoft_WindowsAzure_Diagnostics_ConfigurationInstance $configuration Configuration to apply
177	 * @throws Microsoft_WindowsAzure_Diagnostics_Exception
178	 */
179	public function setConfigurationForCurrentRoleInstance(Microsoft_WindowsAzure_Diagnostics_ConfigurationInstance $configuration)
180	{
181		if (!isset($_SERVER['RdRoleId'])) {
182			throw new Microsoft_WindowsAzure_Diagnostics_Exception('Server variable \'RdRoleId\' is unknown. Please verify the application is running in Development Fabric or Windows Azure Fabric.');
183		}
184		
185		$this->setConfigurationForRoleInstance($this->_getCurrentRoleInstanceId(), $configuration);
186	}
187	
188	/**
189	 * Get configuration for a specific role instance
190	 * 
191	 * @param string $roleInstance Role instance name, can be found in $_SERVER['RdRoleId'] when hosted on Windows Azure.
192	 * @return Microsoft_WindowsAzure_Diagnostics_ConfigurationInstance
193	 * @throws Microsoft_WindowsAzure_Diagnostics_Exception
194	 */
195	public function getConfigurationForRoleInstance($roleInstance = null)
196	{
197		if (is_null($roleInstance)) {
198			throw new Microsoft_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.');
199		}
200
201		if ($this->_blobStorageClient->blobExists($this->_controlContainer, $roleInstance)) {
202			$configurationInstance = new Microsoft_WindowsAzure_Diagnostics_ConfigurationInstance();
203			$configurationInstance->loadXml( $this->_blobStorageClient->getBlobData($this->_controlContainer, $roleInstance) );
204			return $configurationInstance;
205		}
206
207		return new Microsoft_WindowsAzure_Diagnostics_ConfigurationInstance();
208	}
209	
210	/**
211	 * Set configuration for a specific role instance
212	 * 
213	 * @param string $roleInstance Role instance name, can be found in $_SERVER['RdRoleId'] when hosted on Windows Azure.
214	 * @param Microsoft_WindowsAzure_Diagnostics_ConfigurationInstance $configuration Configuration to apply
215	 * @throws Microsoft_WindowsAzure_Diagnostics_Exception
216	 */
217	public function setConfigurationForRoleInstance($roleInstance = null, Microsoft_WindowsAzure_Diagnostics_ConfigurationInstance $configuration)
218	{
219		if (is_null($roleInstance)) {
220			throw new Microsoft_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.');
221		}
222
223		$this->_blobStorageClient->putBlobData($this->_controlContainer, $roleInstance, $configuration->toXml(), array(), null, array('Content-Type' => 'text/xml'));
224	}
225}