View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.webapp.listener;
24  
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import javax.servlet.ServletContext;
29  import javax.servlet.ServletContextEvent;
30  import javax.servlet.ServletContextListener;
31  
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.springframework.beans.factory.NoSuchBeanDefinitionException;
35  import org.springframework.context.ApplicationContext;
36  import org.springframework.security.providers.AuthenticationProvider;
37  import org.springframework.security.providers.ProviderManager;
38  import org.springframework.security.providers.encoding.Md5PasswordEncoder;
39  import org.springframework.security.providers.encoding.PasswordEncoder;
40  import org.springframework.security.providers.rememberme.RememberMeAuthenticationProvider;
41  import org.springframework.web.context.support.WebApplicationContextUtils;
42  
43  import com.gisgraphy.Constants;
44  import com.gisgraphy.service.LookupManager;
45  
46  /**
47   * <p>
48   * StartupListener class used to initialize and database settings and populate
49   * any application-wide drop-downs. <p/>
50   * <p>
51   * Keep in mind that this listener is executed outside of
52   * OpenSessionInViewFilter, so if you're using Hibernate you'll have to
53   * explicitly initialize all loaded data at the GenericDao or service level to
54   * avoid LazyInitializationException. Hibernate.initialize() works well for
55   * doing this.
56   * 
57   * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
58   */
59  public class StartupListener implements ServletContextListener {
60      private static final Log log = LogFactory.getLog(StartupListener.class);
61  
62      @SuppressWarnings( { "unchecked" })
63      public void contextInitialized(ServletContextEvent event) {
64  	log.debug("initializing context...");
65  
66  	ServletContext context = event.getServletContext();
67  
68  	// Orion starts Servlets before Listeners, so check if the config
69  	// object already exists
70  	Map<String, Object> config = (HashMap<String, Object>) context
71  		.getAttribute(Constants.CONFIG);
72  
73  	if (config == null) {
74  	    config = new HashMap<String, Object>();
75  	}
76  
77  	if (context.getInitParameter(Constants.CSS_THEME) != null) {
78  	    config.put(Constants.CSS_THEME, context
79  		    .getInitParameter(Constants.CSS_THEME));
80  	}
81  
82  	ApplicationContext ctx = WebApplicationContextUtils
83  		.getRequiredWebApplicationContext(context);
84  
85  	boolean encryptPassword = true;
86  	
87      try {
88          ProviderManager provider = (ProviderManager) ctx.getBean(ctx.getBeanNamesForType(ProviderManager.class)[0]);
89          for (Object o : provider.getProviders()) {
90              AuthenticationProvider p = (AuthenticationProvider) o;
91              if (p instanceof RememberMeAuthenticationProvider) {
92                  config.put("rememberMeEnabled", Boolean.TRUE);
93              } 
94              config.put(Constants.ENCRYPT_PASSWORD, Boolean.TRUE);
95              config.put(Constants.ENC_ALGORITHM, "SHA");
96          }
97      } catch (NoSuchBeanDefinitionException n) {
98          log.debug("authenticationManager bean not found, assuming test and ignoring...");
99          // ignore, should only happen when testing
100     }
101 	
102 	
103 	context.setAttribute(Constants.CONFIG, config);
104 
105 	// output the retrieved values for the Init and Context Parameters
106 	if (log.isDebugEnabled()) {
107 	    log
108 		    .debug("Remember Me Enabled? "
109 			    + config.get("rememberMeEnabled"));
110 	    log.debug("Encrypt Passwords? " + encryptPassword);
111 	    if (encryptPassword) {
112 		log.debug("Encryption Algorithm: "
113 			+ config.get(Constants.ENC_ALGORITHM));
114 	    }
115 	    log.debug("Populating drop-downs...");
116 	}
117 
118 	setupContext(context);
119     }
120 
121     /**
122      * This method uses the LookupManager to lookup available roles from the
123      * data layer.
124      * 
125      * @param context
126      *                The servlet context
127      */
128     public static void setupContext(ServletContext context) {
129 	ApplicationContext ctx = WebApplicationContextUtils
130 		.getRequiredWebApplicationContext(context);
131 	LookupManager mgr = (LookupManager) ctx.getBean("lookupManager");
132 
133 	// get list of possible roles
134 	context.setAttribute(Constants.AVAILABLE_ROLES, mgr.getAllRoles());
135 	log.debug("Drop-down initialization complete [OK]");
136     }
137 
138     /**
139      * Shutdown servlet context (currently a no-op method).
140      * 
141      * @param servletContextEvent
142      *                The servlet context event
143      */
144     public void contextDestroyed(ServletContextEvent servletContextEvent) {
145 	// LogFactory.release(Thread.currentThread().getContextClassLoader());
146 	// Commented out the above call to avoid warning when SLF4J in
147 	// classpath.
148 	// WARN: The method class
149 	// org.apache.commons.logging.impl.SLF4JLogFactory#release() was
150 	// invoked.
151 	// WARN: Please see http://www.slf4j.org/codes.html for an explanation.
152     }
153 }