/*******************************************************************************
 * Copyright (c) 2010 Oak Ridge National Laboratory.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/
package org.csstudio.sns.jms2rdb;

import java.util.logging.Level;

import org.csstudio.logging.LogConfigurator;
import org.csstudio.platform.httpd.HttpServiceHelper;
import org.csstudio.platform.logging.JMSLogMessage;
import org.csstudio.sns.jms2rdb.httpd.MainServlet;
import org.csstudio.sns.jms2rdb.httpd.StopServlet;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.preferences.IPreferencesService;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.osgi.framework.Constants;
import org.osgi.service.http.HttpContext;
import org.osgi.service.http.HttpService;

/** Application's 'Main' class.
 *  @author Kay Kasemir
 *  reviewed by Katia Danilova 08/20/08
 */
@SuppressWarnings("nls")
public class Application implements IApplication
{
    /** HTTP Server port */
    private int httpd_port = 9500;

    /** JMS Server URL */
    private String jms_url = "tcp://localhost:61616";

    /** JMS Server topic */
    private String jms_topic = JMSLogMessage.DEFAULT_TOPIC;

    /** Filters for suppressed JMS messages
     *  @see Filter
     */
    private String jms_filters = "ALARM;TEXT=IDLE";

    /** RDB Server URL */
    private String rdb_url = "jdbc:mysql://[host]/[database]?user=[user]&password=[password]";

    /** RDB Schema */
    private String rdb_schema = "";

    /** Thread that handles the JMS messages */
    private LogClientThread log_client_thread;

    /** {@inheritDoc} */
    @Override
    public Object start(IApplicationContext context) throws Exception
    {
        // Read settings from preferences.
        // Order of preference lookup:
        // - GUI applications can provide a preferences GUI to allow
        //   users to change settings
        // - Command-line argument
        //      -pluginCustomization my_customization.ini
        //   used when running the application.
        //   File has lines of format:
        //      plugin-id/pref_key=value
        // - File plugin_customization.ini in the application plugin root dir.
        //      plugin-id/pref_key=value
        // - File preferences.ini in this plugin's root directory:
        //      pref_key=value
        // - If all fails, the 'default' argument to
        //   service.getXXX(.., .., default, ..) is used
        final IPreferencesService service = Platform.getPreferencesService();
        httpd_port =
            service.getInt(Activator.ID, "httpd_port", httpd_port, null);
        jms_url =
            service.getString(Activator.ID, "jms_url", jms_url, null);
        jms_topic =
            service.getString(Activator.ID, "jms_topic", jms_topic, null);
        jms_filters =
            service.getString(Activator.ID, "jms_filters", jms_filters, null);
        rdb_url =
            service.getString(Activator.ID, "rdb_url", rdb_url, null);
        rdb_schema =
            service.getString(Activator.ID, "rdb_schema", rdb_schema, null);

        LogConfigurator.configureFromPreferences();

        final String version = (String) context.getBrandingBundle().getHeaders().get(Constants.BUNDLE_VERSION);
        Activator.getLogger().log(Level.CONFIG, "Started JMS Log Tool {0}", version);

        // Start log handler and web interface
        log_client_thread =
            new LogClientThread(jms_url, jms_topic, rdb_url, rdb_schema,
                                Filter.parse(jms_filters));
        startHttpd();
        log_client_thread.start();
        // .. Wait while thread is running ..
        log_client_thread.join();

        // Shutdown
        stopHttpd();

        return IApplication.EXIT_OK;
    }

    /** Start the web server */
    private void startHttpd() throws Exception
    {
        final HttpService httpd = HttpServiceHelper.createHttpService(
                Activator.getInstance().getBundle().getBundleContext(), httpd_port);
        final HttpContext context = httpd.createDefaultHttpContext();
        httpd.registerResources("/", "/webroot", context);
        httpd.registerServlet("/main", new MainServlet(log_client_thread), null, context);
        httpd.registerServlet("/stop", new StopServlet(this), null, context);

        Activator.getLogger().log(Level.CONFIG, "Web server at http://localhost:{0}/main", httpd_port);
    }

    /** Stop the web server */
    private void stopHttpd() throws Exception
    {
        HttpServiceHelper.stopHttpService(httpd_port);
    }

    /** {@inheritDoc} */
    @Override
    public void stop()
    {
        Activator.getLogger().info("Stop requested");
        log_client_thread.cancel();
    }
}

@@ -4632,24 +4632,168 @@
  context);%0A%0A
+        // Format port as string because otherwise it'll show up as %224,913%22 in US locale%0A        // when using the log formatter for an integer%0A
         Acti
@@ -4864,18 +4864,11 @@
 ost:
-%7B0%7D/main%22,
+%22 +
  htt
@@ -4866,32 +4866,42 @@
 t:%22 + httpd_port
+ + %22/main%22
 );%0A    %7D%0A%0A    /*

Replace CentralLogger with java.util.logging