KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > config > EventConfig


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site (http://www.enhydra.org/).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: EventConfig.java,v 1.1 2004/05/28 19:39:27 slobodan Exp $
22  */

23 package org.enhydra.barracuda.config;
24
25 import java.io.*;
26 import java.util.*;
27 import javax.servlet.*;
28 import javax.servlet.http.*;
29
30 import org.apache.log4j.*;
31
32 import org.enhydra.barracuda.config.events.*;
33 import org.enhydra.barracuda.core.comp.*;
34 import org.enhydra.barracuda.core.event.*;
35 import org.enhydra.barracuda.core.event.helper.*;
36 import org.enhydra.barracuda.core.forms.*;
37 import org.enhydra.barracuda.core.util.dom.*;
38 import org.enhydra.barracuda.core.util.srv.*;
39 import org.enhydra.barracuda.plankton.data.*;
40 import org.enhydra.barracuda.plankton.http.*;
41
42 /**
43  * This class provides the methods needed to configure the event
44  * screen
45  */

46 public class EventConfig extends DefaultEventGateway {
47
48     protected static final Logger logger = Logger.getLogger(EventConfig.class.getName());
49
50     //event id (must be unique)
51
public static final String JavaDoc FORM = EventConfig.class.getName()+".Form";
52
53     //model name
54
public static final String JavaDoc MODEL_NAME = "Event";
55
56     //model elements (these are the data items supported)
57
public static final String JavaDoc APPLICATION_GATEWAY_DL = "ApplicationGateway_DebugLevel";
58     public static final String JavaDoc APPLICATION_GATEWAY_USE_EVENT_POOLING = "ApplicationGateway_UseEventPooling";
59     public static final String JavaDoc APPLICATION_GATEWAY_FORCE_GC = "ApplicationGateway_ForceGC";
60     public static final String JavaDoc DEFAULT_BASE_EVENT_USE_ID_ALIASES = "DefaultBaseEvent_UseIDAliases";
61     public static final String JavaDoc DEFAULT_DOM_WRITER_DPP_FALSE = "DefaultDOMWriter_DefaultPrintPretty_False";
62     public static final String JavaDoc DEFAULT_BASE_EVENT_LISTENER_DL = "DefaultBaseEventListener_DebugLevel";
63     public static final String JavaDoc DEFAULT_DISPATCH_QUEUE_DL = "DefaultDispatchQueue_DebugLevel";
64     public static final String JavaDoc DEFAULT_EVENT_BROKER_DL = "DefaultEventBroker_DebugLevel";
65     public static final String JavaDoc DEFAULT_EVENT_CONTEXT_DL = "DefaultEventContext_DebugLevel";
66     public static final String JavaDoc DEFAULT_EVENT_DISPATCHER_DL = "DefaultEventDispatcher_DebugLevel";
67     public static final String JavaDoc DEFAULT_EVENT_DISPATCHER_MAX_POLY = "DefaultEventDispatcher_MaxPolyChainDepth";
68     public static final String JavaDoc DEFAULT_EVENT_DISPATCHER_MAX_DISPATCH = "DefaultEventDispatcher_MaxDispatchQueueDepth";
69     public static final String JavaDoc DEFAULT_EVENT_GATEWAY_DL = "DefaultEventGateway_DebugLevel";
70     public static final String JavaDoc DEFAULT_EVENT_POOL_DL = "DefaultEventPool_DebugLevel";
71     public static final String JavaDoc DEFAULT_EVENT_POOL_CLEANUP_LOCKED_EVENTS = "DefaultEventPool_CleanupLockedEvents";
72     public static final String JavaDoc EVENT_FORWARDING_FACTORY_DL = "EventForwardingFactory_DebugLevel";
73     public static final String JavaDoc EVENT_REDIRECT_FACTORY_DL = "EventRedirectFactory_DebugLevel";
74     public static final String JavaDoc ERROR_MESSAGE = "ErrorMessage";
75     public static final String JavaDoc SUCCESS_MESSAGE = "SuccessMessage";
76     public static final String JavaDoc UPDATE_BUTTON = "UpdateButton";
77
78     //private vars
79
private ListenerFactory updateConfigFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new UpdateConfigHandler();} public String JavaDoc getListenerID() {return getID(UpdateConfigHandler.class);}};
80     private ListenerFactory forceGCFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new ForceGCHandler();} public String JavaDoc getListenerID() {return getID(ForceGCHandler.class);}};
81     private ListenerFactory cleanupLockedEventsFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new CleanupLockedEventsHandler();} public String JavaDoc getListenerID() {return getID(CleanupLockedEventsHandler.class);}};
82
83     public EventConfig() {
84         //specify generic interest
85
specifyLocalEventInterests(updateConfigFactory);
86         specifyLocalEventInterests(forceGCFactory);
87         specifyLocalEventInterests(cleanupLockedEventsFactory);
88     }
89
90     //------------------------------------------------------------
91
// Data Models
92
//------------------------------------------------------------
93
/**
94      * define the template model that backs this screen
95      */

