protected Path configureApplication()

in src/java/org/apache/turbine/Turbine.java [381:483]


    protected Path configureApplication(ServletConfig config, ServletContext context)
            throws IOException, ConfigurationException
    {
        ConfigurationStyle confStyle = ConfigurationStyle.UNSET;
        // first test
        String confFile = findInitParameter(context, config,
                TurbineConfig.CONFIGURATION_PATH_KEY,
                null);
        if (StringUtils.isNotEmpty(confFile))
        {
            confStyle = ConfigurationStyle.XML;
        }
        else // second test
        {
            confFile = findInitParameter(context, config,
                    TurbineConfig.PROPERTIES_PATH_KEY,
                    null);
            if (StringUtils.isNotEmpty(confFile))
            {
                confStyle = ConfigurationStyle.PROPERTIES;
            }
        }
        // more tests ..
        // last test
        if (confStyle == ConfigurationStyle.UNSET)
        { // last resort
            confFile = findInitParameter(context, config,
                    TurbineConfig.PROPERTIES_PATH_KEY,
                    TurbineConfig.PROPERTIES_PATH_DEFAULT);
            confStyle = ConfigurationStyle.PROPERTIES;
        }

        // First report
        log.debug("Loading configuration ({}) from {}", confStyle, confFile);

        // now begin loading
        Parameters params = new Parameters();
        File confPath = getApplicationRootAsFile();

        if (confFile.startsWith("/"))
        {
            confFile = confFile.substring(1); // cft. RFC2396 should not start
                                              // with a slash, if not absolute
                                              // path
        }

        Path confFileRelativePath = Paths.get(confFile);// relative to later
                                                        // join
        Path targetPath = Paths.get(confPath.toURI());
        targetPath = targetPath.resolve(confFileRelativePath);

        // Get the target path directory
        Path targetPathDirectory = targetPath.getParent();
        if (targetPathDirectory != null)
        {
            // set the configuration path
            confPath = targetPathDirectory.normalize().toFile();

            Path targetFilePath = targetPath.getFileName();
            if (targetFilePath != null)
            {
                // set the configuration file name
                confFile = targetFilePath.toString();
            }

        }

        switch (confStyle)
        {
            case XML:
                // relative base path used for this and child configuration
                // files
                CombinedConfigurationBuilder combinedBuilder = new CombinedConfigurationBuilder()
                        .configure(params.fileBased()
                                .setFileName(confFile)
                                .setListDelimiterHandler(new DefaultListDelimiterHandler(','))
                                .setLocationStrategy(new HomeDirectoryLocationStrategy(confPath.getCanonicalPath(), false)));
                configuration = combinedBuilder.getConfiguration();
                break;

            case PROPERTIES:
                FileBasedConfigurationBuilder<PropertiesConfiguration> propertiesBuilder = new FileBasedConfigurationBuilder<>(
                        PropertiesConfiguration.class)
                                .configure(params.properties()
                                        .setFileName(confFile)
                                        .setListDelimiterHandler(new DefaultListDelimiterHandler(','))
                                        .setLocationStrategy(new HomeDirectoryLocationStrategy(confPath.getCanonicalPath(), false)));
                // meta configuration: this may contain any commons configuration: system<>, jndi, env
                configuration = propertiesBuilder.getConfiguration();
                break;
            case JSON:
            case YAML:
                throw new NotImplementedException("JSON or XAML configuration style not yet implemented!");

            default:
                break;
        }
        // Now report our successful configuration to the world
        log.info("Loaded configuration ({}) from {} style: {}",
                confStyle, confFile, configuration.toString());

        return targetPath;
    }