ninja-core/src/main/java/ninja/standalone/StandaloneHelper.java
static Class<? extends Standalone> resolveStandaloneClass(String standaloneClassNameSystemProperty,
                                                              ForwardingServiceLoader<Standalone> standaloneServiceLoader,
                                                              String standaloneClassNameDefaultValue) {
        Class<? extends Standalone> resolvedStandaloneClass = null;
        
        // 1. System property for 'ninja.standalone.class'
        if (standaloneClassNameSystemProperty != null) {
            try {
                resolvedStandaloneClass
                    = (Class<Standalone>)Class.forName(standaloneClassNameSystemProperty);
            } catch (Exception e) {
                throw new RuntimeException("Unable to find standalone class '" + standaloneClassNameSystemProperty + "' (class does not exist)");
            }
        }
        
        // 2. Implementation on classpath that's registered as service?
        if (resolvedStandaloneClass == null) {
            try {
                Iterator<Standalone> standaloneIterator = standaloneServiceLoader.iterator();
                // first one wins
                if (standaloneIterator.hasNext()) {
                    resolvedStandaloneClass = standaloneIterator.next().getClass();
                }
                // more than one triggers warning
                if (standaloneIterator.hasNext()) {
                    log.warn("More than one implementation of {} on classpath! Using {} which was the first", Standalone.class, resolvedStandaloneClass);
                }
            } finally {
                // always kill cache (since ServiceLoader actually instantiates an instance)
                standaloneServiceLoader.reload();
            }
        }
        
        // 3. Fallback to ninja default
        if (resolvedStandaloneClass == null) {
            try {
                resolvedStandaloneClass
                    = (Class<Standalone>)Class.forName(standaloneClassNameDefaultValue);
            } catch (Exception e) {
                throw new RuntimeException("Unable to find standalone class '" + standaloneClassNameDefaultValue + "' (class does not exist)");
            }
        }
        
        return resolvedStandaloneClass;
    }
static Class<? extends Standalone> resolveStandaloneClass(String standaloneClassNameSystemProperty,
                                                              ForwardingServiceLoader<Standalone> standaloneServiceLoader,
                                                              String standaloneClassNameDefaultValue) {
        Class<? extends Standalone> resolvedStandaloneClass = null;
        
        // 1. System property for 'ninja.standalone.class'
        if (standaloneClassNameSystemProperty != null) {
            try {
                resolvedStandaloneClass
                    = (Class<Standalone>)Class.forName(standaloneClassNameSystemProperty);
            } catch (Exception e) {
                throw new RuntimeException("Unable to find standalone class '" + standaloneClassNameSystemProperty + "' (class does not exist)");
            }
        }
        
        // 2. Implementation on classpath that's registered as service?
        if (resolvedStandaloneClass == null) {
            try {
                Iterator<Standalone> standaloneIterator = standaloneServiceLoader.iterator();
                // first one wins
                if (standaloneIterator.hasNext()) {
                    resolvedStandaloneClass = standaloneIterator.next().getClass();
                }
                // more than one triggers warning
                if (standaloneIterator.hasNext()) {
                    log.warn("More than one implementation of {} on classpath! Using {} which was the first", Standalone.class, resolvedStandaloneClass);
                }
            } finally {
                // always kill cache (since ServiceLoader actually instantiates an instance)
                standaloneServiceLoader.reload();
            }
        }
        
        // 3. Fallback to ninja default
        if (resolvedStandaloneClass == null) {
            try {
                resolvedStandaloneClass
                    = (Class<Standalone>)Class.forName(standaloneClassNameDefaultValue);
            } catch (Exception e) {
                throw new RuntimeException("Unable to find standalone class '" + standaloneClassNameDefaultValue + "' (class does not exist)");
            }
        }
        
        return resolvedStandaloneClass;
    }
static Class < ? extends Standalone > resolveStandaloneClass ( String standaloneClassNameSystemProperty , ForwardingServiceLoader < Standalone > standaloneServiceLoader , String standaloneClassNameDefaultValue ) { Class < ? extends Standalone > resolvedStandaloneClass = null ; // 1. System property for 'ninja.standalone.class' if ( standaloneClassNameSystemProperty != null ) { try { resolvedStandaloneClass = ( Class < Standalone > ) Class . forName ( standaloneClassNameSystemProperty ) ; } catch ( Exception e ) { throw new RuntimeException ( "Unable to find standalone class '" + standaloneClassNameSystemProperty + "' (class does not exist)" ) ; } } // 2. Implementation on classpath that's registered as service? if ( resolvedStandaloneClass == null ) { try { Iterator < Standalone > standaloneIterator = standaloneServiceLoader . iterator ( ) ; // first one wins if ( standaloneIterator . hasNext ( ) ) { resolvedStandaloneClass = standaloneIterator . next ( ) . getClass ( ) ; } // more than one triggers warning if ( standaloneIterator . hasNext ( ) ) { log . warn ( "More than one implementation of {} on classpath! Using {} which was the first" , Standalone . class , resolvedStandaloneClass ) ; } } finally { // always kill cache (since ServiceLoader actually instantiates an instance) standaloneServiceLoader . reload ( ) ; } } // 3. Fallback to ninja default if ( resolvedStandaloneClass == null ) { try { resolvedStandaloneClass = ( Class < Standalone > ) Class . forName ( standaloneClassNameDefaultValue ) ; } catch ( Exception e ) { throw new RuntimeException ( "Unable to find standalone class '" + standaloneClassNameDefaultValue + "' (class does not exist)" ) ; } } return resolvedStandaloneClass ; }
used for testing (so we can inject mocks of system static methods)
used for testing ( so we can inject mocks of system static methods )