<commit_before>package org.opennms.netmgt.provision.persist;

import java.io.File;
import java.io.FileWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Set;
import java.util.TreeSet;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

import org.opennms.netmgt.provision.persist.foreignsource.ForeignSource;
import org.opennms.netmgt.provision.persist.requisition.Requisition;


public class FilesystemForeignSourceRepository extends AbstractForeignSourceRepository {
    private String m_requisitionPath;
    private String m_foreignSourcePath;
    private final JAXBContext m_jaxbContext;
    private Marshaller m_marshaller;
    private Unmarshaller m_unMarshaller;

    public FilesystemForeignSourceRepository() throws ForeignSourceRepositoryException {
        try {
            m_jaxbContext = JAXBContext.newInstance(ForeignSource.class, Requisition.class);
            m_marshaller = m_jaxbContext.createMarshaller();
            m_marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            m_unMarshaller = m_jaxbContext.createUnmarshaller();
        } catch (JAXBException e) {
            throw new ForeignSourceRepositoryException("unable to create JAXB context", e);
        } catch (Exception e) {
            throw new ForeignSourceRepositoryException("unable to get schema", e);
        }
    }
    
    public int getForeignSourceCount() throws ForeignSourceRepositoryException {
        return getForeignSources().size();
    }
 
    public Set<ForeignSource> getForeignSources() throws ForeignSourceRepositoryException {
        File directory = new File(m_foreignSourcePath);
        TreeSet<ForeignSource> foreignSources = new TreeSet<ForeignSource>();
        if (directory.exists()) {
            for (File file : directory.listFiles()) {
                if (file.getName().endsWith(".xml")) {
                    foreignSources.add(get(file));
                }
            }
        }
        return foreignSources;
    }

    public ForeignSource getForeignSource(String foreignSourceName) throws ForeignSourceRepositoryException {
        if (foreignSourceName == null) {
            throw new ForeignSourceRepositoryException("can't get a foreign source with a null name!");
        }
        File inputFile = encodeFileName(m_foreignSourcePath, foreignSourceName);
        if (inputFile != null && inputFile.exists()) {
            return get(inputFile);
        } else {
            ForeignSource fs = getDefaultForeignSource();
            fs.setName(foreignSourceName);
            return fs;
        }
    }

    public void save(ForeignSource foreignSource) throws ForeignSourceRepositoryException {
        if (foreignSource == null) {
            throw new ForeignSourceRepositoryException("can't save a null foreign source!");
        }
        File outputFile = getOutputFileForForeignSource(foreignSource);
        try {
            foreignSource.updateDateStamp();
            FileWriter fw = new FileWriter(outputFile);
            m_marshaller.marshal(foreignSource, fw);
            fw.close();
        } catch (Exception e) {
            throw new ForeignSourceRepositoryException("unable to write requisition to " + outputFile.getPath(), e);
        }
    }

    public void delete(ForeignSource foreignSource) throws ForeignSourceRepositoryException {
        File deleteFile = getOutputFileForForeignSource(foreignSource);
        if (deleteFile.exists()) {
            if (!deleteFile.delete()) {
                throw new ForeignSourceRepositoryException("unable to delete foreign source file " + deleteFile);
            }
        }
    }
    
    public Set<Requisition> getRequisitions() throws ForeignSourceRepositoryException {
        File directory = new File(m_requisitionPath);
        TreeSet<Requisition> requisitions = new TreeSet<Requisition>();
        if (directory.exists()) {
            for (File file : directory.listFiles()) {
                if (file.getName().endsWith(".xml")) {
                    requisitions.add(getRequisition(file));
                }
            }
        }
        return requisitions;
    }
    
    public Requisition getRequisition(String foreignSourceName) throws ForeignSourceRepositoryException {
        if (foreignSourceName == null) {
            throw new ForeignSourceRepositoryException("can't get a requisition with a null foreign source name!");
        }
        File inputFile = encodeFileName(m_requisitionPath, foreignSourceName);
        if (inputFile != null && inputFile.exists()) {
            return getRequisition(inputFile);
        }
        return null;
    }

