| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.dozer.config; |
| 17 | |
|
| 18 | |
import org.dozer.util.DozerClassLoader; |
| 19 | |
import org.dozer.util.DozerConstants; |
| 20 | |
import org.dozer.util.MappingUtils; |
| 21 | |
import org.slf4j.Logger; |
| 22 | |
import org.slf4j.LoggerFactory; |
| 23 | |
|
| 24 | |
import java.io.IOException; |
| 25 | |
import java.io.InputStream; |
| 26 | |
import java.net.URL; |
| 27 | |
import java.util.Properties; |
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
public class GlobalSettings { |
| 43 | |
|
| 44 | 4 | private final Logger log = LoggerFactory.getLogger(GlobalSettings.class); |
| 45 | |
|
| 46 | 1 | private static final GlobalSettings instance = new GlobalSettings(); |
| 47 | |
|
| 48 | |
private String loadedByFileName; |
| 49 | 4 | private boolean statisticsEnabled = DozerConstants.DEFAULT_STATISTICS_ENABLED; |
| 50 | 4 | private int converterByDestTypeCacheMaxSize = DozerConstants.DEFAULT_CONVERTER_BY_DEST_TYPE_CACHE_MAX_SIZE; |
| 51 | 4 | private int superTypesCacheMaxSize = DozerConstants.DEFAULT_SUPER_TYPE_CHECK_CACHE_MAX_SIZE; |
| 52 | 4 | private boolean autoregisterJMXBeans = DozerConstants.DEFAULT_AUTOREGISTER_JMX_BEANS; |
| 53 | 4 | private boolean elEnabled = DozerConstants.DEFAULT_EL_ENABLED; |
| 54 | |
|
| 55 | 4 | private String classLoaderBeanName = DozerConstants.DEFAULT_CLASS_LOADER_BEAN; |
| 56 | 4 | private String proxyResolverBeanName = DozerConstants.DEFAULT_PROXY_RESOLVER_BEAN; |
| 57 | |
|
| 58 | |
public static GlobalSettings getInstance() { |
| 59 | 1171 | return instance; |
| 60 | |
} |
| 61 | |
|
| 62 | |
static GlobalSettings createNew() { |
| 63 | 3 | return new GlobalSettings(); |
| 64 | |
} |
| 65 | |
|
| 66 | 4 | private GlobalSettings() { |
| 67 | 4 | loadGlobalSettings(); |
| 68 | 4 | } |
| 69 | |
|
| 70 | |
protected String getLoadedByFileName() { |
| 71 | 5 | return loadedByFileName; |
| 72 | |
} |
| 73 | |
|
| 74 | |
public boolean isAutoregisterJMXBeans() { |
| 75 | 7 | return autoregisterJMXBeans; |
| 76 | |
} |
| 77 | |
|
| 78 | |
public int getConverterByDestTypeCacheMaxSize() { |
| 79 | 1136 | return converterByDestTypeCacheMaxSize; |
| 80 | |
} |
| 81 | |
|
| 82 | |
public boolean isStatisticsEnabled() { |
| 83 | 18 | return statisticsEnabled; |
| 84 | |
} |
| 85 | |
|
| 86 | |
public void setStatisticsEnabled(boolean statisticsEnabled) { |
| 87 | 14 | this.statisticsEnabled = statisticsEnabled; |
| 88 | 14 | } |
| 89 | |
|
| 90 | |
public int getSuperTypesCacheMaxSize() { |
| 91 | 1136 | return superTypesCacheMaxSize; |
| 92 | |
} |
| 93 | |
|
| 94 | |
public String getClassLoaderName() { |
| 95 | 7 | return classLoaderBeanName; |
| 96 | |
} |
| 97 | |
|
| 98 | |
public String getProxyResolverName() { |
| 99 | 7 | return proxyResolverBeanName; |
| 100 | |
} |
| 101 | |
|
| 102 | |
public boolean isElEnabled() { |
| 103 | 9 | return elEnabled; |
| 104 | |
} |
| 105 | |
|
| 106 | |
private synchronized void loadGlobalSettings() { |
| 107 | |
|
| 108 | 4 | String propFileName = System.getProperty(DozerConstants.CONFIG_FILE_SYS_PROP); |
| 109 | 4 | if (MappingUtils.isBlankOrNull(propFileName)) { |
| 110 | 2 | propFileName = DozerConstants.DEFAULT_CONFIG_FILE; |
| 111 | |
} |
| 112 | |
|
| 113 | 4 | log.info("Trying to find Dozer configuration file: {}", propFileName); |
| 114 | |
|
| 115 | 4 | DozerClassLoader classLoader = BeanContainer.getInstance().getClassLoader(); |
| 116 | 4 | URL url = classLoader.loadResource(propFileName); |
| 117 | 4 | if (url == null) { |
| 118 | 1 | log.warn("Dozer configuration file not found: {}. Using defaults for all Dozer global properties.", propFileName); |
| 119 | 1 | return; |
| 120 | |
} else { |
| 121 | 3 | log.info("Using URL [{}] for Dozer global property configuration", url); |
| 122 | |
} |
| 123 | |
|
| 124 | 3 | Properties props = new Properties(); |
| 125 | 3 | InputStream inputStream = null; |
| 126 | |
try { |
| 127 | 3 | log.info("Reading Dozer properties from URL [{}]", url); |
| 128 | 3 | inputStream = url.openStream(); |
| 129 | 3 | props.load(inputStream); |
| 130 | 0 | } catch (IOException e) { |
| 131 | 0 | MappingUtils.throwMappingException("Problem loading Dozer properties from URL [" + propFileName + "]", e); |
| 132 | |
} finally { |
| 133 | 3 | if (inputStream != null) { |
| 134 | |
try { |
| 135 | 3 | inputStream.close(); |
| 136 | 0 | } catch (IOException e) { |
| 137 | 3 | } |
| 138 | |
} |
| 139 | |
} |
| 140 | |
|
| 141 | 3 | populateSettings(props); |
| 142 | |
|
| 143 | 3 | loadedByFileName = propFileName; |
| 144 | 3 | log.debug("Finished configuring Dozer global properties"); |
| 145 | 3 | } |
| 146 | |
|
| 147 | |
private void populateSettings(Properties props) { |
| 148 | 3 | String propValue = props.getProperty(PropertyConstants.STATISTICS_ENABLED); |
| 149 | 3 | if (propValue != null) { |
| 150 | 3 | statisticsEnabled = Boolean.valueOf(propValue); |
| 151 | |
} |
| 152 | 3 | propValue = props.getProperty(PropertyConstants.CONVERTER_CACHE_MAX_SIZE); |
| 153 | 3 | if (propValue != null) { |
| 154 | 3 | converterByDestTypeCacheMaxSize = Integer.parseInt(propValue); |
| 155 | |
} |
| 156 | 3 | propValue = props.getProperty(PropertyConstants.SUPERTYPE_CACHE_MAX_SIZE); |
| 157 | 3 | if (propValue != null) { |
| 158 | 3 | superTypesCacheMaxSize = Integer.parseInt(propValue); |
| 159 | |
} |
| 160 | 3 | propValue = props.getProperty(PropertyConstants.AUTOREGISTER_JMX_BEANS); |
| 161 | 3 | if (propValue != null) { |
| 162 | 3 | autoregisterJMXBeans = Boolean.valueOf(propValue); |
| 163 | |
} |
| 164 | 3 | propValue = props.getProperty(PropertyConstants.CLASS_LOADER_BEAN); |
| 165 | 3 | if (propValue != null) { |
| 166 | 1 | classLoaderBeanName = propValue; |
| 167 | |
} |
| 168 | 3 | propValue = props.getProperty(PropertyConstants.PROXY_RESOLVER_BEAN); |
| 169 | 3 | if (propValue != null) { |
| 170 | 1 | proxyResolverBeanName = propValue; |
| 171 | |
} |
| 172 | 3 | propValue = props.getProperty(PropertyConstants.EL_ENABLED); |
| 173 | 3 | if (propValue != null) { |
| 174 | 3 | elEnabled = Boolean.valueOf(propValue); |
| 175 | |
} |
| 176 | 3 | } |
| 177 | |
|
| 178 | |
} |