KickJava   Java API By Example, From Geeks To Geeks.

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


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: UtilConfig.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.util.dom.*;
35 import org.enhydra.barracuda.core.event.*;
36 import org.enhydra.barracuda.core.forms.*;
37 import org.enhydra.barracuda.core.forms.validators.*;
38 import org.enhydra.barracuda.core.util.l10n.*;
39 import org.enhydra.barracuda.core.util.srv.*;
40 import org.enhydra.barracuda.plankton.data.*;
41 import org.enhydra.barracuda.plankton.http.*;
42
43 /**
44  * This class provides the methods needed to configure the util
45  * screen
46  */

47 public class UtilConfig extends DefaultEventGateway {
48
49     protected static final Logger logger = Logger.getLogger(UtilConfig.class.getName());
50
51     //form id (must be unique)
52
public static final String JavaDoc FORM = UtilConfig.class.getName()+".Form";
53
54     //model name
55
public static final String JavaDoc MODEL_NAME = "Util";
56
57     //model elements (these are the data items supported)
58
// public static final String DEBUG_DL = "Debug_DebugLevel";
59
public static final String JavaDoc LOCALES_DL = "Locales_DebugLevel";
60 // public static final String SERVLET_UTIL_DL = "ServletUtil_DebugLevel";
61
public static final String JavaDoc SESSION_SERVICES_DL = "SessionServices_DebugLevel";
62 // public static final String SESSION_SERVICES_DEFAULT_TIMEOUT = "SessionServices_DefaultTimeout";
63
public static final String JavaDoc SIMPLE_SERVICE_FINDER_DL = "SimpleServiceFinder_DebugLevel";
64     public static final String JavaDoc ERROR_MESSAGE = "ErrorMessage";
65     public static final String JavaDoc SUCCESS_MESSAGE = "SuccessMessage";
66     public static final String JavaDoc UPDATE_BUTTON = "UpdateButton";
67
68     //private vars
69
private ListenerFactory updateConfigFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new UpdateConfigHandler();} public String JavaDoc getListenerID() {return getID(UpdateConfigHandler.class);}};
70
71     public UtilConfig() {
72         //specify generic interest
73
specifyLocalEventInterests(updateConfigFactory);
74     }
75
76     //------------------------------------------------------------
77
// Data Models
78
//------------------------------------------------------------
79
/**
80      * define the template model that backs this screen
81      */

82     class UtilModel extends AbstractTemplateModel {
83
84         //register the model by name
85
public String JavaDoc getName() {return MODEL_NAME;}
86         
87         //provide items by key
88
public Object JavaDoc getItem(String JavaDoc key) {
89             if (logger.isDebugEnabled()) logger.debug("Asking for key:"+key);
90             ViewContext vc = getViewContext();
91
92             //Handle requests for data
93
if (key.equals(LOCALES_DL)) {
94                 return ScreenUtil.getDebugLevelComp2(vc, key, Locales.class);
95             } else if (key.equals(SESSION_SERVICES_DL)) {
96                 return ScreenUtil.getDebugLevelComp2(vc, key, SessionServices.class);
97 // } else if (key.equals(SESSION_SERVICES_DEFAULT_TIMEOUT)) {
98
// DefaultView dlv = new DefaultView(vc.getTemplateNode().cloneNode(true));
99
// return new BInput(null, SESSION_SERVICES_DEFAULT_TIMEOUT, ""+SessionServices.DEFAULT_TIMEOUT, dlv, null);
100
} else if (key.equals(SIMPLE_SERVICE_FINDER_DL)) {
101                 return ScreenUtil.getDebugLevelComp2(vc, key, SimpleServiceFinder.class);
102             } else if (key.equals(ERROR_MESSAGE)) {
103                 return ScreenUtil.getErrMsg(vc, FORM, ERROR_MESSAGE);
104             } else if (key.equals(SUCCESS_MESSAGE)) {
105                 return ScreenUtil.getSuccessMsg(vc, FORM, SUCCESS_MESSAGE);
106             } else if (key.equals(UPDATE_BUTTON)) {
107                 return ScreenUtil.getUpdateButtonComp(vc, updateConfigFactory);
108             } else return super.getItem(key);
109         }
110     }
111
112     //------------------------------------------------------------
113
// Form Mappings, Validators
114
//------------------------------------------------------------
115
/**
116      * define the form map that backs the model
117      */

