001package org.eclipse.aether.internal.impl; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import javax.inject.Named; 023import javax.inject.Singleton; 024 025import java.io.File; 026import java.io.IOException; 027import java.io.InputStream; 028import java.io.OutputStream; 029import java.io.UncheckedIOException; 030import java.nio.file.Files; 031import java.nio.file.Path; 032import java.util.Map; 033import java.util.Properties; 034 035import org.eclipse.aether.util.FileUtils; 036import org.slf4j.Logger; 037import org.slf4j.LoggerFactory; 038 039/** 040 * Manages access to a properties file. 041 */ 042@Singleton 043@Named 044public final class DefaultTrackingFileManager 045 implements TrackingFileManager 046{ 047 private static final Logger LOGGER = LoggerFactory.getLogger( DefaultTrackingFileManager.class ); 048 049 @Override 050 public Properties read( File file ) 051 { 052 Path filePath = file.toPath(); 053 if ( Files.isRegularFile( filePath ) ) 054 { 055 try ( InputStream stream = Files.newInputStream( filePath ) ) 056 { 057 Properties props = new Properties(); 058 props.load( stream ); 059 return props; 060 } 061 catch ( IOException e ) 062 { 063 LOGGER.warn( "Failed to read tracking file '{}'", file, e ); 064 throw new UncheckedIOException( e ); 065 } 066 } 067 return null; 068 } 069 070 @Override 071 public Properties update( File file, Map<String, String> updates ) 072 { 073 Path filePath = file.toPath(); 074 Properties props = new Properties(); 075 076 try 077 { 078 Files.createDirectories( filePath.getParent() ); 079 } 080 catch ( IOException e ) 081 { 082 LOGGER.warn( "Failed to create tracking file parent '{}'", file, e ); 083 throw new UncheckedIOException( e ); 084 } 085 086 try 087 { 088 if ( Files.isReadable( filePath ) ) 089 { 090 try ( InputStream stream = Files.newInputStream( filePath ) ) 091 { 092 props.load( stream ); 093 } 094 } 095 096 for ( Map.Entry<String, String> update : updates.entrySet() ) 097 { 098 if ( update.getValue() == null ) 099 { 100 props.remove( update.getKey() ); 101 } 102 else 103 { 104 props.setProperty( update.getKey(), update.getValue() ); 105 } 106 } 107 108 FileUtils.writeFile( filePath, p -> 109 { 110 try ( OutputStream stream = Files.newOutputStream( p ) ) 111 { 112 LOGGER.debug( "Writing tracking file '{}'", file ); 113 props.store( stream, "NOTE: This is a Maven Resolver internal implementation file" 114 + ", its format can be changed without prior notice." ); 115 } 116 } ); 117 } 118 catch ( IOException e ) 119 { 120 LOGGER.warn( "Failed to write tracking file '{}'", file, e ); 121 throw new UncheckedIOException( e ); 122 } 123 124 return props; 125 } 126}