]> source.dussan.org Git - archiva.git/blob
4c7e05e5ac298266be6b065dd8891e57f1b80fd6
[archiva.git] /
1 package org.apache.maven.archiva.configuration;
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.commons.lang.StringUtils;
23 import org.apache.maven.archiva.proxy.ProxiedArtifactRepository;
24 import org.apache.maven.artifact.repository.ArtifactRepository;
25 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
26 import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
27 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
28
29 import java.io.File;
30 import java.util.ArrayList;
31 import java.util.Iterator;
32 import java.util.List;
33 import java.util.Map;
34
35 /**
36  * Create artifact repositories from a configuration.
37  *
38  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
39  * @plexus.component role="org.apache.maven.archiva.configuration.ConfiguredRepositoryFactory"
40  */
41 public class DefaultConfiguredRepositoryFactory
42     implements ConfiguredRepositoryFactory
43 {
44     /**
45      * @plexus.requirement role="org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout"
46      */
47     private Map repositoryLayouts;
48
49     /**
50      * @plexus.requirement
51      */
52     private ArtifactRepositoryFactory repoFactory;
53
54     public ArtifactRepository createRepository( RepositoryConfiguration configuration )
55     {
56         return createRepository( configuration.getLayout(), configuration.getId(), configuration.getDirectory());
57     }
58
59     public ProxiedArtifactRepository createProxiedRepository( ProxiedRepositoryConfiguration configuration )
60     {
61         boolean enabled = isEnabled( configuration.getSnapshotsPolicy() );
62         String updatePolicy =
63             getUpdatePolicy( configuration.getSnapshotsPolicy(), configuration.getSnapshotsInterval() );
64         ArtifactRepositoryPolicy snapshotsPolicy =
65             new ArtifactRepositoryPolicy( enabled, updatePolicy, ArtifactRepositoryPolicy.CHECKSUM_POLICY_FAIL );
66
67         enabled = isEnabled( configuration.getReleasesPolicy() );
68         updatePolicy = getUpdatePolicy( configuration.getReleasesPolicy(), configuration.getReleasesInterval() );
69         ArtifactRepositoryPolicy releasesPolicy =
70             new ArtifactRepositoryPolicy( enabled, updatePolicy, ArtifactRepositoryPolicy.CHECKSUM_POLICY_FAIL );
71
72         ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) repositoryLayouts.get( configuration.getLayout() );
73
74         if ( layout == null )
75         {
76             throw new IllegalArgumentException( "Invalid layout: " + configuration.getLayout() );
77         }
78
79         ArtifactRepository artifactRepository = repoFactory.createArtifactRepository( configuration.getId(),
80                                                                                       configuration.getUrl(), layout,
81                                                                                       snapshotsPolicy, releasesPolicy );
82         ProxiedArtifactRepository repository = new ProxiedArtifactRepository( artifactRepository );
83         repository.setCacheFailures( configuration.isCacheFailures() );
84         repository.setHardFail( configuration.isHardFail() );
85         repository.setName( configuration.getName() );
86         repository.setUseNetworkProxy( configuration.isUseNetworkProxy() );
87         return repository;
88     }
89
90     public List createRepositories( Configuration configuration )
91     {
92         List managedRepositories = configuration.getRepositories();
93         List repositories = new ArrayList( managedRepositories.size() );
94
95         for ( Iterator i = managedRepositories.iterator(); i.hasNext(); )
96         {
97             repositories.add( createRepository( (RepositoryConfiguration) i.next() ) );
98         }
99
100         return repositories;
101     }
102
103     public List createProxiedRepositories( Configuration configuration )
104     {
105         List proxiedRepositories = configuration.getProxiedRepositories();
106         List repositories = new ArrayList( proxiedRepositories.size() );
107
108         for ( Iterator i = proxiedRepositories.iterator(); i.hasNext(); )
109         {
110             repositories.add( createProxiedRepository( (ProxiedRepositoryConfiguration) i.next() ) );
111         }
112
113         return repositories;
114     }
115
116     public ArtifactRepository createLocalRepository( Configuration configuration )
117     {
118         return createRepository( "default", "local", configuration.getLocalRepository() );
119     }
120
121     public ArtifactRepository createRepository( String layout, String id, String directory )
122     {
123         ArtifactRepositoryLayout repositoryLayout = (ArtifactRepositoryLayout) repositoryLayouts.get( layout );
124         File repository = new File( directory );
125         repository.mkdirs();
126
127         String repoDir = repository.toURI().toString();
128         //workaround for spaces non converted by PathUtils in wagon
129         //TODO: remove it when PathUtils will be fixed
130         if ( repoDir.indexOf( "%20" ) >= 0 )
131         {
132             repoDir = StringUtils.replace( repoDir, "%20", " " );
133         }
134
135         return repoFactory.createArtifactRepository( id, repoDir, repositoryLayout, null, null );
136     }
137
138     private static String getUpdatePolicy( String policy, int interval )
139     {
140         return "interval".equals( policy ) ? policy + ":" + interval : policy;
141     }
142
143     private static boolean isEnabled( String policy )
144     {
145         return !"disabled".equals( policy );
146     }
147 }