This project has retired. For details please refer to its Attic page.
RepositoryContentFactory xref
View Javadoc
1   package org.apache.archiva.repository;
2   
3   /*
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
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
22  import org.apache.archiva.configuration.ArchivaConfiguration;
23  import org.apache.archiva.configuration.ConfigurationNames;
24  import org.apache.archiva.components.registry.Registry;
25  import org.apache.archiva.components.registry.RegistryListener;
26  import org.springframework.context.ApplicationContext;
27  import org.springframework.stereotype.Service;
28  
29  import javax.annotation.PostConstruct;
30  import javax.inject.Inject;
31  import java.util.List;
32  import java.util.Map;
33  import java.util.concurrent.ConcurrentHashMap;
34  
35  /**
36   * RepositoryContentRequest
37   */
38  @Service( "repositoryContentFactory#default" )
39  public class RepositoryContentFactory
40      implements RegistryListener
41  {
42      /**
43       *
44       */
45      @Inject
46      private ArchivaConfiguration archivaConfiguration;
47  
48      @Inject
49      private ApplicationContext applicationContext;
50  
51      @Inject
52      private List<RepositoryContentProvider> repositoryContentProviders;
53  
54      private final Map<String, ManagedRepositoryContent> managedContentMap;
55  
56      private final Map<String, RemoteRepositoryContent> remoteContentMap;
57  
58  
59      public RepositoryContentFactory( )
60      {
61          managedContentMap = new ConcurrentHashMap<String, ManagedRepositoryContent>( );
62          remoteContentMap = new ConcurrentHashMap<String, RemoteRepositoryContent>( );
63      }
64  
65      /**
66       * Get the ManagedRepositoryContent object for the repository Id specified.
67       *
68       * @param repoId the repository id to fetch.
69       * @return the ManagedRepositoryContent object associated with the repository id.
70       * @throws RepositoryNotFoundException if the repository id does not exist within the configuration.
71       * @throws RepositoryException         the repository content object cannot be loaded due to configuration issue.
72       */
73      public ManagedRepositoryContent getManagedRepositoryContent( String repoId )
74          throws RepositoryException
75      {
76          ManagedRepositoryContent repo = managedContentMap.get( repoId );
77  
78          if ( repo != null )
79          {
80              return repo;
81          }
82          else
83          {
84              throw new RepositoryNotFoundException(
85                  "Unable to find managed repository configuration for id " + repoId );
86          }
87  
88      }
89  
90      private RepositoryContentProvider getProvider(final String layout, final RepositoryType repoType) throws RepositoryException
91      {
92          return repositoryContentProviders.stream().filter(p->p.supports( repoType ) && p.supportsLayout( layout )).
93              findFirst().orElseThrow( ( ) -> new RepositoryException( "Could not find content provider for repository type "+repoType+" and layout "+layout ) );
94      }
95  
96      public ManagedRepositoryContent getManagedRepositoryContent( org.apache.archiva.repository.ManagedRepository mRepo )
97          throws RepositoryException
98      {
99          final String id = mRepo.getId();
100         ManagedRepositoryContent content = managedContentMap.get( id );
101 
102         if ( content != null && content.getRepository()==mRepo)
103         {
104             return content;
105         }
106 
107         RepositoryContentProvider contentProvider = getProvider( mRepo.getLayout( ), mRepo.getType( ) );
108         content = contentProvider.createManagedContent( mRepo );
109         if (content==null) {
110             throw new RepositoryException( "Could not create repository content instance for "+mRepo.getId() );
111         }
112         ManagedRepositoryContent previousContent = managedContentMap.put( id, content );
113         if (previousContent!=null) {
114             previousContent.setRepository( null );
115         }
116 
117         return content;
118     }
119 
120     public RemoteRepositoryContent getRemoteRepositoryContent( String repoId )
121         throws RepositoryException
122     {
123         RemoteRepositoryContent repo = remoteContentMap.get( repoId );
124 
125         if ( repo != null )
126         {
127             return repo;
128         }
129         else
130         {
131             throw new RepositoryNotFoundException(
132                 "Unable to find remote repository configuration for id:" + repoId );
133         }
134 
135     }
136 
137     public RemoteRepositoryContent getRemoteRepositoryContent( RemoteRepository mRepo )
138         throws RepositoryException
139     {
140         final String id = mRepo.getId();
141         RemoteRepositoryContent content = remoteContentMap.get( id );
142 
143         if ( content != null && content.getRepository()==mRepo)
144         {
145             return content;
146         }
147 
148         RepositoryContentProvider contentProvider = getProvider( mRepo.getLayout( ), mRepo.getType( ) );
149         content = contentProvider.createRemoteContent( mRepo );
150         if (content==null) {
151             throw new RepositoryException( "Could not create repository content instance for "+mRepo.getId() );
152         }
153         RemoteRepositoryContent previousContent = remoteContentMap.put( id, content );
154         if (previousContent!=null) {
155             previousContent.setRepository( null );
156         }
157         return content;
158     }
159 
160 
161     @Override
162     public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
163     {
164         if ( ConfigurationNames.isManagedRepositories( propertyName ) || ConfigurationNames.isRemoteRepositories(
165             propertyName ) )
166         {
167             initMaps( );
168         }
169     }
170 
171     @Override
172     public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
173     {
174         /* do nothing */
175     }
176 
177     @PostConstruct
178     public void initialize( )
179     {
180         archivaConfiguration.addChangeListener( this );
181     }
182 
183     private void initMaps( )
184     {
185         // olamy we use concurent so no need of synchronize
186         //synchronized ( managedContentMap )
187         //{
188         managedContentMap.clear( );
189         //}
190 
191         //synchronized ( remoteContentMap )
192         //{
193         remoteContentMap.clear( );
194         //}
195     }
196 
197     public ArchivaConfiguration getArchivaConfiguration( )
198     {
199         return archivaConfiguration;
200     }
201 
202     public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
203     {
204         this.archivaConfiguration = archivaConfiguration;
205     }
206 
207     public void setRepositoryContentProviders(List<RepositoryContentProvider> providers) {
208         this.repositoryContentProviders = providers;
209     }
210 
211     public List<RepositoryContentProvider> getRepositoryContentProviders() {
212         return repositoryContentProviders;
213     }
214 }