96     class EventModel extends AbstractTemplateModel {
97
98         //register the model by name
99
public String JavaDoc getName() {return MODEL_NAME;}
100         
101         //provide items by key
102
public Object JavaDoc getItem(String JavaDoc key) {
103             if (logger.isDebugEnabled()) logger.debug("Asking for key:"+key);
104             ViewContext vc = getViewContext();
105
106             //Handle requests for data
107
if (key.equals(APPLICATION_GATEWAY_DL)) {
108                 return ScreenUtil.getDebugLevelComp2(vc, key, ApplicationGateway.class);
109             } else if (key.equals(APPLICATION_GATEWAY_USE_EVENT_POOLING)) {
110                 return ScreenUtil.getToggleButton(vc, APPLICATION_GATEWAY_USE_EVENT_POOLING, "true", ApplicationGateway.USE_EVENT_POOLING);
111             } else if (key.equals(APPLICATION_GATEWAY_FORCE_GC)) {
112                 return ScreenUtil.getActionLink(vc, forceGCFactory);
113             } else if (key.equals(DEFAULT_BASE_EVENT_USE_ID_ALIASES)) {
114                 BToggleButton bcomp = ScreenUtil.getToggleButton(vc, DEFAULT_BASE_EVENT_USE_ID_ALIASES, "true", DefaultBaseEvent.USE_ID_ALIASES);
115                 //because this is a read-only, we want to disable this field
116
//(this doesn't currently work in NN)
117
bcomp.setEnabled(false);
118                 return bcomp;
119             } else if (key.equals(DEFAULT_BASE_EVENT_LISTENER_DL)) {
120                 return ScreenUtil.getDebugLevelComp2(vc, key, DefaultBaseEventListener.class);
121             } else if (key.equals(DEFAULT_DISPATCH_QUEUE_DL)) {
122                 return ScreenUtil.getDebugLevelComp2(vc, key, DefaultDispatchQueue.class);
123             } else if (key.equals(DEFAULT_EVENT_BROKER_DL)) {
124                 return ScreenUtil.getDebugLevelComp2(vc, key, DefaultEventBroker.class);
125             } else if (key.equals(DEFAULT_EVENT_CONTEXT_DL)) {
126                 return ScreenUtil.getDebugLevelComp2(vc, key, DefaultEventContext.class);
127             } else if (key.equals(DEFAULT_EVENT_DISPATCHER_DL)) {
128                 return ScreenUtil.getDebugLevelComp2(vc, key, DefaultEventDispatcher.class);
129             } else if (key.equals(DEFAULT_EVENT_DISPATCHER_MAX_POLY)) {
130                 DefaultListModel dlm = new DefaultListModel();
131                 for (int i=0; i<101; i++) dlm.add(new Integer JavaDoc(i));
132                 return ScreenUtil.getSelectComp(vc, key, dlm, DefaultEventDispatcher.MAX_POLY_CHAIN_DEPTH);
133             } else if (key.equals(DEFAULT_EVENT_DISPATCHER_MAX_DISPATCH)) {
134                 DefaultListModel dlm = new DefaultListModel();
135                 for (int i=0; i<101; i++) dlm.add(new Integer JavaDoc(i));
136                 //because this is a read-only, we want to disable this field
137
//(this doesn't currently work in NN)
138
BSelect bselect = ScreenUtil.getSelectComp(vc, key, dlm, DefaultEventDispatcher.MAX_DISPATCH_QUEUE_DEPTH);
139                 bselect.setEnabled(false);
140                 return bselect;
141             } else if (key.equals(DEFAULT_EVENT_GATEWAY_DL)) {
142                 return ScreenUtil.getDebugLevelComp2(vc, key, DefaultEventGateway.class);
143             } else if (key.equals(DEFAULT_EVENT_POOL_DL)) {
144                 return ScreenUtil.getDebugLevelComp2(vc, key, DefaultEventPool.class);
145             } else if (key.equals(DEFAULT_EVENT_POOL_CLEANUP_LOCKED_EVENTS)) {
146                 BAction baction = ScreenUtil.getActionLink(vc, cleanupLockedEventsFactory);
147                 baction.setEnabled(ApplicationGateway.USE_EVENT_POOLING);
148 //System.out.println("Here we are! Getting a new BAction component!");
149
return baction;
150             } else if (key.equals(EVENT_FORWARDING_FACTORY_DL)) {
151                 return ScreenUtil.getDebugLevelComp2(vc, key, EventForwardingFactory.class);
152             } else if (key.equals(EVENT_REDIRECT_FACTORY_DL)) {
153                 return ScreenUtil.getDebugLevelComp2(vc, key, EventRedirectFactory.class);
154             } else if (key.equals(ERROR_MESSAGE)) {
155                 return ScreenUtil.getErrMsg(vc, FORM, ERROR_MESSAGE);
156             } else if (key.equals(SUCCESS_MESSAGE)) {
157                 return ScreenUtil.getSuccessMsg(vc, FORM, SUCCESS_MESSAGE);
158             } else if (key.equals(UPDATE_BUTTON)) {
159                 return ScreenUtil.getUpdateButtonComp(vc, updateConfigFactory);
160             } else return super.getItem(key);
161         }
162     }
163
164     //------------------------------------------------------------
165
// Form Mappings, Validators
166
//------------------------------------------------------------
167
/**
168      * define the event map that backs the model
169      */

