service_configs_validator.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with this
  4. * work for additional information regarding copyright ownership. The ASF
  5. * licenses this file to you under the Apache License, Version 2.0 (the
  6. * "License"); you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing permissions and limitations under
  15. * the License.
  16. */
  17. var App = require('app');
  18. App.ServiceConfigsValidator = Em.Object.extend({
  19. /**
  20. * Defaults recommended for various properties. This is to be used
  21. * by this validator to validate a property. Generally this value should be
  22. * set to values given by the defaults provider of the service.
  23. *
  24. * The key is the property name, and the value is the recommended default.
  25. */
  26. recommendedDefaults: {},
  27. /**
  28. * Per property validators where key is config name and value
  29. * is the validation function. This field is expected to be
  30. * overridden by extending classes.
  31. */
  32. configValidators: {},
  33. /**
  34. * Validate the given config property with the available
  35. *
  36. * @param config {App.ServiceConfigProperty}
  37. * @return {string} No validation issues when <code>null</code> returned.
  38. */
  39. validateConfig: function(config) {
  40. var validatorFunction = this.get('configValidators')[config.get('name')];
  41. if (validatorFunction) {
  42. return this[validatorFunction](config);
  43. }
  44. return null;
  45. },
  46. /**
  47. * Check if provided <code>config.value</code> is less than <code>recommendedDefaults</code>
  48. * @param {object} config - configProperty name
  49. * @return {string|null}
  50. */
  51. validatorLessThenDefaultValue: function(config) {
  52. var defaultValue = this.get('recommendedDefaults')[config.get('name')];
  53. var currentValue = parseInt(config.get('value').toString().replace( /\D+/g, ''));
  54. if (!defaultValue) {
  55. return null;
  56. }
  57. defaultValue = parseInt(defaultValue.toString().replace( /\D+/g, ''));
  58. if (defaultValue && currentValue && currentValue < defaultValue) {
  59. return "Value is less than the recommended default of "+defaultValue;
  60. }
  61. return null;
  62. },
  63. /**
  64. * Check if provided <code>config.value</code> is less than <code>recommendedDefaults</code>
  65. * Value looks like "-Xmx****m"
  66. * @param {object} config
  67. * @return {string|null}
  68. */
  69. validateXmxValue: function(config) {
  70. var defaultValueRaw = this.get('recommendedDefaults')[config.get('name')];
  71. var currentValueRaw = config.get('value');
  72. if (!this._checkXmxValueFormat(currentValueRaw)) {
  73. return 'Invalid value format';
  74. }
  75. var currentValue = this._formatXmxSizeToBytes(this._getXmxSize(currentValueRaw));
  76. var defaultValue = this._formatXmxSizeToBytes(this._getXmxSize(defaultValueRaw));
  77. if (currentValue < defaultValue) {
  78. return "Value is less than the recommended default of " + defaultValueRaw;
  79. }
  80. return null;
  81. },
  82. /**
  83. * Verify if provided value has proper format (should be like "-Xmx***(b|k|m|g|p|t|B|K|M|G|P|T)")
  84. * @param {string} value
  85. * @returns {boolean}
  86. * @private
  87. */
  88. _checkXmxValueFormat: function(value) {
  89. var regex = /^\-Xmx(\d+)(b|k|m|g|p|t|B|K|M|G|P|T)?(\s+)?(\s.+)?$/;
  90. if (!regex.test(value)) {
  91. return false;
  92. }
  93. // "-Xmx" can be only one
  94. return value.match(/\-Xmx/ig).length == 1;
  95. },
  96. /**
  97. * Parse Xmx size from raw value
  98. * @param {string} value
  99. * @returns {string}
  100. * @private
  101. */
  102. _getXmxSize: function(value) {
  103. var regex = /\-Xmx(\d+)(.?)/;
  104. var result = regex.exec(value);
  105. if (result.length > 1) {
  106. // result[2] - is a space or size formatter (b|k|m|g etc)
  107. return result[1] + result[2].toLowerCase();
  108. }
  109. return result[1];
  110. },
  111. /**
  112. * Calculate bytes size from value
  113. * @param {string} value
  114. * @returns {int}
  115. * @private
  116. */
  117. _formatXmxSizeToBytes: function(value) {
  118. value = value.toLowerCase();
  119. if (value.length == 0) {
  120. return 0;
  121. }
  122. var modifier = value[value.length - 1];
  123. if (modifier == ' ' || "0123456789".indexOf(modifier) != -1) {
  124. modifier = 'b';
  125. }
  126. var m = 1;
  127. switch (modifier) {
  128. case 'b': m = 1; break;
  129. case 'k': m = 1024; break;
  130. case 'm': m = 1024 * 1024; break;
  131. case 'g': m = 1024 * 1024 * 1024; break;
  132. case 't': m = 1024 * 1024 * 1024 * 1024; break;
  133. case 'p': m = 1024 * 1024 * 1024 * 1024 * 1024; break;
  134. }
  135. var result = parseInt(value.replace(modifier, '').trim());
  136. result *= m;
  137. return result;
  138. }
  139. });