    public Requisition getRequisition(ForeignSource foreignSource) throws ForeignSourceRepositoryException {
        if (foreignSource == null) {
            throw new ForeignSourceRepositoryException("can't get a requisition with a null foreign source name!");
        }
        return getRequisition(foreignSource.getName());
    }

    public void save(Requisition requisition) throws ForeignSourceRepositoryException {
        if (requisition == null) {
            throw new ForeignSourceRepositoryException("can't save a null requisition!");
        }
        File outputFile = getOutputFileForRequisition(requisition);
        try {
            requisition.updateDateStamp();
            FileWriter fw = new FileWriter(outputFile);
            m_marshaller.marshal(requisition, fw);
            fw.close();
        } catch (Exception e) {
            throw new ForeignSourceRepositoryException("unable to write requisition to " + outputFile.getPath(), e);
        }
    }

    public void delete(Requisition requisition) throws ForeignSourceRepositoryException {
        File deleteFile = getOutputFileForRequisition(requisition);
        if (deleteFile.exists()) {
            if (!deleteFile.delete()) {
                throw new ForeignSourceRepositoryException("unable to delete requisition file " + deleteFile);
            }
        }
    }
    
    public void setRequisitionPath(String path) {
        m_requisitionPath = path;
    }
    public void setForeignSourcePath(String path) {
        m_foreignSourcePath = path;
    }
    