170     class EventForm extends DefaultFormMap {
171         public EventForm() {
172             //define the elements (note: these don't need any validators). Note
173
//also that we set the defaults to the current values. This allows
174
//us just to reset all current values down below without checking to
175
//see if the elements actually got passed in from the event.
176
if (logger.isDebugEnabled()) logger.debug("Defining form elements");
177             this.defineElement(new DefaultFormElement(APPLICATION_GATEWAY_DL, FormType.INTEGER, new Integer JavaDoc(ScreenUtil.cvtLevelToInt(ApplicationGateway.class)), null, false));
178             this.defineElement(new DefaultFormElement(APPLICATION_GATEWAY_USE_EVENT_POOLING, FormType.BOOLEAN, new Boolean JavaDoc(false), null, false));
179             this.defineElement(new DefaultFormElement(DEFAULT_BASE_EVENT_USE_ID_ALIASES, FormType.BOOLEAN, new Boolean JavaDoc(false), null, false));
180             this.defineElement(new DefaultFormElement(DEFAULT_BASE_EVENT_LISTENER_DL, FormType.INTEGER, new Integer JavaDoc(ScreenUtil.cvtLevelToInt(DefaultBaseEventListener.class)), null, false));
181             this.defineElement(new DefaultFormElement(DEFAULT_DISPATCH_QUEUE_DL, FormType.INTEGER, new Integer JavaDoc(ScreenUtil.cvtLevelToInt(DefaultDispatchQueue.class)), null, false));
182             this.defineElement(new DefaultFormElement(DEFAULT_EVENT_BROKER_DL, FormType.INTEGER, new Integer JavaDoc(ScreenUtil.cvtLevelToInt(DefaultEventBroker.class)), null, false));
183             this.defineElement(new DefaultFormElement(DEFAULT_EVENT_CONTEXT_DL, FormType.INTEGER, new Integer JavaDoc(ScreenUtil.cvtLevelToInt(DefaultEventContext.class)), null, false));
184             this.defineElement(new DefaultFormElement(DEFAULT_EVENT_DISPATCHER_DL, FormType.INTEGER, new Integer JavaDoc(ScreenUtil.cvtLevelToInt(DefaultEventDispatcher.class)), null, false));
185             this.defineElement(new DefaultFormElement(DEFAULT_EVENT_DISPATCHER_MAX_POLY, FormType.INTEGER, new Integer JavaDoc(DefaultEventDispatcher.MAX_POLY_CHAIN_DEPTH), null, false));
186             this.defineElement(new DefaultFormElement(DEFAULT_EVENT_DISPATCHER_MAX_DISPATCH, FormType.INTEGER, new Integer JavaDoc(DefaultEventDispatcher.MAX_DISPATCH_QUEUE_DEPTH), null, false));
187             this.defineElement(new DefaultFormElement(DEFAULT_EVENT_GATEWAY_DL, FormType.INTEGER, new Integer JavaDoc(ScreenUtil.cvtLevelToInt(DefaultEventGateway.class)), null, false));
188             this.defineElement(new DefaultFormElement(DEFAULT_EVENT_POOL_DL, FormType.INTEGER, new Integer JavaDoc(ScreenUtil.cvtLevelToInt(DefaultEventPool.class)), null, false));
189             this.defineElement(new DefaultFormElement(EVENT_FORWARDING_FACTORY_DL, FormType.INTEGER, new Integer JavaDoc(ScreenUtil.cvtLevelToInt(EventForwardingFactory.class)), null, false));
190             this.defineElement(new DefaultFormElement(EVENT_REDIRECT_FACTORY_DL, FormType.INTEGER, new Integer JavaDoc(ScreenUtil.cvtLevelToInt(EventRedirectFactory.class)), null, false));
191         }
192     }
193
194     //------------------------------------------------------------
195
// Model 2 - Controller Event Handlers
196
//------------------------------------------------------------
197
/**
198      * UpdateConfigHandler - handle the request to update the config
199      * screen.
200      */

