KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > contrib > dbroggisch > page > PageFactory


1 /*
2  * Copyright (C) 2003 Diez B. Roggisch [deets@web.de]
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  * $Id: PageFactory.java,v 1.7 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.contrib.dbroggisch.page;
21
22 import java.io.IOException JavaDoc;
23 import javax.servlet.ServletException JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.HashMap JavaDoc;
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 /**
33  * @author <a HREF="mailto:deets@web.de">Diez B. Roggisch</a>
34  * @version 1.0
35  */

36 public class PageFactory {
37
38     public static final String JavaDoc PAGE_KEY = PageFactory.class.getName() + ".PAGE";
39     public static final String JavaDoc SYNCHEE = PageFactory.class.getName() + ".SYNCHEE";
40
41     private static final Logger logger = Logger.getLogger(PageFactory.class.getName());
42     private static Map JavaDoc s_pageClasses = new HashMap JavaDoc();
43
44     /**
45      * Maps events to page objects. The event class is expected to be
46      * assignment-compatible with a
47      * {@link org.enhydra.barracuda.core.event.BaseEvent} object and the page
48      * class assignment-compatible with a {@link Page} object. If these
49      * conditions are not met, the mapping will not take place.
50      *
51      * @param event a class assignment-compatible with a BaseEvent object
52      * @param page a class assignment-compatible with a Page object
53      */

54     public static void addPageEventMapping(Class JavaDoc event, Class JavaDoc 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     /**
66      * Creates pages for events which have been mapped to pages via
67      * {@link #addPageEventMapping(Class, Class)}. This method returns a
68      * page object only for functional events which represent
69      * an actual screen presentation. <code>null</code> will be returned for
70      * auxilary events which are responsible for dealing with data that is
71      * common to all or a subset of screens, such as navigation bars, footers,
72      * headers, teaserlists and so on. They never get their own page - but they
73      * expect the pages they deal with to implement a certain interface and,
74      * more so, maybe even the underlying DOM (this concept is mostly
75      * associated with <a HREF="http://xmlc.enhydra.org/">XMLC</a>) class.
76      *
77      * @param event the current event
78      * @return a Page object for functional events and null for auxilary events
79      */

80     public static Page createPage(BaseEvent event) {
81         if (s_pageClasses.containsKey(event.getClass())) {
82             Class JavaDoc 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 JavaDoc ie) {
92                     logger.error(ie);
93                 } catch (IllegalAccessException JavaDoc iae) {
94                     logger.error(iae);
95                 }
96             } else {
97                 //this should never happen!
98
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         //TODO: Error handling
105
return null;
106     }
107
108     /**
109      * provides access to the {@link Page} class associated with the given
110      * event which has been mapped to the page via
111      * {@link #addPageEventMapping(Class, Class)}.
112      *
113      * @return a class which is assignment-compatible with a Page object
114      */

115     public static Class JavaDoc getPageClass(BaseEvent event) {
116         return (Class JavaDoc)s_pageClasses.get(event.getClass());
117     }
118
119 }
120
Popular Tags