    private ForeignSource get(File file) throws ForeignSourceRepositoryException {
        try {
            return m_unMarshaller.unmarshal(new StreamSource(file), ForeignSource.class).getValue();<commit_msg>don't leak writers<commit_after>package org.opennms.netmgt.provision.persist;

import java.io.File;
import java.io.FileWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Set;
import java.util.TreeSet;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

import org.apache.commons.io.IOUtils;
import org.opennms.netmgt.provision.persist.foreignsource.ForeignSource;
import org.opennms.netmgt.provision.persist.requisition.Requisition;


public class FilesystemForeignSourceRepository extends AbstractForeignSourceRepository {
    private String m_requisitionPath;
    private String m_foreignSourcePath;
    private final JAXBContext m_jaxbContext;
    private Marshaller m_marshaller;
    private Unmarshaller m_unMarshaller;

    public FilesystemForeignSourceRepository() throws ForeignSourceRepositoryException {
        try {
            m_jaxbContext = JAXBContext.newInstance(ForeignSource.class, Requisition.class);
            m_marshaller = m_jaxbContext.createMarshaller();
            m_marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            m_unMarshaller = m_jaxbContext.createUnmarshaller();
        } catch (JAXBException e) {
            throw new ForeignSourceRepositoryException("unable to create JAXB context", e);
        } catch (Exception e) {
            throw new ForeignSourceRepositoryException("unable to get schema", e);
        }
    }
    
    public int getForeignSourceCount() throws ForeignSourceRepositoryException {
        return getForeignSources().size();
    }
 
    public Set<ForeignSource> getForeignSources() throws ForeignSourceRepositoryException {
        File directory = new File(m_foreignSourcePath);
        TreeSet<ForeignSource> foreignSources = new TreeSet<ForeignSource>();
        if (directory.exists()) {
            for (File file : directory.listFiles()) {
                if (file.getName().endsWith(".xml")) {
                    foreignSources.add(get(file));
                }
            }
        }
        return foreignSources;
    }

    public ForeignSource getForeignSource(String foreignSourceName) throws ForeignSourceRepositoryException {
        if (foreignSourceName == null) {
            throw new ForeignSourceRepositoryException("can't get a foreign source with a null name!");
        }
        File inputFile = encodeFileName(m_foreignSourcePath, foreignSourceName);
        if (inputFile != null && inputFile.exists()) {
            return get(inputFile);
        } else {
            ForeignSource fs = getDefaultForeignSource();
            fs.setName(foreignSourceName);
            return fs;
        }
    }

    public void save(ForeignSource foreignSource) throws ForeignSourceRepositoryException {
        if (foreignSource == null) {
            throw new ForeignSourceRepositoryException("can't save a null foreign source!");
        }
        File outputFile = getOutputFileForForeignSource(foreignSource);
        FileWriter writer = null;
        try {
            foreignSource.updateDateStamp();
            writer = new FileWriter(outputFile);
            m_marshaller.marshal(foreignSource, writer);
        } catch (Exception e) {
            throw new ForeignSourceRepositoryException("unable to write requisition to " + outputFile.getPath(), e);
        } finally {
            IOUtils.closeQuietly(writer);
        }
    }

    public void delete(ForeignSource foreignSource) throws ForeignSourceRepositoryException {
        File deleteFile = getOutputFileForForeignSource(foreignSource);
        if (deleteFile.exists()) {
            if (!deleteFile.delete()) {
                throw new ForeignSourceRepositoryException("unable to delete foreign source file " + deleteFile);
            }
        }
    }
    
    public Set<Requisition> getRequisitions() throws ForeignSourceRepositoryException {
        File directory = new File(m_requisitionPath);
        TreeSet<Requisition> requisitions = new TreeSet<Requisition>();
        if (directory.exists()) {
            for (File file : directory.listFiles()) {
                if (file.getName().endsWith(".xml")) {
                    requisitions.add(getRequisition(file));
                }
            }
        }
        return requisitions;
    }
    
    public Requisition getRequisition(String foreignSourceName) throws ForeignSourceRepositoryException {
        if (foreignSourceName == null) {
            throw new ForeignSourceRepositoryException("can't get a requisition with a null foreign source name!");
        }
        File inputFile = encodeFileName(m_requisitionPath, foreignSourceName);
        if (inputFile != null && inputFile.exists()) {
            return getRequisition(inputFile);
        }
        return null;
    }

    public Requisition getRequisition(ForeignSource foreignSource) throws ForeignSourceRepositoryException {
        if (foreignSource == null) {
            throw new ForeignSourceRepositoryException("can't get a requisition with a null foreign source name!");
        }
        return getRequisition(foreignSource.getName());
    }

    public void save(Requisition requisition) throws ForeignSourceRepositoryException {
        if (requisition == null) {
            throw new ForeignSourceRepositoryException("can't save a null requisition!");
        }
        File outputFile = getOutputFileForRequisition(requisition);
        FileWriter writer = null;
        try {
            requisition.updateDateStamp();
            writer = new FileWriter(outputFile);
            m_marshaller.marshal(requisition, writer);
        } catch (Exception e) {
            throw new ForeignSourceRepositoryException("unable to write requisition to " + outputFile.getPath(), e);
        } finally {
            IOUtils.closeQuietly(writer);
        }
    }

    public void delete(Requisition requisition) throws ForeignSourceRepositoryException {
        File deleteFile = getOutputFileForRequisition(requisition);
        if (deleteFile.exists()) {
            if (!deleteFile.delete()) {
                throw new ForeignSourceRepositoryException("unable to delete requisition file " + deleteFile);
            }
        }
    }
    
    public void setRequisitionPath(String path) {
        m_requisitionPath = path;
    }
    public void setForeignSourcePath(String path) {
        m_foreignSourcePath = path;
    }
    
    private ForeignSource get(File file) throws ForeignSourceRepositoryException {
        try {
            return m_unMarshaller.unmarshal(new StreamSource(file), ForeignSource.class).getValue();
{'fraction_non_alphanumeric': 0.08151558608196845, 'fraction_numerical': 0.0, 'mean_word_length': 2.8195445373306427, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, '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': '6278793', 'n_tokens_mistral': 3374, 'n_tokens_neox': 3258, 'n_words': 767}