Project

General

Profile

« Previous | Next » 

Revision 3081

Added by Matt Jones over 17 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:

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