View Javadoc

1   /**
2    * Copyright 2010-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.jdbc.supplier;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.Map;
21  
22  import org.apache.commons.io.FilenameUtils;
23  import org.apache.commons.lang3.StringUtils;
24  import org.kuali.common.util.Assert;
25  import org.kuali.common.util.CollectionUtils;
26  import org.kuali.common.util.LocationUtils;
27  import org.kuali.common.util.ReflectionUtils;
28  import org.kuali.common.util.spring.SpringUtils;
29  import org.springframework.beans.BeanUtils;
30  import org.springframework.beans.factory.FactoryBean;
31  import org.springframework.core.env.Environment;
32  
33  public class LocationSuppliersFactoryBean implements FactoryBean<List<LocationSupplier>> {
34  
35  	public static final String DEFAULT_LIST_SUFFIX = ".list";
36  
37  	String listSuffix = DEFAULT_LIST_SUFFIX;
38  	Environment env;
39  	String property;
40  	Map<String, LocationSupplierSourceBean> extensionMappings;
41  
42  	@Override
43  	public List<LocationSupplier> getObject() {
44  
45  		// Make sure we are configured correctly
46  		Assert.notNull(env, "environment is null");
47  		Assert.notNull(property, "property is null");
48  		Assert.notNull(extensionMappings, "extensionMappings is null");
49  
50  		// Get a list of locations using properties, prefix, and listSuffix
51  		List<String> locations = getLocations(env, property, listSuffix);
52  
53  		// Convert the locations into LocationSupplier's based on extension
54  		return getSuppliers(locations, extensionMappings);
55  	}
56  
57  	protected List<LocationSupplier> getSuppliers(List<String> locations, Map<String, LocationSupplierSourceBean> mappings) {
58  		// Allocate some storage for our suppliers
59  		List<LocationSupplier> suppliers = new ArrayList<LocationSupplier>();
60  
61  		// Cycle through the list of locations, creating one supplier per location
62  		for (String location : locations) {
63  
64  			// Extract the extension from the location
65  			String extension = FilenameUtils.getExtension(location);
66  
67  			// The map holds the concrete LocationSupplier implementation to use for each extension
68  			LocationSupplierSourceBean sourceBean = mappings.get(extension);
69  
70  			// Unknown extension type
71  			if (sourceBean == null) {
72  				throw new IllegalArgumentException("Unknown extension [" + extension + "]");
73  			}
74  
75  			// Request a new supplier from the builder
76  			LocationSupplier supplier = sourceBean.getSupplierInstance();
77  
78  			LocationSupplier newInstance = ReflectionUtils.newInstance(supplier.getClass());
79  			BeanUtils.copyProperties(supplier, newInstance);
80  			newInstance.setLocation(location);
81  
82  			// Add it to the list
83  			suppliers.add(newInstance);
84  		}
85  
86  		// Return the fully configured list of suppliers
87  		return suppliers;
88  	}
89  
90  	protected List<String> getLocations(Environment env, String property, String listSuffix) {
91  
92  		// Extract the list of property keys (comma delimited)
93  		String csv = SpringUtils.getProperty(env, property, "");
94  
95  		// If no keys were provided, we are done
96  		if (StringUtils.isBlank(csv)) {
97  			return new ArrayList<String>();
98  		}
99  
100 		// Parse the property keys into a list
101 		List<String> keys = CollectionUtils.getTrimmedListFromCSV(csv);
102 
103 		// Allocate some storage for the locations we find
104 		List<String> locations = new ArrayList<String>();
105 		for (String key : keys) {
106 
107 			// This is a either a list of locations or a location itself
108 			String value = SpringUtils.getProperty(env, key);
109 
110 			if (StringUtils.endsWithIgnoreCase(key, listSuffix)) {
111 				// If the key ends with .list, it's a list of locations
112 				locations.addAll(LocationUtils.getLocations(value));
113 			} else {
114 				// Otherwise it is a location itself
115 				locations.add(value);
116 			}
117 		}
118 
119 		// Return the list of locations
120 		return locations;
121 	}
122 
123 	@Override
124 	public Class<?> getObjectType() {
125 		return List.class;
126 	}
127 
128 	@Override
129 	public boolean isSingleton() {
130 		return false;
131 	}
132 
133 	public String getListSuffix() {
134 		return listSuffix;
135 	}
136 
137 	public void setListSuffix(String listSuffix) {
138 		this.listSuffix = listSuffix;
139 	}
140 
141 	public Map<String, LocationSupplierSourceBean> getExtensionMappings() {
142 		return extensionMappings;
143 	}
144 
145 	public void setExtensionMappings(Map<String, LocationSupplierSourceBean> extensionMappings) {
146 		this.extensionMappings = extensionMappings;
147 	}
148 
149 	public String getProperty() {
150 		return property;
151 	}
152 
153 	public void setProperty(String property) {
154 		this.property = property;
155 	}
156 
157 	public Environment getEnv() {
158 		return env;
159 	}
160 
161 	public void setEnv(Environment env) {
162 		this.env = env;
163 	}
164 
165 }