Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

FileTransaction.java 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 java.io.File;
  21. import java.io.IOException;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import org.codehaus.plexus.digest.Digester;
  25. /**
  26. * Implement commit/rollback semantics for a set of files.
  27. *
  28. */
  29. public class FileTransaction
  30. {
  31. private List<AbstractTransactionEvent> events = new ArrayList<>();
  32. public void commit()
  33. throws TransactionException
  34. {
  35. List<TransactionEvent> toRollback = new ArrayList<TransactionEvent>( events.size() );
  36. for ( TransactionEvent event : events )
  37. {
  38. try
  39. {
  40. event.commit();
  41. toRollback.add( event );
  42. }
  43. catch ( IOException e )
  44. {
  45. try
  46. {
  47. rollback( toRollback );
  48. throw new TransactionException( "Unable to commit file transaction", e );
  49. }
  50. catch ( IOException ioe )
  51. {
  52. throw new TransactionException(
  53. "Unable to commit file transaction, and rollback failed with error: '" + ioe.getMessage() + "'",
  54. e );
  55. }
  56. }
  57. }
  58. }
  59. private void rollback( List<TransactionEvent> toRollback )
  60. throws IOException
  61. {
  62. for ( TransactionEvent event : toRollback )
  63. {
  64. event.rollback();
  65. }
  66. }
  67. /**
  68. * @param source
  69. * @param destination
  70. * @param digesters {@link List}&lt;{@link org.codehaus.plexus.digest.Digester}&gt; digesters to use for checksumming
  71. */
  72. public void copyFile( File source, File destination, List<? extends Digester> digesters )
  73. {
  74. events.add( new CopyFileEvent( source, destination, digesters ) );
  75. }
  76. /**
  77. * @param content
  78. * @param destination
  79. * @param digesters {@link List}&lt;{@link org.codehaus.plexus.digest.Digester}&gt; digesters to use for checksumming
  80. */
  81. public void createFile( String content, File destination, List<? extends Digester> digesters )
  82. {
  83. events.add( new CreateFileEvent( content, destination, digesters ) );
  84. }
  85. }