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.internal.*; 022 023/** 024 * Classpath-based storage location for configuration files. 025 * 026 * <p> 027 * Looks inside the JVM classpath for configuration files. 028 * 029 * <p> 030 * Configuration files retrieved from the classpath can be modified but not persisted. 031 */ 032public class ConfigClasspathStore extends ConfigStore { 033 034 //------------------------------------------------------------------------------------------------------------------- 035 // Predefined instances 036 //------------------------------------------------------------------------------------------------------------------- 037 038 /** Default memory store, all default values.*/ 039 public static final ConfigClasspathStore DEFAULT = ConfigClasspathStore.create().build(); 040 041 042 //------------------------------------------------------------------------------------------------------------------- 043 // Instance 044 //------------------------------------------------------------------------------------------------------------------- 045 046 /** 047 * Create a new builder for this object. 048 * 049 * @return A new builder for this object. 050 */ 051 public static ConfigClasspathStoreBuilder create() { 052 return new ConfigClasspathStoreBuilder(); 053 } 054 055 @Override /* Context */ 056 public ConfigClasspathStoreBuilder builder() { 057 return new ConfigClasspathStoreBuilder(getPropertyStore()); 058 } 059 060 private final ConcurrentHashMap<String,String> cache = new ConcurrentHashMap<>(); 061 062 /** 063 * Constructor. 064 * 065 * @param ps The settings for this content store. 066 */ 067 protected ConfigClasspathStore(PropertyStore ps) { 068 super(ps); 069 } 070 071 @Override /* ConfigStore */ 072 public synchronized String read(String name) throws IOException { 073 String s = cache.get(name); 074 if (s != null) 075 return s; 076 077 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 078 try (InputStream in = cl.getResourceAsStream(name)) { 079 if (in != null) 080 cache.put(name, IOUtils.read(in, IOUtils.UTF8)); 081 } 082 return emptyIfNull(cache.get(name)); 083 } 084 085 @Override /* ConfigStore */ 086 public synchronized String write(String name, String expectedContents, String newContents) throws IOException { 087 088 // This is a no-op. 089 if (isEquals(expectedContents, newContents)) 090 return null; 091 092 String currentContents = read(name); 093 094 if (expectedContents != null && ! isEquals(currentContents, expectedContents)) 095 return currentContents; 096 097 update(name, newContents); 098 099 return null; 100 } 101 102 @Override /* ConfigStore */ 103 public synchronized boolean exists(String name) { 104 try { 105 return ! read(name).isEmpty(); 106 } catch (IOException e) { 107 return false; 108 } 109 } 110 111 @Override /* ConfigStore */ 112 public synchronized ConfigClasspathStore update(String name, String newContents) { 113 if (newContents == null) 114 cache.remove(name); 115 else 116 cache.put(name, newContents); 117 super.update(name, newContents); 118 return this; 119 } 120 121 /** 122 * No-op. 123 */ 124 @Override /* Closeable */ 125 public void close() throws IOException { 126 // No-op 127 } 128}