package org.appfuse.webapp.listener;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.acegisecurity.providers.AuthenticationProvider;
import org.acegisecurity.providers.ProviderManager;
import org.acegisecurity.providers.encoding.Md5PasswordEncoder;
import org.acegisecurity.providers.rememberme.RememberMeAuthenticationProvider;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.appfuse.Constants;
import org.appfuse.service.LookupManager;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class StartupListener extends ContextLoaderListener
implements ServletContextListener {
private static final Log log = LogFactory.getLog(StartupListener.class);
public void contextInitialized(ServletContextEvent event) {
if (log.isDebugEnabled()) {
log.debug("initializing context...");
}
super.contextInitialized(event);
ServletContext context = event.getServletContext();
Map config = (HashMap) context.getAttribute(Constants.CONFIG);
if (config == null) {
config = new HashMap();
}
if (context.getInitParameter(Constants.CSS_THEME) != null) {
config.put(Constants.CSS_THEME, context.getInitParameter(Constants.CSS_THEME));
}
ApplicationContext ctx =
WebApplicationContextUtils.getRequiredWebApplicationContext(context);
boolean encryptPassword = false;
try {
ProviderManager provider = (ProviderManager) ctx.getBean("authenticationManager");
for (Iterator it = provider.getProviders().iterator(); it.hasNext();) {
AuthenticationProvider p = (AuthenticationProvider) it.next();
if (p instanceof RememberMeAuthenticationProvider) {
config.put("rememberMeEnabled", Boolean.TRUE);
}
}
if (ctx.containsBean("passwordEncoder")) {
encryptPassword = true;
config.put(Constants.ENCRYPT_PASSWORD, Boolean.TRUE);
String algorithm = "SHA";
if (ctx.getBean("passwordEncoder") instanceof Md5PasswordEncoder) {
algorithm = "MD5";
}
config.put(Constants.ENC_ALGORITHM, algorithm);
}
} catch (NoSuchBeanDefinitionException n) {
}
context.setAttribute(Constants.CONFIG, config);
if (log.isDebugEnabled()) {
log.debug("Remember Me Enabled? " + config.get("rememberMeEnabled"));
log.debug("Encrypt Passwords? " + encryptPassword);
if (encryptPassword) {
log.debug("Encryption Algorithm: " + config.get(Constants.ENC_ALGORITHM));
}
log.debug("Populating drop-downs...");
}
setupContext(context);
}
public static void setupContext(ServletContext context) {
ApplicationContext ctx =
WebApplicationContextUtils.getRequiredWebApplicationContext(context);
LookupManager mgr = (LookupManager) ctx.getBean("lookupManager");
context.setAttribute(Constants.AVAILABLE_ROLES, mgr.getAllRoles());
if (log.isDebugEnabled()) {
log.debug("Drop-down initialization complete [OK]");
}
}
}