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.common.utils.PathUtil;
23 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
24 import org.apache.maven.archiva.configuration.ConfigurationNames;
25 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
26 import org.apache.maven.archiva.database.ArchivaDAO;
27 import org.apache.maven.archiva.database.ArchivaDatabaseException;
28 import org.apache.maven.archiva.database.ObjectNotFoundException;
29 import org.apache.maven.archiva.model.ArchivaRepository;
30 import org.apache.maven.archiva.repository.ArchivaConfigurationAdaptor;
31 import org.codehaus.plexus.logging.AbstractLogEnabled;
32 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
33 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
34 import org.codehaus.plexus.redback.role.RoleManager;
35 import org.codehaus.plexus.redback.role.RoleManagerException;
36 import org.codehaus.plexus.registry.Registry;
37 import org.codehaus.plexus.registry.RegistryListener;
39 import java.util.Iterator;
40 import java.util.List;
43 * ConfigurationSynchronization
45 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
47 * @plexus.component role="org.apache.maven.archiva.web.startup.ConfigurationSynchronization"
50 public class ConfigurationSynchronization
51 extends AbstractLogEnabled
52 implements RegistryListener, Initializable
55 * @plexus.requirement role-hint="jdo"
57 private ArchivaDAO dao;
60 * @plexus.requirement role-hint="default"
62 private RoleManager roleManager;
67 private ArchivaConfiguration archivaConfiguration;
69 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
71 // TODO! this used to store both types, but do we even need it?
72 if ( ConfigurationNames.isManagedRepositories( propertyName ) )
74 synchConfiguration( archivaConfiguration.getConfiguration().getManagedRepositories() );
78 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
83 private void synchConfiguration( List repos )
85 Iterator it = repos.iterator();
86 while ( it.hasNext() )
88 ManagedRepositoryConfiguration repoConfig = (ManagedRepositoryConfiguration) it.next();
93 ArchivaRepository repository = dao.getRepositoryDAO().getRepository( repoConfig.getId() );
94 // Found repository. Update it.
96 repository.getModel().setName( repoConfig.getName() );
97 repository.getModel().setUrl( PathUtil.toUrl( repoConfig.getLocation() ) );
98 repository.getModel().setLayoutName( repoConfig.getLayout() );
99 repository.getModel().setCreationSource( "configuration" );
100 repository.getModel().setReleasePolicy( repoConfig.isReleases() );
101 repository.getModel().setSnapshotPolicy( repoConfig.isSnapshots() );
103 dao.getRepositoryDAO().saveRepository( repository );
105 catch ( ObjectNotFoundException e )
107 // Add the repository to the database.
108 getLogger().info( "Adding repository configuration to DB: " + repoConfig );
109 ArchivaRepository drepo = ArchivaConfigurationAdaptor.toArchivaRepository( repoConfig );
110 drepo.getModel().setCreationSource( "configuration" );
111 dao.getRepositoryDAO().saveRepository( drepo );
114 catch ( ArchivaDatabaseException e )
117 getLogger().error( "Unable to add configured repositories to the database: " + e.getMessage(), e );
120 // manage roles for repositories
123 if ( !roleManager.templatedRoleExists( "archiva-repository-observer", repoConfig.getId() ) )
125 roleManager.createTemplatedRole( "archiva-repository-observer", repoConfig.getId() );
128 if ( !roleManager.templatedRoleExists( "archiva-repository-manager", repoConfig.getId() ) )
130 roleManager.createTemplatedRole( "archiva-repository-manager", repoConfig.getId() );
133 catch ( RoleManagerException e )
136 getLogger().error( "Unable to create roles for configured repositories: " + e.getMessage(), e );
142 public void initialize()
143 throws InitializationException
145 synchConfiguration( archivaConfiguration.getConfiguration().getManagedRepositories() );
146 archivaConfiguration.addChangeListener( this );