1 20 package org.enhydra.barracuda.contrib.dbroggisch.page; 21 22 import java.io.IOException ; 23 import javax.servlet.ServletException ; 24 import java.util.Map ; 25 import java.util.HashMap ; 26 import org.enhydra.barracuda.core.event.BaseEvent; 27 import org.enhydra.barracuda.core.event.ControlEventContext; 28 import org.enhydra.barracuda.core.event.EventException; 29 import org.apache.log4j.Logger; 30 31 32 36 public class PageFactory { 37 38 public static final String PAGE_KEY = PageFactory.class.getName() + ".PAGE"; 39 public static final String SYNCHEE = PageFactory.class.getName() + ".SYNCHEE"; 40 41 private static final Logger logger = Logger.getLogger(PageFactory.class.getName()); 42 private static Map s_pageClasses = new HashMap (); 43 44 54 public static void addPageEventMapping(Class event, Class page) { 55 if (BaseEvent.class.isAssignableFrom(event) && Page.class.isAssignableFrom(page)) { 56 s_pageClasses.put(event, page); 57 if (logger.isInfoEnabled()) logger.info("Registered page " + page.getName() + " for event " + event.getName()); 58 } else { 59 logger.error("Failed to register page with event..."); 60 if (!BaseEvent.class.isAssignableFrom(event)) logger.error("Class " + event.getName() + " is not an assignment-compatible with " + BaseEvent.class.getName()); 61 if (!Page.class.isAssignableFrom(page)) logger.error("Class " + page.getName() + " is not an assignment-compatible with " + Page.class.getName()); 62 } 63 } 64 65 80 public static Page createPage(BaseEvent event) { 81 if (s_pageClasses.containsKey(event.getClass())) { 82 Class pageClass = getPageClass(event); 83 if (pageClass != null) { 84 if (logger.isInfoEnabled()) logger.info("Creating page for event " + event.getClass().getName()); 85 if (logger.isDebugEnabled()) { 86 logger.debug("All Registered pages..."); 87 logger.debug(s_pageClasses); 88 } 89 try { 90 return (Page)pageClass.newInstance(); 91 } catch (InstantiationException ie) { 92 logger.error(ie); 93 } catch (IllegalAccessException iae) { 94 logger.error(iae); 95 } 96 } else { 97 logger.error("Couldn't find page for event: " + event.getClass().getName()); 99 } 100 } else { 101 if (logger.isDebugEnabled()) logger.debug("Event may be auxilary, no page registered for event, no new page created"); 102 } 103 104 return null; 106 } 107 108 115 public static Class getPageClass(BaseEvent event) { 116 return (Class )s_pageClasses.get(event.getClass()); 117 } 118 119 } 120 | Popular Tags |