001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.samples.daytrader.web;
018    
019    import javax.servlet.*;
020    import javax.servlet.http.*;
021    
022    import org.apache.geronimo.samples.daytrader.util.*;
023    
024    import java.io.IOException;
025    import org.apache.geronimo.samples.daytrader.*;
026    
027    /**
028     * TradeConfigServlet provides a servlet interface to adjust DayTrader runtime parameters.
029     * TradeConfigServlet updates values in the {@link org.apache.geronimo.samples.daytrader.web.TradeConfig} JavaBean holding 
030     * all configuration and runtime parameters for the Trade application
031     *
032     */
033    public class TradeConfigServlet extends HttpServlet {
034            
035       /**
036            * Servlet initialization method.
037            */
038            public void init(ServletConfig config) throws ServletException
039            {
040                    super.init(config);
041            }
042            /**
043             * Create the TradeConfig bean and pass it the config.jsp page 
044             * to display the current Trade runtime configuration
045             * Creation date: (2/8/2000 3:43:59 PM)
046             */
047            void doConfigDisplay(
048                    HttpServletRequest req, 
049                    HttpServletResponse resp, 
050                    String results)
051                    throws Exception {
052    
053                    TradeConfig currentConfig = new TradeConfig();
054    
055                    req.setAttribute("tradeConfig", currentConfig);
056                    req.setAttribute("status", results);
057                    getServletConfig()
058                            .getServletContext()
059                            .getRequestDispatcher(TradeConfig.getPage(TradeConfig.CONFIG_PAGE))
060                            .include(req, resp); 
061            }
062            
063            void doResetTrade(
064                    HttpServletRequest req, 
065                    HttpServletResponse resp, 
066                    String results)
067                    throws Exception
068            {
069                    RunStatsDataBean runStatsData = new RunStatsDataBean();
070                    TradeConfig currentConfig = new TradeConfig();          
071                    try
072                    {
073                            runStatsData = new TradeAction().resetTrade(false);
074                            
075                            req.setAttribute("runStatsData", runStatsData);
076                            req.setAttribute("tradeConfig", currentConfig);
077                            results += "Trade Reset completed successfully";                                                
078                            req.setAttribute("status", results);
079    
080                    }
081                    catch (Exception e)
082                    {
083                            results += "Trade Reset Error  - see log for details";
084                            Log.error(e,    results);
085                            throw e;
086                    }
087                    getServletConfig()
088                                    .getServletContext()
089                                    .getRequestDispatcher(TradeConfig.getPage(TradeConfig.STATS_PAGE))
090                                    .include(req, resp);                    
091                    
092            }
093            
094            
095            /**
096             * Update Trade runtime configuration paramaters
097             * Creation date: (2/8/2000 3:44:24 PM)
098             */
099            void doConfigUpdate(HttpServletRequest req, HttpServletResponse resp)
100                    throws Exception {
101    
102                    TradeConfig currentConfig = new TradeConfig();
103    
104                    String currentConfigStr = "\n\n########## Trade configuration update. Current config:\n\n";
105                    String runTimeModeStr = req.getParameter("RunTimeMode");
106                    if (runTimeModeStr != null)
107                    {
108                            try
109                            {
110                                    int i = Integer.parseInt(runTimeModeStr);
111                                    if ((i >= 0)
112                                            && (i < TradeConfig.runTimeModeNames.length)) //Input validation
113                                            TradeConfig.runTimeMode = i;
114                            }
115                            catch (Exception e)
116                            {
117                                    //>>rjm
118                                    Log.error(
119                                            e, 
120                                            "TradeConfigServlet.doConfigUpdate(..): minor exception caught", 
121                                            "trying to set runtimemode to " + runTimeModeStr, 
122                                            "reverting to current value");
123    
124                            } // If the value is bad, simply revert to current
125                    }
126                    currentConfigStr += "\t\tRunTimeMode:\t\t" + TradeConfig.runTimeModeNames[TradeConfig.runTimeMode] + "\n";
127                    
128    
129                    String orderProcessingModeStr = req.getParameter("OrderProcessingMode");
130                    if (orderProcessingModeStr != null)
131                    {
132                            try
133                            {
134                                    int i = Integer.parseInt(orderProcessingModeStr);
135                                    if ((i >= 0)
136                                            && (i < TradeConfig.orderProcessingModeNames.length)) //Input validation
137                                            TradeConfig.orderProcessingMode = i;
138                            }
139                            catch (Exception e)
140                            {
141                                    //>>rjm
142                                    Log.error(
143                                            e, 
144                                            "TradeConfigServlet.doConfigUpdate(..): minor exception caught", 
145                                            "trying to set orderProcessing to " + orderProcessingModeStr, 
146                                            "reverting to current value");
147    
148                            } // If the value is bad, simply revert to current
149                    }
150                    currentConfigStr += "\t\tOrderProcessingMode:\t" + TradeConfig.orderProcessingModeNames[TradeConfig.orderProcessingMode]  + "\n";               
151                    
152                    String accessModeStr = req.getParameter("AcessMode");
153                    if (accessModeStr != null)
154                    {
155                            try
156                            {
157                                    int i = Integer.parseInt(accessModeStr);
158                                    if ((i >= 0)
159                                            && (i < TradeConfig.accessModeNames.length) && (i != TradeConfig.getAccessMode())) //Input validation
160                                            TradeConfig.setAccessMode(i);
161                            }
162                            catch (Exception e)
163                            {
164                                    //>>rjm
165                                    Log.error(
166                                            e, 
167                                            "TradeConfigServlet.doConfigUpdate(..): minor exception caught", 
168                                            "trying to set orderProcessing to " + orderProcessingModeStr, 
169                                            "reverting to current value");
170    
171                            } // If the value is bad, simply revert to current
172                    }               
173                    currentConfigStr += "\t\tAcessMode:\t\t" + TradeConfig.accessModeNames[TradeConfig.getAccessMode()]  + "\n";            
174                    
175                            
176                    String workloadMixStr = req.getParameter("WorkloadMix");
177                    if (workloadMixStr != null)
178                    {
179                            try
180                            {
181                                    int i = Integer.parseInt(workloadMixStr);
182                                    if ((i >= 0)
183                                            && (i < TradeConfig.workloadMixNames.length)) //Input validation
184                                            TradeConfig.workloadMix = i;
185                            }
186                            catch (Exception e)
187                            {
188                                    Log.error(
189                                            e, 
190                                            "TradeConfigServlet.doConfigUpdate(..): minor exception caught", 
191                                            "trying to set workloadMix to " + workloadMixStr, 
192                                            "reverting to current value");
193                            } // If the value is bad, simply revert to current
194                    }
195                    currentConfigStr += "\t\tWorkload Mix:\t\t" + TradeConfig.workloadMixNames[TradeConfig.workloadMix]  + "\n";            
196                    
197                    
198                    
199                    String webInterfaceStr = req.getParameter("WebInterface");
200                    if (webInterfaceStr != null)
201                    {
202                            try
203                            {
204                                    int i = Integer.parseInt(webInterfaceStr);
205                                    if ((i >= 0)
206                                            && (i < TradeConfig.webInterfaceNames.length)) //Input validation
207                                            TradeConfig.webInterface = i;
208                            }
209                            catch (Exception e)
210                            {
211                                    Log.error(
212                                            e, 
213                                            "TradeConfigServlet.doConfigUpdate(..): minor exception caught", 
214                                            "trying to set WebInterface to " + webInterfaceStr, 
215                                            "reverting to current value");
216    
217    
218                            } // If the value is bad, simply revert to current
219                    }
220                    currentConfigStr += "\t\tWeb Interface:\t\t" + TradeConfig.webInterfaceNames[TradeConfig.webInterface]  + "\n";         
221                    
222                    String cachingTypeStr = req.getParameter("CachingType");
223                    if (cachingTypeStr != null)
224                    {
225                            try
226                            {
227                                    int i = Integer.parseInt(cachingTypeStr);
228                                    if ((i >= 0)
229                                            && (i < TradeConfig.cachingTypeNames.length)) //Input validation
230                                            TradeConfig.cachingType = i;
231                            }
232                            catch (Exception e)
233                            {
234                                    Log.error(
235                                            e, 
236                                            "TradeConfigServlet.doConfigUpdate(..): minor exception caught", 
237                                            "trying to set CachingType to " + cachingTypeStr, 
238                                            "reverting to current value");
239                                    } // If the value is bad, simply revert to current
240                    }
241                    currentConfigStr += "\t\tCachingType:\t\t" + TradeConfig.cachingTypeNames[TradeConfig.cachingType]  + "\n";             
242    
243                    String parm = req.getParameter("SOAP_URL");
244                    if ((parm != null) && (parm.length() > 0))
245                    {
246                            if (!TradeConfig.getSoapURL().equals(parm)) {
247                                    TradeConfig.setSoapURL(parm);
248                            }
249                    }
250                    else
251                    {
252                            TradeConfig.setSoapURL(null);
253                    }
254    
255                    parm = req.getParameter("MaxUsers");
256                    if ((parm != null) && (parm.length() > 0))
257                    {
258                            try
259                            {
260                                    TradeConfig.setMAX_USERS(Integer.parseInt(parm));
261                            }
262                            catch (Exception e)
263                            {
264                                    Log.error(
265                                            e, 
266                                            "TradeConfigServlet.doConfigUpdate(..): minor exception caught", 
267                                            "Setting maxusers, probably error parsing string to int:" + parm, 
268                                            "revertying to current value: " + TradeConfig.getMAX_USERS());
269    
270                            } //On error, revert to saved
271                    }
272                    parm = req.getParameter("MaxQuotes");
273                    if ((parm != null) && (parm.length() > 0))
274                    {
275                            try
276                            {
277                                    TradeConfig.setMAX_QUOTES(Integer.parseInt(parm));
278                            }
279                            catch (Exception e)
280                            {
281                                    //>>rjm
282                                    Log.error(
283                                            e, 
284                                            "TradeConfigServlet: minor exception caught", 
285                                            "trying to set max_quotes, error on parsing int " + parm, 
286                                            "reverting to current value " + TradeConfig.getMAX_QUOTES());
287                                    //<<rjm
288    
289                            } //On error, revert to saved
290                    }
291                    currentConfigStr += "\t\t#Trade  Users:\t\t" + TradeConfig.getMAX_USERS()  + "\n";              
292                    currentConfigStr += "\t\t#Trade Quotes:\t\t" + TradeConfig.getMAX_QUOTES()  + "\n";             
293                    
294                    parm = req.getParameter("primIterations");
295                    if ((parm != null) && (parm.length() > 0)) {
296                            try {
297                                    TradeConfig.setPrimIterations(Integer.parseInt(parm));
298                            }
299                            catch (Exception e) {
300                                    Log.error(
301                                            e, 
302                                            "TradeConfigServlet: minor exception caught", 
303                                            "trying to set primIterations, error on parsing int " + parm, 
304                                            "reverting to current value " + TradeConfig.getPrimIterations());
305    
306                            }
307                    }
308    
309                    String enableTrace = req.getParameter("EnableTrace");
310                    if (enableTrace != null)
311                            Log.setTrace(true);
312                    else 
313                            Log.setTrace(false);                    
314                    String enableActionTrace = req.getParameter("EnableActionTrace");
315                    if (enableActionTrace != null)
316                            Log.setActionTrace(true);
317                    else 
318                            Log.setActionTrace(false);                                              
319    
320            String enableLongRun = req.getParameter("EnableLongRun");
321            
322            if (enableLongRun != null)
323                TradeConfig.setLongRun(true);
324            else 
325                TradeConfig.setLongRun(false);
326            currentConfigStr += "\t\tLong Run Enabled:\t\t" + TradeConfig.getLongRun()  + "\n";
327                    
328                    
329                    System.out.println(currentConfigStr);
330    
331            }
332    
333            public void service(HttpServletRequest req, HttpServletResponse resp)
334                    throws ServletException, IOException {
335    
336                    String action = null;
337                    String result = "";
338                    
339                    resp.setContentType("text/html");
340                    try
341                    {
342                            action = req.getParameter("action");
343                            if (action == null)
344                            {
345                                    doConfigDisplay(req, resp, result + "<b><br>Current DayTrader Configuration:</br></b>");
346                                    return;
347                            }
348                            else if (action.equals("updateConfig"))
349                            {
350                                    doConfigUpdate(req, resp);
351                                    result = "<B><BR>DayTrader Configuration Updated</BR></B>";
352                            }
353                            else if (action.equals("resetTrade"))
354                            {
355                                    doResetTrade(req, resp, "");
356                                    return;
357                            }
358                            else if (action.equals("buildDB"))
359                            {
360                                    resp.setContentType("text/html");
361                                    new TradeBuildDB(resp.getWriter(), null);
362                                    result = "DayTrader Database Built - " + TradeConfig.getMAX_USERS() + "users created";
363                            }
364                            else if (action.equals("buildDBTables"))
365                            {
366     
367                                    resp.setContentType("text/html");
368                                    new TradeBuildDB(resp.getWriter(), getServletConfig().getServletContext().getRealPath("/"));
369                            }
370                            doConfigDisplay(req, resp, result + "Current DayTrader Configuration:");
371                    }
372                    catch (Exception e)
373                    {
374                            Log.error(
375                                    e, 
376                                    "TradeConfigServlet.service(...)", 
377                                    "Exception trying to perform action=" + action);
378    
379                            resp.sendError(
380                                    500, 
381                                    "TradeConfigServlet.service(...)"
382                                            + "Exception trying to perform action="
383                                            + action
384                                            + "\nException details: " + e.toString()); 
385                            
386                    }
387            }
388    }