package com.sun.j2ee.blueprints.waf.controller.web;
import java.beans.Beans;
import java.util.HashMap;
import javax.servlet.*;
import com.sun.j2ee.blueprints.servicelocator.web.ServiceLocator;
import com.sun.j2ee.blueprints.util.tracer.Debug;
import com.sun.j2ee.blueprints.waf.util.JNDINames;
import com.sun.j2ee.blueprints.waf.controller.web.util.WebKeys;
import com.sun.j2ee.blueprints.waf.controller.web.URLMappingsXmlDAO;
public class ApplicationComponentManager implements ServletContextListener {
public ApplicationComponentManager() {}
public void contextDestroyed(ServletContextEvent sce){
}
public void contextInitialized(ServletContextEvent sce){
try {
doInit(sce.getServletContext());
getWebController(sce.getServletContext());
getRequestProcessor(sce.getServletContext());
getScreenFlowManager(sce.getServletContext());
} catch (Throwable ex) {
System.out.println("WAF ApplicaitonComponentManager Error Initializing:" + ex);
throw new RuntimeException(ex);
}
}
private void doInit(ServletContext context) {
String requestMappingsURL = null;
try {
requestMappingsURL = context.getResource("/WEB-INF/mappings.xml").toString();
} catch (java.net.MalformedURLException ex) {
System.err.println("ApplicationComponentManager: initializing ScreenFlowManager malformed URL exception: " + ex);
}
HashMap urlMappings = URLMappingsXmlDAO.loadRequestMappings(requestMappingsURL);
context.setAttribute(WebKeys.URL_MAPPINGS, urlMappings);
HashMap eventMappings = URLMappingsXmlDAO.loadEventMappings(requestMappingsURL);
context.setAttribute(WebKeys.EVENT_MAPPINGS, eventMappings);
}
public static WebController getWebController(ServletContext context) {
ServiceLocator sl = ServiceLocator.getInstance();
WebController wc = (WebController)context.getAttribute(WebKeys.WEB_CONTROLLER);
if ( wc == null ) {
try {
String wcClassName = sl.getString(JNDINames.DEFAULT_WEB_CONTROLLER);
if ((wcClassName != null) && !wcClassName.toLowerCase().equals("none")) {
wc = (WebController) Class.forName(wcClassName).newInstance();
wc.init(context);
}
} catch (com.sun.j2ee.blueprints.servicelocator.ServiceLocatorException slx) {
} catch (Exception exc) {
throw new RuntimeException("Unable to create WebController: " + exc);
}
}
return wc;
}
public static RequestProcessor getRequestProcessor(ServletContext context) {
RequestProcessor rp = (RequestProcessor)context.getAttribute(WebKeys.REQUEST_PROCESSOR);
if ( rp == null ) {
rp = new RequestProcessor();
rp.init(context);
context.setAttribute(WebKeys.REQUEST_PROCESSOR, rp);
}
return rp;
}
public static ScreenFlowManager getScreenFlowManager(ServletContext context) {
ScreenFlowManager screenManager = (ScreenFlowManager)context.getAttribute(WebKeys.SCREEN_FLOW_MANAGER);
if (screenManager == null ) {
screenManager = new ScreenFlowManager();
screenManager.init(context);
context.setAttribute(WebKeys.SCREEN_FLOW_MANAGER, screenManager);
}
return screenManager;
}
}