Project

General

Profile

« Previous | Next » 

Revision 3081

Added by Matt Jones about 18 years ago

Began work on a web interface for configuring metacat at runtime. This system
checks the 'configured' property when metacat starts up. If configured=false, then
it loads an HTML form containing the properties that need to be set. The user configures the properties and they are written into the metacat.properties file. Then metacat needs to be restarted.

Lots still to do here -- its not really useful yet because I hardcoded 2 properties to be collected (user and password), but really the list should be gotten from the properties file itself.

This should not get in your way because by default I checked in 'configured=true' to the properties file so the configuration code is never called.

View differences:

lib/configure-restart.jsp
1
<%@ page    language="java" %>
2
<% 
3
/**
4
 *  '$RCSfile$'
5
 *    Copyright: 2006 Regents of the University of California and the
6
 *               National Center for Ecological Analysis and Synthesis
7
 *  For Details: http://www.nceas.ucsb.edu/
8
 *
9
 *   '$Author$'
10
 *     '$Date$'
11
 * '$Revision$'
12
 * 
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 */
27
%>
28
<html>
29
<head>
30
<title>Metacat Configuration Succeeded</title>
31
<style language="Javascript">
32
<!--
33
.section {
34
    font-size: 16pt;
35
    font-weight: bold;
36
    border-bottom: solid #dddddd 1px;
37
}
38
-->
39
</style>
40
</head>
41
<body>
42
<p><img src="metacat-logo.png" width="75px" align="right"/> 
43
<h2>Metacat Configuration Succeeded</h2>
44
</p>
45

  
46
<p>Congratulations!</p>
47
<p>You have now successfully configured this installation of Metacat. All that
48
   remains is to restart the metacat servlet for the changes to take effect.
49
</p>
50
<br clear="right"/>
51
<p class="section">Restarting Metacat</p>
52
<p>The simplest way to restart metacat is to restart the entire servlet engine.
53
   For Tomcat, this would mean calling "$TOMCAT_HOME/bin/tomcat.sh restart" or
54
   an equivalent command appropriate to your operating system.
55
</p>
56

  
57
</body>
58
</html>
0 59

  
lib/configure.jsp
1
<%@ page    language="java" %>
2
<% 
3
/**
4
 *  '$RCSfile$'
5
 *    Copyright: 2006 Regents of the University of California and the
6
 *               National Center for Ecological Analysis and Synthesis
7
 *  For Details: http://www.nceas.ucsb.edu/
8
 *
9
 *   '$Author$'
10
 *     '$Date$'
11
 * '$Revision$'
12
 * 
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 */
27
%>
28
<html>
29
<head>
30
<title>Metacat Configuration</title>
31
<style language="Javascript">
32
<!--
33
.section {
34
    font-size: 16pt;
35
    font-weight: bold;
36
    border-bottom: solid #dddddd 1px;
37
}
38
-->
39
</style>
40
</head>
41
<body>
42
<p><img src="metacat-logo.png" width="75px" align="right"/> 
43
<h2>Metacat Configuration</h2>
44
</p>
45

  
46
<p>Welcome to MetaCat!</p>
47
<p>This installation of Metacat needs to be configured before it can
48
   function properly. Please fill out the required information and you'll be
49
   up and running.
50
</p>
51
<br clear="right"/>
52
<p class="section">Database connection</p>
53
<form method="POST" action="/knb/metacat">
54
<p>Database Username: 
55
   <input type="text" name="user" value="<%= request.getAttribute("user") %>"/></p>
56
<p>Database Password: <input type="password" name="password" value="<%= request.getAttribute("password") %>"/></p>
57
<p><input type="hidden" name="action" value="configure"/>
58
<p><input type="submit" value="Save"/></p>
59
</form>
60

  
61
<p class="section">Access controls</p>
62
<p>More options go here.</p>
63

  
64
</body>
65
</html>
0 66

  
lib/metacat.properties
1 1
#
2

  
3 2
# Properties file defining options for the MetaCatServlet.java servlet
4

  
3
#
5 4
# Matt Jones, Dan Higgins, Jivka Bojilova
6

  
7 5
# '$Id$'
8

  
9 6
#
10 7

  
11
configured=false
8
######## Configuration section        #########################################
9
# This flag indicates whether configuration information has been collected from
10
# the user installing metacat yet.
11
configured=true
12 12

  
13
configurationPage=/configure.jsp
14

  
15
configurationSuccessPage=/configure-restart.jsp
16

  
13 17
version=1.6.0
14 18

  
19
######## Database section             #########################################
15 20
user=@user@
16 21

  
17 22
password=@password@
......
44 49

  
45 50
cycleTimeOfDBConnection=30000
46 51

  
52
######## Access section               #########################################
47 53
administrators=@administrators@
48 54

  
49 55
moderators=@moderators@
......
176 182
# that combination has been requested 50 times, set this value to 50
177 183
xml_returnfield_count=0
178 184

  
179
# Harvester properties
185
######## Harvester section            #########################################
180 186
connectToMetacat=true
181 187

  
182 188
delay=0
......
223 229

  
224 230
# End of Harvester properties
225 231

  
226

  
227
# BEGIN spatial options
232
######## Spatial section              #########################################
228 233
runSpatialOption=true
229 234
regenerateCacheOnRestart=true
230 235

  
src/edu/ucsb/nceas/metacat/MetaCatServlet.java
156 156
            Options options = null;
