Java tutorial
/** * Copyright 2015 OpenSearchServer Inc. * * Licensed 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 com.opensearchserver.affinities; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; import org.apache.commons.lang3.StringUtils; import com.fasterxml.jackson.core.JsonGenerationException; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; import com.opensearchserver.affinities.model.Affinity; import com.opensearchserver.utils.json.JsonFileFilter; import com.opensearchserver.utils.json.JsonMapper; public class AffinityList { public volatile static AffinityList INSTANCE = null; private final ReadWriteLock rwl = new ReentrantReadWriteLock(); private final File directory; private final Map<String, Affinity> affinities; private volatile Map<String, Affinity> affinitiesCache; public static void load(File directory) throws IOException { if (INSTANCE != null) throw new IOException("Already loaded"); INSTANCE = new AffinityList(directory); } private AffinityList(File directory) throws JsonGenerationException, JsonMappingException, JsonParseException, IOException { this.directory = directory; this.affinities = new HashMap<String, Affinity>(); load(); } private File getFile(String name) { return new File(directory, name + ".json"); } public void load() throws JsonGenerationException, JsonMappingException, JsonParseException, IOException { try { File[] files = directory.listFiles(JsonFileFilter.INSTANCE); if (files == null) return; for (File file : files) { String name = file.getName(); name = name.substring(0, name.length() - 5); set(name, JsonMapper.MAPPER.readValue(file, Affinity.class)); } } finally { buildCache(); } } private void buildCache() { affinitiesCache = new HashMap<String, Affinity>(affinities); } public Affinity delete(String name) { if (StringUtils.isEmpty(name)) return null; rwl.writeLock().lock(); try { getFile(name).delete(); Affinity affinity = affinities.remove(name); buildCache(); return affinity; } finally { rwl.writeLock().unlock(); } } public void set(String name, Affinity affinity) throws JsonGenerationException, JsonMappingException, IOException { if (affinity == null) return; if (StringUtils.isEmpty(name)) return; rwl.writeLock().lock(); try { JsonMapper.MAPPER.writeValue(getFile(name), affinity); affinities.put(name, affinity); buildCache(); } finally { rwl.writeLock().unlock(); } } public Affinity get(String name) { return affinitiesCache.get(name); } public Set<String> nameSet() { return affinitiesCache.keySet(); } }