You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AutoRenameConsumer.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package org.apache.archiva.consumers.core;
  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.admin.model.beans.ManagedRepository;
  21. import org.apache.archiva.consumers.AbstractMonitoredConsumer;
  22. import org.apache.archiva.consumers.ConsumerException;
  23. import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
  24. import org.apache.commons.io.FileUtils;
  25. import org.springframework.context.annotation.Scope;
  26. import org.springframework.stereotype.Service;
  27. import java.io.File;
  28. import java.io.IOException;
  29. import java.util.ArrayList;
  30. import java.util.Date;
  31. import java.util.HashMap;
  32. import java.util.Iterator;
  33. import java.util.List;
  34. import java.util.Map;
  35. /**
  36. * AutoRenameConsumer
  37. *
  38. *
  39. */
  40. @Service( "knownRepositoryContentConsumer#auto-rename" )
  41. @Scope( "prototype" )
  42. public class AutoRenameConsumer
  43. extends AbstractMonitoredConsumer
  44. implements KnownRepositoryContentConsumer
  45. {
  46. /**
  47. * default-value="auto-rename"
  48. */
  49. private String id = "auto-rename";
  50. /**
  51. * default-value="Automatically rename common artifact mistakes."
  52. */
  53. private String description = "Automatically rename common artifact mistakes.";
  54. private static final String RENAME_FAILURE = "rename_failure";
  55. private File repositoryDir;
  56. private List<String> includes = new ArrayList<>( 3 );
  57. private Map<String, String> extensionRenameMap = new HashMap<String, String>();
  58. public AutoRenameConsumer()
  59. {
  60. includes.add( "**/*.distribution-tgz" );
  61. includes.add( "**/*.distribution-zip" );
  62. includes.add( "**/*.plugin" );
  63. extensionRenameMap.put( ".distribution-tgz", ".tar.gz" );
  64. extensionRenameMap.put( ".distribution-zip", ".zip" );
  65. extensionRenameMap.put( ".plugin", ".jar" );
  66. }
  67. public String getId()
  68. {
  69. return this.id;
  70. }
  71. public String getDescription()
  72. {
  73. return this.description;
  74. }
  75. public boolean isPermanent()
  76. {
  77. return false;
  78. }
  79. public void beginScan( ManagedRepository repository, Date whenGathered )
  80. throws ConsumerException
  81. {
  82. this.repositoryDir = new File( repository.getLocation() );
  83. }
  84. public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
  85. throws ConsumerException
  86. {
  87. beginScan( repository, whenGathered );
  88. }
  89. public void completeScan()
  90. {
  91. /* do nothing */
  92. }
  93. public void completeScan( boolean executeOnEntireRepo )
  94. {
  95. completeScan();
  96. }
  97. public List<String> getExcludes()
  98. {
  99. return null;
  100. }
  101. public List<String> getIncludes()
  102. {
  103. return includes;
  104. }
  105. public void processFile( String path )
  106. throws ConsumerException
  107. {
  108. File file = new File( this.repositoryDir, path );
  109. if ( file.exists() )
  110. {
  111. Iterator<String> itExtensions = this.extensionRenameMap.keySet().iterator();
  112. while ( itExtensions.hasNext() )
  113. {
  114. String extension = itExtensions.next();
  115. if ( path.endsWith( extension ) )
  116. {
  117. String fixedExtension = this.extensionRenameMap.get( extension );
  118. String correctedPath = path.substring( 0, path.length() - extension.length() ) + fixedExtension;
  119. File to = new File( this.repositoryDir, correctedPath );
  120. try
  121. {
  122. // Rename the file.
  123. FileUtils.moveFile( file, to );
  124. }
  125. catch ( IOException e )
  126. {
  127. triggerConsumerWarning( RENAME_FAILURE, "Unable to rename " + path + " to " + correctedPath +
  128. ": " + e.getMessage() );
  129. }
  130. }
  131. }
  132. triggerConsumerInfo( "(Auto) Removing File: " + file.getAbsolutePath() );
  133. file.delete();
  134. }
  135. }
  136. public void processFile( String path, boolean executeOnEntireRepo )
  137. throws ConsumerException
  138. {
  139. processFile( path );
  140. }
  141. }