2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Affero General Public License version 3 as published by the
8 * Free Software Foundation with the addition of the following permission added
9 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
18 * You should have received a copy of the GNU Affero General Public License along with
19 * this program; if not, see http://www.gnu.org/licenses or write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26 * The interactive user interfaces in modified source and object code versions
27 * of this program must display Appropriate Legal Notices, as required under
28 * Section 5 of the GNU Affero General Public License version 3.
30 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31 * these Appropriate Legal Notices must retain the display of the "Powered by
32 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33 * technical reasons, the Appropriate Legal Notices must display the words
34 * "Powered by SugarCRM".
35 ********************************************************************************/
40 * This class is responsible for resource management of SQL queries, file usage, etc.
42 class ResourceManager {
44 private static $instance;
45 private $_observers = array();
48 * The constructor; declared as private
50 private function ResourceManager() {
56 * Singleton method to return static instance of ResourceManager
57 * @return The static singleton ResourceManager instance
59 static public function getInstance(){
60 if (!isset(self::$instance)) {
61 self::$instance = new ResourceManager();
63 return self::$instance;
68 * Handles determining the appropriate setup based on client type.
69 * It will create a SoapResourceObserver instance if the $module parameter is set to
70 * 'Soap'; otherwise, it will try to create a WebResourceObserver instance.
71 * @param $module The module value used to create the corresponding observer
72 * @return boolean value indicating whether or not an observer was successfully setup
74 public function setup($module) {
75 //Check if config.php exists
76 if(!file_exists('config.php') || empty($module)) {
80 if($module == 'Soap') {
81 require_once('include/resource/Observers/SoapResourceObserver.php');
82 $observer = new SoapResourceObserver('Soap');
84 require_once('include/resource/Observers/WebResourceObserver.php');
85 $observer = new WebResourceObserver($module);
89 if(!empty($observer->module)) {
92 if(isset($GLOBALS['sugar_config']['resource_management'])) {
93 $res = $GLOBALS['sugar_config']['resource_management'];
94 if(!empty($res['special_query_modules']) &&
95 in_array($observer->module, $res['special_query_modules']) &&
96 !empty($res['special_query_limit']) &&
97 is_int($res['special_query_limit']) &&
98 $res['special_query_limit'] > 0) {
99 $limit = $res['special_query_limit'];
100 } else if(!empty($res['default_limit']) && is_int($res['default_limit']) && $res['default_limit'] > 0) {
101 $limit = $res['default_limit'];
107 $db = DBManagerFactory::getInstance();
108 $db->setQueryLimit($limit);
109 $observer->setLimit($limit);
110 $this->_observers[] = $observer;
120 * This method notifies the registered observers with the provided message.
121 * @param $msg Message from language file to notify observers with
123 public function notifyObservers($msg) {
125 if(empty($this->_observers)) {
129 //Notify observers limit has been reached
130 if(empty($GLOBALS['app_strings'])) {
131 $GLOBALS['app_strings'] = return_application_language($GLOBALS['current_language']);
133 $limitMsg = $GLOBALS['app_strings'][$msg];
134 foreach( $this->_observers as $observer) {
135 $limit = $observer->limit;
136 $module = $observer->module;
137 eval("\$limitMsg = \"$limitMsg\";");
138 $GLOBALS['log']->fatal($limitMsg);
139 $observer->notify($limitMsg);
146 * Returns the observer instances that have been setup for the ResourceManager instance
147 * @return Array of ResourceObserver(s)
149 function getObservers() {
150 return $this->_observers;