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.util.config.service;
17  
18  import java.io.UnsupportedEncodingException;
19  import java.util.HashMap;
20  import java.util.Map;
21  import java.util.Properties;
22  
23  import org.kuali.common.util.Project;
24  import org.kuali.common.util.ProjectUtils;
25  import org.kuali.common.util.PropertyUtils;
26  import org.kuali.common.util.config.ProjectConfigContainer;
27  import org.kuali.common.util.project.KualiConstants;
28  import org.kuali.common.util.project.UtilConstants;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  import org.springframework.context.support.GenericXmlApplicationContext;
32  import org.springframework.core.io.ByteArrayResource;
33  import org.springframework.core.io.Resource;
34  
35  public class SpringConfigService extends AbstractCachingConfigService {
36  
37  	private static final Logger logger = LoggerFactory.getLogger(SpringConfigService.class);
38  
39  	private static final Map<String, ProjectConfigContainer> PROJECT_CONFIG_CACHE = new HashMap<String, ProjectConfigContainer>();
40  	private static final String FILE = "metadata-spring.xml";
41  	private static final String PROPS = "spring.properties";
42  	private static final String BEAN = "projectConfig";
43  
44  	@Override
45  	protected synchronized ProjectConfigContainer getCachedConfig(String groupId, String artifactId) {
46  		String cacheKey = groupId + ":" + artifactId;
47  		ProjectConfigContainer config = PROJECT_CONFIG_CACHE.get(cacheKey);
48  		if (config == null) {
49  			config = loadMetadata(groupId, artifactId);
50  			logger.debug("Caching [{}]", cacheKey);
51  			PROJECT_CONFIG_CACHE.put(cacheKey, config);
52  		}
53  		return config;
54  	}
55  
56  	@Override
57  	protected synchronized void clearCache() {
58  		PROJECT_CONFIG_CACHE.clear();
59  	}
60  
61  	@Override
62  	protected String getFilename() {
63  		return FILE;
64  	}
65  
66  	@Override
67  	protected Properties getBaseFilterProperties() {
68  		String groupId = KualiConstants.COMMON_GROUP_ID;
69  		String artifactId = UtilConstants.ARTIFACT_ID;
70  		Project project = ProjectUtils.loadProject(groupId, artifactId);
71  		return PropertyUtils.load(getMetadataConfigFilePath(project, PROPS));
72  	}
73  
74  	@Override
75  	protected ProjectConfigContainer getProjectConfig(String content, String encoding) {
76  		GenericXmlApplicationContext context = null;
77  		try {
78  			Resource resource = new ByteArrayResource(content.getBytes(encoding));
79  			context = new GenericXmlApplicationContext();
80  			context.load(resource);
81  			return (ProjectConfigContainer) context.getBean(BEAN);
82  		} catch (UnsupportedEncodingException e) {
83  			throw new IllegalStateException(e);
84  		} finally {
85  			if (context != null) {
86  				context.close();
87  			}
88  		}
89  	}
90  
91  }