您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

FileTypes.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. package org.apache.archiva.configuration;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.archiva.common.FileTypeUtils;
  21. import org.apache.archiva.configuration.functors.FiletypeSelectionPredicate;
  22. import org.apache.archiva.configuration.io.registry.ConfigurationRegistryReader;
  23. import org.apache.archiva.redback.components.registry.Registry;
  24. import org.apache.archiva.redback.components.registry.RegistryException;
  25. import org.apache.archiva.redback.components.registry.RegistryListener;
  26. import org.apache.archiva.redback.components.registry.commons.CommonsConfigurationRegistry;
  27. import org.apache.commons.collections.CollectionUtils;
  28. import org.apache.commons.collections.Predicate;
  29. import org.apache.commons.configuration.CombinedConfiguration;
  30. import org.apache.tools.ant.types.selectors.SelectorUtils;
  31. import org.springframework.stereotype.Service;
  32. import javax.annotation.PostConstruct;
  33. import javax.inject.Inject;
  34. import javax.inject.Named;
  35. import java.lang.reflect.Field;
  36. import java.util.ArrayList;
  37. import java.util.Collections;
  38. import java.util.HashMap;
  39. import java.util.List;
  40. import java.util.Map;
  41. /**
  42. * FileTypes
  43. */
  44. @Service ("fileTypes")
  45. public class FileTypes
  46. implements RegistryListener
  47. {
  48. public static final String ARTIFACTS = "artifacts";
  49. public static final String AUTO_REMOVE = "auto-remove";
  50. public static final String INDEXABLE_CONTENT = "indexable-content";
  51. public static final String IGNORED = "ignored";
  52. /**
  53. *
  54. */
  55. @Inject
  56. @Named (value = "archivaConfiguration#default")
  57. private ArchivaConfiguration archivaConfiguration;
  58. /**
  59. * Map of default values for the file types.
  60. */
  61. private Map<String, List<String>> defaultTypeMap = new HashMap<String, List<String>>();
  62. private List<String> artifactPatterns;
  63. /**
  64. * Default exclusions from artifact consumers that are using the file types. Note that this is simplistic in the
  65. * case of the support files (based on extension) as it is elsewhere - it may be better to match these to actual
  66. * artifacts and exclude later during scanning.
  67. *
  68. * @deprecated
  69. */
  70. public static final List<String> DEFAULT_EXCLUSIONS = FileTypeUtils.DEFAULT_EXCLUSIONS;
  71. public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
  72. {
  73. this.archivaConfiguration = archivaConfiguration;
  74. }
  75. /**
  76. * Get the list of patterns for a specified filetype.
  77. *
  78. * <p>
  79. * You will always get a list. In this order.
  80. * <ul>
  81. * <li>The Configured List</li>
  82. * <li>The Default List</li>
  83. * <li>A single item list of <code>&quot;**&#47;*&quot;</code></li>
  84. * </ul>
  85. *
  86. *
  87. * @param id the id to lookup.
  88. * @return the list of patterns.
  89. */
  90. public List<String> getFileTypePatterns( String id )
  91. {
  92. Configuration config = archivaConfiguration.getConfiguration();
  93. Predicate selectedFiletype = new FiletypeSelectionPredicate( id );
  94. RepositoryScanningConfiguration repositoryScanningConfiguration = config.getRepositoryScanning();
  95. if ( repositoryScanningConfiguration != null )
  96. {
  97. FileType filetype =
  98. (FileType) CollectionUtils.find( config.getRepositoryScanning().getFileTypes(), selectedFiletype );
  99. if ( ( filetype != null ) && CollectionUtils.isNotEmpty( filetype.getPatterns() ) )
  100. {
  101. return filetype.getPatterns();
  102. }
  103. }
  104. List<String> defaultPatterns = defaultTypeMap.get( id );
  105. if ( CollectionUtils.isEmpty( defaultPatterns ) )
  106. {
  107. return Collections.singletonList( "**/*" );
  108. }
  109. return defaultPatterns;
  110. }
  111. public synchronized boolean matchesArtifactPattern( String relativePath )
  112. {
  113. // Correct the slash pattern.
  114. relativePath = relativePath.replace( '\\', '/' );
  115. if ( artifactPatterns == null )
  116. {
  117. artifactPatterns = getFileTypePatterns( ARTIFACTS );
  118. }
  119. for ( String pattern : artifactPatterns )
  120. {
  121. if ( SelectorUtils.matchPath( pattern, relativePath, false ) )
  122. {
  123. // Found match
  124. return true;
  125. }
  126. }
  127. // No match.
  128. return false;
  129. }
  130. public boolean matchesDefaultExclusions( String relativePath )
  131. {
  132. // Correct the slash pattern.
  133. relativePath = relativePath.replace( '\\', '/' );
  134. for ( String pattern : DEFAULT_EXCLUSIONS )
  135. {
  136. if ( SelectorUtils.matchPath( pattern, relativePath, false ) )
  137. {
  138. // Found match
  139. return true;
  140. }
  141. }
  142. // No match.
  143. return false;
  144. }
  145. @PostConstruct
  146. public void initialize()
  147. {
  148. // TODO: why is this done by hand?
  149. // TODO: ideally, this would be instantiated by configuration instead, and not need to be a component
  150. String errMsg = "Unable to load default archiva configuration for FileTypes: ";
  151. try
  152. {
  153. CommonsConfigurationRegistry commonsRegistry = new CommonsConfigurationRegistry();
  154. // Configure commonsRegistry
  155. Field fld = commonsRegistry.getClass().getDeclaredField( "configuration" );
  156. fld.setAccessible( true );
  157. fld.set( commonsRegistry, new CombinedConfiguration() );
  158. commonsRegistry.addConfigurationFromResource( "org/apache/archiva/configuration/default-archiva.xml" );
  159. // Read configuration as it was intended.
  160. ConfigurationRegistryReader configReader = new ConfigurationRegistryReader();
  161. Configuration defaultConfig = configReader.read( commonsRegistry );
  162. initialiseTypeMap( defaultConfig );
  163. }
  164. catch ( RegistryException e )
  165. {
  166. throw new RuntimeException( errMsg + e.getMessage(), e );
  167. }
  168. catch ( SecurityException e )
  169. {
  170. throw new RuntimeException( errMsg + e.getMessage(), e );
  171. }
  172. catch ( NoSuchFieldException e )
  173. {
  174. throw new RuntimeException( errMsg + e.getMessage(), e );
  175. }
  176. catch ( IllegalArgumentException e )
  177. {
  178. throw new RuntimeException( errMsg + e.getMessage(), e );
  179. }
  180. catch ( IllegalAccessException e )
  181. {
  182. throw new RuntimeException( errMsg + e.getMessage(), e );
  183. }
  184. this.archivaConfiguration.addChangeListener( this );
  185. }
  186. private void initialiseTypeMap( Configuration configuration )
  187. {
  188. defaultTypeMap.clear();
  189. // Store the default file type declaration.
  190. List<FileType> filetypes = configuration.getRepositoryScanning().getFileTypes();
  191. for ( FileType filetype : filetypes )
  192. {
  193. List<String> patterns = defaultTypeMap.get( filetype.getId() );
  194. if ( patterns == null )
  195. {
  196. patterns = new ArrayList<>( filetype.getPatterns().size() );
  197. }
  198. patterns.addAll( filetype.getPatterns() );
  199. defaultTypeMap.put( filetype.getId(), patterns );
  200. }
  201. }
  202. public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
  203. {
  204. if ( propertyName.contains( "fileType" ) )
  205. {
  206. artifactPatterns = null;
  207. initialiseTypeMap( archivaConfiguration.getConfiguration() );
  208. }
  209. }
  210. public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
  211. {
  212. /* nothing to do */
  213. }
  214. }