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