View Javadoc

1   package org.appfuse.webapp;
2   
3   import com.google.common.collect.Sets;
4   import de.agilecoders.wicket.core.Bootstrap;
5   import de.agilecoders.wicket.core.settings.BootstrapSettings;
6   import de.agilecoders.wicket.webjars.collectors.AssetPathCollector;
7   import de.agilecoders.wicket.webjars.collectors.VfsJarAssetPathCollector;
8   import de.agilecoders.wicket.webjars.WicketWebjars;
9   import de.agilecoders.wicket.webjars.settings.WebjarsSettings;
10  import javassist.CannotCompileException;
11  import javassist.ClassPool;
12  import javassist.CtClass;
13  import org.apache.wicket.authroles.authentication.AuthenticatedWebApplication;
14  import org.apache.wicket.authroles.authentication.AuthenticatedWebSession;
15  import org.apache.wicket.markup.html.WebPage;
16  import org.apache.wicket.resource.loader.ClassStringResourceLoader;
17  import org.apache.wicket.settings.IRequestCycleSettings;
18  import org.appfuse.webapp.pages.Home;
19  import org.appfuse.webapp.pages.Login;
20  import org.apache.wicket.Page;
21  import org.apache.wicket.spring.injection.annot.SpringComponentInjector;
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  import org.springframework.context.ApplicationContext;
25  import org.springframework.web.context.support.WebApplicationContextUtils;
26  import org.wicketstuff.annotation.scan.AnnotatedMountScanner;
27  
28  import java.util.Set;
29  
30  /**
31   * AppFuse Wicket Frontend Application class.
32   *
33   * @author Marcin ZajÄ…czkowski, 2010-09-02
34   */
35  public class AppFuseWicketApplication extends AuthenticatedWebApplication {
36  
37      private static final String BASE_PACKAGE_FOR_PAGES = "org.appfuse.webapp.pages";
38      public static final String APPFUSE_RESOURCE_FILE_NAME = "ApplicationResources";
39  
40      protected final Logger log = LoggerFactory.getLogger(getClass());
41  
42      @Override
43      protected void init() {
44          super.init();
45          registerSpringComponentInjector();
46          initPageMounting();
47          setOnePassRenderStrategy();
48          registerAppFuseSpecificStringResourceLoader();
49          initBootstrap();
50          initWebjars();
51      }
52  
53      private void initWebjars() {
54          // install 2 default collector instances (FileAssetPathCollector(WEBJARS_PATH_PREFIX), JarAssetPathCollector)
55          // and a webjars resource finder.
56          WebjarsSettings settings = new WebjarsSettings();
57  
58          // register vfs collector to use webjars on jboss (you don't need to add maven dependency)
59          Set<AssetPathCollector> collectors = Sets.newHashSet(settings.assetPathCollectors());
60          collectors.add(new VfsJarAssetPathCollector());
61          settings.assetPathCollectors(collectors.toArray(new AssetPathCollector[collectors.size()]));
62  
63          WicketWebjars.install(this, settings);
64      }
65  
66      private void registerSpringComponentInjector() {
67          getComponentInstantiationListeners().add(new SpringComponentInjector(this, getContext(), true));
68      }
69  
70      private void initPageMounting() {
71          //Hint from:
72          //http://blog.xebia.com/2008/10/09/readable-url%E2%80%99s-in-wicket-an-introduction-to-wicketstuff-annotation/
73          new AnnotatedMountScanner().scanPackage(BASE_PACKAGE_FOR_PAGES).mount(this);
74      }
75  
76      /**
77       * Redirects after post causes page to be shrunk (probably) due to SiteMesh bug:
78       * http://jira.opensymphony.com/browse/SIM-217
79       */
80      private void setOnePassRenderStrategy() {
81          getRequestCycleSettings().setRenderStrategy(IRequestCycleSettings.RenderStrategy.ONE_PASS_RENDER);
82      }
83  
84      //TODO: MZA: Move to a separate class
85      /**
86       * Registers AppFuse specific IStringResourceLoader at the end to use ApplicationResources.properties
87       * for unhandled keys.
88       */
89      private void registerAppFuseSpecificStringResourceLoader() {
90          Class applicationResourcesClass = getOrCreateArtificialApplicationResourcesClass();
91          getResourceSettings().getStringResourceLoaders().add(new ClassStringResourceLoader(applicationResourcesClass));
92      }
93  
94      private Class<?> getOrCreateArtificialApplicationResourcesClass() {
95          try {
96              return Class.forName(APPFUSE_RESOURCE_FILE_NAME);
97          } catch (ClassNotFoundException e) {
98              return createArtificialApplicationResourcesClass();
99          }
100     }
101 
102     private Class<?> createArtificialApplicationResourcesClass() {
103         try {
104             ClassPool classPool = ClassPool.getDefault();
105             CtClass applicationResourcesCtClass = classPool.makeClass(APPFUSE_RESOURCE_FILE_NAME);
106             return applicationResourcesCtClass.toClass();
107         } catch (CannotCompileException e) {
108             throw new RuntimeException("Unable to instantiate Wicket application", e);
109         }
110     }
111 
112     private void initBootstrap() {
113         BootstrapSettings settings = new BootstrapSettings();
114         Bootstrap.install(this, settings);
115     }
116 
117     @Override
118     public Class<? extends Page> getHomePage() {
119         return Home.class;
120     }
121 
122     @Override
123     protected Class<? extends AuthenticatedWebSession> getWebSessionClass() {
124         return SSAuthenticatedWebSession.class;
125     }
126 
127     @Override
128     protected Class<? extends WebPage> getSignInPageClass() {
129         return Login.class;
130     }
131 
132     protected ApplicationContext getContext() {
133         return WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
134     }
135 }