001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: * $Id: BasePO.java,v 1.1 2006-09-11 12:39:40 sinisa Exp $
022: */
023:
024: package jtaDiscRack.presentation;
025:
026: import com.lutris.appserver.server.httpPresentation.*;
027: import com.lutris.appserver.server.Enhydra;
028: import com.lutris.logging.*;
029: import com.lutris.util.KeywordValueException;
030:
031: import org.enhydra.xml.xmlc.XMLObject;
032:
033: import java.lang.reflect.*;
034:
035: import javax.naming.Context;
036: import javax.naming.InitialContext;
037: import javax.transaction.UserTransaction;
038:
039: import jtaDiscRack.spec.*;
040: import jtaDiscRack.*;
041:
042: /**
043: * This is the parent Presentaion object. All presentation objects should extend
044: * this class.
045: *
046: * The run method looks for an event parameter and then calls handle<EventName>.
047: * If the "event" Parameter is not defined then the handleDefault() method is
048: * called in your child class.
049: *
050: *
051: */
052: public abstract class BasePO implements HttpPresentation {
053:
054: protected static final String USER_KEY = "DiscRackPerson";
055:
056: protected static String LOGIN_PAGE = "personMgmt/Login.po";
057:
058: protected static String DISC_CATALOG_PAGE = "discMgmt/DiscCatalog.po";
059:
060: private static String EVENT = "event";
061:
062: private static String STANDARD_METHOD_PREFIX = "handle";
063:
064: /**
065: * This is the procedure that is called if there is no "event" HTTP
066: * parameter found. It must be overriden by the subclass to do default
067: * processing or error checking/handling.
068: *
069: * @return String The String representation of the HTML or (other format) of
070: * the document to be displayed. This method would need to be
071: * changed if you wanted to return binary data as well. It returns a
072: * String for simplicity right now.
073: */
074: public abstract XMLObject handleDefault()
075: throws HttpPresentationException;
076:
077: /**
078: * This method should be implemented in the subclass so that it returns true
079: * if this particular request requires the user to be logged in, otherwise
080: * false.
081: */
082: protected abstract boolean loggedInUserRequired();
083:
084: /**
085: * Saved input and output context, and session data
086: */
087: protected HttpPresentationComms myComms = null;
088:
089: /**
090: * Session Data object reference
091: */
092: protected JtaDiscRackSessionData mySessionData = null;
093:
094: /**
095: * Transaction object reference
096: */
097: protected UserTransaction transaction = null;
098:
099: /**
100: * Gets HttpPresentation object
101: *
102: * @return The saved comms objects to whichever subclass needs it
103: */
104: public HttpPresentationComms getComms() {
105: return this .myComms;
106: }
107:
108: /**
109: * Gets the session data
110: *
111: * @return session data
112: */
113: public JtaDiscRackSessionData getSessionData() {
114: return this .mySessionData;
115: }
116:
117: /**
118: * Sets the user into the session
119: *
120: * @param thePerson
121: * the person to be set in the session
122: * @exception DiscRackPresentationException
123: */
124: public void setUser(Person thePerson)
125: throws JtaDiscRackPresentationException {
126: this .getSessionData().setUser(thePerson);
127: }
128:
129: /**
130: * Gets the user from the session
131: *
132: * @return the person object in the session
133: */
134: public String getUserHandle() {
135: return this .getSessionData().getUserHandle();
136: }
137:
138: /**
139: * Method to remove the current user from the session
140: */
141: public void removeUserFromSession() {
142: this .getSessionData().removeUser();
143: }
144:
145: /**
146: * This implements the run method in HttpPresentation.
147: *
148: * @param comms
149: * HttpPresentationComms
150: * @exception Exception
151: */
152: public void run(HttpPresentationComms comms) throws Exception {
153: boolean doCommit = true;
154: try {
155: // Initialize new or get the existing session data
156: initSessionData(comms);
157:
158: initTransaction();
159:
160: // Check if the user needs to be logged in for this request.
161: if (this .loggedInUserRequired()) {
162: checkForUserLogin();
163: }
164: // Handle the incoming event request
165: handleEvent(comms);
166: } catch (Exception ex) {
167: doCommit = false;
168: throw new Exception(ex);
169: } finally {
170: if (doCommit) {
171: commitTransaction();
172: } else {
173: rollbackTransaction();
174: }
175: }
176: }
177:
178: /**
179: * Method to get or create the AgSessionData object from the user session
180: * This object is saved in the EbrokerPresentation object
181: *
182: * @param comms
183: * HttpPresentationComms
184: * @exception Exception
185: */
186: protected void initSessionData(HttpPresentationComms comms)
187: throws JtaDiscRackPresentationException {
188: this .myComms = comms;
189:
190: try {
191: Object obj = comms.sessionData
192: .get(JtaDiscRackSessionData.SESSION_KEY);
193: // If we found the session data, save it in a private data member
194: if (null != obj) {
195: this .mySessionData = (JtaDiscRackSessionData) obj;
196: } else {
197: // If no session data was found, create a new session data
198: // instance
199: this .mySessionData = new JtaDiscRackSessionData();
200: comms.sessionData.set(
201: JtaDiscRackSessionData.SESSION_KEY,
202: this .mySessionData);
203: }
204: } catch (KeywordValueException ex) {
205: writeDebugMsg("Problem getting session data from session: "
206: + ex.getMessage());
207: }
208: }
209:
210: /**
211: * Method initializes DB transation instance
212: */
213: protected void initTransaction() throws Exception {
214: Context ctx = new InitialContext();
215: transaction = (UserTransaction) ctx
216: .lookup(UserTransactionJNDI.jNDIName);
217: if (transaction != null) {
218: transaction.begin();
219: }
220: }
221:
222: /**
223: * Method commits DB transation instance
224: */
225: protected void commitTransaction() throws Exception {
226: if (transaction != null) {
227: transaction.commit();
228: }
229:
230: }
231:
232: private void rollbackTransaction() throws Exception {
233: if (transaction != null) {
234: transaction.rollback();
235: }
236: }
237:
238: /**
239: * Checks the session data for a User, if not there then redirects to the
240: * login page
241: */
242: protected void checkForUserLogin()
243: throws ClientPageRedirectException,
244: JtaDiscRackPresentationException {
245:
246: try {
247: String userHandle = getUserHandle();
248:
249: if (null == userHandle || "".equals(userHandle)) {
250: writeDebugMsg("USER NOT FOUND IN SESSION");
251: // send to LoginPage if a logged in user is required.
252: writeDebugMsg("REDIRECTING TO LOGIN PAGE");
253: throw new ClientPageRedirectException(
254: getComms().request.getApplicationPath()
255: + LOGIN_PAGE);
256: }
257: } catch (Exception ex) {
258: throw new JtaDiscRackPresentationException(
259: "Trouble checking for user login status", ex);
260: }
261: }
262:
263: /**
264: * Method to call the proper method for the incoming event
265: *
266: * @param comms
267: * HttpPresentationComms
268: * @exception Exception
269: */
270: public void handleEvent(HttpPresentationComms comms)
271: throws Exception {
272: String event = comms.request.getParameter(EVENT);
273:
274: XMLObject returnHTML = null;
275:
276: if (event == null || event.length() == 0) {
277: returnHTML = handleDefault();
278: } else {
279: returnHTML = getPageContentForEvent(event);
280: }
281: comms.response.writeDOM(returnHTML);
282: }
283:
284: /**
285: * If an event parameter is defined then this invokes the method that
286: * handles that event.
287: *
288: * @param event
289: * the incoming event name
290: * @exception Exception
291: */
292: public XMLObject getPageContentForEvent(String event)
293: throws Exception {
294: try {
295: Method method = this .getClass().getMethod(
296: toMethodName(event), null);
297: XMLObject thePage = (XMLObject) method.invoke(this , null);
298: return thePage;
299:
300: } catch (InvocationTargetException ex) {
301: // Rethrow the originating exception if as it should be propagated
302: // as is
303: // It could be a page redirect exception, etc.
304: if (ex.getTargetException() instanceof Exception) {
305: throw (Exception) ex.getTargetException();
306: } else if (ex.getTargetException() instanceof Error) {
307: throw (Error) ex.getTargetException();
308: } else {
309: throw ex;
310: }
311: } catch (NoSuchMethodException ex) {
312: // The method to handle the event does not exist.
313: throw new JtaDiscRackPresentationException(
314: "NO EVENT HANDLER FOUND FOR EVENT: " + event, ex);
315: } catch (IllegalAccessException ex) {
316: // The method to handle the event does not exist.
317: throw new JtaDiscRackPresentationException(
318: "ILLEGAL ACCESS TO EVENT HANDLER (is it public?): "
319: + event, ex);
320: }
321: }
322:
323: /**
324: * This sets the first letter of the event parameter value in order to
325: * adhere to Java method naming conventions.
326: *
327: * @param event
328: * the incoming name of the event
329: * @return String the properly capitalized name
330: */
331: private String toMethodName(String event) {
332: StringBuffer methodName = new StringBuffer(
333: STANDARD_METHOD_PREFIX);
334: methodName.append(Character.toUpperCase(event.charAt(0)));
335:
336: if (event.length() > 1) {
337: methodName.append(event.substring(1));
338: }
339:
340: return methodName.toString();
341: }
342:
343: /**
344: * Returns the application object associated with the current request.
345: *
346: * @return the application object.
347: */
348: public JtaDiscRack getApplication() {
349: return (JtaDiscRack) Enhydra.getApplication();
350: }
351:
352: /**
353: * Method to write a debugging message to the debug log channel when the
354: * DEBUG flag is turned on
355: *
356: * @param msg
357: * The message to write to the DEBUG log channel
358: */
359: public static void writeDebugMsg(String msg) {
360: Enhydra.getLogChannel().write(Logger.DEBUG, msg);
361: }
362: }
|