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.*;
021
022/**
023 * Filesystem-based storage location for configuration files.
024 *
025 * <p>
026 * Points to a file system directory containing configuration files.
027 */
028public class ConfigMemoryStore extends ConfigStore {
029
030   //-------------------------------------------------------------------------------------------------------------------
031   // Predefined instances
032   //-------------------------------------------------------------------------------------------------------------------
033
034   /** Default memory store, all default values.*/
035   public static final ConfigMemoryStore DEFAULT = ConfigMemoryStore.create().build();
036
037
038   //-------------------------------------------------------------------------------------------------------------------
039   // Instance
040   //-------------------------------------------------------------------------------------------------------------------
041
042   /**
043    * Create a new builder for this object.
044    *
045    * @return A new builder for this object.
046    */
047   public static ConfigMemoryStoreBuilder create() {
048      return new ConfigMemoryStoreBuilder();
049   }
050
051   @Override /* Context */
052   public ConfigMemoryStoreBuilder builder() {
053      return new ConfigMemoryStoreBuilder(getPropertyStore());
054   }
055
056   private final ConcurrentHashMap<String,String> cache = new ConcurrentHashMap<>();
057
058   /**
059    * Constructor.
060    *
061    * @param ps The settings for this content store.
062    */
063   protected ConfigMemoryStore(PropertyStore ps) {
064      super(ps);
065   }
066
067   @Override /* ConfigStore */
068   public synchronized String read(String name) {
069      return emptyIfNull(cache.get(name));
070   }
071
072   @Override /* ConfigStore */
073   public synchronized String write(String name, String expectedContents, String newContents) {
074
075      // This is a no-op.
076      if (isEquals(expectedContents, newContents))
077         return null;
078
079      String currentContents = read(name);
080
081      if (expectedContents != null && ! isEquals(currentContents, expectedContents))
082         return currentContents;
083
084      update(name, newContents);
085
086      return null;
087   }
088
089
090   @Override /* ConfigStore */
091   public synchronized ConfigMemoryStore update(String name, String newContents) {
092      cache.put(name, newContents);
093      super.update(name, newContents);  // Trigger any listeners.
094      return this;
095   }
096
097   /**
098    * No-op.
099    */
100   @Override /* Closeable */
101   public void close() throws IOException {
102      // No-op
103   }
104}