001: /*
002: * Enhydra Java Application Server
003: * The Initial Developer of the Original Code is Lutris Technologies Inc.
004: * Portions created by Lutris are Copyright (C) 1997-2000 Lutris Technologies
005: * Inc.
006: * All Rights Reserved.
007: *
008: * The contents of this file are subject to the Enhydra Public License Version
009: * 1.0 (the "License"); you may not use this file except in compliance with the
010: * License. You may obtain a copy of the License at
011: * http://www.enhydra.org/software/license/epl.html
012: *
013: * Software distributed under the License is distributed on an "AS IS" basis,
014: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
015: * License for the specific language governing rights and limitations under the
016: * License.
017: *
018: *
019: */
020:
021: package golfShop;
022:
023: import java.io.*;
024: import java.math.BigDecimal;
025: import java.util.Enumeration;
026: import com.lutris.util.*;
027: import com.lutris.logging.Logger;
028: import com.lutris.logging.LogChannel;
029: import com.lutris.appserver.server.session.*;
030: import com.lutris.appserver.server.user.*;
031: import com.lutris.appserver.server.*;
032: import com.lutris.appserver.server.httpPresentation.*;
033: import golfShop.spec.user.*;
034:
035: import golfShop.spec.cart.*;
036: import golfShop.spec.*;
037:
038: /**
039: * Application class for this specific application. This implements
040: * standard startup and shutdown methods and well as application specific
041: * ones.
042: */
043: public class GolfShop extends StandardApplication {
044: /*
045: * This is the global Config object that is shared by the
046: * whole application. If there was an error reading the file, this
047: * will be null.
048: *
049: * @see Config
050: */
051: public static Config config = null;
052:
053: /**
054: * User manager for this application.
055: */
056: private GolfShopUserManager userManager = null;
057:
058: /**
059: * Return the UserManager for this server.
060: */
061: public GolfShopUserManager getUserManager() {
062: return userManager;
063: }
064:
065: private final String DEFAULT_FILENAME = "user_store";
066: private final String DEFAULT_DIRECTORY = "golfShop/presentation/xmlc/contents/itemdb";
067: private final String DEFAULT_OPTION = "File";
068:
069: /**
070: * Start the application.
071: *
072: * @param appConfig Configuration object.
073: * @exception HarmonyAppException If an error occurs starting the
074: * application.
075: */
076: public void startup(Config appConfig) throws ApplicationException {
077: /*
078: * This will create the config and sessionManager objects.
079: */
080: super .startup(appConfig);
081: if (logChannel != null) {
082: logChannel.write(Logger.INFO,
083: "Welcome to the Lutris GolfShop application!");
084: }
085: config = appConfig;
086:
087: try {
088:
089: userManager = GolfShopUserManagerFactory
090: .getGolfShopUserManager("golfShop.business.user.GolfShopUserManagerImpl");
091:
092: //Set the default values to parameters that we need to initialize storages
093: String userStoreOption = DEFAULT_OPTION;
094: String storeOption = DEFAULT_OPTION;
095: String fileName = DEFAULT_FILENAME;
096: String dir = DEFAULT_DIRECTORY;
097: try {
098:
099: //Take the values from configuration file
100: userStoreOption = config
101: .getString("GolfShop.UserStoreOption");
102: storeOption = config
103: .getString("GolfShop.ItemStore.Option");
104: } catch (ConfigException e) {
105:
106: }
107:
108: File tempFile = null;
109: String tempString = null;
110:
111: // GolfShop.ItemStore.Directory parameter initialization
112:
113: try {
114: dir = config.getString("GolfShop.ItemStore.Directory");
115: tempFile = new File(dir);
116: if (!tempFile.exists() || !tempFile.isDirectory()) {
117: // in case a relative path is given (relative from configuration file)
118: tempString = config.getConfigFile().getFile()
119: .getParent();
120: dir = tempString + File.separator + dir;
121: tempFile = new File(dir);
122:
123: if (!tempFile.exists() || !tempFile.isDirectory()) {
124: tempString = this .getClass().getClassLoader()
125: .getResource(DEFAULT_DIRECTORY)
126: .getPath();
127: dir = tempString;
128: tempFile = new File(dir);
129: }
130: }
131: } catch (ConfigException e) {
132: tempString = this .getClass().getClassLoader()
133: .getResource(DEFAULT_DIRECTORY).getPath();
134: dir = tempString;
135: tempFile = new File(dir);
136: }
137:
138: if (!tempFile.exists() || !tempFile.isDirectory()) {
139: dir = DEFAULT_DIRECTORY;
140: if (logChannel != null) {
141: logChannel
142: .write(Logger.INFO,
143: "GolfShop.ItemStore.Directory application parameter not initialized properly!");
144: }
145: }
146:
147: // GolfShop.UserStoreFilename parameter initialization
148:
149: try {
150: fileName = config
151: .getString("GolfShop.UserStoreFilename");
152: tempFile = new File(fileName);
153: if (!tempFile.exists()) {
154: // in case a relative path is given (relative from configuration file)
155: tempString = config.getConfigFile().getFile()
156: .getParent();
157: fileName = tempString + File.separator + fileName;
158: tempFile = new File(fileName);
159:
160: if (!tempFile.exists()) {
161: tempString = this .getClass().getClassLoader()
162: .getResource(DEFAULT_FILENAME)
163: .getPath();
164: fileName = tempString;
165: tempFile = new File(fileName);
166: }
167: }
168: } catch (ConfigException e) {
169: tempString = this .getClass().getClassLoader()
170: .getResource(DEFAULT_FILENAME).getPath();
171: fileName = tempString;
172: tempFile = new File(fileName);
173: }
174:
175: if (!tempFile.exists()) {
176: fileName = DEFAULT_FILENAME;
177: if (logChannel != null) {
178: logChannel
179: .write(Logger.INFO,
180: "GolfShop.UserStoreFilename application parameter not initialized properly!");
181: }
182: }
183:
184: //This will initialize all storages with given parameters
185: Initialize initialize = InitializeFactory
186: .getInitialize("golfShop.business.InitializeImpl");
187: initialize.storages(userStoreOption, fileName, storeOption,
188: dir);
189:
190: /*
191: * Catch null pointer exception ( we canot make a instances of classes from business layer when we run GolfShop_pres )
192: * We need to allow GolfShop_pres to be functional
193: */
194: } catch (NullPointerException e) {
195: } catch (Exception e) {
196: // N.B. If shutdown methods are available for session and user
197: // manager, call them.
198: userManager = null;
199:
200: // This application isn't designed to be restarted.
201: state = DEAD;
202: if (e instanceof ApplicationException) {
203: throw (ApplicationException) e;
204: } else {
205: throw new ApplicationException(
206: "failure to initialize GolfShop", e);
207: }
208: }
209: }
210:
211: /**
212: * Shutdown the application.
213: */
214: public void shutdown() {
215: userManager = null;
216: super .shutdown();
217: }
218:
219: public boolean requestPreprocessor(HttpPresentationComms comms)
220: throws Exception {
221:
222: if (logChannel != null) {
223: logChannel.write(Logger.DEBUG, "URI=\""
224: + comms.request.getRequestURI() + "\"");
225: }
226:
227: super .requestPreprocessor(comms);
228: if (comms.session == null) {
229: // Session not setup; so its not a presentation object.
230: return false;
231: }
232:
233: String pp = comms.request.getApplicationPath();
234: if (!pp.endsWith("/"))
235: pp += "/";
236: String loginDir = pp + "login/";
237: String mediaDir = pp + "media/";
238: String page = comms.request.getRequestURI();
239: if (page.startsWith(loginDir) || page.startsWith(mediaDir)) {
240: /*
241: * Let them go to the login and logout areas whether or not
242: * they are logged in.
243: */
244: return false;
245: } else {
246: //We need to allow poker_pres to be dynamic , so if the requested url is GolfShop_pres user dont need to be logged
247: String uri = comms.request.getRequestURI();
248: boolean is = uri.startsWith("/GolfShop_pres");
249:
250: if (is)
251:
252: return false;
253:
254: else {
255: if (comms.session.getUser() != null)
256: // It's ok, they are logged in already.
257: return false;
258: else {
259: // They are not logged in! Redirect to login page.
260: String url = pp + "login/Login.po";
261: throw new ClientPageRedirectException(url);
262: }
263: }
264: }
265: }
266:
267: /*
268: * Generate an HTML table of sessions.
269: */
270: private String makeSessionTable(Enumeration allSessions) {
271: StringBuffer results = new StringBuffer();
272:
273: results
274: .append("<TABLE><TR><TD ALIGN=CENTER><B>Username</B></TD>"
275: + "<TD ALIGN=CENTER><B>Current total</B></TD></TR>\n");
276: while (allSessions.hasMoreElements()) {
277: String sessionKey = (String) allSessions.nextElement();
278: Session session;
279: try {
280: session = sessionManager.getSession(sessionKey);
281: } catch (SessionException except) {
282: // Skip session that may have just become invalid
283: continue;
284: }
285: Cart cart = null;
286: String username = null;
287: String totalStr = null;
288:
289: User user = session.getUser();
290: if (user != null) {
291: username = user.getName();
292: } else {
293: username = "<I>Not logged in yet</I>";
294: }
295:
296: SessionData sd = session.getSessionData();
297: try {
298: cart = (Cart) sd.get("cart");
299: } catch (KeywordValueException kve) {
300: }
301: if (cart != null) {
302: double total = cart.getTotal();
303: BigDecimal total2 = new BigDecimal(total);
304: total2 = total2.setScale(2, BigDecimal.ROUND_HALF_UP);
305: totalStr = "$" + total2;
306: } else {
307: totalStr = "<I>No cart</I>";
308: }
309: results.append("<TR><TD ALIGN=CENTER>");
310: results.append(username);
311: results.append("</TD><TD ALIGN=RIGHT>");
312: results.append("</TD></TR>\n");
313: }
314: results.append("</TABLE>\n");
315: return results.toString();
316: }
317:
318: /*
319: * The MultiServer's admin app will notice that this function exists
320: * and will call it when showing the state of this application.
321: * This is my chance to display info specific to this application.
322: */
323: public String toHtml() {
324: Enumeration allSessions;
325: try {
326: allSessions = sessionManager.getSessionKeys();
327: } catch (SessionException except) {
328: // Return error as HTML.
329: return except.getClass().getName() + " "
330: + except.getMessage();
331: }
332: if (!allSessions.hasMoreElements()) {
333: return "There are no current sessions.";
334: } else {
335: return makeSessionTable(allSessions);
336: }
337: }
338: }
|