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.admin.model.RepositoryAdminException;
23  import org.apache.archiva.admin.model.beans.ManagedRepository;
24  import org.apache.archiva.admin.model.beans.RemoteRepository;
25  import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
26  import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin;
27  import org.apache.archiva.configuration.ArchivaConfiguration;
28  import org.apache.archiva.configuration.ConfigurationNames;
29  import org.apache.archiva.redback.components.registry.Registry;
30  import org.apache.archiva.redback.components.registry.RegistryListener;
31  import org.springframework.context.ApplicationContext;
32  import org.springframework.stereotype.Service;
33  
34  import javax.annotation.PostConstruct;
35  import javax.inject.Inject;
36  import java.util.Map;
37  import java.util.concurrent.ConcurrentHashMap;
38  
39  /**
40   * RepositoryContentRequest
41   *
42   *
43   */
44  @Service( "repositoryContentFactory#default" )
45  public class RepositoryContentFactory
46      implements RegistryListener
47  {
48      /**
49       *
50       */
51      @Inject
52      private ArchivaConfiguration archivaConfiguration;
53  
54      @Inject
55      private ManagedRepositoryAdmin managedRepositoryAdmin;
56  
57      @Inject
58      private RemoteRepositoryAdmin remoteRepositoryAdmin;
59  
60      @Inject
61      private ApplicationContext applicationContext;
62  
63      private final Map<String, ManagedRepositoryContent> managedContentMap;
64  
65      private final Map<String, RemoteRepositoryContent> remoteContentMap;
66  
67  
68      public RepositoryContentFactory()
69      {
70          managedContentMap = new ConcurrentHashMap<String, ManagedRepositoryContent>();
71          remoteContentMap = new ConcurrentHashMap<String, RemoteRepositoryContent>();
72      }
73  
74      /**
75       * Get the ManagedRepositoryContent object for the repository Id specified.
76       *
77       * @param repoId the repository id to fetch.
78       * @return the ManagedRepositoryContent object associated with the repository id.
79       * @throws RepositoryNotFoundException if the repository id does not exist within the configuration.
80       * @throws RepositoryException         the repository content object cannot be loaded due to configuration issue.
81       */
82      public ManagedRepositoryContent getManagedRepositoryContent( String repoId )
83          throws RepositoryNotFoundException, RepositoryException
84      {
85          try
86          {
87              ManagedRepositoryContent repo = managedContentMap.get( repoId );
88  
89              if ( repo != null )
90              {
91                  return repo;
92              }
93  
94              ManagedRepository repoConfig = managedRepositoryAdmin.getManagedRepository( repoId );
95              if ( repoConfig == null )
96              {
97                  throw new RepositoryNotFoundException(
98                      "Unable to find managed repository configuration for id:" + repoId );
99              }
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 }