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.ByteArrayInputStream;
019import java.io.IOException;
020import java.io.InputStream;
021import java.util.HashMap;
022import java.util.Map;
023
024import org.apache.commons.io.IOUtils;
025import org.kuali.common.util.project.ProjectService;
026import org.kuali.common.util.xml.service.XmlService;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030/**
031 * @deprecated
032 */
033@Deprecated
034public class DefaultConfigService extends AbstractCachingConfigService {
035
036        public DefaultConfigService(ProjectService projectService, XmlService xmlService) {
037                super(projectService, xmlService);
038        }
039
040        private static final Logger logger = LoggerFactory.getLogger(DefaultConfigService.class);
041
042        private static final Map<String, org.kuali.common.util.config.ProjectConfigContainer> CACHE = new HashMap<String, org.kuali.common.util.config.ProjectConfigContainer>();
043        private static final String FILE = "metadata.xml";
044
045        @Override
046        protected synchronized org.kuali.common.util.config.ProjectConfigContainer getCachedConfig(String groupId, String artifactId) {
047                String cacheKey = groupId + ":" + artifactId;
048                org.kuali.common.util.config.ProjectConfigContainer config = CACHE.get(cacheKey);
049                if (config == null) {
050                        config = loadMetadata(groupId, artifactId);
051                        logger.debug("Caching [{}]", cacheKey);
052                        CACHE.put(cacheKey, config);
053                }
054                return config;
055        }
056
057        @Override
058        protected synchronized void clearCache() {
059                CACHE.clear();
060        }
061
062        @Override
063        protected String getFilename() {
064                return FILE;
065        }
066
067        @Override
068        protected org.kuali.common.util.config.ProjectConfigContainer getProjectConfig(String content, String encoding) {
069                InputStream in = null;
070                try {
071                        in = new ByteArrayInputStream(content.getBytes(encoding));
072                        return getXmlService().getObject(in, org.kuali.common.util.config.ProjectConfigContainer.class);
073                } catch (IOException e) {
074                        throw new IllegalStateException("Unexpected IO error", e);
075                } finally {
076                        IOUtils.closeQuietly(in);
077                }
078        }
079
080}