KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > barracudaDiscRack > BarracudaDiscRackEnhydra


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: BarracudaDiscRackEnhydra.java,v 1.5 2004/12/20 11:15:54 slobodan Exp $
22  */

23
24 package barracudaDiscRack;
25
26 import com.lutris.appserver.server.*;
27 import com.lutris.appserver.server.httpPresentation.*;
28 import com.lutris.appserver.server.session.*;
29 import com.lutris.util.*;
30 import javax.servlet.*;
31 import javax.servlet.http.*;
32 import java.util.*;
33 import java.io.*;
34
35 /**
36  * The application object.
37  *
38  * Application-wide data would go here.
39  */

40 public class BarracudaDiscRackEnhydra extends StandardApplication {
41
42     /*
43      * A few methods you might want to add to.
44      * See StandardApplication for more details.
45      */

46     public void startup(Config appConfig)
47     throws ApplicationException {
48         super.startup(appConfig);
49         // Here is where you would read application-specific settings from
50
// your config file.
51

52         // setup the Disc Rack gateway to barracuda
53
try {
54             // setup the init parameters for the gateway
55
// if present in the applications config file
56
// default to the standard config from multiserver/enhydra
57
ServletConfig servletConfig =
58             this.getHttpPresentationManager().getServlet().getServletConfig();
59             Config gatewayConfig = appConfig.getConfig("InitGateway");
60             if ( null != gatewayConfig ) {
61                 servletConfig =
62                 new ServletConfigWrapper(
63                                                                 this.getHttpPresentationManager().getServletContext(),
64                                                                 gatewayConfig,
65                                                                 servletConfig.getServletName() );
66             }
67
68             this.myDiscRackGateway = new DiscRackGateway();
69             this.myGatewayExtension = this.myDiscRackGateway.getEventExtension();
70             this.myDiscRackGateway.init( servletConfig );
71         } catch ( ServletException ex ) {
72             throw ( new ApplicationException( "Error initilising DiscRackGateway", ex ) );
73         } catch ( KeywordValueException ex ) {
74             throw ( new ApplicationException( "Error initilising DiscRackGateway", ex ) );
75         }
76     }
77     
78     /*
79     * destroy Barracuda Disc Rack gateway
80     */

81     public void shutdown () {
82         super.shutdown();
83         this.myDiscRackGateway.destroy();
84     }
85
86     /**
87      * Default method used by <CODE>requestPreprocessor</CODE>
88      * to ensure that a session exists and initialize the
89      * <CODE>session</CODE> and <CODE>sessionData</CODE> fields
90      * in the <CODE>HttpPresentationComms</CODE> object.
91      * New sessions are only created on requests to presentation
92      * objects, not requests for other types of files (such as gifs).
93      * This avoids allocating multiple sessions when a browser makes
94      * multiple requests for HREFs. If the session already exist, it
95      * is alwasy set in <CODE>comms</CODE><P>
96      *
97      * This normally looks up the session using a session key from
98      * a cookie.
99      *
100      * @param comms
101      * Object containing request, response and redirect objects.
102      * @exception ApplicationException
103      * If an error occurs setting up the session.
104      */

105     protected void ensureSession(HttpPresentationComms comms)
106     throws ApplicationException {
107
108         try {
109             boolean isOkForSession = presentationManager.isPresentationRequest(comms.request) ||
110                                                              comms.request.getRequestURI().indexOf(this.myGatewayExtension) > -1;
111
112             if ( isOkForSession ) {
113                 // FIX - verify OK
114
comms.session = StandardAppUtil.getRequestSession(comms);
115             }
116             if ( comms.session != null &&
117                      sessionManager.sessionExists(comms.session.getSessionKey()) ) {
118                 /*
119                  * InitializeNewSession() sets up comms.sessionData, so
120                  * that it may be used if an application defines it's own
121                  * version of that method (after calling the super method,
122                  * it has a normal comms object to work with).
123                  * Therefore, if we did not call initializeNewSession(),
124                  * we have to initialize comms.sessionData here.
125                  */

126                 comms.sessionData = comms.session.getSessionData();
127             } else {
128
129                 if ( isOkForSession ) {
130                     initializeNewSession(comms);
131                 }
132             }
133         } catch ( HttpPresentationException except ) {
134             throw new ApplicationException(except);
135         } catch ( SessionException except ) {
136             throw new ApplicationException(except);
137         }
138     }
139
140     public boolean requestPreprocessor(HttpPresentationComms comms)
141     throws Exception JavaDoc{
142         boolean retVal = super.requestPreprocessor(comms);
143         if ( !retVal ) {
144             String JavaDoc target = comms.request.getRequestURI();
145             // if has the event extension in the URI then treat it as a barracuda event
146
if ( target.indexOf(this.myGatewayExtension) > -1 ) {
147                 this.myDiscRackGateway.handleDefaultExt(
148                                                                                              comms.request.getHttpServletRequest(),
149                                                                                              comms.response.getHttpServletResponse(),
150                                                                                              comms );
151                 retVal = true;
152             }
153         }
154         return retVal;
155     }
156
157     /**
158      * This is an optional function, used only by the Multiserver's graphical
159      * administration. This bit of HTML appears in the status page for this
160      * application. You could add extra status info, for example
161      * a list of currently logged in users.
162      *
163      * @return HTML that is displayed in the status page of the Multiserver.
164      */

165     public String JavaDoc toHtml() {
166         return "This is <I>DiscRack</I>";
167     }
168
169     private DiscRackGateway myDiscRackGateway;
170     private String JavaDoc myGatewayExtension;
171 }
172
173
174 class ServletConfigWrapper implements ServletConfig {
175
176     private ServletContext myContext;
177     private Config myConfig;
178     private String JavaDoc myName;
179
180     public ServletConfigWrapper( ServletContext context, Config config, String JavaDoc name ) {
181         this.myContext = context;
182         this.myConfig = config;
183         this.myName = name;
184     }
185
186     public ServletContext getServletContext() {
187         return this.myContext;
188     }
189
190     public String JavaDoc getInitParameter(String JavaDoc name) {
191         String JavaDoc retVal = null;
192         try {
193             retVal = this.myConfig.getString(name);
194         } catch ( KeywordValueException ex ) {
195         }
196         return retVal;
197     }
198
199     public Enumeration getInitParameterNames() {
200         return( Collections.enumeration(Arrays.asList(this.myConfig.keys())) );
201     }
202
203     public String JavaDoc getServletName() {
204         return this.myName;
205     }
206
207 }
208
Popular Tags