org.apache.inlong.audit.file.holder.PropertiesConfigHolder Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.inlong.audit.file.holder;
import org.apache.inlong.audit.file.ConfigHolder;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
* properties to map
*/
public class PropertiesConfigHolder extends ConfigHolder {
private static final Logger LOG = LoggerFactory.getLogger(PropertiesConfigHolder.class);
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
private Map holder;
public PropertiesConfigHolder(String fileName, boolean needToCheckChanged) {
super(fileName, needToCheckChanged);
}
@Override
public void loadFromFileToHolder() {
readWriteLock.readLock().lock();
try {
Map tmpHolder = loadProperties();
LOG.info(getFileName() + " load content {}", tmpHolder);
holder = tmpHolder;
} finally {
readWriteLock.readLock().unlock();
}
}
/**
* holder
*
* @return
*/
public Map forkHolder() {
Map tmpHolder = new HashMap();
if (holder != null) {
for (Map.Entry entry : holder.entrySet()) {
tmpHolder.put(entry.getKey(), entry.getValue());
}
}
return tmpHolder;
}
private List getStringListFromHolder(Map tmpHolder) {
List result = new ArrayList();
for (Map.Entry entry : tmpHolder.entrySet()) {
result.add(entry.getKey() + "=" + entry.getValue());
}
return result;
}
/**
* load from holder
* @param tmpHolder
* @return
*/
public boolean loadFromHolderToFile(Map tmpHolder) {
readWriteLock.writeLock().lock();
boolean isSuccess = false;
try {
File sourceFile = new File(getFilePath());
File targetFile = new File(getNextBackupFileName());
File tmpNewFile = new File(getFileName() + ".tmp");
if (sourceFile.exists()) {
FileUtils.copyFile(sourceFile, targetFile);
}
List lines = getStringListFromHolder(tmpHolder);
FileUtils.writeLines(tmpNewFile, lines);
FileUtils.copyFile(tmpNewFile, sourceFile);
tmpNewFile.delete();
isSuccess = true;
getFileChanged().set(true);
} catch (Exception ex) {
LOG.error("error in writing file", ex);
} finally {
readWriteLock.writeLock().unlock();
}
return isSuccess;
}
protected Map loadProperties() {
Map result = new HashMap();
InputStream inStream = null;
try {
URL url = getClass().getClassLoader().getResource(getFileName());
inStream = url != null ? url.openStream() : null;
if (inStream == null) {
LOG.error("InputStream {} is null!", getFileName());
}
Properties props = new Properties();
props.load(inStream);
for (Map.Entry