001
014
015 package com.liferay.portal.settings;
016
017 import com.liferay.portal.kernel.resource.ResourceRetriever;
018 import com.liferay.portal.kernel.resource.manager.ResourceManager;
019 import com.liferay.portal.kernel.settings.BaseSettings;
020 import com.liferay.portal.kernel.settings.Settings;
021 import com.liferay.portal.kernel.util.StringPool;
022 import com.liferay.portal.kernel.util.StringUtil;
023
024 import java.io.IOException;
025
026 import java.util.Properties;
027
028
032 public class PropertiesSettings extends BaseSettings {
033
034 public PropertiesSettings(
035 Properties properties, ResourceManager resourceManager) {
036
037 this(properties, resourceManager, null);
038 }
039
040 public PropertiesSettings(
041 Properties properties, ResourceManager resourceManager,
042 Settings parentSettings) {
043
044 super(parentSettings);
045
046 _properties = properties;
047 _resourceManager = resourceManager;
048 }
049
050 @Override
051 protected String doGetValue(String key) {
052 return readProperty(key);
053 }
054
055 @Override
056 protected String[] doGetValues(String key) {
057 return StringUtil.split(doGetValue(key));
058 }
059
060 protected String getProperty(String key) {
061 return readProperty(key);
062 }
063
064 protected String readProperty(String key) {
065 String value = _properties.getProperty(key);
066
067 if (!isLocationVariable("resource", value)) {
068 return value;
069 }
070
071 ResourceRetriever resourceRetriever =
072 _resourceManager.getResourceRetriever(
073 getLocation("resource", value));
074
075 try {
076 return StringUtil.read(resourceRetriever.getInputStream());
077 }
078 catch (IOException ioe) {
079 throw new RuntimeException("Unable to read " + value, ioe);
080 }
081 }
082
083 private String getLocation(String protocol, String value) {
084 return value.substring(protocol.length() + 3, value.length() - 1);
085 }
086
087 private boolean isLocationVariable(String protocol, String value) {
088 if (value == null) {
089 return false;
090 }
091
092 String prefix =
093 StringPool.DOLLAR + StringPool.OPEN_CURLY_BRACE + protocol +
094 StringPool.COLON;
095
096 if (value.startsWith(prefix) &&
097 value.endsWith(StringPool.CLOSE_CURLY_BRACE)) {
098
099 return true;
100 }
101
102 return false;
103 }
104
105 private final Properties _properties;
106 private final ResourceManager _resourceManager;
107
108 }