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: BarracudaDiscRackEnhydra.java,v 1.1 2006-09-11 12:30:36 sinisa Exp $
022: */
023:
024: package barracudaDiscRack;
025:
026: import com.lutris.appserver.server.*;
027: import com.lutris.appserver.server.httpPresentation.*;
028: import com.lutris.appserver.server.session.*;
029: import com.lutris.util.*;
030: import javax.servlet.*;
031: import javax.servlet.http.*;
032: import java.util.*;
033: import java.io.*;
034:
035: /**
036: * The application object.
037: *
038: * Application-wide data would go here.
039: */
040: public class BarracudaDiscRackEnhydra extends StandardApplication {
041:
042: /*
043: * A few methods you might want to add to.
044: * See StandardApplication for more details.
045: */
046: public void startup(Config appConfig) throws ApplicationException {
047: super .startup(appConfig);
048: // Here is where you would read application-specific settings from
049: // your config file.
050:
051: // setup the Disc Rack gateway to barracuda
052: try {
053: // setup the init parameters for the gateway
054: // if present in the applications config file
055: // default to the standard config from multiserver/enhydra
056: ServletConfig servletConfig = this
057: .getHttpPresentationManager().getServlet()
058: .getServletConfig();
059: Config gatewayConfig = appConfig.getConfig("InitGateway");
060: if (null != gatewayConfig) {
061: servletConfig = new ServletConfigWrapper(this
062: .getHttpPresentationManager()
063: .getServletContext(), gatewayConfig,
064: servletConfig.getServletName());
065: }
066:
067: this .myDiscRackGateway = new DiscRackGateway();
068: this .myGatewayExtension = this .myDiscRackGateway
069: .getEventExtension();
070: this .myDiscRackGateway.init(servletConfig);
071: } catch (ServletException ex) {
072: throw (new ApplicationException(
073: "Error initilising DiscRackGateway", ex));
074: } catch (KeywordValueException ex) {
075: throw (new ApplicationException(
076: "Error initilising DiscRackGateway", ex));
077: }
078: }
079:
080: /*
081: * destroy Barracuda Disc Rack gateway
082: */
083: public void shutdown() {
084: super .shutdown();
085: this .myDiscRackGateway.destroy();
086: }
087:
088: /**
089: * Default method used by <CODE>requestPreprocessor</CODE>
090: * to ensure that a session exists and initialize the
091: * <CODE>session</CODE> and <CODE>sessionData</CODE> fields
092: * in the <CODE>HttpPresentationComms</CODE> object.
093: * New sessions are only created on requests to presentation
094: * objects, not requests for other types of files (such as gifs).
095: * This avoids allocating multiple sessions when a browser makes
096: * multiple requests for HREFs. If the session already exist, it
097: * is alwasy set in <CODE>comms</CODE><P>
098: *
099: * This normally looks up the session using a session key from
100: * a cookie.
101: *
102: * @param comms
103: * Object containing request, response and redirect objects.
104: * @exception ApplicationException
105: * If an error occurs setting up the session.
106: */
107: protected void ensureSession(HttpPresentationComms comms)
108: throws ApplicationException {
109:
110: try {
111: boolean isOkForSession = presentationManager
112: .isPresentationRequest(comms.request)
113: || comms.request.getRequestURI().indexOf(
114: this .myGatewayExtension) > -1;
115:
116: if (isOkForSession) {
117: // FIX - verify OK
118: comms.session = StandardAppUtil
119: .getRequestSession(comms);
120: }
121: if (comms.session != null
122: && sessionManager.sessionExists(comms.session
123: .getSessionKey())) {
124: /*
125: * InitializeNewSession() sets up comms.sessionData, so
126: * that it may be used if an application defines it's own
127: * version of that method (after calling the super method,
128: * it has a normal comms object to work with).
129: * Therefore, if we did not call initializeNewSession(),
130: * we have to initialize comms.sessionData here.
131: */
132: comms.sessionData = comms.session.getSessionData();
133: } else {
134:
135: if (isOkForSession) {
136: initializeNewSession(comms);
137: }
138: }
139: } catch (HttpPresentationException except) {
140: throw new ApplicationException(except);
141: } catch (SessionException except) {
142: throw new ApplicationException(except);
143: }
144: }
145:
146: public boolean requestPreprocessor(HttpPresentationComms comms)
147: throws Exception {
148: boolean retVal = super .requestPreprocessor(comms);
149: if (!retVal) {
150: String target = comms.request.getRequestURI();
151: // if has the event extension in the URI then treat it as a barracuda event
152: if (target.indexOf(this .myGatewayExtension) > -1) {
153: this .myDiscRackGateway.handleDefaultExt(comms.request
154: .getHttpServletRequest(), comms.response
155: .getHttpServletResponse(), comms);
156: retVal = true;
157: }
158: }
159: return retVal;
160: }
161:
162: /**
163: * This is an optional function, used only by the Multiserver's graphical
164: * administration. This bit of HTML appears in the status page for this
165: * application. You could add extra status info, for example
166: * a list of currently logged in users.
167: *
168: * @return HTML that is displayed in the status page of the Multiserver.
169: */
170: public String toHtml() {
171: return "This is <I>DiscRack</I>";
172: }
173:
174: private DiscRackGateway myDiscRackGateway;
175: private String myGatewayExtension;
176: }
177:
178: class ServletConfigWrapper implements ServletConfig {
179:
180: private ServletContext myContext;
181: private Config myConfig;
182: private String myName;
183:
184: public ServletConfigWrapper(ServletContext context, Config config,
185: String name) {
186: this .myContext = context;
187: this .myConfig = config;
188: this .myName = name;
189: }
190:
191: public ServletContext getServletContext() {
192: return this .myContext;
193: }
194:
195: public String getInitParameter(String name) {
196: String retVal = null;
197: try {
198: retVal = this .myConfig.getString(name);
199: } catch (KeywordValueException ex) {
200: }
201: return retVal;
202: }
203:
204: public Enumeration getInitParameterNames() {
205: return (Collections.enumeration(Arrays.asList(this .myConfig
206: .keys())));
207: }
208:
209: public String getServletName() {
210: return this.myName;
211: }
212:
213: }
|