From 3b6c681194134a87292c175e995f77c2c0c743a3 Mon Sep 17 00:00:00 2001 From: Brett Porter Date: Mon, 12 Feb 2007 05:34:22 +0000 Subject: [PATCH] more practical use of configuration change listeners git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/trunk@506292 13f79535-47bb-0310-9956-ffa450edef68 --- .../DefaultArchivaConfiguration.java | 18 +++-- .../src/main/mdo/configuration.mdo | 8 +-- .../DefaultActiveManagedRepositories.java | 20 +++++- .../DefaultRepositoryTaskScheduler.java | 66 ++++++++++++------- .../web/repository/RepositoryServlet.java | 30 ++++++--- 5 files changed, 97 insertions(+), 45 deletions(-) diff --git a/archiva-configuration/src/main/java/org/apache/maven/archiva/configuration/DefaultArchivaConfiguration.java b/archiva-configuration/src/main/java/org/apache/maven/archiva/configuration/DefaultArchivaConfiguration.java index 7c467c884..36cbd5a8e 100644 --- a/archiva-configuration/src/main/java/org/apache/maven/archiva/configuration/DefaultArchivaConfiguration.java +++ b/archiva-configuration/src/main/java/org/apache/maven/archiva/configuration/DefaultArchivaConfiguration.java @@ -69,12 +69,8 @@ public class DefaultArchivaConfiguration public void addChangeListener( RegistryListener listener ) { - registry.addChangeListener( listener ); - } - - public synchronized void notifyOfConfigurationChange( Registry registry ) - { - configuration = null; + Registry section = registry.getSection( KEY ); + section.addChangeListener( listener ); } public void initialize() @@ -82,4 +78,14 @@ public class DefaultArchivaConfiguration { registry.addChangeListener( this ); } + + public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue ) + { + // nothing to do here + } + + public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue ) + { + configuration = null; + } } diff --git a/archiva-configuration/src/main/mdo/configuration.mdo b/archiva-configuration/src/main/mdo/configuration.mdo index 274f83f52..6187c49c4 100644 --- a/archiva-configuration/src/main/mdo/configuration.mdo +++ b/archiva-configuration/src/main/mdo/configuration.mdo @@ -156,7 +156,7 @@ for ( java.util.Iterator i = getRepositories().iterator(); i.hasNext(); ) { RepositoryConfiguration repository = (RepositoryConfiguration) i.next(); - if ( repository.getUrlName().equals( urlName ) ) + if ( urlName.equals( repository.getUrlName() ) ) { return repository; } @@ -169,7 +169,7 @@ for ( java.util.Iterator i = getRepositories().iterator(); i.hasNext(); ) { RepositoryConfiguration repository = (RepositoryConfiguration) i.next(); - if ( repository.getId().equals( id ) ) + if ( id.equals( repository.getId() ) ) { return repository; } @@ -182,7 +182,7 @@ for ( java.util.Iterator i = getSyncedRepositories().iterator(); i.hasNext(); ) { SyncedRepositoryConfiguration repository = (SyncedRepositoryConfiguration) i.next(); - if ( repository.getId().equals( id ) ) + if ( id.equals( repository.getId() ) ) { return repository; } @@ -195,7 +195,7 @@ for ( java.util.Iterator i = getProxiedRepositories().iterator(); i.hasNext(); ) { ProxiedRepositoryConfiguration repository = (ProxiedRepositoryConfiguration) i.next(); - if ( repository.getId().equals( id ) ) + if ( id.equals( repository.getId() ) ) { return repository; } diff --git a/archiva-core/src/main/java/org/apache/maven/archiva/repositories/DefaultActiveManagedRepositories.java b/archiva-core/src/main/java/org/apache/maven/archiva/repositories/DefaultActiveManagedRepositories.java index 14d4478a5..54b50bf52 100644 --- a/archiva-core/src/main/java/org/apache/maven/archiva/repositories/DefaultActiveManagedRepositories.java +++ b/archiva-core/src/main/java/org/apache/maven/archiva/repositories/DefaultActiveManagedRepositories.java @@ -36,6 +36,7 @@ import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProjectBuilder; import org.apache.maven.project.ProjectBuildingException; import org.codehaus.plexus.cache.Cache; +import org.codehaus.plexus.logging.AbstractLogEnabled; import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; import org.codehaus.plexus.registry.Registry; @@ -53,6 +54,7 @@ import java.util.List; * @plexus.component role="org.apache.maven.archiva.repositories.ActiveManagedRepositories" */ public class DefaultActiveManagedRepositories + extends AbstractLogEnabled implements ActiveManagedRepositories, Initializable, RegistryListener { /** @@ -267,8 +269,22 @@ public class DefaultActiveManagedRepositories return path; } - public void notifyOfConfigurationChange( Registry registry ) + public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue ) { - configureSelf( archivaConfiguration.getConfiguration() ); + // nothing to do + } + + public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue ) + { + if ( propertyName.startsWith( "repositories" ) || propertyName.startsWith( "localRepository" ) ) + { + getLogger().debug( "Triggering managed repository configuration change with " + propertyName + " set to " + + propertyValue ); + configureSelf( archivaConfiguration.getConfiguration() ); + } + else + { + getLogger().debug( "Not triggering managed repository configuration change with " + propertyName ); + } } } diff --git a/archiva-core/src/main/java/org/apache/maven/archiva/scheduler/DefaultRepositoryTaskScheduler.java b/archiva-core/src/main/java/org/apache/maven/archiva/scheduler/DefaultRepositoryTaskScheduler.java index 62a1cb355..04ab088d4 100644 --- a/archiva-core/src/main/java/org/apache/maven/archiva/scheduler/DefaultRepositoryTaskScheduler.java +++ b/archiva-core/src/main/java/org/apache/maven/archiva/scheduler/DefaultRepositoryTaskScheduler.java @@ -92,7 +92,7 @@ public class DefaultRepositoryTaskScheduler try { - scheduleJobs( configuration ); + scheduleJobs( configuration.getIndexPath(), configuration.getIndexerCronExpression() ); } catch ( ParseException e ) { @@ -104,16 +104,15 @@ public class DefaultRepositoryTaskScheduler } } - private void scheduleJobs( Configuration configuration ) + private void scheduleJobs( String indexPath, String indexerCronExpression ) throws ParseException, SchedulerException { - if ( configuration.getIndexPath() != null ) + if ( indexPath != null ) { JobDetail jobDetail = createJobDetail( INDEXER_JOB ); - getLogger().info( "Scheduling indexer: " + configuration.getIndexerCronExpression() ); - CronTrigger trigger = - new CronTrigger( INDEXER_JOB + "Trigger", DISCOVERER_GROUP, configuration.getIndexerCronExpression() ); + getLogger().info( "Scheduling indexer: " + indexerCronExpression ); + CronTrigger trigger = new CronTrigger( INDEXER_JOB + "Trigger", DISCOVERER_GROUP, indexerCronExpression ); scheduler.scheduleJob( jobDetail, trigger ); try @@ -156,30 +155,47 @@ public class DefaultRepositoryTaskScheduler } } - public void notifyOfConfigurationChange( Registry registry ) + + public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue ) { - try - { - stop(); - } - catch ( StoppingException e ) - { - getLogger().warn( "Error stopping task scheduler: " + e.getMessage(), e ); - } + // nothing to do + } - try - { - scheduleJobs( archivaConfiguration.getConfiguration() ); - } - catch ( ParseException e ) + public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue ) + { + if ( "indexPath".equals( propertyName ) || "indexerCronExpression".equals( propertyName ) ) { - getLogger().error( - "Error restarting task scheduler after configuration change, due to configuration error: " + - e.getMessage(), e ); + getLogger().debug( "Restarting task scheduler with new configuration after property change: " + + propertyName + " to " + propertyValue ); + try + { + stop(); + } + catch ( StoppingException e ) + { + getLogger().warn( "Error stopping task scheduler: " + e.getMessage(), e ); + } + + try + { + Configuration configuration = archivaConfiguration.getConfiguration(); + scheduleJobs( configuration.getIndexPath(), configuration.getIndexerCronExpression() ); + } + catch ( ParseException e ) + { + getLogger().error( + "Error restarting task scheduler after configuration change, due to configuration error: " + + e.getMessage(), e ); + } + catch ( SchedulerException e ) + { + getLogger().error( "Error restarting task scheduler after configuration change: " + e.getMessage(), e ); + } } - catch ( SchedulerException e ) + else { - getLogger().error( "Error restarting task scheduler after configuration change: " + e.getMessage(), e ); + getLogger().debug( + "Not restarting task scheduler with new configuration after property change: " + propertyName ); } } diff --git a/archiva-webapp/src/main/java/org/apache/maven/archiva/web/repository/RepositoryServlet.java b/archiva-webapp/src/main/java/org/apache/maven/archiva/web/repository/RepositoryServlet.java index 85ab6a2ce..d658dce27 100644 --- a/archiva-webapp/src/main/java/org/apache/maven/archiva/web/repository/RepositoryServlet.java +++ b/archiva-webapp/src/main/java/org/apache/maven/archiva/web/repository/RepositoryServlet.java @@ -90,7 +90,6 @@ public class RepositoryServlet archivaConfiguration = (ArchivaConfiguration) lookup( ArchivaConfiguration.class.getName() ); configuration = archivaConfiguration.getConfiguration(); archivaConfiguration.addChangeListener( this ); - } public void initServers( ServletConfig servletConfig ) @@ -205,19 +204,34 @@ public class RepositoryServlet return true; } - public void notifyOfConfigurationChange( Registry registry ) + public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue ) { - configuration = archivaConfiguration.getConfiguration(); + // nothing to do + } - getDavManager().removeAllServers(); + public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue ) + { + configuration = archivaConfiguration.getConfiguration(); - try + if ( propertyName.startsWith( "repositories" ) ) { - initServers( getServletConfig() ); + log( "Triggering managed repository configuration change with " + propertyName + " set to " + + propertyValue ); + getDavManager().removeAllServers(); + + try + { + initServers( getServletConfig() ); + } + catch ( DavServerException e ) + { + log( "Error restarting WebDAV server after configuration change - service disabled: " + e.getMessage(), + e ); + } } - catch ( DavServerException e ) + else { - log( "Error restarting WebDAV server after configuration change - service disabled: " + e.getMessage(), e ); + log( "Not triggering managed repository configuration change with " + propertyName ); } } } -- 2.39.5