This project has retired. For details please refer to its Attic page.
FileTypes xref
View Javadoc
1   package org.apache.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.archiva.common.FileTypeUtils;
23  import org.apache.archiva.configuration.functors.FiletypeSelectionPredicate;
24  import org.apache.archiva.configuration.io.registry.ConfigurationRegistryReader;
25  import org.apache.archiva.redback.components.registry.Registry;
26  import org.apache.archiva.redback.components.registry.RegistryException;
27  import org.apache.archiva.redback.components.registry.RegistryListener;
28  import org.apache.archiva.redback.components.registry.commons.CommonsConfigurationRegistry;
29  import org.apache.commons.collections.CollectionUtils;
30  import org.apache.commons.collections.Predicate;
31  import org.apache.commons.configuration.CombinedConfiguration;
32  import org.apache.tools.ant.types.selectors.SelectorUtils;
33  import org.springframework.stereotype.Service;
34  
35  import javax.annotation.PostConstruct;
36  import javax.inject.Inject;
37  import javax.inject.Named;
38  import java.lang.reflect.Field;
39  import java.util.ArrayList;
40  import java.util.Collections;
41  import java.util.HashMap;
42  import java.util.List;
43  import java.util.Map;
44  
45  /**
46   * FileTypes
47   */
48  @Service("fileTypes")
49  public class FileTypes
50      implements RegistryListener
51  {
52      public static final String ARTIFACTS = "artifacts";
53  
54      public static final String AUTO_REMOVE = "auto-remove";
55  
56      public static final String INDEXABLE_CONTENT = "indexable-content";
57  
58      public static final String IGNORED = "ignored";
59  
60      @Inject
61      @Named(value = "archivaConfiguration#default")
62      private ArchivaConfiguration archivaConfiguration;
63  
64      /**
65       * Map of default values for the file types.
66       */
67      private Map<String, List<String>> defaultTypeMap = new HashMap<>();
68  
69      private List<String> artifactPatterns;
70  
71      /**
72       * Default exclusions from artifact consumers that are using the file types. Note that this is simplistic in the
73       * case of the support files (based on extension) as it is elsewhere - it may be better to match these to actual
74       * artifacts and exclude later during scanning.
75       *
76       * @deprecated
77       */
78      public static final List<String> DEFAULT_EXCLUSIONS = FileTypeUtils.DEFAULT_EXCLUSIONS;
79  
80      public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
81      {
82          this.archivaConfiguration = archivaConfiguration;
83      }
84  
85      /**
86       * Get the list of patterns for a specified filetype.
87       * You will always get a list.  In this order.
88       * <ul>
89       * <li>The Configured List</li>
90       * <li>The Default List</li>
91       * <li>A single item list of <code>&quot;**&#47;*&quot;</code></li>
92       * </ul>
93       *
94       * @param id the id to lookup.
95       * @return the list of patterns.
96       */
97      public List<String> getFileTypePatterns( String id )
98      {
99          Configuration config = archivaConfiguration.getConfiguration();
100         Predicate selectedFiletype = new FiletypeSelectionPredicate( id );
101         RepositoryScanningConfiguration repositoryScanningConfiguration = config.getRepositoryScanning();
102         if ( repositoryScanningConfiguration != null )
103         {
104             FileType filetype =
105                 (FileType) CollectionUtils.find( config.getRepositoryScanning().getFileTypes(), selectedFiletype );
106 
107             if ( ( filetype != null ) && CollectionUtils.isNotEmpty( filetype.getPatterns() ) )
108             {
109                 return filetype.getPatterns();
110             }
111         }
112         List<String> defaultPatterns = defaultTypeMap.get( id );
113 
114         if ( CollectionUtils.isEmpty( defaultPatterns ) )
115         {
116             return Collections.singletonList( "**/*" );
117         }
118 
119         return defaultPatterns;
120     }
121 
122     public synchronized boolean matchesArtifactPattern( String relativePath )
123     {
124         // Correct the slash pattern.
125         relativePath = relativePath.replace( '\\', '/' );
126 
127         if ( artifactPatterns == null )
128         {
129             artifactPatterns = getFileTypePatterns( ARTIFACTS );
130         }
131 
132         for ( String pattern : artifactPatterns )
133         {
134             if ( SelectorUtils.matchPath( pattern, relativePath, false ) )
135             {
136                 // Found match
137                 return true;
138             }
139         }
140 
141         // No match.
142         return false;
143     }
144 
145     public boolean matchesDefaultExclusions( String relativePath )
146     {
147         // Correct the slash pattern.
148         relativePath = relativePath.replace( '\\', '/' );
149 
150         for ( String pattern : DEFAULT_EXCLUSIONS )
151         {
152             if ( SelectorUtils.matchPath( pattern, relativePath, false ) )
153             {
154                 // Found match
155                 return true;
156             }
157         }
158 
159         // No match.
160         return false;
161     }
162 
163     @PostConstruct
164     public void initialize()
165     {
166         // TODO: why is this done by hand?
167 
168         // TODO: ideally, this would be instantiated by configuration instead, and not need to be a component
169 
170         String errMsg = "Unable to load default archiva configuration for FileTypes: ";
171 
172         try
173         {
174             CommonsConfigurationRegistry commonsRegistry = new CommonsConfigurationRegistry();
175 
176             // Configure commonsRegistry
177             Field fld = commonsRegistry.getClass().getDeclaredField( "configuration" );
178             fld.setAccessible( true );
179             fld.set( commonsRegistry, new CombinedConfiguration() );
180             commonsRegistry.addConfigurationFromResource( "org/apache/archiva/configuration/default-archiva.xml" );
181 
182             // Read configuration as it was intended.
183             ConfigurationRegistryReader configReader = new ConfigurationRegistryReader();
184             Configuration defaultConfig = configReader.read( commonsRegistry );
185 
186             initialiseTypeMap( defaultConfig );
187         }
188         catch ( RegistryException e )
189         {
190             throw new RuntimeException( errMsg + e.getMessage(), e );
191         }
192         catch ( SecurityException e )
193         {
194             throw new RuntimeException( errMsg + e.getMessage(), e );
195         }
196         catch ( NoSuchFieldException e )
197         {
198             throw new RuntimeException( errMsg + e.getMessage(), e );
199         }
200         catch ( IllegalArgumentException e )
201         {
202             throw new RuntimeException( errMsg + e.getMessage(), e );
203         }
204         catch ( IllegalAccessException e )
205         {
206             throw new RuntimeException( errMsg + e.getMessage(), e );
207         }
208 
209         this.archivaConfiguration.addChangeListener( this );
210     }
211 
212     private void initialiseTypeMap( Configuration configuration )
213     {
214         defaultTypeMap.clear();
215 
216         // Store the default file type declaration.
217         List<FileType> filetypes = configuration.getRepositoryScanning().getFileTypes();
218         for ( FileType filetype : filetypes )
219         {
220             List<String> patterns = defaultTypeMap.get( filetype.getId() );
221             if ( patterns == null )
222             {
223                 patterns = new ArrayList<>( filetype.getPatterns().size() );
224             }
225             patterns.addAll( filetype.getPatterns() );
226 
227             defaultTypeMap.put( filetype.getId(), patterns );
228         }
229     }
230 
231     @Override
232     public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
233     {
234         if ( propertyName.contains( "fileType" ) )
235         {
236             artifactPatterns = null;
237 
238             initialiseTypeMap( archivaConfiguration.getConfiguration() );
239         }
240     }
241 
242     @Override
243     public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
244     {
245         /* nothing to do */
246     }
247 }