You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RuntimeManager.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright 2013 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.manager;
  17. import java.io.File;
  18. import java.text.SimpleDateFormat;
  19. import java.util.Date;
  20. import java.util.Locale;
  21. import java.util.Map;
  22. import java.util.TimeZone;
  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;
  25. import com.gitblit.Constants;
  26. import com.gitblit.IStoredSettings;
  27. import com.gitblit.Keys;
  28. import com.gitblit.models.ServerSettings;
  29. import com.gitblit.models.ServerStatus;
  30. import com.gitblit.models.SettingModel;
  31. import com.gitblit.utils.StringUtils;
  32. public class RuntimeManager implements IRuntimeManager {
  33. private final Logger logger = LoggerFactory.getLogger(getClass());
  34. private final IStoredSettings settings;
  35. private final ServerStatus serverStatus;
  36. private final ServerSettings settingsModel;
  37. private File baseFolder;
  38. private TimeZone timezone;
  39. public RuntimeManager(IStoredSettings settings) {
  40. this(settings, null);
  41. }
  42. public RuntimeManager(IStoredSettings settings, File baseFolder) {
  43. this.settings = settings;
  44. this.settingsModel = new ServerSettings();
  45. this.serverStatus = new ServerStatus();
  46. this.baseFolder = baseFolder == null ? new File("") : baseFolder;
  47. }
  48. @Override
  49. public RuntimeManager start() {
  50. logger.info("Basefolder : " + baseFolder.getAbsolutePath());
  51. logger.info("Settings : " + settings.toString());
  52. logTimezone("JVM timezone: ", TimeZone.getDefault());
  53. logTimezone("App timezone: ", getTimezone());
  54. logger.info("JVM locale : " + Locale.getDefault());
  55. logger.info("App locale : " + (getLocale() == null ? "<client>" : getLocale()));
  56. return this;
  57. }
  58. @Override
  59. public RuntimeManager stop() {
  60. return this;
  61. }
  62. @Override
  63. public File getBaseFolder() {
  64. return baseFolder;
  65. }
  66. @Override
  67. public void setBaseFolder(File folder) {
  68. this.baseFolder = folder;
  69. }
  70. /**
  71. * Returns the boot date of the Gitblit server.
  72. *
  73. * @return the boot date of Gitblit
  74. */
  75. @Override
  76. public Date getBootDate() {
  77. return serverStatus.bootDate;
  78. }
  79. @Override
  80. public ServerSettings getSettingsModel() {
  81. // ensure that the current values are updated in the setting models
  82. for (String key : settings.getAllKeys(null)) {
  83. SettingModel setting = settingsModel.get(key);
  84. if (setting == null) {
  85. // unreferenced setting, create a setting model
  86. setting = new SettingModel();
  87. setting.name = key;
  88. settingsModel.add(setting);
  89. }
  90. setting.currentValue = settings.getString(key, "");
  91. }
  92. // settingsModel.pushScripts = getAllScripts();
  93. return settingsModel;
  94. }
  95. /**
  96. * Determine if this Gitblit instance is actively serving git repositories
  97. * or if it is merely a repository viewer.
  98. *
  99. * @return true if Gitblit is serving repositories
  100. */
  101. @Override
  102. public boolean isServingRepositories() {
  103. return isServingHTTP()
  104. || isServingGIT()
  105. || isServingSSH();
  106. }
  107. /**
  108. * Determine if this Gitblit instance is actively serving git repositories
  109. * over the HTTP protocol.
  110. *
  111. * @return true if Gitblit is serving repositories over the HTTP protocol
  112. */
  113. @Override
  114. public boolean isServingHTTP() {
  115. return settings.getBoolean(Keys.git.enableGitServlet, true);
  116. }
  117. /**
  118. * Determine if this Gitblit instance is actively serving git repositories
  119. * over the Git Daemon protocol.
  120. *
  121. * @return true if Gitblit is serving repositories over the Git Daemon protocol
  122. */
  123. @Override
  124. public boolean isServingGIT() {
  125. return settings.getInteger(Keys.git.daemonPort, 0) > 0;
  126. }
  127. /**
  128. * Determine if this Gitblit instance is actively serving git repositories
  129. * over the SSH protocol.
  130. *
  131. * @return true if Gitblit is serving repositories over the SSH protocol
  132. */
  133. @Override
  134. public boolean isServingSSH() {
  135. return settings.getInteger(Keys.git.sshPort, 0) > 0;
  136. }
  137. /**
  138. * Returns the preferred timezone for the Gitblit instance.
  139. *
  140. * @return a timezone
  141. */
  142. @Override
  143. public TimeZone getTimezone() {
  144. if (timezone == null) {
  145. String tzid = settings.getString(Keys.web.timezone, null);
  146. if (StringUtils.isEmpty(tzid)) {
  147. timezone = TimeZone.getDefault();
  148. return timezone;
  149. }
  150. timezone = TimeZone.getTimeZone(tzid);
  151. }
  152. return timezone;
  153. }
  154. private void logTimezone(String type, TimeZone zone) {
  155. SimpleDateFormat df = new SimpleDateFormat("z Z");
  156. df.setTimeZone(zone);
  157. String offset = df.format(new Date());
  158. logger.info("{}{} ({})", new Object [] { type, zone.getID(), offset });
  159. }
  160. @Override
  161. public Locale getLocale() {
  162. String lc = settings.getString(Keys.web.forceDefaultLocale, null);
  163. if (!StringUtils.isEmpty(lc)) {
  164. int underscore = lc.indexOf('_');
  165. if (underscore > 0) {
  166. String lang = lc.substring(0, underscore);
  167. String cc = lc.substring(underscore + 1);
  168. return new Locale(lang, cc);
  169. } else {
  170. return new Locale(lc);
  171. }
  172. }
  173. return null;
  174. }
  175. /**
  176. * Is Gitblit running in debug mode?
  177. *
  178. * @return true if Gitblit is running in debug mode
  179. */
  180. @Override
  181. public boolean isDebugMode() {
  182. return settings.getBoolean(Keys.web.debugMode, false);
  183. }
  184. /**
  185. * Returns the file object for the specified configuration key.
  186. *
  187. * @return the file
  188. */
  189. @Override
  190. public File getFileOrFolder(String key, String defaultFileOrFolder) {
  191. String fileOrFolder = settings.getString(key, defaultFileOrFolder);
  192. return getFileOrFolder(fileOrFolder);
  193. }
  194. /**
  195. * Returns the file object which may have it's base-path determined by
  196. * environment variables for running on a cloud hosting service. All Gitblit
  197. * file or folder retrievals are (at least initially) funneled through this
  198. * method so it is the correct point to globally override/alter filesystem
  199. * access based on environment or some other indicator.
  200. *
  201. * @return the file
  202. */
  203. @Override
  204. public File getFileOrFolder(String fileOrFolder) {
  205. return com.gitblit.utils.FileUtils.resolveParameter(Constants.baseFolder$,
  206. baseFolder, fileOrFolder);
  207. }
  208. /**
  209. * Returns the runtime settings.
  210. *
  211. * @return runtime settings
  212. */
  213. @Override
  214. public IStoredSettings getSettings() {
  215. return settings;
  216. }
  217. /**
  218. * Updates the runtime settings.
  219. *
  220. * @param settings
  221. * @return true if the update succeeded
  222. */
  223. @Override
  224. public boolean updateSettings(Map<String, String> updatedSettings) {
  225. return settings.saveSettings(updatedSettings);
  226. }
  227. @Override
  228. public ServerStatus getStatus() {
  229. // update heap memory status
  230. serverStatus.heapAllocated = Runtime.getRuntime().totalMemory();
  231. serverStatus.heapFree = Runtime.getRuntime().freeMemory();
  232. return serverStatus;
  233. }
  234. }