Add a class visitor that checks for specified immediate parent pattern
[smart-util.git] / smart-bean-util / src / main / java / com / smartitengineering / util / bean / PropertiesLocatorConfigurer.java
blob29524b27e78da439d7e5b7ff6d62a4e35dcbb050
1 /*
2 * This is a utility project for wide range of applications
3 *
4 * Copyright (C) 8 Imran M Yousuf (imyousuf@smartitengineering.com)
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 10-1 USA
18 package com.smartitengineering.util.bean;
20 import java.io.IOException;
21 import java.util.Properties;
22 import org.apache.commons.lang.StringUtils;
23 import org.springframework.beans.factory.BeanFactoryAware;
24 import org.springframework.beans.factory.BeanNameAware;
25 import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
26 import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
27 import org.springframework.core.PriorityOrdered;
28 import org.springframework.core.io.ClassPathResource;
29 import org.springframework.core.io.Resource;
31 /**
32 * This class will mainly search for a designated properties file at locations
33 * predefined by system (that is this module) and user through app context. <p />
34 * Preconfigured locations according to ascending priority is -
35 * <ul>
36 * <li>System properties (if enabled)</li>
37 * <li>Classpath for default resource (defaultResourceSuffix appended to the path)</li>
38 * <li>Current working directory</li>
39 * <li>Home directory</li>
40 * <li>User specified directory in order specified</li>
41 * </ul>
43 * @author imyousuf
45 public class PropertiesLocatorConfigurer
46 extends PropertyPlaceholderConfigurer
47 implements BeanFactoryPostProcessor,
48 PriorityOrdered,
49 BeanNameAware,
50 BeanFactoryAware {
52 private boolean ignoreResourceNotFound = false;
53 private final PropertiesLocator locator = new PropertiesLocator();
55 public PropertiesLocatorConfigurer() {
58 /**
59 * Loads properties file from locations as it is supposed.
60 * @param props The properties object that is filled.
61 * @throws java.io.IOException If failed to load the properties or error
62 * reading a resoure
64 @Override
65 protected void loadProperties(Properties props)
66 throws IOException {
67 boolean resourceFound;
68 resourceFound = locator.loadProperties(props);
69 if (!resourceFound && !this.ignoreResourceNotFound) {
70 throw new IOException();
74 /**
75 * This operation is restricted from this configurer.
76 * @param location
78 protected String getDefaultResourceSuffix() {
79 return locator.getDefaultResourceSuffix();
82 /**
83 * Set the suffix for the default resource file
84 * @param defaultResourceSuffix The suffix of he default resource
86 public void setDefaultResourceSuffix(String defaultResourceSuffix) {
87 locator.setDefaultResourceSuffix(defaultResourceSuffix);
90 /**
91 * Retrieves the context of the search. The context will be added before the
92 * for every resource search. It is primarily useful if you multiple config
93 * group for single application.
94 * @return The context for current configurer
96 protected String getResourceContext() {
97 if (locator.getResourceContext() == null) {
98 return "";
100 return locator.getResourceContext();
104 * Sets the context for the resource context for this config group lookup.
105 * @param resourceContext The context to search the current configs.
107 public void setResourceContext(String resourceContext) {
108 locator.setResourceContext(resourceContext);
112 * Load the current resource into the provided properties file. It respects
113 * type of properties and encoding if set.
114 * @param props Properties file to fill
115 * @param resource Resource to load if present
116 * @return The input stream of the resource.
118 @Override
119 public void setLocation(Resource location) {
120 throw new UnsupportedOperationException();
124 * This operation is restricted from this configurer.
125 * @param locations
127 @Override
128 public void setLocations(Resource[] locations) {
129 throw new UnsupportedOperationException();
133 * Set the encoding of the resource file to read in. It basically delegates
134 * through to parents method, but also sets the value in current method to
135 * used for reading the input stream.
136 * @param encoding Encoding of the resource
138 @Override
139 public void setFileEncoding(String encoding) {
140 locator.setFileEncoding(encoding);
141 super.setFileEncoding(encoding);
145 * Set the single custom resource to search at.
146 * @param smartLocation The custom resource
148 public void setSmartLocation(String smartLocation) {
149 locator.setSmartLocations(new String[]{smartLocation});
150 super.setLocation(new ClassPathResource(smartLocation));
154 * The custom resources as CSV. Its main intended use would be to supply
155 * custom resources through another properties file to keep the resources
156 * dynamic.
157 * @param smartLocationsAsCsv The resources as comma separated values (csv)
159 public void setSmartLocationsAsCsv(String smartLocationsAsCsv) {
160 setSmartLocations(smartLocationsAsCsv.split(","));
164 * The custom resources as array, its main intended use case would be from
165 * an application context XML file.
166 * @param smartLocations The resources as an array
168 public void setSmartLocations(String[] smartLocations) {
169 locator.setSmartLocations(smartLocations);
170 Resource[] resources = new Resource[smartLocations.length];
171 for (int i = 0; i < smartLocations.length; ++i) {
172 String smartLocation = StringUtils.trim(smartLocations[i]);
173 if (StringUtils.isNotEmpty(smartLocation)) {
174 resources[i] = new ClassPathResource(smartLocation);
177 super.setLocations(resources);
181 * Retrieves whether search in classpath is enabled or not.
182 * @return True if search is enabled in classpath
184 protected boolean isClasspathSearchEnabled() {
185 return locator.isClasspathSearchEnabled();
189 * Sets whether search in classpath is enabled or not
190 * @param classpathSearchEnabled True if search is enabled for classpath
192 public void setClasspathSearchEnabled(boolean classpathSearchEnabled) {
193 locator.setClasspathSearchEnabled(classpathSearchEnabled);
197 * Retrieves whether search in current directory is enabled or not.
198 * @return True if search is enabled in current directory
200 protected boolean isCurrentDirSearchEnabled() {
201 return locator.isCurrentDirSearchEnabled();
205 * Sets whether search in current directory is enabled or not
206 * @param currentDirSearchEnabled True if search is enabled for current dir
208 public void setCurrentDirSearchEnabled(boolean currentDirSearchEnabled) {
209 locator.setCurrentDirSearchEnabled(currentDirSearchEnabled);
213 * Retrieves whether search in classpath for default is enabled or not.
214 * @return True if search is enabled for enabled
216 protected boolean isDefaultSearchEnabled() {
217 return locator.isDefaultSearchEnabled();
221 * Sets whether search in default classpath is enabled or not
222 * @param defaultSearchEnabled True if search is enabled for default cp
224 public void setDefaultSearchEnabled(boolean defaultSearchEnabled) {
225 locator.setDefaultSearchEnabled(defaultSearchEnabled);
229 * Retrieves whether search in user home directory is enabled or not.
230 * @return True if search is enabled in user home directory
232 protected boolean isUserHomeSearchEnabled() {
233 return locator.isUserHomeSearchEnabled();
237 * Sets whether search in user home directory is enabled or not
238 * @param userHomeSearchEnabled True if search is enabled for user home dir
240 public void setUserHomeSearchEnabled(boolean userHomeSearchEnabled) {
241 locator.setUserHomeSearchEnabled(userHomeSearchEnabled);
245 * Get configured custom search locations
246 * @return Custom search locations
248 protected String[] getSearchLocations() {
249 return locator.getSmartLocations();
253 * The custom search location for the current configurer.
254 * @param searchLocation The custom search location
256 public void setSearchLocation(String searchLocation) {
257 if (StringUtils.isNotEmpty(searchLocation)) {
258 setSearchLocations(new String[]{searchLocation});
263 * The custom search locations as comma separated values (csv). It will
264 * primarily split the search locations by ',' and its intended use case is
265 * to inject the search locations via another properties configurer.
266 * @param searchLocationAsCsv The search locations as CSV
268 public void setSearchLocationsAsCsv(String searchLocationAsCsv) {
269 if (StringUtils.isNotEmpty(searchLocationAsCsv)) {
270 setSearchLocations(searchLocationAsCsv.split(","));
275 * The custom search locations intended to be mainly used via application
276 * context XML.
277 * @param searchLocations The search locations
279 public void setSearchLocations(String[] searchLocations) {
280 locator.setSearchLocations(searchLocations);
284 * Set whether to ignore if resource is not found
285 * @param ignoreResourceNotFound Flag to note whether to ignore missing rsrc
287 @Override
288 public void setIgnoreResourceNotFound(boolean ignoreResourceNotFound) {
289 this.ignoreResourceNotFound = ignoreResourceNotFound;
290 super.setIgnoreResourceNotFound(ignoreResourceNotFound);