1 package org.apache.maven.repository.scheduler;
4 * Copyright 2005-2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.apache.maven.repository.configuration.Configuration;
20 import org.apache.maven.repository.configuration.ConfigurationChangeException;
21 import org.apache.maven.repository.configuration.ConfigurationChangeListener;
22 import org.apache.maven.repository.configuration.ConfigurationStore;
23 import org.apache.maven.repository.configuration.ConfigurationStoreException;
24 import org.apache.maven.repository.configuration.InvalidConfigurationException;
25 import org.codehaus.plexus.logging.AbstractLogEnabled;
26 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Startable;
27 import org.codehaus.plexus.personality.plexus.lifecycle.phase.StartingException;
28 import org.codehaus.plexus.personality.plexus.lifecycle.phase.StoppingException;
29 import org.codehaus.plexus.scheduler.AbstractJob;
30 import org.codehaus.plexus.scheduler.Scheduler;
31 import org.quartz.CronTrigger;
32 import org.quartz.JobDataMap;
33 import org.quartz.JobDetail;
34 import org.quartz.SchedulerException;
36 import java.text.ParseException;
39 * Default implementation of a scheduling component for the application.
41 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
42 * @todo should we use plexus-taskqueue instead of or in addition to this?
43 * @plexus.component role="org.apache.maven.repository.scheduler.RepositoryTaskScheduler"
45 public class DefaultRepositoryTaskScheduler
46 extends AbstractLogEnabled
47 implements RepositoryTaskScheduler, Startable, ConfigurationChangeListener
52 private Scheduler scheduler;
57 private ConfigurationStore configurationStore;
59 private static final String DISCOVERER_GROUP = "DISCOVERER";
61 private static final String INDEXER_JOB = "indexerTask";
64 * @plexus.requirement role-hint="indexer"
66 private RepositoryTask indexerTask;
69 throws StartingException
71 Configuration configuration;
74 configuration = configurationStore.getConfigurationFromStore();
75 configurationStore.addChangeListener( this );
77 catch ( ConfigurationStoreException e )
79 throw new StartingException( "Unable to read configuration from the store", e );
84 scheduleJobs( configuration );
86 catch ( ParseException e )
88 throw new StartingException( "Invalid configuration: " + configuration.getIndexerCronExpression(), e );
90 catch ( SchedulerException e )
92 throw new StartingException( "Unable to start scheduler: " + e.getMessage(), e );
96 private void scheduleJobs( Configuration configuration )
97 throws ParseException, SchedulerException
99 if ( configuration.getIndexPath() != null )
101 JobDetail jobDetail = new JobDetail( INDEXER_JOB, DISCOVERER_GROUP, RepositoryTaskJob.class );
102 JobDataMap dataMap = new JobDataMap();
103 dataMap.put( AbstractJob.LOGGER, getLogger() );
104 dataMap.put( RepositoryTaskJob.TASK_KEY, indexerTask );
105 jobDetail.setJobDataMap( dataMap );
107 getLogger().info( "Scheduling indexer: " + configuration.getIndexerCronExpression() );
108 CronTrigger trigger =
109 new CronTrigger( INDEXER_JOB + "Trigger", DISCOVERER_GROUP, configuration.getIndexerCronExpression() );
110 scheduler.scheduleJob( jobDetail, trigger );
112 // TODO: run as a job so it doesn't block startup/configuration saving
115 indexerTask.executeNowIfNeeded();
117 catch ( TaskExecutionException e )
119 getLogger().error( "Error executing task first time, continuing anyway: " + e.getMessage(), e );
124 getLogger().info( "Not scheduling indexer - index path is not configured" );
127 // TODO: wire in the converter
131 throws StoppingException
135 scheduler.unscheduleJob( INDEXER_JOB, DISCOVERER_GROUP );
137 catch ( SchedulerException e )
139 throw new StoppingException( "Unable to unschedule tasks", e );
143 public void notifyOfConfigurationChange( Configuration configuration )
144 throws InvalidConfigurationException, ConfigurationChangeException
150 scheduleJobs( configuration );
152 catch ( StoppingException e )
154 throw new ConfigurationChangeException( "Unable to unschedule previous tasks", e );
156 catch ( ParseException e )
158 throw new InvalidConfigurationException( "indexerCronExpression", "Invalid cron expression", e );
160 catch ( SchedulerException e )
162 throw new ConfigurationChangeException( "Unable to schedule new tasks", e );
166 public void runIndexer()
167 throws TaskExecutionException
169 indexerTask.execute();