1 | |
package org.apache.maven.archiva.configuration; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.lang.reflect.Field; |
23 | |
import java.util.ArrayList; |
24 | |
import java.util.Arrays; |
25 | |
import java.util.Collections; |
26 | |
import java.util.HashMap; |
27 | |
import java.util.List; |
28 | |
import java.util.Map; |
29 | |
|
30 | |
import org.apache.commons.collections.CollectionUtils; |
31 | |
import org.apache.commons.collections.Predicate; |
32 | |
import org.apache.commons.configuration.CombinedConfiguration; |
33 | |
import org.apache.maven.archiva.common.utils.Slf4JPlexusLogger; |
34 | |
import org.apache.maven.archiva.configuration.functors.FiletypeSelectionPredicate; |
35 | |
import org.apache.maven.archiva.configuration.io.registry.ConfigurationRegistryReader; |
36 | |
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; |
37 | |
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; |
38 | |
import org.codehaus.plexus.registry.Registry; |
39 | |
import org.codehaus.plexus.registry.RegistryException; |
40 | |
import org.codehaus.plexus.registry.RegistryListener; |
41 | |
import org.codehaus.plexus.registry.commons.CommonsConfigurationRegistry; |
42 | |
import org.codehaus.plexus.util.SelectorUtils; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | 0 | public class FileTypes |
52 | |
implements Initializable, RegistryListener |
53 | |
{ |
54 | |
public static final String ARTIFACTS = "artifacts"; |
55 | |
|
56 | |
public static final String AUTO_REMOVE = "auto-remove"; |
57 | |
|
58 | |
public static final String INDEXABLE_CONTENT = "indexable-content"; |
59 | |
|
60 | |
public static final String IGNORED = "ignored"; |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
private ArchivaConfiguration archivaConfiguration; |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | 0 | private Map<String, List<String>> defaultTypeMap = new HashMap<String, List<String>>(); |
71 | |
|
72 | |
private List<String> artifactPatterns; |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | 0 | public static final List<String> DEFAULT_EXCLUSIONS = |
80 | |
Arrays.asList( "**/maven-metadata.xml", "**/maven-metadata-*.xml", "**/*.sha1", "**/*.asc", "**/*.md5", |
81 | |
"**/*.pgp", "**/.index/**", "**/.indexer/**" ); |
82 | |
|
83 | |
public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration ) |
84 | |
{ |
85 | 0 | this.archivaConfiguration = archivaConfiguration; |
86 | 0 | } |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
public List<String> getFileTypePatterns( String id ) |
106 | |
{ |
107 | 0 | Configuration config = archivaConfiguration.getConfiguration(); |
108 | 0 | Predicate selectedFiletype = new FiletypeSelectionPredicate( id ); |
109 | 0 | FileType filetype = (FileType) CollectionUtils.find( config.getRepositoryScanning().getFileTypes(), |
110 | |
selectedFiletype ); |
111 | |
|
112 | 0 | if ( ( filetype != null ) && CollectionUtils.isNotEmpty( filetype.getPatterns() ) ) |
113 | |
{ |
114 | 0 | return filetype.getPatterns(); |
115 | |
} |
116 | |
|
117 | 0 | List<String> defaultPatterns = defaultTypeMap.get( id ); |
118 | |
|
119 | 0 | if ( CollectionUtils.isEmpty( defaultPatterns ) ) |
120 | |
{ |
121 | 0 | return Collections.singletonList( "**/*" ); |
122 | |
} |
123 | |
|
124 | 0 | return defaultPatterns; |
125 | |
} |
126 | |
|
127 | |
public synchronized boolean matchesArtifactPattern( String relativePath ) |
128 | |
{ |
129 | |
|
130 | 0 | relativePath = relativePath.replace( '\\', '/' ); |
131 | |
|
132 | 0 | if ( artifactPatterns == null ) |
133 | |
{ |
134 | 0 | artifactPatterns = getFileTypePatterns( ARTIFACTS ); |
135 | |
} |
136 | |
|
137 | 0 | for ( String pattern : artifactPatterns ) |
138 | |
{ |
139 | 0 | if ( SelectorUtils.matchPath( pattern, relativePath, false ) ) |
140 | |
{ |
141 | |
|
142 | 0 | return true; |
143 | |
} |
144 | |
} |
145 | |
|
146 | |
|
147 | 0 | return false; |
148 | |
} |
149 | |
|
150 | |
public boolean matchesDefaultExclusions( String relativePath ) |
151 | |
{ |
152 | |
|
153 | 0 | relativePath = relativePath.replace( '\\', '/' ); |
154 | |
|
155 | 0 | for ( String pattern : DEFAULT_EXCLUSIONS ) |
156 | |
{ |
157 | 0 | if ( SelectorUtils.matchPath( pattern, relativePath, false ) ) |
158 | |
{ |
159 | |
|
160 | 0 | return true; |
161 | |
} |
162 | |
} |
163 | |
|
164 | |
|
165 | 0 | return false; |
166 | |
} |
167 | |
|
168 | |
public void initialize() |
169 | |
throws InitializationException |
170 | |
{ |
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | 0 | String errMsg = "Unable to load default archiva configuration for FileTypes: "; |
176 | |
|
177 | |
try |
178 | |
{ |
179 | 0 | CommonsConfigurationRegistry commonsRegistry = new CommonsConfigurationRegistry(); |
180 | |
|
181 | |
|
182 | 0 | Field fld = commonsRegistry.getClass().getDeclaredField( "configuration" ); |
183 | 0 | fld.setAccessible( true ); |
184 | 0 | fld.set( commonsRegistry, new CombinedConfiguration() ); |
185 | 0 | commonsRegistry.enableLogging( new Slf4JPlexusLogger( FileTypes.class ) ); |
186 | 0 | commonsRegistry.addConfigurationFromResource( "org/apache/maven/archiva/configuration/default-archiva.xml" ); |
187 | |
|
188 | |
|
189 | 0 | ConfigurationRegistryReader configReader = new ConfigurationRegistryReader(); |
190 | 0 | Configuration defaultConfig = configReader.read( commonsRegistry ); |
191 | |
|
192 | 0 | initialiseTypeMap( defaultConfig ); |
193 | |
} |
194 | 0 | catch ( RegistryException e ) |
195 | |
{ |
196 | 0 | throw new InitializationException( errMsg + e.getMessage(), e ); |
197 | |
} |
198 | 0 | catch ( SecurityException e ) |
199 | |
{ |
200 | 0 | throw new InitializationException( errMsg + e.getMessage(), e ); |
201 | |
} |
202 | 0 | catch ( NoSuchFieldException e ) |
203 | |
{ |
204 | 0 | throw new InitializationException( errMsg + e.getMessage(), e ); |
205 | |
} |
206 | 0 | catch ( IllegalArgumentException e ) |
207 | |
{ |
208 | 0 | throw new InitializationException( errMsg + e.getMessage(), e ); |
209 | |
} |
210 | 0 | catch ( IllegalAccessException e ) |
211 | |
{ |
212 | 0 | throw new InitializationException( errMsg + e.getMessage(), e ); |
213 | 0 | } |
214 | |
|
215 | 0 | this.archivaConfiguration.addChangeListener( this ); |
216 | 0 | } |
217 | |
|
218 | |
private void initialiseTypeMap( Configuration configuration ) |
219 | |
{ |
220 | 0 | defaultTypeMap.clear(); |
221 | |
|
222 | |
|
223 | 0 | List<FileType> filetypes = configuration.getRepositoryScanning().getFileTypes(); |
224 | 0 | for ( FileType filetype : filetypes ) |
225 | |
{ |
226 | 0 | List<String> patterns = defaultTypeMap.get( filetype.getId() ); |
227 | 0 | if ( patterns == null ) |
228 | |
{ |
229 | 0 | patterns = new ArrayList<String>(); |
230 | |
} |
231 | 0 | patterns.addAll( filetype.getPatterns() ); |
232 | |
|
233 | 0 | defaultTypeMap.put( filetype.getId(), patterns ); |
234 | 0 | } |
235 | 0 | } |
236 | |
|
237 | |
public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue ) |
238 | |
{ |
239 | 0 | if ( propertyName.contains( "fileType" ) ) |
240 | |
{ |
241 | 0 | artifactPatterns = null; |
242 | |
|
243 | 0 | initialiseTypeMap( archivaConfiguration.getConfiguration() ); |
244 | |
} |
245 | 0 | } |
246 | |
|
247 | |
public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue ) |
248 | |
{ |
249 | |
|
250 | 0 | } |
251 | |
} |