<commit_before>//
// This file is part of the OpenNMS(R) Application.
//
// OpenNMS(R) is Copyright (C) 2006 The OpenNMS Group, Inc.  All rights reserved.
// OpenNMS(R) is a derivative work, containing both original code, included code and modified
// code that was published under the GNU General Public License. Copyrights for modified
// and included code are below.
//
// OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
//
// Modifications:
//
// 2007 Apr 08: Switch to use Spring Resources instead of File for the
//              configuration files, since this is what FileReloadContainer
//              now uses. - <EMAIL>
// 2007 Apr 07: Reorganize a bit.  Close Readers in the same method that causes
//              them to be opened as suggested by brozow@. - <EMAIL> 
//
// Original code base Copyright (C) 1999-2001 Oculan Corp.  All rights reserved.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// For more information contact:
//      OpenNMS Licensing       <<EMAIL>>
//      http://www.opennms.org/
//      http://www.opennms.com/
//
package org.opennms.netmgt.dao.castor;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Category;
import org.opennms.core.utils.ThreadCategory;
import org.opennms.netmgt.config.surveillanceViews.SurveillanceViewConfiguration;
import org.opennms.netmgt.config.surveillanceViews.View;
import org.opennms.netmgt.config.surveillanceViews.Views;
import org.opennms.netmgt.dao.SurveillanceViewConfigDao;
import org.opennms.netmgt.dao.support.FileReloadCallback;
import org.opennms.netmgt.dao.support.FileReloadContainer;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;

public class DefaultSurveillanceViewConfigDao implements SurveillanceViewConfigDao, InitializingBean {
    private static final CastorExceptionTranslator CASTOR_EXCEPTION_TRANSLATOR = new CastorExceptionTranslator();

    private Resource m_configResource;
    private FileReloadContainer<SurveillanceViewConfig> m_container;
    private SurveillanceViewCallback m_callback = new SurveillanceViewCallback();
    
    public DefaultSurveillanceViewConfigDao() {
    }
    
    public View getView(String viewName) {
        return m_container.getObject().getViewsMap().get(viewName);
    }
    
    /**
     * Use this method to get the generated Views class generated by the XSD.
     */
    public Views getViews() {
        return m_container.getObject().getConfig().getViews();
    }

    /**
     * Use this method to get a Map of view names to marshalled classes based on the generated View class
     * from the XSD.
     * @return <code>Map</> of View classes.
     */
    public Map<String, View> getViewMap() {
        return m_container.getObject().getViewsMap();
    }

    public View getDefaultView() {
        String defaultView = m_container.getObject().getConfig().getDefaultView();
        return getView(defaultView);
    }

    private Category log() {
        return ThreadCategory.getInstance();
    }
    
    public class SurveillanceViewConfig {
        private SurveillanceViewConfiguration m_config;
        private Map<String, View> m_viewsMap;
        
        public SurveillanceViewConfig(Reader reader) {
            log().debug("loading surveillance views");
            m_config = CastorUtils.unmarshalWithTranslatedExceptions(SurveillanceViewConfiguration.class, reader);
            createViewsMap();
            log().debug("surveillance views loaded");
        }
        
        private void createViewsMap() {
            ArrayList<View> viewList = getViewList();
            m_viewsMap = new HashMap<String, View>(viewList.size());
            for (View view : viewList) {
                m_viewsMap.put(view.getName(), view);
            }
        }

        @SuppressWarnings("unchecked")
        private ArrayList<View> getViewList() {
            return m_config.getViews().getViewCollection();
        }

        public SurveillanceViewConfiguration getConfig() {
            return m_config;
        }

        public Map<String, View> getViewsMap() {
            return m_viewsMap;
        }
    }
    
    private class SurveillanceViewCallback implements FileReloadCallback<SurveillanceViewConfig> {
        public SurveillanceViewConfig reload(SurveillanceViewConfig object, Resource resource) {
            return loadConfig(resource);
        }
    }

    private SurveillanceViewConfig loadConfig(Resource resource) {
        Reader reader;
        try {
            reader = new InputStreamReader(resource.getInputStream());
        } catch (IOException e) {
            throw CASTOR_EXCEPTION_TRANSLATOR.translate("opening XML configuration file for resource '" + resource + "'", e);
        }

        SurveillanceViewConfig config;
        try {
            config = new SurveillanceViewConfig(reader);
        } finally {
            IOUtils.closeQuietly(reader);
        }
        
        return config;
    }

    public void afterPropertiesSet() throws IOException {
        Assert.state(m_configResource != null, "property configResource must be set and be non-null");

        SurveillanceViewConfig config = loadConfig(m_configResource);
        m_container = new FileReloadContainer<SurveillanceViewConfig>(config, m_configResource, m_callback);
    }

    public Resource getConfigResource() {
        return m_configResource;
    }

