1 package org.apache.maven.archiva.web.startup;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
23 import org.apache.maven.archiva.configuration.ConfigurationNames;
24 import org.apache.maven.archiva.configuration.RepositoryConfiguration;
25 import org.apache.maven.archiva.database.ArchivaDAO;
26 import org.apache.maven.archiva.database.ArchivaDatabaseException;
27 import org.apache.maven.archiva.database.ObjectNotFoundException;
28 import org.apache.maven.archiva.model.ArchivaRepository;
29 import org.apache.maven.archiva.repository.ArchivaConfigurationAdaptor;
30 import org.codehaus.plexus.logging.AbstractLogEnabled;
31 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
32 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
33 import org.codehaus.plexus.redback.role.RoleManager;
34 import org.codehaus.plexus.redback.role.RoleManagerException;
35 import org.codehaus.plexus.registry.Registry;
36 import org.codehaus.plexus.registry.RegistryListener;
38 import java.util.Iterator;
39 import java.util.List;
42 * ConfigurationSynchronization
44 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
48 * role="org.apache.maven.archiva.web.startup.ConfigurationSynchronization"
51 public class ConfigurationSynchronization
52 extends AbstractLogEnabled
53 implements RegistryListener, Initializable
56 * @plexus.requirement role-hint="jdo"
58 private ArchivaDAO dao;
61 * @plexus.requirement role-hint="default"
63 private RoleManager roleManager;
68 private ArchivaConfiguration archivaConfiguration;
70 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
72 if ( ConfigurationNames.isRepositories( propertyName ) )
78 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
83 private void synchConfiguration()
85 List repos = archivaConfiguration.getConfiguration().getRepositories();
86 Iterator it = repos.iterator();
87 while ( it.hasNext() )
89 RepositoryConfiguration repoConfig = (RepositoryConfiguration) it.next();
94 ArchivaRepository repository = dao.getRepositoryDAO().getRepository( repoConfig.getId() );
95 // Found repository. Update it.
97 repository.getModel().setName( repoConfig.getName() );
98 repository.getModel().setUrl( repoConfig.getUrl() );
99 repository.getModel().setLayoutName( repoConfig.getLayout() );
100 repository.getModel().setCreationSource( "configuration" );
101 repository.getModel().setReleasePolicy( repoConfig.isReleases() );
102 repository.getModel().setSnapshotPolicy( repoConfig.isSnapshots() );
104 dao.getRepositoryDAO().saveRepository( repository );
106 catch ( ObjectNotFoundException e )
108 // Add the repository to the database.
109 getLogger().info( "Adding repository configuration to DB: " + repoConfig );
110 ArchivaRepository drepo = ArchivaConfigurationAdaptor.toArchivaRepository( repoConfig );
111 drepo.getModel().setCreationSource( "configuration" );
112 dao.getRepositoryDAO().saveRepository( drepo );
115 catch ( ArchivaDatabaseException e )
118 getLogger().error( "Unable to add configured repositories to the database: " + e.getMessage(), e );
121 // manage roles for repositories
124 if ( !roleManager.templatedRoleExists( "archiva-repository-observer", repoConfig.getId() ) )
126 roleManager.createTemplatedRole( "archiva-repository-observer", repoConfig.getId() );
129 if ( !roleManager.templatedRoleExists( "archiva-repository-manager", repoConfig.getId() ) )
132 roleManager.createTemplatedRole( "archiva-repository-manager", repoConfig.getId() );
135 catch ( RoleManagerException e )
138 getLogger().error( "Unable to create roles for configured repositories: " + e.getMessage(), e );
144 public void initialize()
145 throws InitializationException
147 synchConfiguration();
148 archivaConfiguration.addChangeListener( this );