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.

AbstractDeploymentConfiguration.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright 2011 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.lang.reflect.Constructor;
  18. import java.util.Iterator;
  19. import java.util.Properties;
  20. import java.util.ServiceLoader;
  21. import java.util.logging.Logger;
  22. public abstract class AbstractDeploymentConfiguration implements
  23. DeploymentConfiguration {
  24. private final Class<?> systemPropertyBaseClass;
  25. private final Properties applicationProperties;
  26. private AddonContext addonContext;
  27. private boolean productionMode;
  28. private boolean xsrfProtectionEnabled;
  29. private int resourceCacheTime;
  30. private int heartbeatInterval;
  31. private boolean idleRootCleanupEnabled;
  32. public AbstractDeploymentConfiguration(Class<?> systemPropertyBaseClass,
  33. Properties applicationProperties) {
  34. this.systemPropertyBaseClass = systemPropertyBaseClass;
  35. this.applicationProperties = applicationProperties;
  36. checkProductionMode();
  37. checkXsrfProtection();
  38. checkResourceCacheTime();
  39. checkHeartbeatInterval();
  40. checkIdleUICleanup();
  41. }
  42. @Override
  43. public String getApplicationOrSystemProperty(String propertyName,
  44. String defaultValue) {
  45. String val = null;
  46. // Try application properties
  47. val = getApplicationProperty(propertyName);
  48. if (val != null) {
  49. return val;
  50. }
  51. // Try system properties
  52. val = getSystemProperty(propertyName);
  53. if (val != null) {
  54. return val;
  55. }
  56. return defaultValue;
  57. }
  58. /**
  59. * Gets an system property value.
  60. *
  61. * @param parameterName
  62. * the Name or the parameter.
  63. * @return String value or null if not found
  64. */
  65. protected String getSystemProperty(String parameterName) {
  66. String val = null;
  67. String pkgName;
  68. final Package pkg = systemPropertyBaseClass.getPackage();
  69. if (pkg != null) {
  70. pkgName = pkg.getName();
  71. } else {
  72. final String className = systemPropertyBaseClass.getName();
  73. pkgName = new String(className.toCharArray(), 0,
  74. className.lastIndexOf('.'));
  75. }
  76. val = System.getProperty(pkgName + "." + parameterName);
  77. if (val != null) {
  78. return val;
  79. }
  80. // Try lowercased system properties
  81. val = System.getProperty(pkgName + "." + parameterName.toLowerCase());
  82. return val;
  83. }
  84. @Override
  85. public ClassLoader getClassLoader() {
  86. final String classLoaderName = getApplicationOrSystemProperty(
  87. "ClassLoader", null);
  88. ClassLoader classLoader;
  89. if (classLoaderName == null) {
  90. classLoader = getClass().getClassLoader();
  91. } else {
  92. try {
  93. final Class<?> classLoaderClass = getClass().getClassLoader()
  94. .loadClass(classLoaderName);
  95. final Constructor<?> c = classLoaderClass
  96. .getConstructor(new Class[] { ClassLoader.class });
  97. classLoader = (ClassLoader) c
  98. .newInstance(new Object[] { getClass().getClassLoader() });
  99. } catch (final Exception e) {
  100. throw new RuntimeException(
  101. "Could not find specified class loader: "
  102. + classLoaderName, e);
  103. }
  104. }
  105. return classLoader;
  106. }
  107. /**
  108. * Gets an application property value.
  109. *
  110. * @param parameterName
  111. * the Name or the parameter.
  112. * @return String value or null if not found
  113. */
  114. protected String getApplicationProperty(String parameterName) {
  115. String val = applicationProperties.getProperty(parameterName);
  116. if (val != null) {
  117. return val;
  118. }
  119. // Try lower case application properties for backward compatibility with
  120. // 3.0.2 and earlier
  121. val = applicationProperties.getProperty(parameterName.toLowerCase());
  122. return val;
  123. }
  124. @Override
  125. public Properties getInitParameters() {
  126. return applicationProperties;
  127. }
  128. @Override
  129. public Iterator<AddonContextListener> getAddonContextListeners() {
  130. // Called once for init and then no more, so there's no point in caching
  131. // the instance
  132. ServiceLoader<AddonContextListener> contextListenerLoader = ServiceLoader
  133. .load(AddonContextListener.class, getClassLoader());
  134. return contextListenerLoader.iterator();
  135. }
  136. @Override
  137. public void setAddonContext(AddonContext addonContext) {
  138. this.addonContext = addonContext;
  139. }
  140. @Override
  141. public AddonContext getAddonContext() {
  142. return addonContext;
  143. }
  144. /**
  145. * {@inheritDoc}
  146. *
  147. * The default is false.
  148. */
  149. @Override
  150. public boolean isProductionMode() {
  151. return productionMode;
  152. }
  153. /**
  154. * {@inheritDoc}
  155. *
  156. * The default is true.
  157. */
  158. @Override
  159. public boolean isXsrfProtectionEnabled() {
  160. return xsrfProtectionEnabled;
  161. }
  162. /**
  163. * {@inheritDoc}
  164. *
  165. * The default interval is 3600 seconds (1 hour).
  166. */
  167. @Override
  168. public int getResourceCacheTime() {
  169. return resourceCacheTime;
  170. }
  171. /**
  172. * {@inheritDoc}
  173. *
  174. * The default interval is 300 seconds (5 minutes).
  175. */
  176. @Override
  177. public int getHeartbeatInterval() {
  178. return heartbeatInterval;
  179. }
  180. @Override
  181. public boolean isIdleUICleanupEnabled() {
  182. return idleRootCleanupEnabled;
  183. }
  184. /**
  185. * Log a warning if Vaadin is not running in production mode.
  186. */
  187. private void checkProductionMode() {
  188. productionMode = getApplicationOrSystemProperty(
  189. Constants.SERVLET_PARAMETER_PRODUCTION_MODE, "false").equals(
  190. "true");
  191. if (!productionMode) {
  192. getLogger().warning(Constants.NOT_PRODUCTION_MODE_INFO);
  193. }
  194. }
  195. /**
  196. * Log a warning if cross-site request forgery protection is disabled.
  197. */
  198. private void checkXsrfProtection() {
  199. xsrfProtectionEnabled = !getApplicationOrSystemProperty(
  200. Constants.SERVLET_PARAMETER_DISABLE_XSRF_PROTECTION, "false")
  201. .equals("true");
  202. if (!xsrfProtectionEnabled) {
  203. getLogger().warning(Constants.WARNING_XSRF_PROTECTION_DISABLED);
  204. }
  205. }
  206. /**
  207. * Log a warning if resource cache time is set but is not an integer.
  208. */
  209. private void checkResourceCacheTime() {
  210. try {
  211. resourceCacheTime = Integer
  212. .parseInt(getApplicationOrSystemProperty(
  213. Constants.SERVLET_PARAMETER_RESOURCE_CACHE_TIME,
  214. "3600"));
  215. } catch (NumberFormatException e) {
  216. getLogger().warning(
  217. Constants.WARNING_RESOURCE_CACHING_TIME_NOT_NUMERIC);
  218. resourceCacheTime = 3600;
  219. }
  220. }
  221. private void checkHeartbeatInterval() {
  222. try {
  223. heartbeatInterval = Integer
  224. .parseInt(getApplicationOrSystemProperty(
  225. Constants.SERVLET_PARAMETER_HEARTBEAT_RATE, "300"));
  226. } catch (NumberFormatException e) {
  227. getLogger().warning(
  228. Constants.WARNING_HEARTBEAT_INTERVAL_NOT_NUMERIC);
  229. heartbeatInterval = 300;
  230. }
  231. }
  232. private void checkIdleUICleanup() {
  233. idleRootCleanupEnabled = getApplicationOrSystemProperty(
  234. Constants.SERVLET_PARAMETER_CLOSE_IDLE_UIS, "false").equals(
  235. "true");
  236. }
  237. private Logger getLogger() {
  238. return Logger.getLogger(getClass().getName());
  239. }
  240. }