<commit_before>package org.openmrs.module.appframework.factory;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.openmrs.module.appframework.domain.AppDescriptor;
import org.openmrs.module.appframework.domain.AppTemplate;
import org.openmrs.module.appframework.domain.Extension;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Component
public class AppConfigurationLoaderFactory implements AppFrameworkFactory {

    protected Log logger = LogFactory.getLog(getClass());

    private ObjectMapper objectMapper = new ObjectMapper();

    private PathMatchingResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();

    public AppConfigurationLoaderFactory() {
    	// Tell the parser to all // and /* style comments.
    	objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
    }
    
    @Override
    public List<AppTemplate> getAppTemplates() throws IOException {
        Resource[] appDefinitionJsonResources = resourceResolver.getResources("classpath*:/apps/*AppTemplates.json");
        List<AppTemplate> templates = new ArrayList<AppTemplate>();
        for (Resource appDefinitionResource : appDefinitionJsonResources) {
            List<AppTemplate> forResource;
            try {
                forResource = objectMapper.readValue(appDefinitionResource.getInputStream(), new TypeReference<List<AppTemplate>>() {});
                templates.addAll(forResource);
            } catch (IOException e) {
                logger.fatal("Error reading AppTemplates configuration file", e);
            }
        }
        return templates;
    }

    @Override
    public List<AppDescriptor> getAppDescriptors() throws IOException {
        Resource[] appDefinitionJsonResources = resourceResolver.getResources("classpath*:/apps/*app.json");
        List<AppDescriptor> appDescriptors = new ArrayList<AppDescriptor>();
        for (Resource appDefinitionResource : appDefinitionJsonResources) {
            try {
                List<AppDescriptor> appDescriptorsForResource = getAppDescriptorsForResource(appDefinitionResource.getInputStream());
                appDescriptors.addAll(appDescriptorsForResource);
            } catch (IOException e) {
                logger.fatal("Error reading app configuration file: " + appDefinitionResource.getFile().getPath(), e);
            }
        }
        return appDescriptors;
    }

    public List<AppDescriptor> getAppDescriptorsForResource(InputStream inputStream) throws IOException {
        return objectMapper.readValue(inputStream, new TypeReference<List<AppDescriptor>>() {});
    }

    @Override
    public List<Extension> getExtensions() throws IOException {
        Resource[] extensionDefinitionJsonResources = resourceResolver.getResources("classpath*:/apps/*extension.json");
        List<Extension> extensions = new ArrayList<Extension>();
        for (Resource extensionResource : extensionDefinitionJsonResources) {
            List<Extension> extensionsForResource;
            try {
                extensionsForResource = objectMapper.readValue(extensionResource.getInputStream(), new TypeReference<List<Extension>>() {});
                extensions.addAll(extensionsForResource);
            } catch (IOException e) {
                logger.fatal("Error reading extension configuration file: " + extensionResource.getFilename(), e);
            }
        }
        return extensions;
    }
}<commit_msg>AF-62: Fix FileNotFoundException when loading file from classpath (#48)<commit_after>package org.openmrs.module.appframework.factory;

import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.openmrs.module.appframework.domain.AppDescriptor;
import org.openmrs.module.appframework.domain.AppTemplate;
import org.openmrs.module.appframework.domain.Extension;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Component
public class AppConfigurationLoaderFactory implements AppFrameworkFactory {

    protected Logger logger = LoggerFactory.getLogger(getClass());

    private ObjectMapper objectMapper = new ObjectMapper();

    private PathMatchingResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();

    public AppConfigurationLoaderFactory() {
    	// Tell the parser to all // and /* style comments.
    	objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
    }
    
    @Override
    public List<AppTemplate> getAppTemplates() throws IOException {
        Resource[] appDefinitionJsonResources = resourceResolver.getResources("classpath*:/apps/*AppTemplates.json");
        List<AppTemplate> templates = new ArrayList<AppTemplate>();
        for (Resource appDefinitionResource : appDefinitionJsonResources) {
            List<AppTemplate> forResource;
            try {
                forResource = objectMapper.readValue(appDefinitionResource.getInputStream(), new TypeReference<List<AppTemplate>>() {});
                templates.addAll(forResource);
            } catch (IOException e) {
                logger.error("Error reading AppTemplates configuration file: {}", appDefinitionResource.getDescription(), e);
            }
        }
        return templates;
    }

    @Override
    public List<AppDescriptor> getAppDescriptors() throws IOException {
        Resource[] appDefinitionJsonResources = resourceResolver.getResources("classpath*:/apps/*app.json");
        List<AppDescriptor> appDescriptors = new ArrayList<AppDescriptor>();
        for (Resource appDefinitionResource : appDefinitionJsonResources) {
            try {
                List<AppDescriptor> appDescriptorsForResource = getAppDescriptorsForResource(appDefinitionResource.getInputStream());
                appDescriptors.addAll(appDescriptorsForResource);
            } catch (IOException e) {
                logger.error("Error reading app configuration file: {}", appDefinitionResource.getDescription(), e);
            }
        }
        return appDescriptors;
    }

    public List<AppDescriptor> getAppDescriptorsForResource(InputStream inputStream) throws IOException {
        return objectMapper.readValue(inputStream, new TypeReference<List<AppDescriptor>>() {});
    }

    @Override
    public List<Extension> getExtensions() throws IOException {
        Resource[] extensionDefinitionJsonResources = resourceResolver.getResources("classpath*:/apps/*extension.json");
        List<Extension> extensions = new ArrayList<Extension>();
        for (Resource extensionResource : extensionDefinitionJsonResources) {
            List<Extension> extensionsForResource;
            try {
                extensionsForResource = objectMapper.readValue(extensionResource.getInputStream(), new TypeReference<List<Extension>>() {});
                extensions.addAll(extensionsForResource);
            } catch (IOException e) {
                logger.error("Error reading extension configuration file: {}", extensionResource.getDescription(), e);
            }
        }
        return extensions;
    }
}
{'fraction_non_alphanumeric': 0.09207984546039923, 'fraction_numerical': 0.00077269800386349, 'mean_word_length': 3.7009685230024214, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '381290', 'n_tokens_mistral': 1848, 'n_tokens_neox': 1820, 'n_words': 371}