View Javadoc
1   /**
2    * Copyright By Grandsoft Company Limited.  
3    * 2013-1-11 下午01:50:45
4    */
5   package gboat2.base.plugin.struts.velocity;
6   
7   import java.util.Properties;
8   
9   import org.apache.struts2.StrutsConstants;
10  import org.apache.struts2.views.velocity.VelocityManager;
11  import org.apache.velocity.app.Velocity;
12  
13  import com.opensymphony.xwork2.inject.Inject;
14  
15  /**
16   * 平台对 {@link VelocityManager} 的扩展,实现 vm 文件的热部署。<br>
17   * 使用方法为 在 struts.xml 中加入:<pre><code>
18   * &lt;!-- 热部署vm文件之用,生产环境可以去掉本项 --&gt;
19   * &lt;constant name="struts.velocity.manager.classname" value="gboat2.base.plugin.struts.velocity.GboatVelocityManager" /&gt;
20   * </code></pre>
21   * @date 2013-1-11
22   * @author tanxw
23   * @since 1.0
24   */
25  public class GboatVelocityManager extends VelocityManager {
26  
27      private boolean devMode = false;
28  
29      @Inject(StrutsConstants.STRUTS_DEVMODE)
30      public void setDevMode(String devMode) {
31          this.devMode = "true".equals(devMode);
32      }
33  
34      private void debugIntercept(Properties velocityProperties) {
35          String resourceLoader = velocityProperties.getProperty(Velocity.RESOURCE_LOADER);
36          if (null == resourceLoader || -1 == resourceLoader.indexOf("debuggboat2")) {
37              velocityProperties.setProperty("debuggboat2.resource.loader.description", "debug velocity bundle loader");
38              velocityProperties.setProperty("debuggboat2.resource.loader.class", DebugVelocityResourceLoader.class.getName());
39              velocityProperties.setProperty("debuggboat2.resource.loader.cache", "false");
40              if (null == resourceLoader) {
41                  velocityProperties.setProperty(Velocity.RESOURCE_LOADER, "debuggboat2,strutsfile,strutsclass,osgi");
42              } else {
43                  velocityProperties.setProperty(Velocity.RESOURCE_LOADER, "debuggboat2," + resourceLoader);
44              }
45          } else {
46              if (resourceLoader.endsWith(",debuggboat2")
47                      || resourceLoader.indexOf(",strutsfile,") != -1) { // tail
48                  resourceLoader = resourceLoader.replace(",debuggboat2", "");
49                  velocityProperties.setProperty(Velocity.RESOURCE_LOADER, "debuggboat2," + resourceLoader);
50              }
51          }
52      }
53  
54      @Override
55      public void setVelocityProperties(Properties velocityProperties) {
56          if (devMode) {// only intercept in develop mode
57              debugIntercept(velocityProperties);
58          }
59          super.setVelocityProperties(velocityProperties);
60      }
61  }