This project has retired. For details please refer to its Attic page.
Source code
001package org.apache.archiva.repository;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.archiva.admin.model.RepositoryAdminException;
023import org.apache.archiva.admin.model.beans.ManagedRepository;
024import org.apache.archiva.admin.model.beans.RemoteRepository;
025import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
026import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin;
027import org.apache.archiva.configuration.ArchivaConfiguration;
028import org.apache.archiva.configuration.ConfigurationNames;
029import org.apache.archiva.redback.components.registry.Registry;
030import org.apache.archiva.redback.components.registry.RegistryListener;
031import org.springframework.context.ApplicationContext;
032import org.springframework.stereotype.Service;
033
034import javax.annotation.PostConstruct;
035import javax.inject.Inject;
036import java.util.Map;
037import java.util.concurrent.ConcurrentHashMap;
038
039/**
040 * RepositoryContentRequest
041 *
042 *
043 */
044@Service( "repositoryContentFactory#default" )
045public class RepositoryContentFactory
046    implements RegistryListener
047{
048    /**
049     *
050     */
051    @Inject
052    private ArchivaConfiguration archivaConfiguration;
053
054    @Inject
055    private ManagedRepositoryAdmin managedRepositoryAdmin;
056
057    @Inject
058    private RemoteRepositoryAdmin remoteRepositoryAdmin;
059
060    @Inject
061    private ApplicationContext applicationContext;
062
063    private final Map<String, ManagedRepositoryContent> managedContentMap;
064
065    private final Map<String, RemoteRepositoryContent> remoteContentMap;
066
067
068    public RepositoryContentFactory()
069    {
070        managedContentMap = new ConcurrentHashMap<String, ManagedRepositoryContent>();
071        remoteContentMap = new ConcurrentHashMap<String, RemoteRepositoryContent>();
072    }
073
074    /**
075     * Get the ManagedRepositoryContent object for the repository Id specified.
076     *
077     * @param repoId the repository id to fetch.
078     * @return the ManagedRepositoryContent object associated with the repository id.
079     * @throws RepositoryNotFoundException if the repository id does not exist within the configuration.
080     * @throws RepositoryException         the repository content object cannot be loaded due to configuration issue.
081     */
082    public ManagedRepositoryContent getManagedRepositoryContent( String repoId )
083        throws RepositoryNotFoundException, RepositoryException
084    {
085        try
086        {
087            ManagedRepositoryContent repo = managedContentMap.get( repoId );
088
089            if ( repo != null )
090            {
091                return repo;
092            }
093
094            ManagedRepository repoConfig = managedRepositoryAdmin.getManagedRepository( repoId );
095            if ( repoConfig == null )
096            {
097                throw new RepositoryNotFoundException(
098                    "Unable to find managed repository configuration for id:" + repoId );
099            }
100
101            repo = applicationContext.getBean( "managedRepositoryContent#" + repoConfig.getLayout(),
102                                               ManagedRepositoryContent.class );
103            repo.setRepository( repoConfig );
104            managedContentMap.put( repoId, repo );
105
106            return repo;
107        }
108        catch ( RepositoryAdminException e )
109        {
110            throw new RepositoryException( e.getMessage(), e );
111        }
112    }
113
114    public RemoteRepositoryContent getRemoteRepositoryContent( String repoId )
115        throws RepositoryNotFoundException, RepositoryException
116    {
117        try
118        {
119            RemoteRepositoryContent repo = remoteContentMap.get( repoId );
120
121            if ( repo != null )
122            {
123                return repo;
124            }
125
126            RemoteRepository repoConfig = remoteRepositoryAdmin.getRemoteRepository( repoId );
127            if ( repoConfig == null )
128            {
129                throw new RepositoryNotFoundException(
130                    "Unable to find remote repository configuration for id:" + repoId );
131            }
132
133            repo = applicationContext.getBean( "remoteRepositoryContent#" + repoConfig.getLayout(),
134                                               RemoteRepositoryContent.class );
135            repo.setRepository( repoConfig );
136            remoteContentMap.put( repoId, repo );
137
138            return repo;
139        }
140        catch ( RepositoryAdminException e )
141        {
142            throw new RepositoryException( e.getMessage(), e );
143        }
144    }
145
146
147    @Override
148    public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
149    {
150        if ( ConfigurationNames.isManagedRepositories( propertyName ) || ConfigurationNames.isRemoteRepositories(
151            propertyName ) )
152        {
153            initMaps();
154        }
155    }
156
157    @Override
158    public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
159    {
160        /* do nothing */
161    }
162
163    @PostConstruct
164    public void initialize()
165    {
166        archivaConfiguration.addChangeListener( this );
167    }
168
169    private void initMaps()
170    {
171        // olamy we use concurent so no need of synchronize
172        //synchronized ( managedContentMap )
173        //{
174        managedContentMap.clear();
175        //}
176
177        //synchronized ( remoteContentMap )
178        //{
179        remoteContentMap.clear();
180        //}
181    }
182
183    public ArchivaConfiguration getArchivaConfiguration()
184    {
185        return archivaConfiguration;
186    }
187
188    public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
189    {
190        this.archivaConfiguration = archivaConfiguration;
191    }
192}