157 157
            try {
158 158
                options = Options.initialize(propertyFile);
159
                logMetacat.info("Options configured: "
160
                    + options.getOption("configured"));
161 159
            } catch (IOException ioe) {
162 160
                logMetacat.error("Error in loading options: "
163 161
                        + ioe.getMessage());
164 162
            }
165 163

  
164
            // Check if the servlet was configured, and if not stop the
165
            // initialization and skip to the configuration web form when 
166
            // metacat is called
167
            boolean configured = options.getOption("configured").equals("true");
168
            if (!configured) {
169
                logMetacat.info("Options configured: " + configured);
170
                return;
171
            }
172
            
166 173
            MetaCatUtil util = new MetaCatUtil();
167 174
            
168 175
            //initialize DBConnection pool
......
422 429
        MetaCatUtil util = new MetaCatUtil();
423 430
        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
424 431
        
432
        // Each time metacat is called, see if it has been configured,
433
        // and if not, then go ahead and configure it's options
434
        boolean configured = MetaCatUtil.getOption("configured").equals("true");
435
        if (!configured) {
436
            handleServletConfiguration(request, response);
437
            return;
438
        }
439
        
425 440
        /*
426 441
         * logMetacat.debug("Connection pool size: "
427 442
         * +connPool.getSizeOfDBConnectionPool(),10);
......
3210 3225
        //logMetacat.debug("The docid will be read is "+newAccNum);
3211 3226
        return newAccNum;
3212 3227
  }
3228
    
3229
    /**
3230
     * Handle configuration of the servlet the first time that Metacat starts
3231
     * by collecting necessary configuration information from the administrator.
3232
     * 
3233
     * @param request the http request information
3234
     * @param response the http response to be sent back to the client
3235
     */
3236
    private void handleServletConfiguration(HttpServletRequest request,
3237
            HttpServletResponse response) {
3238
        String action = request.getParameter("action");
3239
        if (action == null || !action.equals("configure")) {
3240
            // The servlet configuration parameters have not been set, so
3241
            // redirect to a web form for configuring metacat
3242
            
3243
            // First add attributes to the request for all configuration options
3244
            // TODO: MBJ should not hard code these, but get them from the config
3245
            // Then the list of attributes can be passed to the form so that the
3246
            // form can autogenerate the right number of fields.  Still need
3247
            // to determine a way to get some metadata about each attribute to
3248
            // the form jsp, such as a description and possibly a widget type
3249
            // to use and possibly a controlled values list
3250
            request.setAttribute( "user", MetaCatUtil.getOption("user"));
3251
            request.setAttribute( "password", MetaCatUtil.getOption("password"));
3252
            
3253
            // Forward the request
3254
            forwardRequest(request, response, 
3255
                    MetaCatUtil.getOption("configurationPage"));
3256
        } else {
3257
            // The configuration form is being submitted and needs to be
3258
            // processed, setting the properties in the configuration file
3259
            // then restarting metacat
3260
            
3261
            // TODO: Should first validate that the options provided are 
3262
            // legitimate, and possibly even test them (for example, use
3263
            // the database connection info to connect to the database)
3264
            
3265
            // TODO: MBJ should not hard code these, but get them from the config
3266
            checkAndSetOption(request, response, "user");
3267
            checkAndSetOption(request, response, "password");
3268
            
3269
            // Now that the options have been set, change the 'configured'
3270
            // option to 'true' so that normal metacat requests will go through
3271
            MetaCatUtil.setOption("configured", "true");
3272
            
3273
            // Reload the servlet so that the database connections are loaded
3274
            // In the absence of being able to do this programatically, then
3275
            // redirect to a response page with instructions to restart
3276
            forwardRequest(request, response, 
3277
                    MetaCatUtil.getOption("configurationSuccessPage"));
3278
        }
3279
    }
3280
    
3281
    /**
3282
     * Take input from the user in an HTTP request about an option to be 
3283
     * changed and update the options property file with that new value
3284
     * if it has changed from the value that was originally set.
3285
     * 
3286
     * @param request that was generated by the user
3287
     * @param response to send output back to the user
3288
     * @param optionName the name of the option to be checked and set
3289
     */
3290
    private void checkAndSetOption(HttpServletRequest request,
3291
            HttpServletResponse response, String optionName) {
3292
        String value = MetaCatUtil.getOption(optionName);
3293
        String newValue = request.getParameter(optionName);
3294
        if (!newValue.equals(value)) {
3295
            MetaCatUtil.setOption(optionName, newValue);
3296
        }
3297
    }
3298
    
3299
    /**
3300
     * Forward a request that was received by this servlet on to another 
3301
     * JSP page or servlet to continue handling the request.  
3302
     * 
3303
     * @param request to be forwarded
3304
     * @param response that can be used for writing output to the client
3305
     * @param destination the context-relative URL to which the request is forwarded
3306
     */
3307
    private void forwardRequest(HttpServletRequest request,
3308
            HttpServletResponse response, String destination) {
3309
            
3310
        try {
3311
            getServletConfig().getServletContext().getRequestDispatcher(       
3312
                    destination).forward(request, response);
3313
        } catch (Exception e1) {
3314
            try {
3315
                PrintWriter out = response.getWriter();
3316
                out.println("Error while forwarding to: " + destination);
3317
                out.close();
3318
            } catch (IOException e) {
3319
                Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
3320
                logMetacat.debug("Unable to return error message to servlet.");
3321
            }
3322
        }
3323
    }
3213 3324
}

Also available in: Unified diff