static

in app/src/main/java/org/apache/roller/weblogger/config/WebloggerConfig.java [51:155]


    static {
        config = new Properties();

        try {
            // we'll need this to get at our properties files in the classpath
            Class<?> configClass = Class.forName("org.apache.roller.weblogger.config.WebloggerConfig");

            // first, lets load our default properties
            try (InputStream is = configClass.getResourceAsStream(default_config)) {
                config.load(is);
            }
            
            // first, see if we can find our junit testing config
            try (InputStream test = configClass.getResourceAsStream(junit_config)) {
                
                if (test != null) {

                    config.load(test);
                    System.out.println("Roller Weblogger: Successfully loaded junit properties file from classpath");
                    System.out.println("File path : " + configClass.getResource(junit_config).getFile());

                } else {

                    // now, see if we can find our custom config
                    try(InputStream custom = configClass.getResourceAsStream(custom_config)) {

                        if (custom != null) {
                            config.load(custom);
                            System.out.println("Roller Weblogger: Successfully loaded custom properties file from classpath");
                            System.out.println("File path : " + configClass.getResource(custom_config).getFile());
                        } else {
                            System.out.println("Roller Weblogger: No custom properties file found in classpath");
                        }

                        System.out.println("(To run eclipse junit local tests see docs/testing/roller-junit.properties)");
                    }
                }
            }

            // finally, check for an external config file
            String env_file = System.getProperty(custom_jvm_param);
            if(env_file != null && env_file.length() > 0) {
                File custom_config_file = new File(env_file);

                // make sure the file exists, then try and load it
                if(custom_config_file.exists()) {
                    try(InputStream is = new FileInputStream(custom_config_file)) {
                        config.load(is);
                    }
                    System.out.println("Roller Weblogger: Successfully loaded custom properties from "+
                            custom_config_file.getAbsolutePath());
                } else {
                    System.out.println("Roller Weblogger: Failed to load custom properties from "+
                            custom_config_file.getAbsolutePath());
                }

            } 

            // Now expand system properties for properties in the config.expandedProperties list,
            // replacing them by their expanded values.
            String expandedPropertiesDef = config.getProperty("config.expandedProperties");
            if (expandedPropertiesDef != null) {
                String[] expandedProperties = expandedPropertiesDef.split(",");
                for (int i = 0; i < expandedProperties.length; i++) {
                    String propName = expandedProperties[i].trim();
                    String initialValue = config.getProperty(propName);
                    if (initialValue != null) {
                        String expandedValue = PropertyExpander.expandSystemProperties(initialValue);
                        config.setProperty(propName, expandedValue);
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        // tell log4j2 to use the optionally specified config file instead of Roller's,
        // but only if it hasn't already been set with -D at JVM startup.
        String log4j2ConfigKey = "log4j.configurationFile";
        String customLog4j2File = config.getProperty(log4j2ConfigKey);
        if(customLog4j2File != null && !customLog4j2File.isBlank() && System.getProperty(log4j2ConfigKey) == null) {
            System.setProperty(log4j2ConfigKey, customLog4j2File);
        }
        
        // this bridges java.util.logging -> SLF4J which ends up being log4j2, probably.
        org.slf4j.bridge.SLF4JBridgeHandler.removeHandlersForRootLogger();
        org.slf4j.bridge.SLF4JBridgeHandler.install();
        
        // finally we can start logging...
        log = LogFactory.getLog(WebloggerConfig.class);

        // some debugging for those that want it
        if(log.isDebugEnabled()) {
            log.debug("WebloggerConfig looks like this ...");

            String key;
            Enumeration<Object> keys = config.keys();
            while(keys.hasMoreElements()) {
                key = (String) keys.nextElement();
                log.debug(key+"="+config.getProperty(key));
            }
        }

    }