com.talis.inject.guice.PropertiesConfiguredModuleFactory.java Source code

Java tutorial

Introduction

Here is the source code for com.talis.inject.guice.PropertiesConfiguredModuleFactory.java

Source

/*
 * Copyright 2010 Talis Information Ltd
 * 
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 * 
 *        http://www.apache.org/licenses/LICENSE-2.0
 * 
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.talis.inject.guice;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.google.inject.Module;
import com.google.inject.spi.Message;

public class PropertiesConfiguredModuleFactory implements ModuleFactory {

    private static final Log LOG = LogFactory.getLog(PropertiesConfiguredModuleFactory.class);

    public static final String INITIALISATION_ERROR_MESSAGE = "Unable to initialise Module list for Injector factory";

    public static final String PROPERTIES_FILENAME = "injector-module.properties";
    public static final String PROPERTIES_LOCATION_PROP = "com.talis.inject.guice.injector.modules.properties";
    public static final String MODULE_PROPERTY = "com.talis.inject.guice.module";

    @Override
    public Module[] getModules() {
        List<Module> modules = new ArrayList<Module>();
        String filename = System.getProperty(PROPERTIES_LOCATION_PROP, PROPERTIES_FILENAME);
        try {
            PropertiesConfiguration config = new PropertiesConfiguration(filename);

            List<String> classes = config.getList(MODULE_PROPERTY);
            LOG.debug(String.format("Configuration properties list %s module classes", classes.size()));

            for (String classname : classes) {
                LOG.debug(String.format("Found module specified, instantiating instance: %s", classname));
                Module module = (Module) Class.forName(classname).newInstance();
                modules.add(module);
            }

        } catch (ConfigurationException e) {
            LOG.error(String.format("Error reading properties file from %s. " + "Current classpath is %s", filename,
                    System.getProperty("java.class.path")));
            throw new com.google.inject.ConfigurationException(
                    getExceptionMessages("Unable to read configuration for ModuleFactory", e));

        } catch (Exception e) {
            LOG.error(String.format("A module specified in %s could not be instantiated", filename));
            throw new com.google.inject.ConfigurationException(
                    getExceptionMessages("Unable to initialise ModuleFactory from configuration", e));

        }
        return modules.toArray(new Module[modules.size()]);
    }

    private Iterable<Message> getExceptionMessages(String messageString, Throwable cause) {
        List<Object> sources = new ArrayList<Object>();
        sources.add(this);
        List<Message> messages = new ArrayList<Message>();
        messages.add(new Message(sources, messageString, cause));
        return messages;
    }

}