001/*
002 * #%L
003 * Netarchivesuite - harvester
004 * %%
005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library,
006 *             the National Library of France and the Austrian National Library.
007 * %%
008 * This program is free software: you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation, either version 2.1 of the
011 * License, or (at your option) any later version.
012 * 
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Lesser Public License for more details.
017 * 
018 * You should have received a copy of the GNU General Lesser Public
019 * License along with this program.  If not, see
020 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
021 * #L%
022 */
023
024package dk.netarkivet.harvester.scheduler;
025
026import javax.inject.Provider;
027
028import dk.netarkivet.common.distribute.JMSConnection;
029import dk.netarkivet.common.distribute.JMSConnectionFactory;
030import dk.netarkivet.common.lifecycle.LifeCycleComponent;
031import dk.netarkivet.common.utils.Notifications;
032import dk.netarkivet.common.utils.NotificationsFactory;
033import dk.netarkivet.common.utils.Settings;
034import dk.netarkivet.harvester.HarvesterSettings;
035import dk.netarkivet.harvester.datamodel.HarvestChannelDAO;
036import dk.netarkivet.harvester.datamodel.HarvestDBConnection;
037import dk.netarkivet.harvester.datamodel.HarvestDefinitionDAO;
038import dk.netarkivet.harvester.datamodel.JobDAO;
039
040/**
041 * Handles the dispatching of scheduled harvest to the harvest servers based on the harvests defined in the database.
042 * <p>
043 */
044public class HarvestJobManager extends LifeCycleComponent {
045    /**
046     * Creates the components handling the harvest job management and hooks them up to the
047     * <code>HarvestJobManager</code>s lifecycle.
048     */
049    public HarvestJobManager() {
050        JobDispatcher jobDispather = new JobDispatcher(getJMSConnectionProvider().get(), HarvestDefinitionDAO.getInstance(),
051                JobDAO.getInstance());
052        HarvestChannelRegistry harvestChannelRegistry = new HarvestChannelRegistry();
053
054        addChild(new HarvesterStatusReceiver(jobDispather, getJMSConnectionProvider().get(), HarvestChannelDAO.getInstance(),
055                harvestChannelRegistry));
056
057        addChild(new HarvestJobGenerator(harvestChannelRegistry));
058
059        addChild(new HarvestSchedulerMonitorServer(
060                getJMSConnectionProvider(),
061                getJobDAOProvider(),
062                getHarvestDefinitionDAOProvider(),
063                getNotificationsProvider()
064        ));
065
066        addChild(new JobSupervisor(getJobDAOProvider(), Settings.getLong(HarvesterSettings.JOB_TIMEOUT_TIME)));
067    }
068
069    @Override
070    public void shutdown() {
071        super.shutdown();
072        HarvestDBConnection.cleanup();
073    }
074/*
075    public static Provider<JMSConnection> getJMSConnectionProvider() {
076        return () -> JMSConnectionFactory.getInstance();
077    }
078    public static Provider<JobDAO> getJobDAOProvider() {
079        return () -> JobDAO.getInstance();
080    }
081    public static Provider<HarvestDefinitionDAO> getHarvestDefinitionDAOProvider() {
082        return () -> HarvestDefinitionDAO.getInstance();
083    }
084    public static Provider<Notifications> getNotificationsProvider() {
085        return () -> NotificationsFactory.getInstance();
086    }
087*/
088
089    public static Provider<JMSConnection> getJMSConnectionProvider() {
090        return new Provider<JMSConnection>() {
091
092            @Override
093            public JMSConnection get() {
094                return JMSConnectionFactory.getInstance();
095            }};
096    }
097    public static Provider<JobDAO> getJobDAOProvider() {
098        return new Provider<JobDAO>() {
099
100            @Override
101            public JobDAO get() {
102                return JobDAO.getInstance();
103            }};
104    }
105    public static Provider<HarvestDefinitionDAO> getHarvestDefinitionDAOProvider() {
106        return new Provider<HarvestDefinitionDAO>() {
107
108            @Override
109            public HarvestDefinitionDAO get() {
110                return HarvestDefinitionDAO.getInstance();
111            }};
112    }
113    public static Provider<Notifications> getNotificationsProvider() {
114        return new Provider<Notifications>() {
115
116            @Override
117            public Notifications get() {
118                return NotificationsFactory.getInstance();
119            }};
120    }
121 
122}