201     class UpdateConfigHandler extends DefaultBaseEventListener {
202         public void handleControlEvent(ControlEventContext context) throws EventException, ServletException, IOException {
203             //figure out our target locale and get the appropriate screen
204
Locale locale = context.getViewCapabilities().getClientLocale();
205             MasterScreen screen = new MasterScreenFactory().getInstance(EventConfig.this, context, locale);
206             if (logger.isDebugEnabled()) ServletUtil.showParams(context.getRequest(), logger);
207
208             //create the login event
209
ValidationException ve = null;
210             EventForm formMap = new EventForm();
211             try {
212                 //map/validate the event
213
formMap.map(context.getRequest()).validate(true);
214                 
215                 //If there were no errors, update the data. Note that we just
216
//assume that all the data is here. The reason we can do this is
217
//because we prepopulated the event up above with the default values.
218
//The more proper way would be to check each value, see if it's set,
219
//and then only update it if the value has actually changed. Lot more
220
//code to do that though, and since we're just setting statics, the
221
//cost of doing it this way is minimal.
222
ScreenUtil.setLevel(ApplicationGateway.class, formMap.getIntegerVal(APPLICATION_GATEWAY_DL).intValue());
223                 ApplicationGateway.USE_EVENT_POOLING = formMap.getBooleanVal(APPLICATION_GATEWAY_USE_EVENT_POOLING).booleanValue();
224                 ScreenUtil.setLevel(DefaultBaseEventListener.class, formMap.getIntegerVal(DEFAULT_BASE_EVENT_LISTENER_DL).intValue());
225                 ScreenUtil.setLevel(DefaultDispatchQueue.class, formMap.getIntegerVal(DEFAULT_DISPATCH_QUEUE_DL).intValue());
226                 ScreenUtil.setLevel(DefaultEventBroker.class, formMap.getIntegerVal(DEFAULT_EVENT_BROKER_DL).intValue());
227                 ScreenUtil.setLevel(DefaultEventContext.class, formMap.getIntegerVal(DEFAULT_EVENT_CONTEXT_DL).intValue());
228                 ScreenUtil.setLevel(DefaultEventDispatcher.class, formMap.getIntegerVal(DEFAULT_EVENT_DISPATCHER_DL).intValue());
229                 DefaultEventDispatcher.MAX_POLY_CHAIN_DEPTH = formMap.getIntegerVal(DEFAULT_EVENT_DISPATCHER_MAX_POLY).intValue();
230                 ScreenUtil.setLevel(DefaultEventGateway.class, formMap.getIntegerVal(DEFAULT_EVENT_GATEWAY_DL).intValue());
231                 ScreenUtil.setLevel(DefaultEventPool.class, formMap.getIntegerVal(DEFAULT_EVENT_POOL_DL).intValue());
232                 ScreenUtil.setLevel(EventForwardingFactory.class, formMap.getIntegerVal(EVENT_FORWARDING_FACTORY_DL).intValue());
233                 ScreenUtil.setLevel(EventRedirectFactory.class, formMap.getIntegerVal(EVENT_REDIRECT_FACTORY_DL).intValue());
234
235                 //remember our success
236
formMap.putState(SUCCESS_MESSAGE, new Boolean JavaDoc(true));
237
238             } catch (ValidationException e) {
239                 ve = e;
240             }
241
242             //store the error message in the event and then put the event in the
243
//the event context
244
formMap.putState(ERROR_MESSAGE, ve);
245             context.putState(FORM, formMap);
246
247             //fire an update so that the screen will redraw
248
((EventModel) screen.eventModel).fireModelChanged();
249
250             //redirect to the get screen again
251
throw new ClientSideRedirectException(new GetBConfig());
252         }
253     }
254
255     /**
256      * ForceGCHandler - Force a System.gc()
257      */

