1. /*
  2. * Copyright 2002-2004 the original author or authors.
  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 org.springframework.web.servlet.view.velocity;
  17. import java.io.IOException;
  18. import org.apache.velocity.app.VelocityEngine;
  19. import org.apache.velocity.exception.VelocityException;
  20. import org.springframework.beans.factory.InitializingBean;
  21. import org.springframework.context.ResourceLoaderAware;
  22. import org.springframework.ui.velocity.VelocityEngineFactory;
  23. /**
  24. * JavaBean to configure Velocity for web usage, via the "configLocation"
  25. * and/or "velocityProperties" and/or "resourceLoaderPath" bean properties.
  26. * The simplest way to use this class is to specify just a "resourceLoaderPath":
  27. * You do not need any further configuration then.
  28. *
  29. * <pre>
  30. * <bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
  31. * <property name="resourceLoaderPath"><value>/WEB-INF/velocity/</value></property>
  32. * </bean></pre>
  33. *
  34. * This bean must be included in the application context of any application
  35. * using Spring's VelocityView for web MVC. It exists purely to configure Velocity.
  36. * It is not meant to be referenced by application components but just internally
  37. * by VelocityView. Implements VelocityConfig to be found by VelocityView without
  38. * depending on the bean name the configurer. Each DispatcherServlet can define its
  39. * own VelocityConfigurer if desired.
  40. *
  41. * <p>Note that you can also refer to a preconfigured VelocityEngine instance, for
  42. * example one set up by VelocityEngineFactoryBean, via the "velocityEngine" property.
  43. * This allows to shared a VelocityEngine for web and email usage, for example.
  44. *
  45. * @author Rod Johnson
  46. * @author Juergen Hoeller
  47. * @version $Id: VelocityConfigurer.java,v 1.14 2004/04/22 07:58:27 jhoeller Exp $
  48. * @see #setConfigLocation
  49. * @see #setVelocityProperties
  50. * @see #setResourceLoaderPath
  51. * @see #setVelocityEngine
  52. * @see org.springframework.ui.velocity.VelocityEngineFactoryBean
  53. * @see VelocityView
  54. */
  55. public class VelocityConfigurer extends VelocityEngineFactory
  56. implements VelocityConfig, InitializingBean, ResourceLoaderAware {
  57. private VelocityEngine velocityEngine;
  58. /**
  59. * Set a preconfigured VelocityEngine to use for the Velocity web config, e.g.
  60. * a shared one for web and email usage, set up via VelocityEngineFactoryBean.
  61. * If this is not set, VelocityEngineFactory's properties (inherited by this
  62. * class) have to be specified.
  63. * @see org.springframework.ui.velocity.VelocityEngineFactoryBean
  64. */
  65. public void setVelocityEngine(VelocityEngine velocityEngine) {
  66. this.velocityEngine = velocityEngine;
  67. }
  68. /**
  69. * Initialize VelocityEngineFactory's VelocityEngine
  70. * if not overridden by a preconfigured VelocityEngine.
  71. * @see #createVelocityEngine
  72. * @see #setVelocityEngine
  73. */
  74. public void afterPropertiesSet() throws IOException, VelocityException {
  75. if (this.velocityEngine == null) {
  76. this.velocityEngine = createVelocityEngine();
  77. }
  78. }
  79. public VelocityEngine getVelocityEngine() {
  80. return this.velocityEngine;
  81. }
  82. }