/*
 * Copyright (c) 2016, Ronald DeSantis
 *
 *	Licensed under the Apache License, Version 2.0 (the "License");
 *	you may not use this file except in compliance with the License.
 *	You may obtain a copy of the License at
 *
 *		http://www.apache.org/licenses/LICENSE-2.0
 *
 *	Unless required by applicable law or agreed to in writing, software
 *	distributed under the License is distributed on an "AS IS" BASIS,
 *	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *	See the License for the specific language governing permissions and
 *	limitations under the License.
 */

package com.hauldata.dbpa;

import io.dropwizard.Application;
import io.dropwizard.Configuration;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;

import com.hauldata.dbpa.manage.JobManager;
import com.hauldata.dbpa.manage.exceptions.DefaultExceptionMapper;
import com.hauldata.dbpa.manage.exceptions.FileNotFoundExceptionMapper;
import com.hauldata.dbpa.manage.exceptions.JobManagerExceptionMapper;
import com.hauldata.dbpa.manage.exceptions.NameNotFoundExceptionMapper;
import com.hauldata.dbpa.manage.exceptions.NoSuchElementExceptionMapper;
import com.hauldata.dbpa.manage.exceptions.NoSuchFileExceptionMapper;
import com.hauldata.dbpa.manage.exceptions.RuntimeExceptionMapper;
import com.hauldata.dbpa.manage.health.ManagerHealthCheck;
import com.hauldata.dbpa.manage.health.SchemaHealthCheck;
import com.hauldata.dbpa.manage.resources.JobsResource;
import com.hauldata.dbpa.manage.resources.ManagerResource;
import com.hauldata.dbpa.manage.resources.PropertiesFilesResource;
import com.hauldata.dbpa.manage.resources.SchedulesResource;
import com.hauldata.dbpa.manage.resources.SchemaResource;
import com.hauldata.dbpa.manage.resources.ScriptsResource;
import com.hauldata.dbpa.manage.resources.ServiceResource;

public class ManageDbp extends Application<Configuration> {

	public static void main(String[] args) throws Exception {
		new ManageDbp().run(args);
	}

    @Override
    public String getName() {
        return "dbpa-manage";
    }

    @Override
    public void initialize(Bootstrap<Configuration> bootstrap) {

		JobManager manager = JobManager.instantiate(false);

		try {
			if (manager.canStartup()) {
				manager.startup();
			}
		}
		catch (Exception ex) {
			ex.printStackTrace();
			System.exit(1);
		}

		// Eventually may subclass Configuration as ServiceConfiguration and receive that here
		// to control startup behavior.
    }

	@Override
	public void run(Configuration configuration, Environment environment) {

		// Eventually may subclass Configuration as ServiceConfiguration and use members
		// as parameters when instantiating resources.

		final ServiceResource service = new ServiceResource();
		final ManagerResource manager = new ManagerResource();
		final SchemaResource schema = new SchemaResource();
		final ScriptsResource scripts = new ScriptsResource();
		final PropertiesFilesResource propFiles = new PropertiesFilesResource();
		final SchedulesResource schedules = new SchedulesResource();
		final JobsResource jobs = new JobsResource();

		// Health checks

		environment.healthChecks().register("schema", new SchemaHealthCheck());
		environment.healthChecks().register("manager", new ManagerHealthCheck());

		// Exception mappers

		environment.jersey().register(new DefaultExceptionMapper());
		environment.jersey().register(new FileNotFoundExceptionMapper());
		environment.jersey().register(new JobManagerExceptionMapper());
		environment.jersey().register(new NameNotFoundExceptionMapper());
		environment.jersey().register(new NoSuchElementExceptionMapper());
		environment.jersey().register(new NoSuchFileExceptionMapper());
		environment.jersey().register(new RuntimeExceptionMapper());

		// Resources

		environment.jersey().register(service);
		environment.jersey().register(manager);
		environment.jersey().register(schema);
		environment.jersey().register(scripts);
		environment.jersey().register(propFiles);
		environment.jersey().register(schedules);
		environment.jersey().register(jobs);
	}

	static {QuietLog4j.please();}
}

@@ -689,24 +689,64 @@
 figuration;%0A
+import io.dropwizard.lifecycle.Managed;%0A
 import io.dr
@@ -2214,194 +2214,28 @@
 ager
- manager = JobManager.instantiate(false);%0A%0A%09%09try %7B%0A%09%09%09if (manager.canStartup()) %7B%0A%09%09%09%09manager.startup();%0A%09%09%09%7D%0A%09%09%7D%0A%09%09catch (Exception ex) %7B%0A%09%09%09ex.printStackTrace();%0A%09%09%09System.exit(1);%0A%09%09%7D
+.instantiate(false);
 %0A%0A%09%09
@@ -3965,41 +3965,490 @@
 s);%0A
-%09%7D%0A%0A%09static %7BQuietLog4j.please();
+%0A%09%09// Managed objects%0A%0A%09%09environment.lifecycle().manage(new ManagedJobManager());%0A%09%7D%0A%0A%09static %7BQuietLog4j.please();%7D%0A%7D%0A%0Aclass ManagedJobManager implements Managed %7B%0A%0A%09@Override%0A%09public void start() throws Exception %7B%0A%09%09JobManager manager = JobManager.getInstance();%0A%09%09if (manager.canStartup()) %7B%0A%09%09%09manager.startup();%0A%09%09%7D%0A%09%7D%0A%0A%09@Override%0A%09public void stop() throws Exception %7B%0A%09%09JobManager manager = JobManager.getInstance();%0A%09%09if (manager.isStarted()) %7B%0A%09%09%09manager.shutdown();%0A%09%09%7D%0A%09
 %7D%0A%7D%0A

Manage JobManager lifecycle using Dropwizard Managed interface to assure shutdown