Search in sources :

Example 1 with OrderedPathMatchingResourcePatternResolver

use of org.jaffa.util.OrderedPathMatchingResourcePatternResolver in project jaffa-framework by jaffa-projects.

the class ApplicationResourceLoader method getApplicationResourcesOverride.

/**
 * This method gets the properties object for override application resources.
 *
 * @return ApplicationResourceOverride
 */
public Properties getApplicationResourcesOverride(String localeKey) {
    if (log.isDebugEnabled()) {
        log.debug("ApplicationResourceLoader::loadOverrideResources");
    }
    OrderedPathMatchingResourcePatternResolver resolver = OrderedPathMatchingResourcePatternResolver.getInstance();
    Properties properties = new Properties();
    String dataDirectory = (String) ContextManagerFactory.instance().getProperty(DATA_DIRECTORY);
    String applicationResourcesOverrideLocation = null;
    try {
        if (dataDirectory != null && dataDirectory.length() > 0) {
            applicationResourcesOverrideLocation = dataDirectory + File.separator + PROP_APPLICATION_RESOURCES_OVERRIDE + (localeKey != null ? "_" + localeKey : "") + ".properties";
            Config.setProperty(Config.PROP_APPLICATION_RESOURCES_OVERRIDE_LOCATION, applicationResourcesOverrideLocation);
        }
        if (applicationResourcesOverrideLocation != null && !"".equals(applicationResourcesOverrideLocation)) {
            // added file prefix for ant style search pattern to search the file from I/O File
            Resource resource = resolver.getResource(FILE_PREFIX + applicationResourcesOverrideLocation);
            if (resource != null) {
                if (!resource.exists()) {
                    Path resourcePath = Paths.get(resource.getURI());
                    Files.createDirectories(resourcePath.getParent());
                    Files.createFile(resourcePath);
                }
                properties.load(resource.getInputStream());
            }
        }
    } catch (IOException e) {
        log.error(ERROR_READING_APP_RESOURCES_OVERRIDE + applicationResourcesOverrideLocation, e);
        throw new RuntimeException(ERROR_READING_APP_RESOURCES_OVERRIDE + applicationResourcesOverrideLocation, e);
    }
    return properties;
}
Also used : Path(java.nio.file.Path) OrderedPathMatchingResourcePatternResolver(org.jaffa.util.OrderedPathMatchingResourcePatternResolver) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) Properties(java.util.Properties)

Example 2 with OrderedPathMatchingResourcePatternResolver

use of org.jaffa.util.OrderedPathMatchingResourcePatternResolver in project jaffa-framework by jaffa-projects.

the class ProcessEventHandler method createProcessEvent.

/**
 * Returns a IProcessEvent instance, initialized with the values from the input graph.
 * @param processEventGraph
 * @return a IProcessEvent instance, initialized with the values from the input graph.
 * @throws ApplicationExceptions
 * @throws FrameworkException
 */
public static IProcessEvent createProcessEvent(ProcessEventGraph processEventGraph) throws ApplicationExceptions, FrameworkException {
    // Do nothing if the 'cancelEvent' flag is true
    if (processEventGraph.getCancelEvent() != null && processEventGraph.getCancelEvent())
        return null;
    if (processEventGraph.getEventName() == null)
        throw new ApplicationExceptions(new ApplicationException(EXCEPTION_CANNOT_FIND_PROPERTY, new String[] { "null" }));
    try {
        Properties props = new Properties();
        InputStream input = null;
        try {
            OrderedPathMatchingResourcePatternResolver resolver = OrderedPathMatchingResourcePatternResolver.getInstance();
            Resource[] resources = resolver.getResources("classpath*:" + SOA_EVENT_LOCATION);
            if (resources != null && resources.length > 0) {
                for (Resource resource : resources) {
                    input = resource.getInputStream();
                    if (input != null) {
                        props.load(input);
                        if (log.isDebugEnabled())
                            log.debug("Loaded " + props.size() + " rule(s) from " + SOA_EVENT_LOCATION);
                    }
                }
            } else {
                throw new ApplicationExceptions(new ApplicationException(EXCEPTION_NO_EVENT_LOCATION_FILE, new String[] { SOA_EVENT_LOCATION }));
            }
        } catch (IOException e) {
            throw new ApplicationExceptions(new ApplicationException(EXCEPTION_NO_EVENT_LOCATION_FILE, new String[] { SOA_EVENT_LOCATION }));
        } finally {
            if (input != null)
                input.close();
        }
        String className = props.getProperty(processEventGraph.getEventName());
        if (className != null) {
            // get the event class
            IProcessEvent processEvent = createProcessEvent(className);
            // reflect params
            if (processEventGraph.getParams() != null) {
                Param[] params = processEventGraph.getParams();
                for (Param param : params) {
                    if (log.isDebugEnabled())
                        log.debug("Processing " + param.getName() + " = " + param.getValue());
                    BeanHelper.setField(processEvent, param.getName(), param.getValue());
                }
            }
            return processEvent;
        } else {
            throw new ApplicationExceptions(new ApplicationException(EXCEPTION_CANNOT_FIND_PROPERTY, new String[] { processEventGraph.getEventName() }));
        }
    } catch (Exception e) {
        throw ExceptionHelper.throwAFR(e);
    }
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) InputStream(java.io.InputStream) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) Properties(java.util.Properties) FrameworkException(org.jaffa.exceptions.FrameworkException) IOException(java.io.IOException) ApplicationException(org.jaffa.exceptions.ApplicationException) ApplicationException(org.jaffa.exceptions.ApplicationException) OrderedPathMatchingResourcePatternResolver(org.jaffa.util.OrderedPathMatchingResourcePatternResolver)

Aggregations

IOException (java.io.IOException)2 Properties (java.util.Properties)2 OrderedPathMatchingResourcePatternResolver (org.jaffa.util.OrderedPathMatchingResourcePatternResolver)2 Resource (org.springframework.core.io.Resource)2 InputStream (java.io.InputStream)1 Path (java.nio.file.Path)1 ApplicationException (org.jaffa.exceptions.ApplicationException)1 ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)1 FrameworkException (org.jaffa.exceptions.FrameworkException)1