    public void setConfigResource(Resource configResource) {
        m_configResource = configResource;
    }
}
<commit_msg>Use a block comment for the copyright.<commit_after>/*
 * This file is part of the OpenNMS(R) Application.
 *
 * OpenNMS(R) is Copyright (C) 2006 The OpenNMS Group, Inc.  All rights reserved.
 * OpenNMS(R) is a derivative work, containing both original code, included code and modified
 * code that was published under the GNU General Public License. Copyrights for modified
 * and included code are below.
 *
 * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
 *
 * Modifications:
 *
 * 2007 Apr 08: Switch to use Spring Resources instead of File for the
 *              configuration files, since this is what FileReloadContainer
 *              now uses. - <EMAIL>
 * 2007 Apr 07: Reorganize a bit.  Close Readers in the same method that causes
 *              them to be opened as suggested by brozow@. - <EMAIL> 
 *
 * Original code base Copyright (C) 1999-2001 Oculan Corp.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * For more information contact:
 *      OpenNMS Licensing       <<EMAIL>>
 *      http://www.opennms.org/
 *      http://www.opennms.com/
 */
package org.opennms.netmgt.dao.castor;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Category;
import org.opennms.core.utils.ThreadCategory;
import org.opennms.netmgt.config.surveillanceViews.SurveillanceViewConfiguration;
import org.opennms.netmgt.config.surveillanceViews.View;
import org.opennms.netmgt.config.surveillanceViews.Views;
import org.opennms.netmgt.dao.SurveillanceViewConfigDao;
import org.opennms.netmgt.dao.support.FileReloadCallback;
import org.opennms.netmgt.dao.support.FileReloadContainer;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;

public class DefaultSurveillanceViewConfigDao implements SurveillanceViewConfigDao, InitializingBean {
    private static final CastorExceptionTranslator CASTOR_EXCEPTION_TRANSLATOR = new CastorExceptionTranslator();

    private Resource m_configResource;
    private FileReloadContainer<SurveillanceViewConfig> m_container;
    private SurveillanceViewCallback m_callback = new SurveillanceViewCallback();
    
    public DefaultSurveillanceViewConfigDao() {
    }
    
    public View getView(String viewName) {
        return m_container.getObject().getViewsMap().get(viewName);
    }
    
    /**
     * Use this method to get the generated Views class generated by the XSD.
     */
    public Views getViews() {
        return m_container.getObject().getConfig().getViews();
    }

    /**
     * Use this method to get a Map of view names to marshalled classes based on the generated View class
     * from the XSD.
     * @return <code>Map</> of View classes.
     */
    public Map<String, View> getViewMap() {
        return m_container.getObject().getViewsMap();
    }

    public View getDefaultView() {
        String defaultView = m_container.getObject().getConfig().getDefaultView();
        return getView(defaultView);
    }

    private Category log() {
        return ThreadCategory.getInstance();
    }
    
    public class SurveillanceViewConfig {
        private SurveillanceViewConfiguration m_config;
        private Map<String, View> m_viewsMap;
        
        public SurveillanceViewConfig(Reader reader) {
            log().debug("loading surveillance views");
            m_config = CastorUtils.unmarshalWithTranslatedExceptions(SurveillanceViewConfiguration.class, reader);
            createViewsMap();
            log().debug("surveillance views loaded");
        }
        
        private void createViewsMap() {
            ArrayList<View> viewList = getViewList();
            m_viewsMap = new HashMap<String, View>(viewList.size());
            for (View view : viewList) {
                m_viewsMap.put(view.getName(), view);
            }
        }

        @SuppressWarnings("unchecked")
        private ArrayList<View> getViewList() {
            return m_config.getViews().getViewCollection();
        }

        public SurveillanceViewConfiguration getConfig() {
            return m_config;
        }

        public Map<String, View> getViewsMap() {
            return m_viewsMap;
        }
    }
    
    private class SurveillanceViewCallback implements FileReloadCallback<SurveillanceViewConfig> {
        public SurveillanceViewConfig reload(SurveillanceViewConfig object, Resource resource) {
            return loadConfig(resource);
        }
    }

    private SurveillanceViewConfig loadConfig(Resource resource) {
        Reader reader;
        try {
            reader = new InputStreamReader(resource.getInputStream());
        } catch (IOException e) {
            throw CASTOR_EXCEPTION_TRANSLATOR.translate("opening XML configuration file for resource '" + resource + "'", e);
        }

        SurveillanceViewConfig config;
        try {
            config = new SurveillanceViewConfig(reader);
        } finally {
            IOUtils.closeQuietly(reader);
        }
        
        return config;
    }

    public void afterPropertiesSet() throws IOException {
        Assert.state(m_configResource != null, "property configResource must be set and be non-null");

        SurveillanceViewConfig config = loadConfig(m_configResource);
        m_container = new FileReloadContainer<SurveillanceViewConfig>(config, m_configResource, m_callback);
    }

    public Resource getConfigResource() {
        return m_configResource;
    }

    public void setConfigResource(Resource configResource) {
        m_configResource = configResource;
    }
}

{'fraction_non_alphanumeric': 0.08290478416705625, 'fraction_numerical': 0.006233442418575658, 'mean_word_length': 3.0310929648241207, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4828560', 'n_tokens_mistral': 3495, 'n_tokens_neox': 3381, 'n_words': 1028}