118     class UtilForm extends DefaultFormMap {
119         public UtilForm() {
120             //define the elements (note: these don't need any validators). Note
121
//also that we set the defaults to the current values. This allows
122
//us just to reset all current values down below without checking to
123
//see if the elements actually got passed in from the form.
124
if (logger.isDebugEnabled()) logger.debug("Defining form elements");
125             this.defineElement(new DefaultFormElement(LOCALES_DL, FormType.INTEGER, new Integer JavaDoc(ScreenUtil.cvtLevelToInt(Locales.class)), null, false));
126             this.defineElement(new DefaultFormElement(SESSION_SERVICES_DL, FormType.INTEGER, new Integer JavaDoc(ScreenUtil.cvtLevelToInt(SessionServices.class)), null, false));
127 // FormElement feTimeout = new DefaultFormElement(SESSION_SERVICES_DEFAULT_TIMEOUT, FormType.INTEGER, new Integer(SessionServices.DEFAULT_TIMEOUT), null, false);
128
// feTimeout.setValidator(new ValidTypeValidator("Invalid Timeout value! You must enter an integer value."));
129
// this.defineElement(feTimeout);
130
this.defineElement(new DefaultFormElement(SIMPLE_SERVICE_FINDER_DL, FormType.INTEGER, new Integer JavaDoc(ScreenUtil.cvtLevelToInt(SimpleServiceFinder.class)), null, false));
131         }
132     }
133
134     //------------------------------------------------------------
135
// Model 2 - Controller Event Handlers
136
//------------------------------------------------------------
137
/**
138      * UpdateConfigHandler - handle the request to update the config
139      * screen.
140      */

141     class UpdateConfigHandler extends DefaultBaseEventListener {
142         public void handleControlEvent(ControlEventContext context) throws EventException, ServletException, IOException {
143             //figure out our target locale and get the appropriate screen
144
Locale locale = context.getViewCapabilities().getClientLocale();
145             MasterScreen screen = new MasterScreenFactory().getInstance(UtilConfig.this, context, locale);
146             if (logger.isDebugEnabled()) ServletUtil.showParams(context.getRequest(), logger);
147             
148             //create the login form
149
ValidationException ve = null;
150             UtilForm formMap = new UtilForm();
151             try {
152                 //map/validate the form
153
formMap.map(context.getRequest()).validate(true);
154                 
155                 //If there were no errors, update the data. Note that we just
156
//assume that all the data is here. The reason we can do this is
157
//because we prepopulated the form up above with the default values.
158
//The more proper way would be to check each value, see if it's set,
159
//and then only update it if the value has actually changed. Lot more
160
//code to do that though, and since we're just setting statics, the
161
//cost of doing it this way is minimal.
162
ScreenUtil.setLevel(Locales.class, formMap.getIntegerVal(LOCALES_DL).intValue());
163                 ScreenUtil.setLevel(SessionServices.class, formMap.getIntegerVal(SESSION_SERVICES_DL).intValue());
164 // SessionServices.DEFAULT_TIMEOUT = formMap.getIntegerVal(SESSION_SERVICES_DEFAULT_TIMEOUT).intValue();
165
ScreenUtil.setLevel(SimpleServiceFinder.class, formMap.getIntegerVal(SIMPLE_SERVICE_FINDER_DL).intValue());
166
167                 //remember our success
168
formMap.putState(SUCCESS_MESSAGE, new Boolean JavaDoc(true));
169
170             } catch (ValidationException e) {
171                 ve = e;
172             }
173
174             //store the error message in the form and then put the form in the
175
//the event context
176
formMap.putState(ERROR_MESSAGE, ve);
177             context.putState(FORM, formMap);
178
179             //fire an update so that the screen will redraw
180
((UtilModel) screen.utilModel).fireModelChanged();
181
182             //redirect to the get screen again
183
throw new ClientSideRedirectException(new GetBConfig());
184         }
185     }
186
187     //------------------------------------------------------------
188
// Utility Methods
189
//------------------------------------------------------------
190
public TemplateModel getModel() {
191         return new UtilModel();
192     }
193 }
194
195
Popular Tags