1 20 21 package golfShop; 22 23 import java.io.*; 24 import java.math.BigDecimal ; 25 import java.util.Enumeration ; 26 import com.lutris.util.*; 27 import com.lutris.logging.Logger; 28 import com.lutris.logging.LogChannel; 29 import com.lutris.appserver.server.session.*; 30 import com.lutris.appserver.server.user.*; 31 import com.lutris.appserver.server.*; 32 import com.lutris.appserver.server.httpPresentation.*; 33 import golfShop.spec.user.*; 34 35 import golfShop.spec.cart.*; 36 import golfShop.spec.*; 37 42 public class GolfShop extends StandardApplication { 43 50 public static Config config = null; 51 52 55 private GolfShopUserManager userManager = null; 56 57 60 public GolfShopUserManager getUserManager() { 61 return userManager; 62 } 63 64 private final String DEFAULT_FILENAME = "user_store"; 65 private final String DEFAULT_DIRECTORY = 66 "golfShop/presentation/xmlc/contents/itemdb"; 67 private final String DEFAULT_OPTION ="File"; 68 69 70 77 public void startup(Config appConfig) throws ApplicationException { 78 81 super.startup(appConfig); 82 if (logChannel != null) { 83 logChannel.write(Logger.INFO, 84 "Welcome to the Lutris GolfShop application!"); 85 } 86 config = appConfig; 87 88 89 try { 90 91 userManager = GolfShopUserManagerFactory.getGolfShopUserManager("golfShop.business.user.GolfShopUserManagerImpl"); 92 93 String userStoreOption = DEFAULT_OPTION; 95 String storeOption = DEFAULT_OPTION; 96 String fileName = DEFAULT_FILENAME; 97 String dir = DEFAULT_DIRECTORY; 98 try{ 99 100 userStoreOption = config.getString("GolfShop.UserStoreOption"); 102 storeOption = config.getString("GolfShop.ItemStore.Option"); 103 } catch (ConfigException e) { 104 105 } 106 107 File tempFile = null; 108 String tempString = null; 109 110 112 try{ 113 dir = config.getString("GolfShop.ItemStore.Directory"); 114 tempFile = new File(dir); 115 if (!tempFile.exists()||!tempFile.isDirectory()){ 116 tempString = config.getConfigFile().getFile().getParent(); 118 dir = tempString+File.separator+dir; 119 tempFile = new File(dir); 120 121 if (!tempFile.exists()||!tempFile.isDirectory()){ 122 tempString = this.getClass().getClassLoader().getResource(DEFAULT_DIRECTORY).getPath(); 123 dir = tempString; 124 tempFile = new File(dir); 125 } 126 } 127 } catch (ConfigException e) { 128 tempString = this.getClass().getClassLoader().getResource(DEFAULT_DIRECTORY).getPath(); 129 dir = tempString; 130 tempFile = new File(dir); 131 } 132 133 if (!tempFile.exists()||!tempFile.isDirectory()){ 134 dir = DEFAULT_DIRECTORY; 135 if (logChannel != null) { 136 logChannel.write(Logger.INFO, 137 "GolfShop.ItemStore.Directory application parameter not initialized properly!"); 138 } 139 } 140 141 143 try{ 144 fileName = config.getString("GolfShop.UserStoreFilename"); 145 tempFile = new File(fileName); 146 if (!tempFile.exists()){ 147 tempString = config.getConfigFile().getFile().getParent(); 149 fileName = tempString+File.separator+fileName; 150 tempFile = new File(fileName); 151 152 if (!tempFile.exists()){ 153 tempString = this.getClass().getClassLoader().getResource(DEFAULT_FILENAME).getPath(); 154 fileName = tempString; 155 tempFile = new File(fileName); 156 } 157 } 158 } catch (ConfigException e) { 159 tempString = this.getClass().getClassLoader().getResource(DEFAULT_FILENAME).getPath(); 160 fileName = tempString; 161 tempFile = new File(fileName); 162 } 163 164 if (!tempFile.exists()){ 165 fileName = DEFAULT_FILENAME; 166 if (logChannel != null) { 167 logChannel.write(Logger.INFO, 168 "GolfShop.UserStoreFilename application parameter not initialized properly!"); 169 } 170 } 171 172 Initialize initialize = InitializeFactory.getInitialize("golfShop.business.InitializeImpl"); 174 initialize.storages(userStoreOption,fileName,storeOption,dir); 175 176 180 }catch(NullPointerException e){ 181 } catch (Exception e) { 182 userManager = null; 185 186 state = DEAD; 188 if (e instanceof ApplicationException) { 189 throw (ApplicationException)e; 190 } else { 191 throw new ApplicationException("failure to initialize GolfShop", e); 192 } 193 } 194 } 195 196 199 public void shutdown() { 200 userManager = null; 201 super.shutdown(); 202 } 203 204 205 public boolean requestPreprocessor(HttpPresentationComms comms) 206 throws Exception { 207 208 if (logChannel != null) { 209 logChannel.write(Logger.DEBUG, 210 "URI=\"" + comms.request.getRequestURI() + "\""); 211 } 212 213 super.requestPreprocessor(comms); 214 if (comms.session == null) { 215 return false; 217 } 218 219 String pp = comms.request.getApplicationPath(); 220 if (!pp.endsWith("/")) 221 pp += "/"; 222 String loginDir = pp + "login/"; 223 String mediaDir = pp + "media/"; 224 String page = comms.request.getRequestURI(); 225 if (page.startsWith(loginDir) || page.startsWith(mediaDir)) { 226 230 return false; 231 } else { 232 String uri = comms.request.getRequestURI(); 234 boolean is= uri.startsWith("/GolfShop_pres"); 235 236 if(is) 237 238 return false; 239 240 else{ 241 if (comms.session.getUser() != null) 242 return false; 244 else { 245 String url = pp + "login/Login.po"; 247 throw new ClientPageRedirectException(url); 248 } 249 } 250 } 251 } 252 253 256 private String makeSessionTable(Enumeration allSessions) { 257 StringBuffer results = new StringBuffer (); 258 259 results.append("<TABLE><TR><TD ALIGN=CENTER><B>Username</B></TD>" + 260 "<TD ALIGN=CENTER><B>Current total</B></TD></TR>\n"); 261 while (allSessions.hasMoreElements()) { 262 String sessionKey = (String )allSessions.nextElement(); 263 Session session; 264 try { 265 session = sessionManager.getSession(sessionKey); 266 } catch (SessionException except) { 267 continue; 269 } 270 Cart cart = null; 271 String username = null; 272 String totalStr = null; 273 274 User user = session.getUser(); 275 if (user != null) { 276 username = user.getName(); 277 } else { 278 username = "<I>Not logged in yet</I>"; 279 } 280 281 SessionData sd = session.getSessionData(); 282 try { 283 cart = (Cart)sd.get("cart"); 284 } catch (KeywordValueException kve) { 285 } 286 if (cart != null) { 287 double total = cart.getTotal(); 288 BigDecimal total2 = new BigDecimal (total); 289 total2 = total2.setScale(2, BigDecimal.ROUND_HALF_UP); 290 totalStr = "$" + total2; 291 } else { 292 totalStr = "<I>No cart</I>"; 293 } 294 results.append("<TR><TD ALIGN=CENTER>"); 295 results.append(username); 296 results.append("</TD><TD ALIGN=RIGHT>"); 297 results.append("</TD></TR>\n"); 298 } 299 results.append("</TABLE>\n"); 300 return results.toString(); 301 } 302 303 308 public String toHtml() { 309 Enumeration allSessions; 310 try { 311 allSessions = sessionManager.getSessionKeys(); 312 } catch (SessionException except) { 313 return except.getClass().getName() + " " + except.getMessage(); 315 } 316 if (!allSessions.hasMoreElements()) { 317 return "There are no current sessions."; 318 } else { 319 return makeSessionTable(allSessions); 320 } 321 } 322 } 323 | Popular Tags |