public static void configureOptions ( Map < String , String > options , CreateContainerCmd cmd ) throws TargetException { Logger logger = Logger . getLogger ( DockerUtils . class . getName ( ) ) ; // Basically, we had two choices: // 1. Map our properties to the Java REST API. // 2. By-pass it and send our custom JSon object. // // The second option is much more complicated. // So, we use Java reflection and some hacks to match Docker properties // with the setter methods available in the API. The API remains in charge // of generating the right JSon objects. Map < String , List < String > > hackedSetterNames = new HashMap <> ( ) ; // List known types List < Class < ? > > types = new ArrayList <> ( ) ; types . add ( String . class ) ; types . add ( String [ ] . class ) ; types . add ( long . class ) ; types . add ( Long . class ) ; types . add ( int . class ) ; types . add ( Integer . class ) ; types . add ( boolean . class ) ; types . add ( Boolean . class ) ; types . add ( Capability [ ] . class ) ; // Deal with the options for ( Map . Entry < String , String > entry : options . entrySet ( ) ) { String optionValue = entry . getValue ( ) ; // Now, guess what option to set String methodName = entry . getKey ( ) . replace ( "-" , " " ) . trim ( ) ; methodName = WordUtils . capitalize ( methodName ) ; methodName = methodName . replace ( " " , "" ) ; methodName = "with" + methodName ; Method _m = null ; for ( Method m : cmd . getClass ( ) . getMethods ( ) ) { boolean sameMethod = methodName . equalsIgnoreCase ( m . getName ( ) ) ; boolean methodWithAlias = hackedSetterNames . containsKey ( methodName ) && hackedSetterNames . get ( methodName ) . contains ( m . getName ( ) ) ; if ( sameMethod || methodWithAlias ) { // Only one parameter? if ( m . getParameterTypes ( ) . length != 1 ) { logger . warning ( "A method was found for " + entry . getKey ( ) + " but it does not have the right number of parameters." ) ; continue ; } // The right type? if ( ! types . contains ( m . getParameterTypes ( ) [ 0 ] ) ) { // Since Docker-java 3.x, there are two methods to set cap-add and cap-drop. // One takes an array as parameter, the other takes a list. logger . warning ( "A method was found for " + entry . getKey ( ) + " but it does not have the right parameter type. " + "Skipping it. You may want to add a feature request." ) ; continue ; } // That's probably the right one. _m = m ; break ; } } // Handle errors if ( _m == null ) throw new TargetException ( "Nothing matched the " + entry . getKey ( ) + " option in the REST API. Please, report it." ) ; // Try to set the option in the REST client try { Object o = prepareParameter ( optionValue , _m . getParameterTypes ( ) [ 0 ] ) ; _m . invoke ( cmd , o ) ; } catch ( ReflectiveOperationException | IllegalArgumentException e ) { throw new TargetException ( "Option " + entry . getKey ( ) + " could not be set." ) ; } } }
Finds the options and tries to configure them on the creation command .