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.

CopyFileEvent.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package org.apache.archiva.transaction;
  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.commons.io.FileUtils;
  21. import org.codehaus.plexus.digest.Digester;
  22. import java.io.IOException;
  23. import java.nio.file.Files;
  24. import java.nio.file.Path;
  25. import java.nio.file.Paths;
  26. import java.util.List;
  27. /**
  28. * Event to copy a file.
  29. *
  30. *
  31. */
  32. public class CopyFileEvent
  33. extends AbstractTransactionEvent
  34. {
  35. private final Path source;
  36. private final Path destination;
  37. /**
  38. *
  39. * @param source
  40. * @param destination
  41. * @param digesters {@link List}<{@link Digester}> digesters to use for checksumming
  42. */
  43. public CopyFileEvent( Path source, Path destination, List<? extends Digester> digesters )
  44. {
  45. super( digesters );
  46. this.source = source;
  47. this.destination = destination;
  48. }
  49. @Override
  50. public void commit()
  51. throws IOException
  52. {
  53. createBackup( destination );
  54. mkDirs( destination.getParent() );
  55. FileUtils.copyFile( source.toFile(), destination.toFile() );
  56. createChecksums( destination, true );
  57. copyChecksums();
  58. copyChecksum( "asc" );
  59. }
  60. /**
  61. * Copy checksums of source file with all digesters if exist
  62. *
  63. * @throws IOException
  64. */
  65. private void copyChecksums()
  66. throws IOException
  67. {
  68. for ( Digester digester : getDigesters() )
  69. {
  70. copyChecksum( getDigesterFileExtension( digester ) );
  71. }
  72. }
  73. /**
  74. * Copy checksum of source file with extension provided if exists
  75. *
  76. * @param extension
  77. * @return whether the checksum exists or not
  78. * @throws IOException
  79. */
  80. private boolean copyChecksum( String extension )
  81. throws IOException
  82. {
  83. Path checksumSource = Paths.get( source.toAbsolutePath() + "." + extension );
  84. if ( Files.exists(checksumSource) )
  85. {
  86. Path checksumDestination = Paths.get( destination.toAbsolutePath() + "." + extension );
  87. FileUtils.copyFile( checksumSource.toFile(), checksumDestination.toFile() );
  88. return true;
  89. }
  90. return false;
  91. }
  92. @Override
  93. public void rollback()
  94. throws IOException
  95. {
  96. Files.deleteIfExists(destination);
  97. revertFilesCreated();
  98. revertMkDirs();
  99. restoreBackups();
  100. }
  101. }