258     class ForceGCHandler extends DefaultBaseEventListener {
259         public void handleControlEvent(ControlEventContext context) throws EventException, ServletException, IOException {
260             //release locked events
261
logger.debug("Forcing System.gc();");
262             System.gc();
263             
264             //redirect to the config screen
265
throw new ClientSideRedirectException(new GetBConfig());
266         }
267     }
268
269     /**
270      * CleanupLockedEventsHandler
271      */

272     class CleanupLockedEventsHandler extends DefaultBaseEventListener {
273         public void handleControlEvent(ControlEventContext context) throws EventException, ServletException, IOException {
274             //release locked events
275
logger.debug("Looking for ApplicationGateway...");
276             ApplicationGateway ag = (ApplicationGateway) SimpleServiceFinder.findInstance(ApplicationGateway.class, EventConfig.this);
277             if (ag!=null) {
278                 logger.debug("Found it! Getting event pool and manually cleaning up locked events.");
279                 EventPool ep = ag.getEventPool();
280                 if (ep!=null) ep.cleanupLockedEvents();
281             }
282
283             //figure out our target locale and get the appropriate screen
284
Locale locale = context.getViewCapabilities().getClientLocale();
285             MasterScreen screen = new MasterScreenFactory().getInstance(EventConfig.this, context, locale);
286
287             //fire an update so that the screen will redraw
288
((EventModel) screen.eventModel).fireModelChanged();
289             
290             //redirect to the config screen
291
throw new ClientSideRedirectException(new GetBConfig());
292         }
293     }
294
295
296     //------------------------------------------------------------
297
// Utility Methods
298
//------------------------------------------------------------
299
public TemplateModel getModel() {
300         return new EventModel();
301     }
302 }
303
304
Popular Tags