📄 midpservice.java
字号:
/* * Copyright 1999-2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistribution in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * Neither the name of Sun Microsystems, Inc. or the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * This software is provided "AS IS," without a warranty of any * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that Software is not designed, licensed or intended * for use in the design, construction, operation or maintenance of * any nuclear facility. */package com.sun.j2ee.blueprints.smarticket.web;import shared.*;import com.sun.j2ee.blueprints.smarticket.ejb.customer.*;import com.sun.j2ee.blueprints.smarticket.ejb.movieinfo.*;import com.sun.j2ee.blueprints.smarticket.ejb.ticketsales.*;import com.sun.j2ee.blueprints.smarticket.ejb.localeinfo.*;import java.io.*;import java.util.*;import java.rmi.*;import javax.ejb.*;import javax.naming.*;import javax.rmi.*;/** * A service that handles MIDP client requests for this application. */public class MIDPService implements MessageConstants { /** Version of data format sent to the client */ static final byte CODEC_VERSION = 1; MovieInfo movieInfo = null; CustomerInformation customerInfo = null; TicketSales ticketSales = null; String sessionURL = null; public MIDPService() throws MIDPException { try { movieInfo = createMovieInfoEJB(); } catch (Exception e) { serverError(e); } } public void removeMovieInfo() throws MIDPException { if (movieInfo != null) { try { movieInfo.remove(); movieInfo = null; } catch (RemoteException rem) { serverError(rem); } catch (RemoveException rev) { serverError(rev); } } } /** * <p>Processes the given request, which is passed as a string. * Returns a string which contains the data requested.</p> * * <p>This assumes the message is formatted as:</p> * * <p><code>command^param1,param2,...</code></p> * * <p>where command is a constant from * <code>MessageConstants</code>.</p> * * @param req request represented as a string * @return the requested data * @see MessageConstants */ public boolean processRequest(DataOutputStream out, String req, String sessionURL) throws MIDPException { this.sessionURL = sessionURL; StringTokenizer params; int cmd; boolean invalidate = false; try { // Split the request string into its parts. // Interpret the command and deal with it appropriately. int i = req.indexOf("^"); cmd = Integer.parseInt(req.substring(0, i)); params = new StringTokenizer(req.substring(i+1), ","); } catch (Exception e) { throw new MIDPException(MESSAGE_ERROR); } switch (cmd) { case CREATE_USER: createUser(out, params); break; case LOGIN_USER: loginUser(out, params); break; case DISPLAY_LOCALES: displayLocales(out, params); break; case LOAD_MESSAGES: loadMessages(out, params); break; case DISPLAY_MOVIES: displayMovies(out, params); break; case DISPLAY_LOCATIONS: displayLocations(out, params); break; case DISPLAY_SHOWTIMES: displayShowtimes(out, params); break; case DISPLAY_SEATINGPLAN: displaySeatingPlan(out, params); break; case RESERVE_SEATS: reserveSeats(out, params); break; case CONFIRM_SEATS: confirmSeats(out, params); invalidate = true; break; case CANCEL_SEATS: cancelSeats(out, params); break; default: // We shouldn't get to this point. throw new MIDPException(MESSAGE_ERROR); } return invalidate; } // Parameters: username, password, zipCode, creditCard void createUser(DataOutputStream out, StringTokenizer params) throws MIDPException { Customer c = null; try { String username = params.nextToken(); String password = params.nextToken(); String zipCode = params.nextToken(); String creditCard = params.nextToken(); c = createCustomerEJB(username, password, zipCode, creditCard); customerInfo = c.getInformation(); out.writeUTF(sessionURL); } catch (DuplicateKeyException dke) { throw new MIDPException(USER_ALREADY_EXISTS_ERROR); } catch (Exception e) { serverError(e); } finally { if (c != null) { try { c.remove(); } catch (RemoteException rem) { serverError(rem); } catch (RemoveException rev) { serverError(rev); } } } } // Parameters: <none> void displayLocales(DataOutputStream out, StringTokenizer params) throws MIDPException { LocaleInfo li = null; try { li = createLocaleInfoEJB(); List locales = li.getLocales(); out.writeInt(locales.size()); for (int i = 0; i < locales.size(); i++) { MessageLocale locale = (MessageLocale)locales.get(i); out.writeInt(locale.getID()); out.writeUTF(locale.getLocale()); } } catch (Exception e) { serverError(e); } finally { if (li != null) { try { li.remove(); } catch (RemoteException rem) { serverError(rem); } catch (RemoveException rev) { serverError(rev); } } } } // Parameters: localeID void loadMessages(DataOutputStream out, StringTokenizer params) throws MIDPException { LocaleInfo li = null; try { int localeID = Integer.parseInt(params.nextToken()); li = createLocaleInfoEJB(); List messages = li.getMessages(localeID); StringBuffer sb = new StringBuffer(256); for (int i = 0; i < messages.size(); i++) { Message message = (Message) messages.get(i); sb.append(Integer.toString(message.getID())); sb.append("="); sb.append(message.getMessage()); sb.append("\n"); } out.writeUTF(sb.toString()); } catch (Exception e) { serverError(e); } finally { if (li != null) { try { li.remove(); } catch (RemoteException rem) { serverError(rem); } catch (RemoveException rev) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -