001/**
002 * Copyright 2010-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.common.util.config.service;
017
018import java.io.UnsupportedEncodingException;
019import java.util.HashMap;
020import java.util.Map;
021import java.util.Properties;
022
023import org.kuali.common.util.PropertyUtils;
024import org.kuali.common.util.project.KualiUtilProjectConstants;
025import org.kuali.common.util.project.ProjectService;
026import org.kuali.common.util.project.ProjectUtils;
027import org.kuali.common.util.project.model.Project;
028import org.kuali.common.util.project.model.ProjectIdentifier;
029import org.kuali.common.util.xml.service.XmlService;
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032import org.springframework.context.support.GenericXmlApplicationContext;
033import org.springframework.core.io.ByteArrayResource;
034import org.springframework.core.io.Resource;
035
036/**
037 * @deprecated
038 */
039@Deprecated
040public class SpringConfigService extends AbstractCachingConfigService {
041
042        public SpringConfigService(ProjectService projectService, XmlService xmlService) {
043                super(projectService, xmlService);
044        }
045
046        private static final Logger logger = LoggerFactory.getLogger(SpringConfigService.class);
047
048        private static final Map<String, org.kuali.common.util.config.ProjectConfigContainer> CACHE = new HashMap<String, org.kuali.common.util.config.ProjectConfigContainer>();
049        private static final String FILE = "metadata-spring.xml";
050        private static final String PROPS = "spring.properties";
051        private static final String BEAN = "projectConfig";
052
053        @Override
054        protected synchronized org.kuali.common.util.config.ProjectConfigContainer getCachedConfig(String groupId, String artifactId) {
055                String cacheKey = groupId + ":" + artifactId;
056                org.kuali.common.util.config.ProjectConfigContainer config = CACHE.get(cacheKey);
057                if (config == null) {
058                        config = loadMetadata(groupId, artifactId);
059                        logger.debug("Caching [{}]", cacheKey);
060                        CACHE.put(cacheKey, config);
061                }
062                return config;
063        }
064
065        @Override
066        protected synchronized void clearCache() {
067                CACHE.clear();
068        }
069
070        @Override
071        protected String getFilename() {
072                return FILE;
073        }
074
075        @Override
076        protected Properties getBaseFilterProperties() {
077                ProjectIdentifier identifier = KualiUtilProjectConstants.PROJECT_ID;
078                Project project = getProjectService().getProject(identifier);
079                String location = getMetadataConfigFilePath(project, PROPS);
080                String encoding = ProjectUtils.getEncoding(project);
081                return PropertyUtils.load(location, encoding);
082        }
083
084        @Override
085        protected org.kuali.common.util.config.ProjectConfigContainer getProjectConfig(String content, String encoding) {
086                GenericXmlApplicationContext context = null;
087                try {
088                        Resource resource = new ByteArrayResource(content.getBytes(encoding));
089                        context = new GenericXmlApplicationContext();
090                        context.load(resource);
091                        return (org.kuali.common.util.config.ProjectConfigContainer) context.getBean(BEAN);
092                } catch (UnsupportedEncodingException e) {
093                        throw new IllegalStateException(e);
094                } finally {
095                        if (context != null) {
096                                context.close();
097                        }
098                }
099        }
100
101}