001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site (http://www.enhydra.org/).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: * $Id: BarracudaConfig.java,v 1.1 2006-09-11 12:30:02 sinisa Exp $
022: */
023: package org.enhydra.barracuda.config;
024:
025: import java.io.*;
026: import java.util.*;
027: import java.lang.ref.*;
028: import java.net.*;
029: import javax.servlet.*;
030: import javax.servlet.http.*;
031:
032: import org.apache.log4j.*;
033: import org.w3c.dom.*;
034: import org.w3c.dom.html.*;
035:
036: import org.enhydra.barracuda.config.events.*;
037: import org.enhydra.barracuda.config.xmlc.*;
038: import org.barracudamvc.core.comp.*;
039: import org.barracudamvc.core.event.*;
040: import org.barracudamvc.core.event.helper.*;
041: import org.barracudamvc.core.forms.*;
042: import org.barracudamvc.core.helper.servlet.*;
043: import org.barracudamvc.core.util.dom.*; //import org.barracudamvc.core.util.dom.io.*;
044: import org.enhydra.xml.io.*;
045: import org.barracudamvc.core.util.http.*;
046: import org.barracudamvc.core.view.*;
047: import org.barracudamvc.plankton.data.*;
048:
049: /**
050: * This mini-app is can be used to configure the core Barracuda
051: * classes. This class contains event handlers (both Controller
052: * and View) for the Config screen (which is the only screen)
053: */
054: public class BarracudaConfig extends DefaultEventGateway {
055:
056: //public constants
057: protected static final Logger logger = Logger
058: .getLogger(BarracudaConfig.class.getName());
059:
060: public static final String MASTER_SCREEN = BarracudaConfig.class
061: .getName()
062: + ".MasterScreen";
063: public static final String FORM = BarracudaConfig.class.getName()
064: + ".Form";
065: public static final String ERR = BarracudaConfig.class.getName()
066: + ".Errors";
067:
068: //this defines the various event handlers
069: private ListenerFactory getConfigFactory = new DefaultListenerFactory() {
070: public BaseEventListener getInstance() {
071: return new GetConfigHandler();
072: }
073:
074: public String getListenerID() {
075: return getID(GetConfigHandler.class);
076: }
077: };
078: private ListenerFactory getNextConfigFactory = new DefaultListenerFactory() {
079: public BaseEventListener getInstance() {
080: return new GetNextConfigHandler();
081: }
082:
083: public String getListenerID() {
084: return getID(GetConfigHandler.class);
085: }
086: };
087: private ListenerFactory renderConfigFactory = new DefaultListenerFactory() {
088: public BaseEventListener getInstance() {
089: return new RenderConfigHandler();
090: }
091:
092: public String getListenerID() {
093: return getID(RenderConfigHandler.class);
094: }
095: };
096:
097: //------------------- BarracudaConfig ------------------------
098: /**
099: * Public constructor
100: */
101: public BarracudaConfig() {
102: //specify who's interested in what
103: specifyLocalEventInterests(getConfigFactory, GetBConfig.class);
104: specifyLocalEventInterests(getNextConfigFactory,
105: GetNextBConfig.class);
106: specifyLocalEventInterests(renderConfigFactory,
107: RenderBConfig.class);
108:
109: //add in sub-gateways
110: this .add(new CompConfig());
111: this .add(new DataConfig());
112: this .add(new DomConfig());
113: this .add(new EventConfig());
114: this .add(new FormsConfig());
115: this .add(new UtilConfig());
116: this .add(new ViewConfig());
117: this .add(new MasterConfig());
118: }
119:
120: //------------------------------------------------------------
121: // Model 2 - Controller Event Handlers
122: //------------------------------------------------------------
123: /**
124: * GetConfigHandler - handle the request to get the config
125: * screen.
126: */
127: class GetConfigHandler extends DefaultBaseEventListener {
128: public void handleControlEvent(ControlEventContext context)
129: throws EventException, ServletException, IOException {
130: //get the appropriate screen for the desired context and locale
131: Locale locale = context.getViewCapabilities()
132: .getClientLocale();
133: MasterScreen screen = new MasterScreenFactory()
134: .getInstance(BarracudaConfig.this , context, locale);
135:
136: //make sure the proper tab is selected (get curtab from session)
137: HttpSession session = SessionServices.getSession(context
138: .getRequest());
139: String curtab = (String) session
140: .getAttribute(TabsModel.CUR_TAB);
141: screen.tabsModel.setCurrentTab(curtab);
142:
143: //place the screen in the context (so the renderer can access it)
144: context.putState(MASTER_SCREEN, screen);
145:
146: //forward control to the renderer
147: context.getQueue().addEvent(new RenderBConfig());
148: }
149: }
150:
151: /**
152: * GetNextConfigHandler - get the next barracuda config screen (expects
153: * a TabsModel.CUR_TAB parameter in the request
154: */
155: class GetNextConfigHandler extends DefaultBaseEventListener {
156: public void handleControlEvent(ControlEventContext context)
157: throws EventException, ServletException, IOException {
158: //get the curtab parameter
159: String curtab = context.getRequest().getParameter(
160: TabsModel.CUR_TAB);
161:
162: //place it in the session (so it'll be available when we handle
163: //the get event request)
164: HttpSession session = SessionServices.getSession(context
165: .getRequest());
166: session.setAttribute(TabsModel.CUR_TAB, curtab);
167:
168: //now redirect to the main get event
169: throw new ClientSideRedirectException(new GetBConfig());
170: }
171: }
172:
173: //------------------------------------------------------------
174: // Model 2 - View Event Handlers
175: //------------------------------------------------------------
176: /**
177: * RenderConfigHandler - handle the request to render the config
178: * screen
179: */
180: class RenderConfigHandler extends DefaultBaseEventListener {
181: public void handleViewEvent(ViewEventContext context)
182: throws EventException, ServletException, IOException {
183: //get the screen from context
184: MasterScreen screen = (MasterScreen) context
185: .getState(MASTER_SCREEN);
186:
187: //TODO: note we really should be calling the initCycle and destroyCycle methods
188: //on this component hierarchy (not here of course, but it does need to be done)
189:
190: //grab the component hierarchy and render it
191: ViewContext vc = new DefaultViewContext(context);
192: try {
193: screen.bcRoot.render(vc);
194: } catch (RenderException e) {
195: logger.debug("Error rendering views:", e);
196: }
197:
198: //csc_112901.2
199: //now adjust the outgoing page (this is critical to make sure we
200: //can accurately detect client scripting)
201: ScriptDetector.prepareClientResp(screen.dom, vc);
202:
203: //now render the DOM
204: //06_19_2001_Jacob_Kjome_start - hoju@visi.com
205: //get the output options
206: OutputOptions oo = DefaultDOMWriter
207: .getDefaultOutputOptions(screen.dom
208: .getOwnerDocument());
209:
210: //add doctype - need to do here because XMLC can't yet add the one embedded in the html to the DOM
211: oo.setOmitDocType(false);
212: oo.setPublicId("-//W3C//DTD HTML 4.01 Transitional//EN");
213: oo.setSystemId("http://www.w3.org/TR/html401/loose.dtd");
214:
215: //now render the DOM
216: // new DefaultDOMWriter().write(screen.dom, context.getResponse());
217: new DefaultDOMWriter(oo).write(screen.dom, context
218: .getResponse());
219: //06_19_2001_Jacob_Kjome_end
220: }
221: }
222: }
|