一、前言
关于directmemory-common-0.2.jar源码包中org.apache.directmemory.conf.Configuration配置设计实现,如下图所示
二、源码说明
1.Configuration配置
package org.apache.directmemory.conf;@b@@b@import org.slf4j.Logger;@b@import org.slf4j.LoggerFactory;@b@@b@public final class Configuration@b@{@b@ private static final Logger logger = LoggerFactory.getLogger(Configuration.class);@b@ private static ConfigurationService configurationService;@b@@b@ public static int getNumberOfBuffers()@b@ {@b@ return configurationService.getNumberOfBuffers();@b@ }@b@@b@ public static int getInitialCapacity()@b@ {@b@ return configurationService.getInitialCapacity();@b@ }@b@@b@ public static int getRamMegaBytes()@b@ {@b@ return configurationService.getRamMegaBytes();@b@ }@b@@b@ public static long getDisposalTime()@b@ {@b@ return configurationService.getDisposalTime();@b@ }@b@@b@ public static int getConcurrencyLevel()@b@ {@b@ return configurationService.getConcurrencyLevel();@b@ }@b@@b@ public static void configureFromYaml()@b@ {@b@ logger.info("using yaml configuration implementation");@b@ configurationService = YamlConfiguration.load();@b@ }@b@@b@ static@b@ {@b@ if (configurationService == null)@b@ {@b@ logger.info("using default configuration implementation");@b@ configurationService = new SimplePropertiesConfiguration();@b@ }@b@ }@b@}
2.ConfigurationService接口
package org.apache.directmemory.conf;@b@@b@public abstract interface ConfigurationService@b@{@b@ public abstract int getNumberOfBuffers();@b@@b@ public abstract int getInitialCapacity();@b@@b@ public abstract int getRamMegaBytes();@b@@b@ public abstract int getConcurrencyLevel();@b@@b@ public abstract long getDisposalTime();@b@@b@ public abstract void setNumberOfBuffers(int paramInt);@b@@b@ public abstract void setInitialCapacity(int paramInt);@b@@b@ public abstract void setRamMegaBytes(int paramInt);@b@@b@ public abstract void setConcurrencyLevel(int paramInt);@b@@b@ public abstract void setDisposalTime(long paramLong);@b@}
3.DefaultConfiguration默认配置实现类
package org.apache.directmemory.conf;@b@@b@public abstract class DefaultConfiguration@b@ implements ConfigurationService@b@{@b@ private int numberOfBuffers;@b@ private int initialCapacity;@b@ private int ramMegaBytes;@b@ private int concurrencyLevel;@b@ private long disposalTime;@b@@b@ public DefaultConfiguration()@b@ {@b@ this.numberOfBuffers = 1;@b@@b@ this.initialCapacity = 100000;@b@@b@ this.ramMegaBytes = 1;@b@@b@ this.concurrencyLevel = 4;@b@@b@ this.disposalTime = 10L;@b@ }@b@@b@ public int getNumberOfBuffers()@b@ {@b@ return this.numberOfBuffers;@b@ }@b@@b@ public int getInitialCapacity()@b@ {@b@ return this.initialCapacity;@b@ }@b@@b@ public int getRamMegaBytes()@b@ {@b@ return this.ramMegaBytes;@b@ }@b@@b@ public int getConcurrencyLevel()@b@ {@b@ return this.concurrencyLevel;@b@ }@b@@b@ public long getDisposalTime()@b@ {@b@ return this.disposalTime;@b@ }@b@@b@ public void setNumberOfBuffers(int numberOfBuffers)@b@ {@b@ this.numberOfBuffers = numberOfBuffers;@b@ }@b@@b@ public void setInitialCapacity(int initialCapacity)@b@ {@b@ this.initialCapacity = initialCapacity;@b@ }@b@@b@ public void setRamMegaBytes(int ramMegaBytes)@b@ {@b@ this.ramMegaBytes = ramMegaBytes;@b@ }@b@@b@ public void setConcurrencyLevel(int concurrencyLevel)@b@ {@b@ this.concurrencyLevel = concurrencyLevel;@b@ }@b@@b@ public void setDisposalTime(long disposalTime)@b@ {@b@ this.disposalTime = disposalTime;@b@ }@b@}
4.差异化配置SimplePropertiesConfiguration、YamlConfiguration子实现类
package org.apache.directmemory.conf;@b@@b@import org.yaml.snakeyaml.Yaml;@b@import org.yaml.snakeyaml.constructor.Constructor;@b@@b@public class YamlConfiguration extends DefaultConfiguration@b@{@b@ public static ConfigurationService load()@b@ {@b@ Yaml yaml = new Yaml(new Constructor(YamlConfiguration.class));@b@ return ((YamlConfiguration)yaml.load(Configuration.class.getClassLoader().getResourceAsStream("directmemory.yaml")));@b@ }@b@}
package org.apache.directmemory.conf;@b@@b@import java.util.Properties;@b@import org.slf4j.Logger;@b@import org.slf4j.LoggerFactory;@b@@b@public class SimplePropertiesConfiguration extends DefaultConfiguration@b@ implements ConfigurationService@b@{@b@ private static final Logger logger = LoggerFactory.getLogger(SimplePropertiesConfiguration.class);@b@@b@ public SimplePropertiesConfiguration()@b@ {@b@ Properties properties = new Properties();@b@ try@b@ {@b@ properties.load(getClass().getClassLoader().getResourceAsStream("directmemory.properties"));@b@ setNumberOfBuffers(new Integer(properties.getProperty("numberOfBuffers")).intValue());@b@ setInitialCapacity(new Integer(properties.getProperty("initialCapacity")).intValue());@b@ setRamMegaBytes(new Integer(properties.getProperty("ramMegaBytes")).intValue());@b@ setConcurrencyLevel(new Integer(properties.getProperty("concurrencyLevel")).intValue());@b@ setDisposalTime(new Long(properties.getProperty("disposalTime")).longValue());@b@ }@b@ catch (Exception e)@b@ {@b@ logger.warn("no properties file found or invalid file - using defaults");@b@ }@b@ }@b@}