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.

DefaultDeploymentConfiguration.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * 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, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.server;
  17. import java.util.Properties;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20. import com.vaadin.shared.communication.PushMode;
  21. /**
  22. * The default implementation of {@link DeploymentConfiguration} based on a base
  23. * class for resolving system properties and a set of init parameters.
  24. *
  25. * @author Vaadin Ltd
  26. * @since 7.0.0
  27. */
  28. public class DefaultDeploymentConfiguration extends
  29. AbstractDeploymentConfiguration {
  30. /**
  31. * Default value for {@link #getResourceCacheTime()} = {@value} .
  32. */
  33. public static final int DEFAULT_RESOURCE_CACHE_TIME = 3600;
  34. /**
  35. * Default value for {@link #getHeartbeatInterval()} = {@value} .
  36. */
  37. public static final int DEFAULT_HEARTBEAT_INTERVAL = 300;
  38. /**
  39. * Default value for {@link #isCloseIdleSessions()} = {@value} .
  40. */
  41. public static final boolean DEFAULT_CLOSE_IDLE_SESSIONS = false;
  42. /**
  43. * Default value for {@link #getLegacyPropertyToStringMode()} =
  44. * {@link LegacyProperyToStringMode#WARNING}.
  45. */
  46. public static final LegacyProperyToStringMode DEFAULT_LEGACY_PROPERTY_TO_STRING = LegacyProperyToStringMode.WARNING;
  47. /**
  48. * Default value for {@link #isSyncIdCheckEnabled()} = {@value} .
  49. *
  50. * @since 7.3
  51. */
  52. public static final boolean DEFAULT_SYNC_ID_CHECK = true;
  53. public static final boolean DEFAULT_SEND_URLS_AS_PARAMETERS = true;
  54. private final Properties initParameters;
  55. private boolean productionMode;
  56. private boolean xsrfProtectionEnabled;
  57. private int resourceCacheTime;
  58. private int heartbeatInterval;
  59. private boolean closeIdleSessions;
  60. private PushMode pushMode;
  61. private final Class<?> systemPropertyBaseClass;
  62. private LegacyProperyToStringMode legacyPropertyToStringMode;
  63. private boolean syncIdCheck;
  64. private boolean sendUrlsAsParameters;
  65. /**
  66. * Create a new deployment configuration instance.
  67. *
  68. * @param systemPropertyBaseClass
  69. * the class that should be used as a basis when reading system
  70. * properties
  71. * @param initParameters
  72. * the init parameters that should make up the foundation for
  73. * this configuration
  74. */
  75. public DefaultDeploymentConfiguration(Class<?> systemPropertyBaseClass,
  76. Properties initParameters) {
  77. this.initParameters = initParameters;
  78. this.systemPropertyBaseClass = systemPropertyBaseClass;
  79. checkProductionMode();
  80. checkXsrfProtection();
  81. checkResourceCacheTime();
  82. checkHeartbeatInterval();
  83. checkCloseIdleSessions();
  84. checkPushMode();
  85. checkLegacyPropertyToString();
  86. checkSyncIdCheck();
  87. checkSendUrlsAsParameters();
  88. }
  89. private void checkLegacyPropertyToString() {
  90. String param = getApplicationOrSystemProperty(
  91. Constants.SERVLET_PARAMETER_LEGACY_PROPERTY_TOSTRING,
  92. DEFAULT_LEGACY_PROPERTY_TO_STRING.getPropertyString());
  93. for (LegacyProperyToStringMode mode : LegacyProperyToStringMode
  94. .values()) {
  95. if (mode.getPropertyString().equals(param)) {
  96. legacyPropertyToStringMode = mode;
  97. return;
  98. }
  99. }
  100. getLogger()
  101. .log(Level.WARNING,
  102. Constants.WARNING_UNKNOWN_LEGACY_PROPERTY_TOSTRING_VALUE,
  103. param);
  104. legacyPropertyToStringMode = DEFAULT_LEGACY_PROPERTY_TO_STRING;
  105. }
  106. @Override
  107. public String getApplicationOrSystemProperty(String propertyName,
  108. String defaultValue) {
  109. String val = null;
  110. // Try application properties
  111. val = getApplicationProperty(propertyName);
  112. if (val != null) {
  113. return val;
  114. }
  115. // Try system properties
  116. val = getSystemProperty(propertyName);
  117. if (val != null) {
  118. return val;
  119. }
  120. return defaultValue;
  121. }
  122. /**
  123. * Gets an system property value.
  124. *
  125. * @param parameterName
  126. * the Name or the parameter.
  127. * @return String value or null if not found
  128. */
  129. protected String getSystemProperty(String parameterName) {
  130. String val = null;
  131. String pkgName;
  132. final Package pkg = systemPropertyBaseClass.getPackage();
  133. if (pkg != null) {
  134. pkgName = pkg.getName();
  135. } else {
  136. final String className = systemPropertyBaseClass.getName();
  137. int index = className.lastIndexOf('.');
  138. if (index >= 0) {
  139. pkgName = className.substring(0, index);
  140. } else {
  141. pkgName = null;
  142. }
  143. }
  144. if (pkgName == null) {
  145. pkgName = "";
  146. } else {
  147. pkgName += '.';
  148. }
  149. val = System.getProperty(pkgName + parameterName);
  150. if (val != null) {
  151. return val;
  152. }
  153. // Try lowercased system properties
  154. val = System.getProperty(pkgName + parameterName.toLowerCase());
  155. return val;
  156. }
  157. /**
  158. * Gets an application property value.
  159. *
  160. * @param parameterName
  161. * the Name or the parameter.
  162. * @return String value or null if not found
  163. */
  164. public String getApplicationProperty(String parameterName) {
  165. String val = initParameters.getProperty(parameterName);
  166. if (val != null) {
  167. return val;
  168. }
  169. // Try lower case application properties for backward compatibility with
  170. // 3.0.2 and earlier
  171. val = initParameters.getProperty(parameterName.toLowerCase());
  172. return val;
  173. }
  174. /**
  175. * {@inheritDoc}
  176. *
  177. * The default is false.
  178. */
  179. @Override
  180. public boolean isProductionMode() {
  181. return productionMode;
  182. }
  183. /**
  184. * {@inheritDoc}
  185. * <p>
  186. * The default is true.
  187. */
  188. @Override
  189. public boolean isXsrfProtectionEnabled() {
  190. return xsrfProtectionEnabled;
  191. }
  192. /**
  193. * {@inheritDoc}
  194. * <p>
  195. * The default interval is 3600 seconds (1 hour).
  196. */
  197. @Override
  198. public int getResourceCacheTime() {
  199. return resourceCacheTime;
  200. }
  201. /**
  202. * {@inheritDoc}
  203. * <p>
  204. * The default interval is 300 seconds (5 minutes).
  205. */
  206. @Override
  207. public int getHeartbeatInterval() {
  208. return heartbeatInterval;
  209. }
  210. /**
  211. * {@inheritDoc}
  212. * <p>
  213. * The default value is false.
  214. */
  215. @Override
  216. public boolean isCloseIdleSessions() {
  217. return closeIdleSessions;
  218. }
  219. /**
  220. * {@inheritDoc}
  221. * <p>
  222. * The default value is <code>true</code>.
  223. */
  224. @Override
  225. public boolean isSyncIdCheckEnabled() {
  226. return syncIdCheck;
  227. }
  228. /**
  229. * {@inheritDoc}
  230. * <p>
  231. * The default value is <code>true</code>.
  232. */
  233. @Override
  234. public boolean isSendUrlsAsParameters() {
  235. return sendUrlsAsParameters;
  236. }
  237. /**
  238. * {@inheritDoc}
  239. * <p>
  240. * The default mode is {@link PushMode#DISABLED}.
  241. */
  242. @Override
  243. public PushMode getPushMode() {
  244. return pushMode;
  245. }
  246. @Override
  247. public Properties getInitParameters() {
  248. return initParameters;
  249. }
  250. /**
  251. * Log a warning if Vaadin is not running in production mode.
  252. */
  253. private void checkProductionMode() {
  254. productionMode = getApplicationOrSystemProperty(
  255. Constants.SERVLET_PARAMETER_PRODUCTION_MODE, "false").equals(
  256. "true");
  257. if (!productionMode) {
  258. getLogger().warning(Constants.NOT_PRODUCTION_MODE_INFO);
  259. }
  260. }
  261. /**
  262. * Log a warning if cross-site request forgery protection is disabled.
  263. */
  264. private void checkXsrfProtection() {
  265. xsrfProtectionEnabled = !getApplicationOrSystemProperty(
  266. Constants.SERVLET_PARAMETER_DISABLE_XSRF_PROTECTION, "false")
  267. .equals("true");
  268. if (!xsrfProtectionEnabled) {
  269. getLogger().warning(Constants.WARNING_XSRF_PROTECTION_DISABLED);
  270. }
  271. }
  272. /**
  273. * Log a warning if resource cache time is set but is not an integer.
  274. */
  275. private void checkResourceCacheTime() {
  276. try {
  277. resourceCacheTime = Integer
  278. .parseInt(getApplicationOrSystemProperty(
  279. Constants.SERVLET_PARAMETER_RESOURCE_CACHE_TIME,
  280. Integer.toString(DEFAULT_RESOURCE_CACHE_TIME)));
  281. } catch (NumberFormatException e) {
  282. getLogger().warning(
  283. Constants.WARNING_RESOURCE_CACHING_TIME_NOT_NUMERIC);
  284. resourceCacheTime = DEFAULT_RESOURCE_CACHE_TIME;
  285. }
  286. }
  287. private void checkHeartbeatInterval() {
  288. try {
  289. heartbeatInterval = Integer
  290. .parseInt(getApplicationOrSystemProperty(
  291. Constants.SERVLET_PARAMETER_HEARTBEAT_INTERVAL,
  292. Integer.toString(DEFAULT_HEARTBEAT_INTERVAL)));
  293. } catch (NumberFormatException e) {
  294. getLogger().warning(
  295. Constants.WARNING_HEARTBEAT_INTERVAL_NOT_NUMERIC);
  296. heartbeatInterval = DEFAULT_HEARTBEAT_INTERVAL;
  297. }
  298. }
  299. private void checkCloseIdleSessions() {
  300. closeIdleSessions = getApplicationOrSystemProperty(
  301. Constants.SERVLET_PARAMETER_CLOSE_IDLE_SESSIONS,
  302. Boolean.toString(DEFAULT_CLOSE_IDLE_SESSIONS)).equals("true");
  303. }
  304. private void checkPushMode() {
  305. String mode = getApplicationOrSystemProperty(
  306. Constants.SERVLET_PARAMETER_PUSH_MODE,
  307. PushMode.DISABLED.toString());
  308. try {
  309. pushMode = Enum.valueOf(PushMode.class, mode.toUpperCase());
  310. } catch (IllegalArgumentException e) {
  311. getLogger().warning(Constants.WARNING_PUSH_MODE_NOT_RECOGNIZED);
  312. pushMode = PushMode.DISABLED;
  313. }
  314. }
  315. private void checkSyncIdCheck() {
  316. syncIdCheck = getApplicationOrSystemProperty(
  317. Constants.SERVLET_PARAMETER_SYNC_ID_CHECK,
  318. Boolean.toString(DEFAULT_SYNC_ID_CHECK)).equals("true");
  319. }
  320. private void checkSendUrlsAsParameters() {
  321. sendUrlsAsParameters = getApplicationOrSystemProperty(
  322. Constants.SERVLET_PARAMETER_SENDURLSASPARAMETERS,
  323. Boolean.toString(DEFAULT_SEND_URLS_AS_PARAMETERS)).equals(
  324. "true");
  325. }
  326. private Logger getLogger() {
  327. return Logger.getLogger(getClass().getName());
  328. }
  329. @Override
  330. @Deprecated
  331. public LegacyProperyToStringMode getLegacyPropertyToStringMode() {
  332. return legacyPropertyToStringMode;
  333. }
  334. }