KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > discRack > DiscRackGateway


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: DiscRackGateway.java,v 1.5 2004/06/04 19:12:14 slobodan Exp $
22  */

23 package org.enhydra.barracuda.discRack;
24
25 import java.io.*;
26 import java.util.*;
27 import java.net.*;
28 import javax.servlet.*;
29 import javax.servlet.http.*;
30
31 import com.lutris.appserver.server.Application;
32 import com.lutris.appserver.server.Enhydra;
33 import com.lutris.appserver.server.StandardApplication;
34 import com.lutris.logging.StandardLogger;
35 import com.lutris.util.Config;
36 import com.lutris.util.ConfigFile;
37
38 import org.enhydra.barracuda.core.event.*;
39 import org.enhydra.barracuda.core.event.helper.*;
40 import org.apache.log4j.*;
41
42 import org.enhydra.barracuda.discRack.pres.screens.*;
43 //import org.enhydra.barracuda.examples.ex2.ConfigScreen;
44

45 //import org.enhydra.instantdb.jdbc.idbDriver;
46

47 /**
48  * <p>Application Gateway for the Barracuda DiscRack example. Couple of
49  * special notes regarding deployment.
50  *
51  * <p>1. This example was built using Enhydra 3.1 and XMLC 1.2. For
52  * best results, use these versions or higher.
53  *
54  * <p>2. This example uses Instant DB and the standard DODS generated
55  * data classes from previous DiscRack examples. To make it work,
56  * you will need to add 3 jar files to the enhydra classpath:
57  * <ul>
58  * <li>idb.jar (v3.21)</li>
59  * <li>jdbc2_0-stdext.jar</li>
60  * <li>jta-spec1_0_1.jar</li>
61  * </ul>
62  *
63  * <p>We can't just put these files in /Web-inf/lib because DODs is
64  * dependant on the enhydra Application interface, and the
65  * StandardApplication implementation requires these jar files
66  * be part of the enhydra classpath
67  *
68  * <p>3. You must manually build/populate the IDB databases in /Web-inf/db.
69  * To do this use the idb ScriptTool on sql1.txt (in the db directory)
70  *
71  * <p>To understand this example, you should really look at the class diagram
72  * which illustrates how everythign fits together.
73  * <a HREF="sm_barracudaDiscRack.gif">You can view it here...</a>
74  *
75  */

76 public class DiscRackGateway extends ApplicationGateway {
77
78     //public constants
79
public static final String JavaDoc ENHYDRA_APPLICATION = DiscRackGateway.class.getName()+"EnhydraApplication";
80     protected static Logger logger = Logger.getLogger(DiscRackGateway.class.getName());
81
82     //private vars
83
private static final String JavaDoc CONFIG_FILE = "ConfFile";
84 // private ServletConfig config = null;
85
private LoginScreen lnkLoginScreen; //added by TJ to show association
86
private RegistrationScreen lnkRegistrationScreen; //added by TJ to show association
87
private DiscScreen lnkDiscScreen; //added by TJ to show association
88

89
90
91     //-------------------- ApplicationGateway --------------------
92
/**
93      * Perform any local initialization (this is where you should
94      * specify any known EventGateways)
95      */

96     public void initializeLocal() {
97         //set up the enhydra app for DODs stuff
98
// this.config = config;
99
setUpEnhydraApplication();
100
101         //specify the event gateways
102
this.specifyEventGateways(new LoginScreen());
103         this.specifyEventGateways(new RegistrationScreen());
104         this.specifyEventGateways(new DiscScreen());
105     }
106
107
108     /**
109      * Set up an underlying Enhydra application for database
110      * related stuff (DODS needs this to function properly).
111      * We'll store a reference to the Application in the
112      * ServletContext, so if anyone needs access to it, they
113      * can get it.
114      */

115 // private void setUpEnhydraApplication(ServletConfig config) {
116
private void setUpEnhydraApplication() {
117         //get the context and see if the app is already set
118
ServletContext context = this.getServletConfig().getServletContext();
119         Application app = (Application) context.getAttribute(ENHYDRA_APPLICATION);
120
121         //if not, create it...
122
if (app==null) {
123             try {
124                 //create the underlying enhydra Application
125
if (logger.isDebugEnabled()) logger.debug("Creating underlying Enhydra Application");
126                 app = new DiscRackApplication();
127
128                 //set the log channel
129
app.setLogChannel(new StandardLogger(true).getChannel(""));
130
131                 //get the config file
132
String JavaDoc configFile = this.getServletConfig().getInitParameter(CONFIG_FILE);
133
134                 if (logger.isDebugEnabled()) logger.debug("Config:"+configFile);
135
136                 //InputStream is = this.getClass().getResourceAsStream(configFile);
137
//InputStream is = context.getResourceAsStream(configFile);
138
//if (is==null) is = this.getClass().getResourceAsStream(configFile);
139

140         String JavaDoc is = context.getRealPath(configFile);
141                 File isf = new File (is);
142
143                 Config conf = new ConfigFile(isf).getConfig();
144                 if (logger.isDebugEnabled()) logger.debug("Got Config file:"+conf);
145
146                 //startup the app
147
app.startup(conf);
148                 if (logger.isDebugEnabled()) logger.debug("Started app:"+app);
149
150                 //save it in the context
151
context.setAttribute(ENHYDRA_APPLICATION, app);
152             } catch(Exception JavaDoc e) {
153                 System.out.println("Error setting up Enhydra Application:"+e);
154                 e.printStackTrace();
155             }
156         }
157     }
158
159     /**
160      * Handle all HTTP requests
161      */

162     public void handleDefault (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
163         ServletContext context = this.getServletConfig().getServletContext();
164         Application app = (Application) context.getAttribute(ENHYDRA_APPLICATION);
165         try {
166             if (logger.isDebugEnabled()) logger.debug("\n\n"+req.getRequestURI()+" @"+Integer.toHexString(req.hashCode()));
167             Enhydra.register(app);
168             super.handleDefault(req, resp);
169         } finally {
170             Enhydra.unRegister();
171         }
172     }
173
174     class DiscRackApplication extends StandardApplication {
175
176     }
177
178 }
179
Popular Tags