001    package 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    
022    import org.apache.archiva.admin.model.RepositoryAdminException;
023    import org.apache.archiva.admin.model.beans.ManagedRepository;
024    import org.apache.archiva.admin.model.beans.RemoteRepository;
025    import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
026    import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin;
027    import org.apache.archiva.configuration.ArchivaConfiguration;
028    import org.apache.archiva.configuration.ConfigurationNames;
029    import org.apache.archiva.redback.components.registry.Registry;
030    import org.apache.archiva.redback.components.registry.RegistryListener;
031    import org.springframework.context.ApplicationContext;
032    import org.springframework.stereotype.Service;
033    
034    import javax.annotation.PostConstruct;
035    import javax.inject.Inject;
036    import java.util.Map;
037    import java.util.concurrent.ConcurrentHashMap;
038    
039    /**
040     * RepositoryContentRequest
041     *
042     *
043     */
044    @Service( "repositoryContentFactory#default" )
045    public 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        public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
148        {
149            if ( ConfigurationNames.isManagedRepositories( propertyName ) || ConfigurationNames.isRemoteRepositories(
150                propertyName ) )
151            {
152                initMaps();
153            }
154        }
155    
156        public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
157        {
158            /* do nothing */
159        }
160    
161        @PostConstruct
162        public void initialize()
163        {
164            archivaConfiguration.addChangeListener( this );
165        }
166    
167        private void initMaps()
168        {
169            // olamy we use concurent so no need of synchronize
170            //synchronized ( managedContentMap )
171            //{
172            managedContentMap.clear();
173            //}
174    
175            //synchronized ( remoteContentMap )
176            //{
177            remoteContentMap.clear();
178            //}
179        }
180    
181        public ArchivaConfiguration getArchivaConfiguration()
182        {
183            return archivaConfiguration;
184        }
185    
186        public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
187        {
188            this.archivaConfiguration = archivaConfiguration;
189        }
190    }