001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.config.store; 014 015import static org.apache.juneau.internal.StringUtils.*; 016 017import java.io.*; 018import java.util.concurrent.*; 019 020import org.apache.juneau.*; 021import org.apache.juneau.annotation.*; 022import org.apache.juneau.collections.*; 023 024/** 025 * Filesystem-based storage location for configuration files. 026 * 027 * <p> 028 * Points to a file system directory containing configuration files. 029 */ 030@ConfigurableContext 031public class ConfigMemoryStore extends ConfigStore { 032 033 //------------------------------------------------------------------------------------------------------------------- 034 // Configurable properties 035 //------------------------------------------------------------------------------------------------------------------- 036 037 static final String PREFIX = "ConfigMemoryStore"; 038 039 //------------------------------------------------------------------------------------------------------------------- 040 // Predefined instances 041 //------------------------------------------------------------------------------------------------------------------- 042 043 /** Default memory store, all default values.*/ 044 public static final ConfigMemoryStore DEFAULT = ConfigMemoryStore.create().build(); 045 046 047 //------------------------------------------------------------------------------------------------------------------- 048 // Instance 049 //------------------------------------------------------------------------------------------------------------------- 050 051 /** 052 * Create a new builder for this object. 053 * 054 * @return A new builder for this object. 055 */ 056 public static ConfigMemoryStoreBuilder create() { 057 return new ConfigMemoryStoreBuilder(); 058 } 059 060 @Override /* Context */ 061 public ConfigMemoryStoreBuilder builder() { 062 return new ConfigMemoryStoreBuilder(getPropertyStore()); 063 } 064 065 private final ConcurrentHashMap<String,String> cache = new ConcurrentHashMap<>(); 066 067 /** 068 * Constructor. 069 * 070 * @param ps The settings for this content store. 071 */ 072 protected ConfigMemoryStore(PropertyStore ps) { 073 super(ps); 074 } 075 076 @Override /* ConfigStore */ 077 public synchronized String read(String name) { 078 return emptyIfNull(cache.get(name)); 079 } 080 081 @Override /* ConfigStore */ 082 public synchronized String write(String name, String expectedContents, String newContents) { 083 084 // This is a no-op. 085 if (isEquals(expectedContents, newContents)) 086 return null; 087 088 String currentContents = read(name); 089 090 if (expectedContents != null && ! isEquals(currentContents, expectedContents)) 091 return currentContents; 092 093 update(name, newContents); 094 095 return null; 096 } 097 098 @Override /* ConfigStore */ 099 public synchronized boolean exists(String name) { 100 return cache.containsKey(name); 101 } 102 103 @Override /* ConfigStore */ 104 public synchronized ConfigMemoryStore update(String name, String newContents) { 105 if (newContents == null) 106 cache.remove(name); 107 else 108 cache.put(name, newContents); 109 super.update(name, newContents); // Trigger any listeners. 110 return this; 111 } 112 113 /** 114 * No-op. 115 */ 116 @Override /* Closeable */ 117 public void close() throws IOException { 118 // No-op 119 } 120 121 //----------------------------------------------------------------------------------------------------------------- 122 // Other methods. 123 //----------------------------------------------------------------------------------------------------------------- 124 125 @Override /* Context */ 126 public OMap toMap() { 127 return super.toMap() 128 .a("ConfigMemoryStore", new DefaultFilteringOMap() 129 ); 130 } 131}